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 shared-creds /root/.aws/credentials
|
||||||
COPY atmos.py /usr/bin/atmos
|
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
|
||||||
55
atmos.py
55
atmos.py
|
|
@ -1,33 +1,52 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse, subprocess, shlex, sys, os
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
parser = argparse.ArgumentParser(description='Control Terraform Workspaces.')
|
parser = argparse.ArgumentParser(description='Control Terraform Workspaces.')
|
||||||
parser.add_argument("command", help="Send commands to terraform", default=False)
|
g = parser.add_mutually_exclusive_group()
|
||||||
parser.add_argument("-auto", help="Flag to skip waiting for user input", action="store_true")
|
g.add_argument("command", help="Send commands to terraform with workspace variable context", nargs='?', default=False)
|
||||||
args = parser.parse_args()
|
g.add_argument("-configure", help="Setup atmos parameters", nargs='?', default=False)
|
||||||
determine_actions(args)
|
args, params = parser.parse_known_args()
|
||||||
|
if args.command:
|
||||||
|
determine_actions(args, params)
|
||||||
|
if args.configure == None:
|
||||||
|
configure_atmos()
|
||||||
|
|
||||||
def determine_actions(args):
|
def determine_actions(args, params):
|
||||||
if args.auto:
|
env_actions = ["plan", "apply", "destroy"] # Commands that require env context
|
||||||
args.command = args.command + " -auto-approve"
|
cmd = 'terraform {args}'.format(args=args.command)
|
||||||
valid_actions = ["plan", "apply", "destroy"]
|
|
||||||
if args.command in valid_actions:
|
for param in params: # Pass terraform params directly through
|
||||||
print('Terraform {args} using env vars in {env}'.format(args=args.command, env=get_env()))
|
cmd = cmd + ' ' + param
|
||||||
print(subprocess.getoutput('terraform {args} -var-file=vars/{env}.tfvars'.format(args=args.command, env=get_env())))
|
|
||||||
else:
|
if args.command in env_actions: # Append with env context
|
||||||
print(subprocess.getoutput('terraform {args}'.format(args=args.command)))
|
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()))
|
||||||
|
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():
|
def get_env():
|
||||||
valid_envs = ["dev","preprod","emea","apac"]
|
|
||||||
tf_env = subprocess.getoutput('cat .terraform/environment')
|
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)
|
return(tf_env)
|
||||||
else:
|
else:
|
||||||
return("qa")
|
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__":
|
if __name__ == "__main__":
|
||||||
main(sys.argv)
|
main(sys.argv)
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
[default]
|
[default]
|
||||||
AWS_SOMETHING
|
|
||||||
|
|
||||||
[ldm-preprod]
|
[ldm-preprod]
|
||||||
Loading…
Add table
Reference in a new issue