add control

This commit is contained in:
Mario Fetka
2026-05-21 16:30:41 +02:00
parent 362be76b7c
commit aa60166da6

127
control.cmake Normal file
View File

@@ -0,0 +1,127 @@
#!/usr/bin/perl
#
# SMArT service control helper for MARS_NWE systemd unit.
# Installed next to the other SMArT perl helpers.
#
use strict;
use warnings;
our ($server_id, $mars_nwe_service, $smart_systemctl_path, $cc, %p);
sub html_escape($)
{
my ($s) = @_;
$s = '' unless defined $s;
$s =~ s/&/&/g;
$s =~ s/</&lt;/g;
$s =~ s/>/&gt;/g;
$s =~ s/"/&quot;/g;
return $s;
}
sub handle_request()
{
my $service = $mars_nwe_service || '@MARS_NWE_SYSTEMD_SERVICE@';
my $systemctl = $smart_systemctl_path || '@SYSTEMCTL_EXECUTABLE@';
my $query = defined($cc) ? $cc : '';
my $action = '';
$query =~ s/^\?//;
if( $query =~ /^(start|stop|restart|status)$/ )
{
$action = $1;
}
elsif( $query =~ /(?:^|&)action=(start|stop|restart|status)(?:&|$)/ )
{
$action = $1;
}
elsif( defined($p{action}) && $p{action} =~ /^(start|stop|restart|status)$/ )
{
$action = $1;
}
print <<EOF;
HTTP/1.0 200 OK
Content-Type: text/html
$server_id
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MARS_NWE service control</title>
<link rel="icon" href="/static/favicon.ico" type="image/x-icon">
<style>
body{margin:0;padding:22px;background:#f6f1ea;color:#342a25;font-family:Arial,Helvetica,sans-serif}
a{color:#9f1f1f;text-decoration:none;font-weight:bold}a:hover{text-decoration:underline}
.box{max-width:900px;margin:0 auto;background:#fffdfa;border:1px solid #e5d6c6;border-radius:16px;padding:22px;box-shadow:0 10px 30px rgba(60,30,10,.10)}
h1{margin-top:0;color:#9f1f1f}.meta{padding:12px 14px;background:#fbf6ef;border:1px solid #ecdcc8;border-radius:12px;margin-bottom:14px}
pre{background:#2b241f;color:#f8eadc;padding:16px;border-radius:12px;overflow:auto;white-space:pre-wrap}.ok{color:#2f6f35;font-weight:bold}.bad{color:#9f1f1f;font-weight:bold}
.actions a{display:inline-block;margin:5px 8px 5px 0;padding:9px 12px;border-radius:10px;background:#9f1f1f;color:#fff}.actions a.secondary{background:#6f5b4f}
</style>
</head>
<body>
<div class="box">
<h1>MARS_NWE service control</h1>
EOF
if( $action eq '' )
{
print "<p class=\"bad\">Invalid action.</p>\n";
print "<p>Allowed actions: start, stop, restart, status</p>\n";
print "<p><a href=\"/static/start.html\">Back</a></p>\n";
print "</div></body></html>\n";
return;
}
print '<div class="meta">Action: <b>' . html_escape($action) . '</b><br>' . "\n";
print 'Service: <b>' . html_escape($service) . '</b><br>' . "\n";
print 'systemctl: <b>' . html_escape($systemctl) . '</b></div>' . "\n";
print "<pre>\n";
my @cmd = ( $systemctl, $action, $service );
my $fh;
if( ! open( $fh, '-|', @cmd ) )
{
print 'Could not execute systemctl: ' . html_escape($!) . "\n";
print "</pre><p class=\"bad\">Failed.</p>\n";
print "<p><a href=\"/static/start.html\">Back</a></p>\n";
print "</div></body></html>\n";
return;
}
while( my $line = <$fh> )
{
print html_escape($line);
}
close($fh);
my $rc = $? >> 8;
if( $rc == 0 )
{
print "</pre><p class=\"ok\">Command completed successfully.</p>\n";
}
else
{
print "</pre><p class=\"bad\">Command failed with exit code $rc.</p>\n";
}
print <<EOF;
<div class="actions">
<a href="/cgi-bin/control?status" class="secondary">Status</a>
<a href="/cgi-bin/control?start">Start</a>
<a href="/cgi-bin/control?stop" class="secondary">Stop</a>
<a href="/cgi-bin/control?restart">Restart</a>
</div>
<p><a href="/static/start.html">Back</a></p>
</div>
</body>
</html>
EOF
}
1;