cleaned up code, added additional safety checks, ramped version no up to 1.0-ynh2

This commit is contained in:
Eclipse 2023-10-02 22:36:44 +02:00
parent 2f9472ec42
commit 3d7f414c1e
4 changed files with 34 additions and 22 deletions

View File

@ -1,11 +1,10 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="en-US">
<head> <head>
<title>Restarter</title> <title>Restarter</title>
<script type="text/javascript"> <script type="text/javascript">
function restart(command) { function restart(service) {
let query = new URLSearchParams({ "q" : command }); let query = new URLSearchParams({ "s" : service });
console.log(query);
let xhttp = new XMLHttpRequest(); let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
@ -20,8 +19,7 @@ console.log(query);
</script> </script>
</head> </head>
<body> <body>
<button type=button onClick="restart('sudo systemctl restart minidlna.service')">MiniDLNA</button> <button type=button onClick="restart(`minidlna`)">MiniDLNA</button>
<!-- <button type=button onClick="restart('ls -al')">MiniDLNA</button> -->
<div id="result"> <div id="result">
</div> </div>
</body> </body>

View File

@ -1,23 +1,34 @@
<?php <?php
// define restartable services
$restartables = array("minidlna");
// make sure script is called through a web server, not the command line // make sure script is called through a web server, not the command line
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "nope\n"; echo "This script needs to be run through a web server; aborting.";
die(1); die(1);
} }
// retrieve query value for key "q" // retrieve query value for key "s" (as in service)
$command = $_GET['q']; $service = $_GET['s'];
echo "Command is <code>", $command, "</code><br>";//DEBUG
// make sure command exists and is not empty // make sure command exists and is not empty
if ($command === "" || empty($command)) { if ($service === "" || empty($service)) {
echo "empty query"; echo "No service to restart; aborting.";
die(2); 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 <code>", $command, "</code><br>";//DEBUG
// execute command
echo "Restarting service ", $service, "";
exec($command, $output, $exitcode); exec($command, $output, $exitcode);
echo "Output is <pre>", join("<br>", $output), "</pre><br>";//DEBUG // echo "Output is <pre>", join("<br>", $output), "</pre><br>";//DEBUG
if ( $exitcode == "0" ) { if ( $exitcode == "0" ) {
echo "Success"; echo "OK";
} else { } else {
echo "Command failed with exit code", $exitcode; echo "failed with exit code ", $exitcode;
} }
?> ?>

View File

@ -2,9 +2,9 @@ packaging_format = 2
id = "restarter" id = "restarter"
name = "Restarter for yunohost" 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"] maintainers = ["eclipse"]
@ -43,6 +43,3 @@ ram.runtime = "1M"
[resources.permissions] [resources.permissions]
main.url = "/" main.url = "/"
# [resources.apt]
# packages = "php8.2-fpm"

View File

@ -3,6 +3,13 @@
source /usr/share/yunohost/helpers 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 # CHECK IF RESTARTABLE SERVICES EXIST
#================================================= #=================================================
@ -46,7 +53,6 @@ phpversion="${php_executable:(-3)}"
ynh_app_setting_set --app=$app --key="phpversion" --value="$phpversion" ynh_app_setting_set --app=$app --key="phpversion" --value="$phpversion"
ynh_add_fpm_config --phpversion="$phpversion" ynh_add_fpm_config --phpversion="$phpversion"
ynh_script_progression --message="Adding nginx configuration …" --weight=1 ynh_script_progression --message="Adding nginx configuration …" --weight=1
# get local subnet address ranges from routing table # 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]\+") ranges=$(netstat -nr | grep -o "^\(192\.168\|172.\(1[6-9]\|2[0-9]\|3[01]\)\|10\.[0-9]\+\)\.[0-9]\+\.[0-9]\+")