1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """
16 System plugin.
17 """
18
19 from subprocess import Popen, PIPE, call
20 from gofer.decorators import *
21 from gofer.agent.plugin import Plugin
22 from gofer.pam import PAM
23 from logging import getLogger
24
25 log = getLogger(__name__)
26 plugin = Plugin.find(__name__)
30
31 @remote
32 @pam(user='root')
33 - def halt(self, when=1):
34 """
35 Halt the system.
36 @param when: When to perform the shutdown.
37 One of:
38 - now : immediate. note: reply not sent.
39 - +m : where m is minutes.
40 - hh:mm : time (hours:minutes) 24hr clock.
41 @type when: str
42 @see: shutdown(8)
43 """
44 command = 'shutdown -h %s &' % when
45 call(command, shell=1)
46
47 @remote
48 @pam(user='root')
50 """
51 Reboot the system.
52 @param when: When to perform the reboot.
53 One of:
54 - now : immediate. note: reply not sent.
55 - +m : where m is minutes.
56 - hh:mm : time (hours:minutes) 24hr clock.
57 @type when: str
58 @see: shutdown(8)
59 """
60 command = 'shutdown -r %s &' % when
61 call(command, shell=1)
62
63 @remote
64 @pam(user='root')
66 """
67 Cancel a scheduled shutdown; halt() or reboot().
68 """
69 call('shutdown -c', shell=1)
70
73
74 @remote
75 - def run(self, cmd, user, password):
76 """
77 Run a shell command.
78 The command is executed as: "su - <user> -c <cmd>" and the
79 user/password is authenticated using PAM.
80 @param cmd: The command & arguments.
81 @type cmd: str
82 @param user: A user name.
83 @type user: str
84 @param password: The password.
85 @type password: str
86 @return: tuple (status, output)
87 @rtype: tuple
88 """
89 auth = PAM()
90 auth.authenticate(user, password)
91 command = ('su', '-', user, '-c', cmd)
92 p = Popen(command, stdout=PIPE)
93 try:
94 result = p.stdout.read()
95 p.stdout.close()
96 status = p.wait()
97 return (status, result)
98 except OSError, e:
99 return (-1, str(e))
100