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