[pve-devel] r5497 - in pve-manager/pve2: . lib/PVE/API2

svn-commits at proxmox.com svn-commits at proxmox.com
Wed Feb 9 11:58:41 CET 2011


Author: dietmar
Date: 2011-02-09 11:58:40 +0100 (Wed, 09 Feb 2011)
New Revision: 5497

Added:
   pve-manager/pve2/lib/PVE/API2/Services.pm
Modified:
   pve-manager/pve2/ChangeLog
   pve-manager/pve2/lib/PVE/API2/Makefile.am
   pve-manager/pve2/lib/PVE/API2/Nodes.pm
Log:


Modified: pve-manager/pve2/ChangeLog
===================================================================
--- pve-manager/pve2/ChangeLog	2011-02-09 09:01:45 UTC (rev 5496)
+++ pve-manager/pve2/ChangeLog	2011-02-09 10:58:40 UTC (rev 5497)
@@ -1,3 +1,7 @@
+2011-02-09  Proxmox Support Team  <support at proxmox.com>
+
+	* lib/PVE/API2/Services.pm: new class for services.
+
 2011-02-03  Proxmox Support Team  <support at proxmox.com>
 
 	* www/manager/PVEUtils.js: add 'requestexception' event listener,

Modified: pve-manager/pve2/lib/PVE/API2/Makefile.am
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Makefile.am	2011-02-09 09:01:45 UTC (rev 5496)
+++ pve-manager/pve2/lib/PVE/API2/Makefile.am	2011-02-09 10:58:40 UTC (rev 5497)
@@ -5,6 +5,7 @@
 pvelib_DATA = 			\
 	Cluster.pm		\
 	Nodes.pm		\
+	Services.pm		\
 	VM.pm
 
 CLEANFILES = *~

Modified: pve-manager/pve2/lib/PVE/API2/Nodes.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Nodes.pm	2011-02-09 09:01:45 UTC (rev 5496)
+++ pve-manager/pve2/lib/PVE/API2/Nodes.pm	2011-02-09 10:58:40 UTC (rev 5497)
@@ -11,11 +11,17 @@
 use PVE::RPCEnvironment;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::AccessControl;
+use PVE::API2::Services;
 use JSON;
 
 use base qw(PVE::RESTHandler);
 
 __PACKAGE__->register_method ({
+    subclass => "PVE::API2::Services",  
+    path => 'services',
+});
+
+__PACKAGE__->register_method ({
     name => 'index', 
     path => '', 
     method => 'GET',
@@ -40,6 +46,7 @@
 	my $result = [
 	    { name => 'syslog' },
 	    { name => 'vncshell' },
+	    { name => 'services' },
 	    ];
 
 	return $result;

Added: pve-manager/pve2/lib/PVE/API2/Services.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Services.pm	                        (rev 0)
+++ pve-manager/pve2/lib/PVE/API2/Services.pm	2011-02-09 10:58:40 UTC (rev 5497)
@@ -0,0 +1,222 @@
+package PVE::API2::Services;
+
+use strict;
+use warnings;
+
+use PVE::Tools;
+use PVE::SafeSyslog;
+use PVE::Cluster;
+use PVE::INotify;
+use PVE::Exception qw(raise_param_exc);
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::AccessControl;
+use IO::File;
+
+use base qw(PVE::RESTHandler);
+
+my $service_list = {
+    apache => { name => 'WWW', desc => 'Web Server' },
+    postfix => { name => 'SMTP', desc => 'Simple Mail Tranfer Protocol' },
+    ntpd => { name => 'NTP', desc => 'Network Time Protocol' },
+    sshd => { name => 'SSH', desc => 'Secure Shell Daemon' },
+    syslog => { name => 'Syslog', desc => 'Syslog Daemon' },
+    cron => { name => 'CRON', desc => 'Daemon to execute scheduled commands' },
+    pvedaemon => { name => 'NodeManager', desc => 'PVE Node Manager Daemon' },
+};
+
+my $service_cmd = sub {
+    my ($service, $cmd) = @_;
+
+    my $initd_cmd;
+
+    die "unknown service command '$cmd'\n"
+	if $cmd !~ m/^(start|stop|restart|reload)$/;
+
+    $cmd = $1; # untaint
+
+    if ($service eq 'postfix') {
+	$initd_cmd = '/etc/init.d/postfix';
+    } elsif ($service eq 'pvedaemon') {
+	if ($cmd eq 'restart') {    
+	    $initd_cmd = '/etc/init.d/pvedaemon';
+	} else {
+	    die "invalid service cmd 'pvedaemon $cmd': ERROR";
+	}
+    } elsif  ($service eq 'apache') {
+	if ($cmd eq 'restart') {    
+	    $initd_cmd = '/usr/sbin/apache2ctl';
+	    $cmd = 'graceful';
+	} else {
+	    die "invalid service cmd 'apache $cmd': ERROR";
+	}
+    } elsif  ($service eq 'ntpd') {
+	# debian start/stop scripts does not work for us
+	if ($cmd eq 'stop') {
+	    system ('/etc/init.d/ntp stop');
+	    #system ('/usr/bin/killall /usr/sbin/ntpd'); 
+	} elsif ($cmd eq 'start') {
+	    system ('/etc/init.d/ntp start');
+	    system ('/sbin/hwclock --systohc');
+	} elsif ($cmd eq 'restart') {
+	    system ('/etc/init.d/ntp restart');
+	    system ('/sbin/hwclock --systohc');
+	    # restart cron/syslog to get right schedules and log time/dates
+	    system ('/etc/init.d/rsyslog restart');
+	    system ('/etc/init.d/cron restart');
+	}
+	return 0;
+    } elsif  ($service eq 'syslog') {
+	$initd_cmd = '/etc/init.d/rsyslog';
+    } elsif  ($service eq 'cron') {
+	$initd_cmd = '/etc/init.d/cron';
+    } elsif  ($service eq 'sshd') {
+	$initd_cmd = '/etc/init.d/ssh';
+    } else {
+	die "unknown service '$service': ERROR";
+    }    
+
+    PVE::Tools::run_command ([$initd_cmd, $cmd]);
+};
+
+my $service_state = sub {
+    my ($service) = @_;
+
+    my $pid_file;
+
+    if ($service eq 'postfix') {
+	$pid_file = '/var/spool/postfix/pid/master.pid';
+    } elsif  ($service eq 'apache') {
+	$pid_file = '/var/run/apache2.pid';
+    } elsif  ($service eq 'pvedaemon') {
+	$pid_file = '/var/run/pvedaemon.pid';
+    } elsif  ($service eq 'ntpd') {
+	$pid_file = '/var/run/ntpd.pid';
+    } elsif  ($service eq 'sshd') {
+	$pid_file = '/var/run/sshd.pid';
+    } elsif  ($service eq 'cron') {
+	$pid_file = '/var/run/crond.pid';
+    } elsif  ($service eq 'syslog') {
+	$pid_file = '/var/run/rsyslogd.pid';
+    } else {
+	die "unknown service '$service': ERROR";
+    }    
+
+    my $pid;
+    if (my $fh = IO::File->new ($pid_file, "r")) {
+	my $line = <$fh>;
+	chomp $line;
+	    
+	if ($line  && ($line =~ m/^\s*(\d+)\s*$/)) {
+	    $pid = $1;
+	}
+    }
+
+    return 'running' if ($pid && kill (0, $pid));
+
+    return 'stopped';
+};
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    path => '', 
+    method => 'GET',
+    description => "Service list.",
+    proxyto => 'node',
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => {},
+	},
+	links => [ { rel => 'child', href => "{service}" } ],
+    },
+    code => sub {
+	my ($param) = @_;
+  
+	my $res = [];
+
+	foreach my $id (keys %{$service_list}) {
+	    push @$res, { 
+		service => $id,
+		name => $service_list->{$id}->{name},
+		desc => $service_list->{$id}->{desc},
+		state => &$service_state($id),
+	    };
+	}
+
+	return $res;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'state', 
+    path => '{service}', 
+    method => 'GET',
+    description => "Read service properties",
+    proxyto => 'node',
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    service => {
+		description => "Service ID",
+		type => 'string',
+		enum => [ keys %{$service_list} ],
+	    },
+	},
+    },
+    returns => {
+	type => "object",
+	properties => {},
+    },
+    code => sub {
+	my ($param) = @_;
+  
+	my $si = $service_list->{$param->{service}};
+	return {
+	    service => $param->{service},
+	    name => $si->{name},
+	    desc => $si->{desc},
+	    state => &$service_state($param->{service}),
+	};
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'cmd', 
+    path => '{service}', 
+    method => 'PUT',
+    description => "Execute service commands.",
+    proxyto => 'node',
+    protected => 1,
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    service => {
+		description => "Service ID",
+		type => 'string',
+		enum => [ keys %{$service_list} ],
+	    },
+	    command => {
+		description => "The command to execute. The only valid command for service 'apache' and 'pvedaemon' is 'restart', because both services are required by this API.",
+		type => 'string',
+		enum => [qw(start stop restart reload)],
+	    },
+	},
+    },
+    returns => { type => 'null'},
+    code => sub {
+	my ($param) = @_;
+  
+	my $si = $service_list->{$param->{service}};
+	&$service_cmd($param->{service}, $param->{command});
+
+	return undef;
+    }});



More information about the pve-devel mailing list