def establish_replica(ctx): s_rt_props = ctx.source.instance.runtime_properties t_rt_props = ctx.target.instance.runtime_properties t_props = ctx.target.node.properties if t_rt_props["fqdn"] != s_rt_props["members"][0]: return # Only first member instantiates replica, all others do nothing ctx.logger.info("Creating replica {}".format(ctx.target.node.id)) script = _prep_replica_script(s_rt_props["members"], t_props["type"], ctx.target.node.id) ctx.logger.debug("Replica script: {}".format(script)) with tempfile.NamedTemporaryFile(suffix=".js") as f: f.write(script) f.flush() cmd = ["mongo", "--host", t_rt_props["fqdn"], f.name] ctx.logger.debug("Calling {}".format(cmd)) proc, log = utils.call(cmd, run_in_background=False) proc.wait() ctx.logger.debug("Return value: {}".format(proc.returncode)) with open(log, "r") as f: ctx.logger.debug("Mongo output:") ctx.logger.debug(f.read()) if proc.returncode != 0: # We rety here because mongod is a bit slow when it comes to starting ctx.operation.retry("Mongo failed to create replica") s_rt_props["replica_name"] = ctx.target.node.id
def create_user(ctx): props = ctx.node.properties attrs = ctx.instance.runtime_properties ctx.logger.info("Creating user {}".format(props["username"])) host = next(rel for rel in ctx.instance.relationships if rel.type == "dice.relationships.ContainedIn").target admin_user = host.instance.runtime_properties["admin_user"] admin_pass = host.instance.runtime_properties["admin_pass"] chars = string.letters + string.digits user_pass = ''.join(random.choice(chars) for _ in range(30)) script = _prepare_add_user_script(props["username"], user_pass, attrs["databases"]) cmd = [ "mongo", "-u", admin_user, "-p", admin_pass, "--authenticationDatabase", "admin", "--eval", script ] proc, log = utils.call(cmd, run_in_background=False) proc.wait() with open(log, "r") as f: ctx.logger.debug("Mongo output:") ctx.logger.debug(f.read()) if proc.returncode != 0: # We rety here because mongo is a bit slow when it comes to starting ctx.operation.retry("Mongo failed to add user") attrs["password"] = user_pass
def monitor_db(ctx): if not ctx.node.properties["monitoring"]["enabled"]: ctx.logger.info("Monitoring is disabled. Skiping registration.") return config_file = "/etc/collectd/collectd.conf.d/mongo.conf" name = ctx.node.properties["name"] ctx.logger.info("Adding {} to monitored database list".format(name)) with open(config_file) as c: config = c.read() if re.search(r'Database[^\n]*"{}"'.format(name), config) is not None: return # Database already monitored with tempfile.NamedTemporaryFile(delete=False) as f: f.write(re.sub(r"(Database.*)\n", r'\1 "{}"\n'.format(name), config)) utils.call(["sudo", "cp", f.name, config_file], False)[0].wait() utils.call(["sudo", "systemctl", "restart", "collectd"], False)[0].wait()
def submit(ctx, jar, name, klass, args): ctx.logger.info("Obtaining application jar '{}'".format(jar)) local_jar = utils.obtain_resource(ctx, jar) ctx.logger.info("Application jar stored as '{}'".format(local_jar)) ctx.logger.info("Submitting '{}' as '{}'".format(local_jar, name)) base_cmd = ["spark-submit", "--deploy-mode", "client", "--class", klass, local_jar, name] ctx.logger.info("COMMAND: {}".format(base_cmd + args)) proc, log = utils.call(base_cmd + args, run_in_background=True) ctx.logger.info("LOG FILE: {}".format(log))
def submit(ctx, jar, name, klass, args): ctx.logger.info("Obtaining application jar '{}'".format(jar)) local_jar = utils.obtain_resource(ctx, jar) ctx.logger.info("Application jar stored as '{}'".format(local_jar)) ctx.logger.info("Submitting '{}' as '{}'".format(local_jar, name)) base_cmd = [ "spark-submit", "--deploy-mode", "client", "--class", klass, local_jar, name ] ctx.logger.info("COMMAND: {}".format(base_cmd + args)) proc, log = utils.call(base_cmd + args, run_in_background=True) ctx.logger.info("LOG FILE: {}".format(log))
def add_shard(ctx): s_rt_props = ctx.source.instance.runtime_properties t_rt_props = ctx.target.instance.runtime_properties ctx.logger.info("Adding shard {}".format(t_rt_props["replica_name"])) script = _prep_shard_script(t_rt_props["members"][0], t_rt_props["replica_name"]) ctx.logger.debug("Shard script: {}".format(script)) cmd = ["mongo", "--host", s_rt_props["fqdn"], "--eval", script] ctx.logger.debug("Calling {}".format(cmd)) proc, log = utils.call(cmd, run_in_background=False) proc.wait() with open(log, "r") as f: ctx.logger.debug("Mongo output:") ctx.logger.debug(f.read()) if proc.returncode != 0: # We rety here because mongos is a bit slow when it comes to starting ctx.operation.retry("Mongo failed to add shard")