Is now possible to delete services from the monitor.

This commit is contained in:
sergiotarxz 2021-11-13 03:41:38 +01:00
parent 0c945e1177
commit d6812f56bc
2 changed files with 57 additions and 11 deletions

View File

@ -4,7 +4,17 @@ use v5.30.0;
use strict; use strict;
use warnings; use warnings;
use Carp qw/confess/;
use Cualsea::Server::Loop; use Cualsea::Server::Loop;
my $loop = Cualsea::Server::Loop->new; my $loop = Cualsea::Server::Loop->new;
$loop->run while 1;
while (1) {
try {
$loop->run;
} catch {
confess $_;
};
}

View File

@ -23,8 +23,7 @@ sub handle_add {
my $init = $params{init}; my $init = $params{init};
my $pidfile = $params{pidfile}; my $pidfile = $params{pidfile};
my $binpath = $params{binpath}; my $binpath = $params{binpath};
if ( !-e $binpath || $init ne 'sysvinit' || !_validate_init_name($name) ) if ( !-e $binpath || $init ne 'sysvinit' || !_validate_init_name($name) ) {
{
return { return {
is_error => 1, is_error => 1,
desc => "Parameters didn't pass contraints on add", desc => "Parameters didn't pass contraints on add",
@ -39,11 +38,11 @@ INSERT INTO services (name, init, pidfile, binpath) VALUES (?, ?, ?, ?);
EOF EOF
}; };
if ($@) { if ($@) {
if ($@ =~ /UNIQUE/i) { if ( $@ =~ /UNIQUE/i ) {
my $return_value = { my $return_value = {
status => 400, status => 400,
is_error => 1, is_error => 1,
desc => 'Endpoint already exists.', desc => 'Endpoint already exists.',
}; };
return $return_value; return $return_value;
} }
@ -56,6 +55,34 @@ EOF
}; };
} }
sub handle_del {
my %params = @_;
my $name = $params{name};
my $dbh = Cualsea::Server::DB->dbh();
my $service = $dbh->selectrow_hashref( <<"EOF", undef, $name );
SELECT name FROM services WHERE name = ?
EOF
if ( !defined $service ) {
return {
is_error => 1,
desc => 'Service not found.',
status => 404,
};
}
$dbh->do( <<'EOF', undef, $name );
DELETE FROM services WHERE name = ?
EOF
return {
is_error => 0,
desc => "Succesful deletion of init service $name.",
status => 200,
};
}
my %COMMANDS = ( my %COMMANDS = (
add => { add => {
params => [ params => [
@ -81,6 +108,7 @@ my %COMMANDS = (
name => { type => Str }, name => { type => Str },
} }
), ),
handle => \&handle_del
}, },
report => { report => {
params => [], params => [],
@ -149,6 +177,7 @@ sub new {
my $message = $message_manager->read_message; my $message = $message_manager->read_message;
if ( !$self->check_if_user_has_permissions( socket => $socket ) ) { if ( !$self->check_if_user_has_permissions( socket => $socket ) ) {
$self->write_no_permission( message_manager => $message_manager ); $self->write_no_permission( message_manager => $message_manager );
return;
} }
if ( !$self->check_is_command( message => $message ) ) { if ( !$self->check_is_command( message => $message ) ) {
$self->write_malformed( message_manager => $message_manager ); $self->write_malformed( message_manager => $message_manager );
@ -186,9 +215,12 @@ sub new {
# We want to forget about $params arrayref # We want to forget about $params arrayref
# and previous hash as soon as possible. # and previous hash as soon as possible.
%params = (); %params = ();
for my $param_info_index ( scalar @$params_info ? (0..$#$params_info) : () ) { for my $param_info_index (
scalar @$params_info ? ( 0 .. $#$params_info ) : () )
{
my $param_info = $params_info->[$param_info_index]; my $param_info = $params_info->[$param_info_index];
# May not come defined, # May not come defined,
if ( $param_info_index > scalar $#$params ) { if ( $param_info_index > scalar $#$params ) {
last; last;
@ -229,12 +261,16 @@ sub new {
my $socket = $params{socket}; my $socket = $params{socket};
my ( $pid, $uid, $gid ) = unpack 'LLL', $socket->sockopt(SO_PEERCRED); my ( $pid, $uid, $gid ) = unpack 'LLL', $socket->sockopt(SO_PEERCRED);
my $user = getpwuid($uid); my $user = getpwuid($uid);
while ( my ( $name, undef, undef, $members ) = getgrent() ) { state $name = '';
if ( $name eq 'cualsea' ) { state $members = '';
return any { $_ eq $user } split ',', $members; if (!$name || !$members) {
last; while ( ( $name, undef, undef, $members ) = getgrent() ) {
if ( $name eq 'cualsea' ) {
last;
}
} }
} }
return any { $_ eq $user } split ',', $members;
} }
} }
{ {