- Change .py files to use 4-space indents and no hard tab characters.
[nodemanager.git] / controller.py
1 # $Id$
2 # $URL$
3
4 """Delegate accounts are used to provide secure access to the XMLRPC API.
5 They are normal Unix accounts with a shell that tunnels XMLRPC requests to the API server."""
6
7 from pwd import getpwnam
8 from grp import getgrnam
9
10 import logger
11 import tools
12 import accounts
13
14 class Controller(accounts.Account):
15     SHELL = '/usr/bin/forward_api_calls'  # tunneling shell
16     TYPE = 'controller.Controller'
17
18     @staticmethod
19     def create(name, vref = None):
20         add_shell(Controller.SHELL)
21         group = getgrnam("slices")[2]
22         logger.log_call(['/usr/sbin/useradd', '-p', '*', '-g', str(group), '-s', Controller.SHELL, name, ])
23
24     @staticmethod
25     def destroy(name): logger.log_call(['/usr/sbin/userdel', '-r', name, ])
26
27     def is_running(self):
28         logger.verbose("controller: is_running:  %s" % self.name)
29         return getpwnam(self.name)[6] == self.SHELL
30
31
32 def add_shell(shell):
33     """Add <shell> to /etc/shells if it's not already there."""
34     etc_shells = open('/etc/shells')
35     valid_shells = etc_shells.read().split()
36     etc_shells.close()
37     if shell not in valid_shells:
38         etc_shells = open('/etc/shells', 'a')
39         print >>etc_shells, shell
40         etc_shells.close()