X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=accounts.py;h=30654080c41b1ade6352a06793a6737383ec86a8;hb=2ff424968fb0629aa53f62b78227b682bb8f2a1d;hp=08ceac15738a0b4c8824ea7eb4a4c45258f37eb0;hpb=aa9e33baa88ec8ddb97468436c8c3972478e1b93;p=nodemanager.git diff --git a/accounts.py b/accounts.py index 08ceac1..3065408 100644 --- a/accounts.py +++ b/accounts.py @@ -23,6 +23,7 @@ maximum stack size. import Queue import os import pwd +from grp import getgrnam import threading import logger @@ -30,8 +31,9 @@ import tools # When this variable is true, start after any ensure_created -startingup = False -# Cumulative delay for starts when startingup is true +Startingup = False +# Cumulative delay for starts when Startingup is true +csd_lock = threading.Lock() cumstartdelay = 0 # shell path -> account class association @@ -67,6 +69,7 @@ def get(name): class Account: def __init__(self, rec): + logger.verbose('Initing account %s'%rec['name']) self.name = rec['name'] self.keys = '' self.initscriptchanged = False @@ -79,20 +82,23 @@ class Account: def configure(self, rec): """Write to my authorized_keys file.""" + logger.verbose('in accounts:configure for %s'%self.name) new_keys = rec['keys'] if new_keys != self.keys: self.keys = new_keys dot_ssh = '/home/%s/.ssh' % self.name - def do_installation(): - if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh) - os.chmod(dot_ssh, 0700) - tools.write_file(dot_ssh + '/authorized_keys', lambda f: f.write(new_keys)) + if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh) + os.chmod(dot_ssh, 0700) + tools.write_file(dot_ssh + '/authorized_keys', lambda f: f.write(new_keys)) logger.log('%s: installing ssh keys' % self.name) - tools.fork_as(self.name, do_installation) + user = pwd.getpwnam(self.name)[2] + group = getgrnam("slices")[2] + os.chown(dot_ssh, user, group) + os.chown(dot_ssh + '/authorized_keys', user, group) def start(self, delay=0): pass def stop(self): pass - + def is_running(self): pass class Worker: # these semaphores are acquired before creating/destroying an account @@ -110,9 +116,12 @@ class Worker: def ensure_created(self, rec): """Cause the account specified by to exist if it doesn't already.""" - self._q.put((self._ensure_created, rec.copy())) + if rec.has_key('name'): + logger.verbose('Worker.ensure_created with name=%s'%rec['name']) + self._q.put((self._ensure_created, rec.copy(), Startingup)) + logger.verbose('Worker queue has %d item(s)'%self._q.qsize()) - def _ensure_created(self, rec): + def _ensure_created(self, rec, startingup): curr_class = self._get_class() next_class = type_acct_class[rec['type']] if next_class != curr_class: @@ -122,10 +131,13 @@ class Worker: finally: self._create_sem.release() if not isinstance(self._acct, next_class): self._acct = next_class(rec) else: self._acct.configure(rec) - if startingup: + if startingup or not self.is_running(): + csd_lock.acquire() global cumstartdelay - self._acct.start(delay=cumstartdelay) + delay = cumstartdelay cumstartdelay += 2 + csd_lock.release() + self._acct.start(delay=delay) elif next_class != curr_class or self._acct.initscriptchanged: self._acct.start() @@ -138,6 +150,14 @@ class Worker: def stop(self): self._q.put((self._stop,)) def _stop(self): self._acct.stop() + def is_running(self): + if self._acct.is_running(): + status = True + else: + status = False + logger.verbose("Worker(%s): is not running" % self.name) + return status + def _destroy(self, curr_class): self._acct = None if curr_class: @@ -154,6 +174,8 @@ class Worker: """Repeatedly pull commands off the queue and execute. If memory usage becomes an issue, it might be wise to terminate after a while.""" while True: try: + logger.verbose('Worker:_run : getting - size is %d'%self._q.qsize()) cmd = self._q.get() cmd[0](*cmd[1:]) - except: logger.log_exc() + except: + logger.log_exc(self.name)