Work out valid envs from vars folder. Handle no workspace scenario

This commit is contained in:
Conor.McManus 2019-04-10 13:47:03 +02:00
parent 9ff0f37356
commit 7052e91861
3 changed files with 16 additions and 17 deletions

View file

@ -1,2 +1,9 @@
# Terraform Atmosphere
> Docker build platform for Terraform
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

View file

@ -1 +0,0 @@
dev,preprod

View file

@ -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)