def do_bash_script(working_dir, filename, verb, data): # bash scripts logger.info(f"bash script: {filename}") logger.info( f"write JSON to file at ${constants.KEY_INTERMEDIATE_DATABAG} to add to databag" ) util.run_cmd(f"bash {filename} {verb}", data) load_intermediate_databag(data)
def system_info(): aws_version = util.run_cmd(["aws", "--version"]).strip() eksctl_version = util.run_cmd(["eksctl", "version"]).strip() kubectl_version = util.run_cmd( ["kubectl", "version", "--short", "--client"]).strip() helm_version = util.run_cmd(["helm", "version", "--short"]).strip() message = util.process_res_template( "system_info.txt", aws_version=aws_version, boto3_version=aws.boto3_version(), cloudflare_version=cloudflare.cloudflare_version(), eksctl_version=eksctl_version, helm_version=helm_version, kubectl_version=kubectl_version, snowflake_connector_version=snowflake.snowflake_connector_version()) logger.info(message)
def create_csr(hostname): _, csr_temp = tempfile.mkstemp() _, private_key_temp = tempfile.mkstemp() cmd = f"""openssl \ req -new -newkey rsa:2048 -nodes \ -out {csr_temp} \ -keyout {private_key_temp} \ -subj "/C=US/ST=California/L=/O=/CN={hostname}" """ util.run_cmd(cmd) # read both files into memory and return them, deleteing the tempfiles with open(csr_temp) as f: csr_data = f.read() with open(private_key_temp) as f: private_key_data = f.read() os.remove(csr_temp) os.remove(private_key_temp) return csr_data, private_key_data
def helm_repos(base_cmd, config, filename): try: repos_list = util.run_cmd_json(base_cmd + ["repo", "list", "--output", "json"]) except RuntimeError as e: logger.warning( f"""Listing helm repositories failed, this could be because you have no repositories installed helm could be broken completely: {e}""") repos_list = [] logger.debug(f"helm repos installed: {repos_list}") repos_installed = list(map(lambda x: x["name"], repos_list)) logger.debug(f"helm repos installed: {repos_installed}") repos = config.get("repos", {}) if repos: for repo_key, source in config.get("repos", {}).items(): logger.debug(f"helm - request install repo:{repo_key}") if repo_key not in repos_installed: logger.info(f"helm - installing repo:{repo_key}") cmd = base_cmd + ["repo", "add", repo_key, source] util.run_cmd(cmd) else: logger.warning(f"helm - no helm repos specified in {filename}")
def do_eksctl(working_dir, filename, verb, data): logger.info(f"eksctl: ${filename}") processed_filename = util.substitute_placeholders_from_file_to_file( working_dir, filename, "#", verb, data) config = util.read_yaml_file(processed_filename) # eksctl needs cluster name - grab this well-known fields from # processed yaml file to avoid needing well-known keys in databag try: cluster_name = config["metadata"]["name"] except KeyError as e: raise RuntimeError( f"eksctl file:{filename} missing required value {e}") try: util.run_cmd(get_eksctl_cmd() + ["get", "cluster", "-n", cluster_name], data) exists = True except RuntimeError as e: logger.debug(f"cluster probably doesnt exist: {e}") exists = False if verb == constants.UP_VERB and exists: logger.info(constants.MSG_UP_TO_DATE) elif verb == constants.UP_VERB and not exists: util.run_cmd( get_eksctl_cmd() + ["create", "cluster", "-f", processed_filename], data) elif verb == constants.DOWN_VERB and exists: util.run_cmd( get_eksctl_cmd() + ["delete", "cluster", "--name", cluster_name], data) elif verb == constants.DOWN_VERB and not exists: logger.info(constants.MSG_UP_TO_DATE) else: raise RuntimeError(f"eksctl - invalid verb: {verb}") if verb == constants.UP_VERB: eks_cluster_info(cluster_name, data)
def do_helm(working_dir, filename, verb, data=None): logger.info(f"helm: {filename}") # settings for helm... processed_filename = util.substitute_placeholders_from_file_to_file( working_dir, filename, "#", verb, data) config = util.read_yaml_file(processed_filename) # if there is an adjacent `values.yaml` file, process it for substitutions and use it values_yaml = os.path.join(os.path.dirname(filename), "values.yaml") if os.path.exists(values_yaml): processed_values_file = util.substitute_placeholders_from_file_to_file( working_dir, values_yaml, "#", verb, data) logger.debug(f"helm - values: {processed_values_file}") else: processed_values_file = False if verb == constants.UP_VERB: helm_command = "install" elif verb == constants.DOWN_VERB: helm_command = "uninstall" else: raise RuntimeError(f"helm - invalid verb: {verb}") base_cmd = get_helm_cmd() if data.get("debug"): base_cmd.append("--debug") if config.get("namespace"): namespace = config['namespace'] logger.debug(f"using namespace {namespace}") base_cmd.append("-n") base_cmd.append(namespace) try: # helm repos... if verb == constants.UP_VERB: helm_repos(base_cmd, config, filename) # helm deployments helm_list = util.run_cmd_json(base_cmd + ["list", "--output", "json"]) helm_deployments = list(map(lambda x: x["name"], helm_list)) logger.debug(f"helm deployments installed: {helm_deployments}") exists = config["name"] in helm_deployments if (verb == constants.UP_VERB and exists) or (verb == constants.DOWN_VERB and not exists): logger.info(f"helm - already installed:{config['name']}") elif (verb == constants.UP_VERB and not exists) or (verb == constants.DOWN_VERB and exists): logger.info(f"helm - {helm_command}:{config['name']}") cmd = base_cmd + [helm_command, config["name"]] if verb == constants.UP_VERB: cmd.append(config["install"]) if processed_values_file: cmd.append("--values") cmd.append(processed_values_file) else: for setting in config.get("set", []): cmd.append("--set") cmd.append(setting) cmd += config.get("options", []) version = config.get("version") if version: cmd += ["--version", version] else: logger.warning( f"recommending versioning helm chart in {filename}") cmd += config.get("options", []) util.run_cmd(["helm", "repo", "update"]) util.run_cmd(cmd) else: raise RuntimeError(f"helm - invalid verb {verb}") except KeyError as e: raise RuntimeError(f"helm - {filename} missing key: {e}") except RuntimeError as e: if verb == constants.DOWN_VERB: logger.error(f"helm - error running helm, moving on: {e}") else: raise e