def _remove_user(): """remove user from dbs""" dbs = get_akrr_db(su=True), get_ak_db(su=True), get_xd_db(su=True) users = cfg.akrr_db_user, cfg.ak_db_user, cfg.xd_db_user for user, (db, cur) in zip(users, dbs): # see if user exists cur.execute("SELECT * FROM mysql.user WHERE user = %s", (user,)) records = cur.fetchall() for rec in records: log.info("Removing %s@%s from %s ", rec["User"], rec["Host"], str(db)) msg = "DRY RUN:\n" if dry_run else "" msg = msg + "SQL(%s): " % (str(db),) msg = msg + "DROP USER IF EXISTS %s@%s" % (rec["User"], rec["Host"]) if dry_run: log.info(msg) else: log.dry_run(msg) if dry_run: continue # remove user cur.execute("DROP USER IF EXISTS %s@%s", (rec["User"], rec["Host"])) db.commit() if len(records) == 0: log.info("There is no user with name %s on %s ", user, str(db)) for db, cur in dbs: cur.close() db.close()
def _make_dirs(path): """Recursively create directories if not in dry run mode""" if not dry_run: log.debug("Creating directory: {}".format(path)) os.makedirs(path) else: log.dry_run("_make_dirs({})".format(path))
def create_and_populate_tables( default_tables, population_statements, starting_comment, ending_comment, connection_function, host=None, user=None, password=None, db=None, dry_run=False ): """ :param default_tables: :param population_statements: :param starting_comment: :param ending_comment: :param connection_function: :type connection_function: function """ log.info(starting_comment) try: if not dry_run: if host and user and password and db: connection = MySQLdb.connect(host, user, password, db) cursor = connection.cursor() else: connection, cursor = connection_function(True) with connection: for (table_name, table_script) in default_tables: log.info("CREATING: %s" % table_name) try: result = cursor.execute(table_script) log.debug("Result of: %s -> %d" % (table_name, result)) log.info("CREATED: %s SUCCESSFULLY!" % table_name) except MySQLdb.Warning: pass for (description, statement) in population_statements: log.info("EXECUTING: %s" % description) result = cursor.execute(statement) log.debug("Result of: %s -> %d" % (table_name, result)) log.info("EXECUTED: %s SUCCESSFULLY!" % description) else: for (table_name, table_script) in default_tables: log.dry_run("CREATING: %s" % table_name) #log.info("CREATED: %s SUCCESSFULLY!" % table_name) for (description, statement) in population_statements: log.dry_run("EXECUTING: %s" % description) #log.info("EXECUTED: %s SUCCESSFULLY!" % description) log.info(ending_comment) except MySQLdb.Error as e: log.critical("Error %d: %s" % (e.args[0], e.args[1])) sys.exit(1)
def cursor_execute(cur, query, args=None, dry_run=False): """Execute database affecting command if not in dry run mode""" if not dry_run: cur.execute(query, args) else: from akrr import log if args is not None: if isinstance(args, dict): args = dict((key, cur.connection.literal(item)) for key, item in args.items()) else: args = tuple(map(cur.connection.literal, args)) query = query % args log.dry_run("SQL: " + query)
def check_appsig(rsh, resource): log.info("Testing app.signature calculator on headnode\n") out = cfg.sshCommand( rsh, "%s/execs/bin/appsigcheck.sh `which md5sum`" % (resource['appKerDir'], )) if out.count("===ExeBinSignature===") > 0 and out.count("MD5:") > 0: log.info("App.signature calculator is working on headnode\n") else: if dry_run: log.dry_run("App.signature calculator is not working\n") return log.error( "App.signature calculator is not working\n" + "See full error report below\n%s", out) exit(1)
def drop_db(db, cur, db_name, dry_run=False): """remove dbname database from db,cur connection""" from akrr import log cur.execute("SHOW databases") databases = cur.fetchall() if db_name in {v["Database"] for v in databases}: log.info("Removing %s database from %s ", db_name, str(db)) msg = "SQL(%s): " % (str(db), ) msg = msg + "DROP DATABASE IF EXISTS %s" % (db_name, ) if dry_run: log.dry_run(msg) if dry_run: return # remove user cur.execute("DROP DATABASE IF EXISTS %s" % (db_name, )) db.commit() else: log.info("Database %s is not present on %s ", db_name, str(db))
def generate_self_signed_certificate(self): log.info("Generating self-signed certificate for REST-API") cmd = """ openssl req \ -new \ -newkey rsa:4096 \ -days 365 \ -nodes \ -x509 \ -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=localhost" \ -keyout {akrr_cfg_dir}/server.key \ -out {akrr_cfg_dir}/server.cert cp {akrr_cfg_dir}/server.key {akrr_cfg_dir}/server.pem cat {akrr_cfg_dir}/server.cert >> {akrr_cfg_dir}/server.pem """.format(akrr_cfg_dir=os.path.join(akrr_home, 'etc')) if not dry_run: output = subprocess.check_output(cmd, shell=True) log.info(output.decode("utf-8")) log.info("New self-signed certificate have been generated") else: log.dry_run("run command: " + cmd)
def generate_settings_file(self): log.info("Generating Settings File...") with open(os.path.join(akrr_mod_dir, 'templates', 'akrr.conf'), 'r') as f: akrr_inp_template = f.read() restapi_rw_password = self.get_random_password() restapi_ro_password = self.get_random_password() var = { 'akrr_db_user_name': self.akrr_db_user_name, 'akrr_db_user_password': self.akrr_db_user_password, 'xd_db_user_name': self.xd_db_user_name, 'xd_db_user_password': self.xd_db_user_password, 'restapi_rw_password': restapi_rw_password, 'restapi_ro_password': restapi_ro_password } akrr_inp = akrr_inp_template.format(**var) if not dry_run: with open(akrr_cfg, 'w') as f: akrr_inp_template = f.write(akrr_inp) log.info("Settings written to: {0}".format(akrr_cfg)) else: log.dry_run("New config should be written to: {}".format(akrr_cfg)) log.debug2(akrr_inp)
def _stop_akrr(): akrr_daemon_pids = _get_akrr_pids() if akrr_daemon_pids is None: log.info("AKRR daemon is not running.") return log.info("AKRR daemon is running, trying to stop it.") try: if os.path.exists(os.path.join(cfg.which_akrr)): cmd = "{} {} daemon stop".format(sys.executable, cfg.which_akrr) if not dry_run: subprocess.check_output( cmd, shell=True, timeout=60) else: log.dry_run("should execute: " + cmd) except subprocess.CalledProcessError: pass akrr_daemon_pids = _get_akrr_pids() if akrr_daemon_pids is None: return try: for pid in akrr_daemon_pids: cmd = "kill -9 {}".format(pid) if not dry_run: subprocess.check_output( cmd, shell=True, timeout=60) else: log.dry_run("should execute: " + cmd) except subprocess.CalledProcessError: pass akrr_daemon_pids = _get_akrr_pids() if akrr_daemon_pids is None: return if dry_run: log.dry_run("at this point AKRR daemon should be stopped") return raise Exception("was not able to stop AKRR daemon")