From 7052e91861b10e77f3b317e61e6189af91f7bdc4 Mon Sep 17 00:00:00 2001 From: "Conor.McManus" Date: Wed, 10 Apr 2019 13:47:03 +0200 Subject: [PATCH] Work out valid envs from vars folder. Handle no workspace scenario --- README.md | 9 ++++++++- atmos.config | 1 - atmos.py | 23 ++++++++--------------- 3 files changed, 16 insertions(+), 17 deletions(-) delete mode 100644 atmos.config diff --git a/README.md b/README.md index 22510c9..c9d28f4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # Terraform Atmosphere -> Docker build platform for Terraform \ No newline at end of file +Atmos is a thin wrapper for managing Terraform Workspaces easily. Using the workspace name it will select the correct .tfvar file, defaulting to a qa var file for any other workspace. This is primarily for pipelines but works just as well from the command line. It can process all terraform commands and parameters passing them directly to it. + +# Quick Start + +- Build the atmos image +- Use atmos as the build image in your CI/CD +- Include switching/creating terraform workspaces +- Use `$ atmos apply/plan/destroy` to run terraform apply whilst maintaining environment context \ No newline at end of file diff --git a/atmos.config b/atmos.config deleted file mode 100644 index 022a74a..0000000 --- a/atmos.config +++ /dev/null @@ -1 +0,0 @@ -dev,preprod \ No newline at end of file diff --git a/atmos.py b/atmos.py index 8207984..8fe6710 100755 --- a/atmos.py +++ b/atmos.py @@ -1,17 +1,14 @@ #!/usr/bin/env python3 -import argparse, subprocess, shlex, sys, os +import argparse, subprocess, shlex, sys, os, glob def main(argv): parser = argparse.ArgumentParser(description='Control Terraform Workspaces.') g = parser.add_mutually_exclusive_group() g.add_argument("command", help="Send commands to terraform with workspace variable context", nargs='?', default=False) - g.add_argument("-configure", help="Setup atmos parameters", nargs='?', default=False) args, params = parser.parse_known_args() if args.command: determine_actions(args, params) - if args.configure == None: - configure_atmos() def determine_actions(args, params): env_actions = ["plan", "apply", "destroy"] # Commands that require env context @@ -20,7 +17,7 @@ def determine_actions(args, params): for param in params: # Pass terraform params directly through cmd = cmd + ' ' + param - if args.command in env_actions: # Append with env context + if (args.command in env_actions) and (get_env() != "master"): # Append with env context cmd = cmd + ' -var-file=vars/{env}.tfvars'.format(env=get_env()) print('Terraform {args} using env vars in {env}'.format(args=args.command, env=get_env())) @@ -29,24 +26,20 @@ def determine_actions(args, params): def get_valid_envs(): try: - return open(os.path.expanduser('~/.config/atmos.config'), 'r').read().split(",") + # Use var files when present, otherwise default to qa + return [os.path.splitext(os.path.basename(x))[0] for x in glob.glob("vars/*.tfvars")] except FileNotFoundError: return False def get_env(): - tf_env = subprocess.getoutput('cat .terraform/environment') + try: + tf_env = open('.terraform/environment', 'r').read() + except: + return("master") if str(tf_env) in get_valid_envs(): return(tf_env) else: return("qa") -def configure_atmos(): - current_valid_envs = get_valid_envs() - if current_valid_envs: - print("Current special environments: {envs}".format(envs=current_valid_envs)) - valid_envs = input("Enter special environments, e.g dev,preprod,prod: ") - with open(os.path.expanduser('~/.config/atmos.config'),"w+") as f: - f.write(valid_envs) - if __name__ == "__main__": main(sys.argv) \ No newline at end of file