def create(self, listemail, listadmin, password): """create a new mailing list. newlist -u webdomain -e emaildomain listname listadmin-addr admin-password""" email, domain = listemail.split('@') if email not in self.list(): log.info('adding mailman list: %s' % (email)) cmd = "newlist -q -u %s -e %s %s %s '%s'" % (domain, domain, email, listadmin, password) run(cmd)
def psql(self, cmd): """run cmd against psql""" ret, out = run("psql %s" % cmd) if ret: log.error('psql error: %s' % out) return False else: return out
def runAdmin(self, arg): """run mysqladmin util with cmd as ''""" cmd = "mysqladmin %s " % (arg) if self.rootpw: cmd += "--password=%s" % (self.rootpw) ret, out = run(cmd) if ret: log.error('problem with mysqladmin:%s' % arg) else: return out
def dumpAll(self, filename): """dump all databases to given filename""" cmd = "mysqldump" if self.rootpw: cmd += " --password=%s" % (self.rootpw) cmd += " --all-databases > %s" % getFilename(filename) ret, out = run(cmd) if ret: log.error('problem with dumpAll:%s' % out) else: return out
def genSSHKey(self, user): """generate a user SSH key, assuming it's not already created. returns full path to public key file. """ homedir = pwd.getpwnam(user)[5] sshdir = os.path.join(homedir, '.ssh') keyfile = self.findSSHKeyFile(user) if keyfile: return keyfile #otherwise, we need to generate it. if not os.path.exists(sshdir): os.makedirs(sshdir) run('sudo -u %s ssh-keygen -q -t rsa -N "" -f %sid_rsa' % (user, sshdir + os.sep)) keyfile = self.findSSHKeyFile(user) if keyfile: return keyfile else: raise 'error creating sshKeyFile for user: %s' return False
def list(self, refresh=False): """list mailing lists. 2 matching mailing lists found: Africa-list - [no description available] Mailman - [no description available]""" lists = {} ret, out = run('list_lists') for l in out[1:]: name, desc = l.split(' - ') lists[name.strip()] = desc.strip() return lists
def query(self, query, DB=None): """given a SQL query, execute and return results as []""" if DB: query = "USE %s;\"%s\"" % (DB, query) else: query = "\"%s;\"" % query cmd = "echo %s | mysql -s mysql " % (query) if self.rootpw: cmd += "--password=%s" % (self.rootpw) ret, out = run(cmd) if not ret: return out else: log.error('problem executing query: %s' % query)
def runFile(self, sqlfile, db, username=None, password=None): """run file against mysql""" if not username: username = '******' if not password: password = self.rootpw cmd = "mysql --user=%s" % (username) if password: cmd += " --password=%s" % (password) cmd += " %s < %s" % (db, sqlfile) log.debug('running runFile:%s' % (sqlfile)) ret, out = run(cmd) if not ret: return out else: log.error('problem loading mysql file: %s' % (sqlfile)) return False
def run(self, action): if self.enabled: host = self.options.setdefault('host', 'localhost') cmd = "%s" % (self.check_postgres) if self.options.setdefault('host'): cmd += " -H %s" % self.options['host'] if self.options.setdefault('user'): cmd += " -u %s" % self.options['user'] if self.options.setdefault('pass'): cmd += " --dbpass=%s" % (self.options['pass']) cmd += " --action=%s" % action ret, output = run(cmd) if ret: #check_postgres returns log.critical('PGMonitor:%s %s' % (action, output)) else: ret = None else: ret = False return ret