Module system
[hide private]
[frames] | no frames]

Source Code for Module system

  1  # 
  2  # Copyright (c) 2011 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either version 
  6  # 2 of the License (LGPLv2) or (at your option) any later version. 
  7  # There is NO WARRANTY for this software, express or implied, 
  8  # including the implied warranties of MERCHANTABILITY, 
  9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
 10  # have received a copy of LGPLv2 along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
 12  # 
 13  # Jeff Ortel <jortel@redhat.com> 
 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__) 
27 28 29 -class System:
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')
49 - def reboot(self, when=1):
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')
65 - def cancel(self):
66 """ 67 Cancel a scheduled shutdown; halt() or reboot(). 68 """ 69 call('shutdown -c', shell=1)
70
71 72 -class Shell:
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