Atmos v.1
This commit is contained in:
parent
6b0d8d0974
commit
9ff0f37356
5 changed files with 52 additions and 19 deletions
13
.circleci/config.yml
Normal file
13
.circleci/config.yml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
version: 2.1
|
||||
orbs:
|
||||
aws-ecr: circleci/aws-ecr@1.0.0
|
||||
workflows:
|
||||
build_and_push_image:
|
||||
jobs:
|
||||
- aws-ecr/build_and_push_image:
|
||||
account-url: AWS_ECR_ACCOUNT_URL
|
||||
aws-access-key-id: AWS_ACCESS_KEY_ID
|
||||
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
|
||||
create-repo: false
|
||||
repo: tf-builder
|
||||
region: AWS_REGION
|
||||
|
|
@ -7,3 +7,4 @@ RUN mv terraform /usr/bin/
|
|||
|
||||
COPY shared-creds /root/.aws/credentials
|
||||
COPY atmos.py /usr/bin/atmos
|
||||
COPY atmos.config /root/.config/atmos.config
|
||||
1
atmos.config
Normal file
1
atmos.config
Normal file
|
|
@ -0,0 +1 @@
|
|||
dev,preprod
|
||||
53
atmos.py
53
atmos.py
|
|
@ -1,33 +1,52 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
import sys
|
||||
import argparse, subprocess, shlex, sys, os
|
||||
|
||||
def main(argv):
|
||||
parser = argparse.ArgumentParser(description='Control Terraform Workspaces.')
|
||||
parser.add_argument("command", help="Send commands to terraform", default=False)
|
||||
parser.add_argument("-auto", help="Flag to skip waiting for user input", action="store_true")
|
||||
args = parser.parse_args()
|
||||
determine_actions(args)
|
||||
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
|
||||
cmd = 'terraform {args}'.format(args=args.command)
|
||||
|
||||
for param in params: # Pass terraform params directly through
|
||||
cmd = cmd + ' ' + param
|
||||
|
||||
if args.command in env_actions: # Append with env context
|
||||
cmd = cmd + ' -var-file=vars/{env}.tfvars'.format(env=get_env())
|
||||
|
||||
def determine_actions(args):
|
||||
if args.auto:
|
||||
args.command = args.command + " -auto-approve"
|
||||
valid_actions = ["plan", "apply", "destroy"]
|
||||
if args.command in valid_actions:
|
||||
print('Terraform {args} using env vars in {env}'.format(args=args.command, env=get_env()))
|
||||
print(subprocess.getoutput('terraform {args} -var-file=vars/{env}.tfvars'.format(args=args.command, env=get_env())))
|
||||
else:
|
||||
print(subprocess.getoutput('terraform {args}'.format(args=args.command)))
|
||||
with subprocess.Popen(shlex.split(cmd)) as proc:
|
||||
exit # Start process but kill py program
|
||||
|
||||
def get_valid_envs():
|
||||
try:
|
||||
return open(os.path.expanduser('~/.config/atmos.config'), 'r').read().split(",")
|
||||
except FileNotFoundError:
|
||||
return False
|
||||
|
||||
def get_env():
|
||||
valid_envs = ["dev","preprod","emea","apac"]
|
||||
tf_env = subprocess.getoutput('cat .terraform/environment')
|
||||
if str(tf_env) in valid_envs:
|
||||
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)
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
[default]
|
||||
AWS_SOMETHING
|
||||
|
||||
[ldm-preprod]
|
||||
Loading…
Add table
Reference in a new issue