def removeuser(self): logging.info("Defining email and username ...") email = self.args.email username = self.args.username member = self._getMemberByUsernameOrEmail(username, email, True) logging.info("Removing the user from the database ...") member.destroySelf() print "Done."
def adduser(self): logging.info("Defining username, name and email ...") username = self.args.username name = self.args.name email = self.args.email dummy = self._getMemberByUsernameOrEmail(username, email) logging.info("Creating and saving the new user ...") Member(username=username, email=email, name=name) print "Done."
def _getRepositoryByNameOrPath(self, member, name, path, must=False): repo = None logging.info("Trying to find a repository by name or path ...") if name: repo = member.repositoryByName(name) if path: repo = member.repositoryByPath(path) if must and not repo: raise AdminException("Repository with name `%s' or path `%s' can not be found." % (name, path)) elif not must and repo: raise AdminException("The repository `%s' already exists." % repo.name) return repo
def removerepo(self): logging.info("Defining repository name, email and username ...") email = self.args.email username = self.args.username name = self.args.repository_name path = self.args.repository_path member = self._getMemberByUsernameOrEmail(username, email, True) repo = self._getRepositoryByNameOrPath(member, name, path, True) logging.info("Removing the repository ...") repo.destroySelf() print "Done."
def _getMemberByUsernameOrEmail(self, username, email, must=False): logging.info("Trying to find the user by username or email ...") member = None try: if username: member = Member.selectBy(username=username)[0] if email: member = Member.selectBy(email=email)[0] except IndexError: if must and not member: raise AdminException("The user can not be found (username: `%s', email: `%s')" % (username, email)) if not must and member: raise AdminException("The user `%s' with email `%s' already exists." % (member.username, member.email)) return member
def addhook(self): logging.info("Defining username, email, repository name, hook and command ...") username = self.args.username email = self.args.email name = self.args.repository_name path = self.args.repository_path hook = self.args.hook command = self.args.command member = self._getMemberByUsernameOrEmail(username, email, True) repo = self._getRepositoryByNameOrPath(member, name, path, True) logging.info("Save new hook in database ...") repo.addHook(hook, command) print "Done."
def _getRepositoryByNameOrPath(self, member, name, path, must=False): repo = None logging.info("Trying to find a repository by name or path ...") if name: repo = member.repositoryByName(name) if path: repo = member.repositoryByPath(path) if must and not repo: raise AdminException( "Repository with name `%s' or path `%s' can not be found." % (name, path)) elif not must and repo: raise AdminException("The repository `%s' already exists." % repo.name) return repo
def addhook(self): logging.info( "Defining username, email, repository name, hook and command ...") username = self.args.username email = self.args.email name = self.args.repository_name path = self.args.repository_path hook = self.args.hook command = self.args.command member = self._getMemberByUsernameOrEmail(username, email, True) repo = self._getRepositoryByNameOrPath(member, name, path, True) logging.info("Save new hook in database ...") repo.addHook(hook, command) print "Done."
def _getMemberByUsernameOrEmail(self, username, email, must=False): logging.info("Trying to find the user by username or email ...") member = None try: if username: member = Member.selectBy(username=username)[0] if email: member = Member.selectBy(email=email)[0] except IndexError: if must and not member: raise AdminException( "The user can not be found (username: `%s', email: `%s')" % (username, email)) if not must and member: raise AdminException( "The user `%s' with email `%s' already exists." % (member.username, member.email)) return member
def runhook(self): logging.disable(logging.INFO) logging.info("Defining hook, repo and arguments ...") repo = Repository.get(self.args.repository_id) arguments = " " + self.args.arguments if self.args.arguments else "" logging.info("Looking up all hooks for repository `%s' and hook `%s' ..." % (repo.id, self.args.hook)) for hook in repo.getHooksByName(self.args.hook): logging.info("Running command `%s' ..." % hook.command + arguments) Popen(split(str(hook.command + arguments)))
def runhook(self): logging.disable(logging.INFO) logging.info("Defining hook, repo and arguments ...") repo = Repository.get(self.args.repository_id) arguments = " " + self.args.arguments if self.args.arguments else "" logging.info( "Looking up all hooks for repository `%s' and hook `%s' ..." % (repo.id, self.args.hook)) for hook in repo.getHooksByName(self.args.hook): logging.info("Running command `%s' ..." % hook.command + arguments) Popen(split(str(hook.command + arguments)))
def removehook(self): logging.info("Defining hook id ...") id = self.args.hook_id logging.info("Checking if hook exists ...") hook = Hook.get(id) if not hook: raise Exception("The hook with the id `%s' does not exist." % id) logging.info("Removing the hook from the database ...") hook.destroySelf() print "Done."
def removekey(self): logging.info("Defining key id ...") id = self.args.key_id logging.info("Checking if the key exists ...") key = Key.get(id) if not key: raise Exception("The key with the id `%s' does not exits." % id) logging.info("Removing the key from the database ...") key.destroySelf() print "Done."
def addkey(self): logging.info("Read the key ...") key = __import__("sys").stdin.readline().strip() if key == "": raise Exception("Key can not be empty.") logging.info("Define username, email and repository ...") email = self.args.email username = self.args.username member = self._getMemberByUsernameOrEmail(username, email, True) logging.info("Save new key in database ...") member.addKey(key) print "Done."
def addrepo(self): logging.info("Defining username, email and repository name ...") username = self.args.username email = self.args.email name = self.args.repository_name path = self.args.repository_path member = self._getMemberByUsernameOrEmail(username, email, True) dummy = self._getRepositoryByNameOrPath(member, name, path) logging.info("Changing to the git user ...") __import__("os").setgid(__import__("pwd").getpwnam(Config.get("phoenix", "user")).pw_gid) __import__("os").setuid(__import__("pwd").getpwnam(Config.get("phoenix", "user")).pw_uid) logging.info("Creating and saving the new repository ...") member.addRepository(name, path) print "Done."
def addrepo(self): logging.info("Defining username, email and repository name ...") username = self.args.username email = self.args.email name = self.args.repository_name path = self.args.repository_path member = self._getMemberByUsernameOrEmail(username, email, True) dummy = self._getRepositoryByNameOrPath(member, name, path) logging.info("Changing to the git user ...") __import__("os").setgid( __import__("pwd").getpwnam(Config.get("phoenix", "user")).pw_gid) __import__("os").setuid( __import__("pwd").getpwnam(Config.get("phoenix", "user")).pw_uid) logging.info("Creating and saving the new repository ...") member.addRepository(name, path) print "Done."
def _createDirectoryStructure(self, repo, tar, ssh): if not path.exists(repo): logging.info("Creating repository dir at `%s' ..." % repo) mkdir(repo) else: logging.warning("The folder `%s' already exists." % repo) Config.set("phoenix", "repository_dir", repo) if not path.exists(tar): logging.info("Creating tarball dir at `%s' ..." % tar) mkdir(tar) else: logging.warning("The folder `%s' already exists." % tar) Config.set("phoenix", "tarball_dir", tar) if not path.exists(ssh): logging.info("Creating ssh dir at `%s' ..." % ssh) mkdir(ssh, 0700) else: logging.warning("The folder `%s' already exists." % ssh) Config.set("phoenix", "ssh_dir", ssh)
def init(self): if Config.get("phoenix", "initialized") == "True": raise AdminException("Already initialized.") logging.info("Defining variables for init ...") user = self.args.git_user base = path.join(self.args.base_dir, user) repo = path.join(base, self.args.repository_dir) tar = path.join(base, self.args.tarball_dir) ssh = path.join(base, ".ssh") auth_keys = path.join(ssh, "authorized_keys") admin_repo = self.args.admin_repo email = self.args.admin_email name = self.args.admin_name username = self.args.admin_username sql = self.args.sql_connect or "sqlite://%s" % path.join( base, "phoenix.db") logging.info("Checking for permission to write the config file ...") if not File.writePermission(Config.get("CONF_FILE")): raise AdminException( "You don't have permission to write the config file `%s' ..." % Config.get("CONF_FILE")) if not SysUser.exists(self.args.git_user): logging.info("Creating user `%s' ... " % user) SysUser.create(user, base) Config.set("phoenix", "user", user) Config.set("phoenix", "base", base) else: raise AdminException("The user `%s' already exists." % user) logging.info("Saving SQL connection string `%s' ..." % sql) Config.set("phoenix", "sql_connect", sql) Config.set("phoenix", "initialized", True) Config.set("phoenix", "authorized_keys", auth_keys) __import__("os").setgid(__import__("pwd").getpwnam(user).pw_gid) __import__("os").setuid(__import__("pwd").getpwnam(user).pw_uid) logging.info( "Checking for permission to write the config file as `%s' ..." % user) if not File.writePermission(Config.get("CONF_FILE")): raise AdminException( "You don't have permission to write the config file `%s' ..." % Config.get("CONF_FILE")) from sqlobject import connectionForURI, sqlhub connection = connectionForURI(Config.get("phoenix", "sql_connect")) sqlhub.processConnection = connection self._sqlChanges() self._createDirectoryStructure(repo, tar, ssh) logging.info("Creating `%s' ..." % auth_keys) File.touch(auth_keys) logging.info( "Saving admin user information `%s' and `%s' in database ..." % (name, email)) admin = Member(username=username, email=email, name=name) if admin_repo: logging.info( "Initializing development repository at `%s/phoenix.git' ..." % repo) admin.addRepository("Phoenix Server Management", "phoenix.git") print "Done."
def init(self): if Config.get("phoenix", "initialized") == "True": raise AdminException("Already initialized.") logging.info("Defining variables for init ...") user = self.args.git_user base = path.join(self.args.base_dir, user) repo = path.join(base, self.args.repository_dir) tar = path.join(base, self.args.tarball_dir) ssh = path.join(base, ".ssh") auth_keys = path.join(ssh, "authorized_keys") admin_repo = self.args.admin_repo email = self.args.admin_email name = self.args.admin_name username = self.args.admin_username sql = self.args.sql_connect or "sqlite://%s" % path.join(base, "phoenix.db") logging.info("Checking for permission to write the config file ...") if not File.writePermission(Config.get("CONF_FILE")): raise AdminException("You don't have permission to write the config file `%s' ..." % Config.get("CONF_FILE")) if not SysUser.exists(self.args.git_user): logging.info("Creating user `%s' ... " % user) SysUser.create(user, base) Config.set("phoenix", "user", user) Config.set("phoenix", "base", base) else: raise AdminException("The user `%s' already exists." % user) logging.info("Saving SQL connection string `%s' ..." % sql) Config.set("phoenix", "sql_connect", sql) Config.set("phoenix", "initialized", True) Config.set("phoenix", "authorized_keys", auth_keys) __import__("os").setgid(__import__("pwd").getpwnam(user).pw_gid) __import__("os").setuid(__import__("pwd").getpwnam(user).pw_uid) logging.info("Checking for permission to write the config file as `%s' ..." % user) if not File.writePermission(Config.get("CONF_FILE")): raise AdminException("You don't have permission to write the config file `%s' ..." % Config.get("CONF_FILE")) from sqlobject import connectionForURI, sqlhub connection = connectionForURI(Config.get("phoenix", "sql_connect")) sqlhub.processConnection = connection self._sqlChanges() self._createDirectoryStructure(repo, tar, ssh) logging.info("Creating `%s' ..." % auth_keys) File.touch(auth_keys) logging.info("Saving admin user information `%s' and `%s' in database ..." % (name, email)) admin = Member(username=username, email=email, name=name) if admin_repo: logging.info("Initializing development repository at `%s/phoenix.git' ..." % repo) admin.addRepository("Phoenix Server Management", "phoenix.git") print "Done."