def create_terraform_stack(cluster_name, tf_vars, dir_path, state_path): hostname = re.sub(r"[^a-zA-Z0-9]+", '-', cluster_name).lower() tf_vars['hostname'] = hostname state_file = "{}/{}.tfstate".format(state_path, tf_vars['cluster_uuid']) tf_vars_file = create_tf_vars_file(state_path, tf_vars) tf = Terraform(dir_path) return_code, stdout, stderr = tf.get(capture_output=False) return_code, stdout, stderr = tf.init(capture_output=False) return_code, stdout, stderr = tf.apply(var_file=tf_vars_file, skip_plan=True, auto_approve=IsFlagged, capture_output=False, state=state_file) return return_code, stdout, stderr
def delete_terraform_stack(cluster_uuid, project_id, dir_path, state_path, proejct_deleted): state_file = "{}/{}.tfstate".format(state_path, cluster_uuid) tf_vars_file = "{}/vars.tf".format(state_path) tf = Terraform(dir_path) return_code, stdout, stderr = tf.get(capture_output=False) return_code, stdout, stderr = tf.init(capture_output=False) return_code, stdout, stderr = tf.destroy(var_file=tf_vars_file, auto_approve=IsFlagged, capture_output=False, state=state_file) shutil.rmtree(state_path) if proejct_deleted: shutil.rmtree("{}/{}".format(dir_path, project_id)) return return_code, stdout, stderr