]>
git.proxmox.com Git - pve-manager.git/blob - lib/PVE/API2/Services.pm
1 package PVE
:: API2
:: Services
;
10 use PVE
:: Exception
qw(raise_param_exc) ;
12 use PVE
:: RPCEnvironment
;
13 use PVE
:: JSONSchema
qw(get_standard_option) ;
14 use PVE
:: AccessControl
;
17 use base
qw(PVE::RESTHandler) ;
20 apache
=> { name
=> 'WWW' , desc
=> 'Web/API server' },
21 postfix
=> { name
=> 'SMTP' , desc
=> 'Simple Mail Tranfer Protocol' },
22 ntpd
=> { name
=> 'NTP' , desc
=> 'Network time protocol' },
23 sshd
=> { name
=> 'SSH' , desc
=> 'Secure shell daemon' },
24 syslog
=> { name
=> 'Syslog' , desc
=> 'Syslog daemon' },
25 cron
=> { name
=> 'CRON' , desc
=> 'Daemon to execute scheduled commands' },
26 pvedaemon
=> { name
=> 'NodeManager' , desc
=> 'PVE node manager daemon' },
27 corosync
=> { name
=> 'Corosync' , desc
=> 'Corosync cluster daemon' },
28 pvecluster
=> { name
=> 'PVECluster' , desc
=> 'Proxmox VE cluster file system' },
31 my $service_cmd = sub {
32 my ( $service, $cmd ) = @_ ;
36 die "unknown service command ' $cmd ' \n "
37 if $cmd !~ m/^(start|stop|restart|reload)$/ ;
41 if ( $service eq 'postfix' ) {
42 $initd_cmd = '/etc/init.d/postfix' ;
43 } elsif ( $service eq 'pvecluster' ) {
44 if ( $cmd eq 'restart' ) {
45 $initd_cmd = '/etc/init.d/pve-cluster' ;
47 die "invalid service cmd 'pve-cluster $cmd ': ERROR" ;
49 } elsif ( $service eq 'pvedaemon' ) {
50 if ( $cmd eq 'restart' ) {
51 $initd_cmd = '/etc/init.d/pvedaemon' ;
53 die "invalid service cmd ' $service $cmd ': ERROR" ;
55 } elsif ( $service eq 'apache' ) {
56 if ( $cmd eq 'restart' ) {
57 $initd_cmd = '/usr/sbin/apache2ctl' ;
60 die "invalid service cmd ' $service $cmd ': ERROR" ;
62 } elsif ( $service eq 'ntpd' ) {
63 # debian start/stop scripts does not work for us
65 system ( '/etc/init.d/ntp stop' );
66 #system ('/usr/bin/killall /usr/sbin/ntpd');
67 } elsif ( $cmd eq 'start' ) {
68 system ( '/etc/init.d/ntp start' );
69 system ( '/sbin/hwclock --systohc' );
70 } elsif ( $cmd eq 'restart' ) {
71 system ( '/etc/init.d/ntp restart' );
72 system ( '/sbin/hwclock --systohc' );
73 # restart cron/syslog to get right schedules and log time/dates
74 system ( '/etc/init.d/rsyslog restart' );
75 system ( '/etc/init.d/cron restart' );
78 } elsif ( $service eq 'syslog' ) {
79 $initd_cmd = '/etc/init.d/rsyslog' ;
80 } elsif ( $service eq 'cron' ) {
81 $initd_cmd = '/etc/init.d/cron' ;
82 } elsif ( $service eq 'corosync' ) {
83 $initd_cmd = '/etc/init.d/cman' ;
84 } elsif ( $service eq 'sshd' ) {
85 $initd_cmd = '/etc/init.d/ssh' ;
87 die "unknown service ' $service ': ERROR" ;
90 PVE
:: Tools
:: run_command
([ $initd_cmd, $cmd ]);
93 my $service_state = sub {
98 if ( $service eq 'postfix' ) {
99 $pid_file = '/var/spool/postfix/pid/master.pid' ;
100 } elsif ( $service eq 'apache' ) {
101 $pid_file = '/var/run/apache2.pid' ;
102 } elsif ( $service eq 'pvedaemon' ) {
103 $pid_file = '/var/run/pvedaemon.pid' ;
104 } elsif ( $service eq 'pvecluster' ) {
105 $pid_file = '/var/run/pve-cluster.pid' ;
106 } elsif ( $service eq 'ntpd' ) {
107 $pid_file = '/var/run/ntpd.pid' ;
108 } elsif ( $service eq 'sshd' ) {
109 $pid_file = '/var/run/sshd.pid' ;
110 } elsif ( $service eq 'cron' ) {
111 $pid_file = '/var/run/crond.pid' ;
112 } elsif ( $service eq 'corosync' ) {
113 $pid_file = '/var/run/corosync.pid' ;
114 } elsif ( $service eq 'syslog' ) {
115 $pid_file = '/var/run/rsyslogd.pid' ;
117 die "unknown service ' $service ': ERROR" ;
121 if ( my $fh = IO
:: File-
> new ( $pid_file, "r" )) {
125 if ( $line && ( $line =~ m/^\s*(\d+)\s*$/ )) {
130 return 'running' if ( $pid && kill ( 0 , $pid ));
135 __PACKAGE__-
> register_method ({
140 path
=> '/nodes/{node}' ,
141 privs
=> [ 'Sys.Audit' ],
143 description
=> "Service list." ,
147 additionalProperties
=> 0 ,
149 node
=> get_standard_option
( 'pve-node' ),
158 links
=> [ { rel
=> 'child' , href
=> "{service}" } ],
165 foreach my $id ( keys %{ $service_list }) {
168 name
=> $service_list ->{ $id }->{ name
},
169 desc
=> $service_list ->{ $id }->{ desc
},
170 state => & $service_state ( $id ),
177 __PACKAGE__-
> register_method ({
182 path
=> '/nodes/{node}' ,
183 privs
=> [ 'Sys.Audit' ],
185 description
=> "Read service properties" ,
189 additionalProperties
=> 0 ,
191 node
=> get_standard_option
( 'pve-node' ),
193 description
=> "Service ID" ,
195 enum
=> [ keys %{ $service_list } ],
206 my $si = $service_list ->{ $param ->{ service
}};
208 service
=> $param ->{ service
},
211 state => & $service_state ( $param ->{ service
}),
215 __PACKAGE__-
> register_method ({
219 description
=> "Execute service commands." ,
223 additionalProperties
=> 0 ,
225 node
=> get_standard_option
( 'pve-node' ),
227 description
=> "Service ID" ,
229 enum
=> [ keys %{ $service_list } ],
232 description
=> "The command to execute. The only valid command for service 'apache' and 'pvedaemon' is 'restart', because both services are required by this API." ,
234 enum
=> [ qw(start stop restart reload) ],
238 returns
=> { type
=> 'null' },
242 my $si = $service_list ->{ $param ->{ service
}};
243 & $service_cmd ( $param ->{ service
}, $param ->{ command
});