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

View File

@ -1,23 +1,34 @@
<?php
// define restartable services
$restartables = array("minidlna");
// make sure script is called through a web server, not the command line
if (PHP_SAPI === 'cli') {
echo "nope\n";
echo "This script needs to be run through a web server; aborting.";
die(1);
}
// retrieve query value for key "q"
$command = $_GET['q'];
echo "Command is <code>", $command, "</code><br>";//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 <code>", $command, "</code><br>";//DEBUG
// execute command
echo "Restarting service ", $service, "";
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" ) {
echo "Success";
echo "OK";
} 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"
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"

View File

@ -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]\+")