class Role(AbstractRole): def __init__(self, main_instance): AbstractRole.__init__(self, main_instance) self.shares = {} self.FSBackend = FSBackend() def init(self): Logger.info("FileServer init") if not Config.init_role(): return False if not self.FSBackend.start(): Logger.error("FileServer: failed to initialize FSBackend") self.FSBackend.stop() return False if not self.cleanup_samba(): Logger.error("FileServer: unable to cleanup samba") return False if not self.purgeGroup(): Logger.error("FileServer: unable to cleanup users") return False if not self.cleanup_repository(): Logger.error("FileServer: unable to cleanup users") return False self.shares = self.get_existing_shares() return True @staticmethod def getName(): return "FileServer" def finalize(self): self.cleanup_samba() self.purgeGroup() if not self.FSBackend.stop(): self.FSBackend.force_stop() def run(self): self.status = Role.STATUS_RUNNING while self.loop: time.sleep(2) if self.status == Role.STATUS_STOPPING: if len(self.get_enabled_usershares()) > 0: Logger.debug("FileServer:: Waiting for usershares removal") continue if len(System.groupMember( Config.group)) > 1: # because www-data Logger.debug("FileServer:: Waiting for groups removal") continue break self.status = Role.STATUS_STOP def cleanup_samba(self): # check samba conf ret = True for share in self.shares.values(): if share.isActive(): if not share.disable(): ret = False for usershare in self.get_enabled_usershares(): Logger.debug("FileServer:: Removing samba usershare '%s'" % (usershare)) p = System.execute("net usershare delete %s" % (usershare)) if p.returncode is not 0: Logger.error("FS: unable to 'net usershare delete': %d => %s" % (p.returncode, p.stdout.read())) ret = False return ret def cleanup_repository(self): cmd = 'chown -R %s:%s "%s"' % (Config.uid, Config.gid, Config.backendSpool) p = System.execute(cmd) if p.returncode is not 0: Logger.debug("FS: following command '%s' returned %d => %s" % (cmd, p.returncode, p.stdout.read())) return False cmd = 'chmod -R u=rwX,g=rwX,o-rwx "%s"' % (Config.backendSpool) p = System.execute(cmd) if p.returncode is not 0: Logger.debug("FS: following command '%s' returned %d => %s" % (cmd, p.returncode, p.stdout.read())) return False return True def purgeGroup(self): users = System.groupMember(Config.group) if users is None: return False ret = True for user in users: if user == Config.dav_user: continue Logger.debug("FileServer:: deleting user '%s'" % (user)) u = User(user) u.clean() if u.existSomeWhere(): Logger.error("FS: unable to del user %s" % (user)) ret = False htgroup = HTGroup(Config.dav_group_file) htgroup.purge() try: groups = [ g.gr_name for g in grp.getgrall() if g.gr_name.startswith("ovd_share_") ] for g in groups: System.groupDelete(g) except Exception: Logger.exception("Failed to purge groups") ret = False return ret @staticmethod def get_existing_shares(): shares = {} for f in glob.glob(Config.backendSpool + "/*"): name = os.path.basename(f) share = Share(name, Config.backendSpool, Config.spool) shares[name] = share return shares def update_shares(self): shares = self.get_existing_shares() for (share_id, share) in shares.items(): if self.shares.has_key(share_id): continue Logger.error("FS: New share '%s' found, adding it" % (share_id)) self.shares[share_id] = share for (share_id, share) in self.shares.items(): if shares.has_key(share_id): continue Logger.error("FS: Share '%s' became invalid, deleting it" % (share_id)) del (self.shares[share_id]) def get_enabled_usershares(self): p = System.execute("net usershare list") if p.returncode is not 0: Logger.error("FS: unable to 'net usershare list': %d => %s" % (p.returncode, p.stdout.read())) res = [] try: res = os.listdir("/var/lib/samba/usershares/") except Exception, e: Logger.exception( "FS: unable to list content of /var/lib/samba/usershares") return res names = [s.strip() for s in p.stdout.read().splitlines()] return names
class Role(AbstractRole): def __init__(self, main_instance): AbstractRole.__init__(self, main_instance) self.shares = {} self.FSBackend = FSBackend() def init(self): Logger.info("FileServer init") if not Config.init_role(): return False if not self.FSBackend.start(): Logger.error("FileServer: failed to initialize FSBackend") self.FSBackend.stop() return False if not self.cleanup_samba(): Logger.error("FileServer: unable to cleanup samba") return False if not self.purgeGroup(): Logger.error("FileServer: unable to cleanup users") return False if not self.cleanup_repository(): Logger.error("FileServer: unable to cleanup users") return False self.shares = self.get_existing_shares() return True @staticmethod def getName(): return "FileServer" def finalize(self): self.cleanup_samba() self.purgeGroup() if not self.FSBackend.stop(): self.FSBackend.force_stop() def run(self): self.status = Role.STATUS_RUNNING while self.loop: time.sleep(2) if self.status == Role.STATUS_STOPPING: if len(self.get_enabled_usershares()) > 0: Logger.debug("FileServer:: Waiting for usershares removal") continue if len(System.groupMember(Config.group)) > 1: # because www-data Logger.debug("FileServer:: Waiting for groups removal") continue break self.status = Role.STATUS_STOP def cleanup_samba(self): # check samba conf ret = True for share in self.shares.values(): if share.isActive(): if not share.disable(): ret = False for usershare in self.get_enabled_usershares(): Logger.debug("FileServer:: Removing samba usershare '%s'"%(usershare)) p = System.execute("net usershare delete %s"%(usershare)) if p.returncode is not 0: Logger.error("FS: unable to 'net usershare delete': %d => %s"%(p.returncode, p.stdout.read())) ret = False return ret def cleanup_repository(self): cmd = 'chown -R %s:%s "%s"'%(Config.uid, Config.gid, Config.backendSpool) p = System.execute(cmd) if p.returncode is not 0: Logger.debug("FS: following command '%s' returned %d => %s"%(cmd, p.returncode, p.stdout.read())) return False cmd = 'chmod -R u=rwX,g=rwX,o-rwx "%s"'%(Config.backendSpool) p = System.execute(cmd) if p.returncode is not 0: Logger.debug("FS: following command '%s' returned %d => %s"%(cmd, p.returncode, p.stdout.read())) return False return True def purgeGroup(self): users = System.groupMember(Config.group) if users is None: return False ret = True for user in users: if user == Config.dav_user: continue Logger.debug("FileServer:: deleting user '%s'"%(user)) u = User(user) u.clean() if u.existSomeWhere(): Logger.error("FS: unable to del user %s"%(user)) ret = False htgroup = HTGroup(Config.dav_group_file) htgroup.purge() try: groups = [g.gr_name for g in grp.getgrall() if g.gr_name.startswith("ovd_share_")] for g in groups: System.groupDelete(g) except Exception: Logger.exception("Failed to purge groups") ret = False return ret @staticmethod def get_existing_shares(): shares = {} for f in glob.glob(Config.backendSpool+"/*"): name = os.path.basename(f) share = Share(name, Config.backendSpool, Config.spool) shares[name] = share return shares def update_shares(self): shares = self.get_existing_shares() for (share_id, share) in shares.items(): if self.shares.has_key(share_id): continue Logger.error("FS: New share '%s' found, adding it"%(share_id)) self.shares[share_id] = share for (share_id, share) in self.shares.items(): if shares.has_key(share_id): continue Logger.error("FS: Share '%s' became invalid, deleting it"%(share_id)) del(self.shares[share_id]) def get_enabled_usershares(self): p = System.execute("net usershare list") if p.returncode is not 0: Logger.error("FS: unable to 'net usershare list': %d => %s"%(p.returncode, p.stdout.read())) res = [] try: res = os.listdir("/var/lib/samba/usershares/") except Exception, e: Logger.exception("FS: unable to list content of /var/lib/samba/usershares") return res names = [s.strip() for s in p.stdout.read().splitlines()] return names