Added method to verify if controller account is setup.
[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
8 class Controller(accounts.Account):
9     SHELL = '/usr/bin/forward_api_calls'  # tunneling shell
10     TYPE = 'controller.Controller'
11
12     @staticmethod
13     def create(name, vref = None):
14         add_shell(Controller.SHELL)
15         logger.log_call('/usr/sbin/useradd', '-p', '*', '-s', Controller.SHELL, name)
16
17     @staticmethod
18     def destroy(name): logger.log_call('/usr/sbin/userdel', '-r', name)
19
20     def is_running(self):
21         logger.verbose("Delegate:  %s" % self.name)
22         return getpwnam(self.name)[6] == self.SHELL
23     
24
25 def add_shell(shell):
26     """Add <shell> to /etc/shells if it's not already there."""
27     etc_shells = open('/etc/shells')
28     valid_shells = etc_shells.read().split()
29     etc_shells.close()
30     if shell not in valid_shells:
31         etc_shells = open('/etc/shells', 'a')
32         print >>etc_shells, shell
33         etc_shells.close()