From 3d7f414c1e9d0f1e64f4ba1c3c6d674e7fcd6edf Mon Sep 17 00:00:00 2001 From: Eclipse Date: Mon, 2 Oct 2023 22:36:44 +0200 Subject: [PATCH] cleaned up code, added additional safety checks, ramped version no up to 1.0-ynh2 --- conf/index.html | 10 ++++------ conf/restarter.php | 31 +++++++++++++++++++++---------- manifest.toml | 7 ++----- scripts/install | 8 +++++++- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/conf/index.html b/conf/index.html index ea254cc..3586665 100644 --- a/conf/index.html +++ b/conf/index.html @@ -1,11 +1,10 @@ - + Restarter - - +
diff --git a/conf/restarter.php b/conf/restarter.php index 3282145..655eb39 100644 --- a/conf/restarter.php +++ b/conf/restarter.php @@ -1,23 +1,34 @@ ", $command, "
";//DEBUG + // retrieve query value for key "s" (as in service) + $service = $_GET['s']; // make sure command exists and is not empty - if ($command === "" || empty($command)) { - echo "empty query"; + if ($service === "" || empty($service)) { + echo "No service to restart; aborting."; die(2); } - // execute shell command + // make sure service from query is available for restart + if ( ! in_array($service, $restartables) ) { + echo "Can't restart service ", $service, "; aborting"; + die(3); + } + // put together command + $command = "/usr/bin/sudo /usr/bin/systemctl restart " . $service . ".service"; + echo "Command is ", $command, "
";//DEBUG + // execute command + echo "Restarting service ", $service, " … "; exec($command, $output, $exitcode); - echo "Output is
", join("
", $output), "

";//DEBUG +// echo "Output is
", join("
", $output), "

";//DEBUG if ( $exitcode == "0" ) { - echo "Success"; + echo "OK"; } else { - echo "Command failed with exit code", $exitcode; + echo "failed with exit code ", $exitcode; } ?> diff --git a/manifest.toml b/manifest.toml index 710dd1a..e556eda 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,9 +2,9 @@ packaging_format = 2 id = "restarter" name = "Restarter for yunohost" -description.en = "A simple service restarter for Yunohost, supporting these apps: MiniDLNA" +description.en = "A simple service restarter for Yunohost. Currently supporting these apps: MiniDLNA" -version = "0.2~ynh1" +version = "1.0~ynh2" maintainers = ["eclipse"] @@ -43,6 +43,3 @@ ram.runtime = "1M" [resources.permissions] main.url = "/" - -# [resources.apt] -# packages = "php8.2-fpm" diff --git a/scripts/install b/scripts/install index 1cd63c9..49f49ed 100644 --- a/scripts/install +++ b/scripts/install @@ -3,6 +3,13 @@ source /usr/share/yunohost/helpers +#TODO: define restartable services in one place and use those values to checking which restartable services are installed +# then add corresponding lines dynamically to files: +# - index.html: one button per service +# - restarter.php: add services as array +# - sudoer: add command for each service + + #================================================= # CHECK IF RESTARTABLE SERVICES EXIST #================================================= @@ -46,7 +53,6 @@ phpversion="${php_executable:(-3)}" ynh_app_setting_set --app=$app --key="phpversion" --value="$phpversion" ynh_add_fpm_config --phpversion="$phpversion" - ynh_script_progression --message="Adding nginx configuration …" --weight=1 # get local subnet address ranges from routing table ranges=$(netstat -nr | grep -o "^\(192\.168\|172.\(1[6-9]\|2[0-9]\|3[01]\)\|10\.[0-9]\+\)\.[0-9]\+\.[0-9]\+")