2019-04-09 16:54:28 +02:00
#!/usr/bin/env python3
2019-04-10 13:47:03 +02:00
import argparse , subprocess , shlex , sys , os , glob
2019-10-29 21:29:27 +01:00
import workspaces
import credentials
2019-04-09 16:54:28 +02:00
def main ( argv ) :
parser = argparse . ArgumentParser ( description = ' Control Terraform Workspaces. ' )
2019-04-10 12:00:01 +02:00
g = parser . add_mutually_exclusive_group ( )
g . add_argument ( " command " , help = " Send commands to terraform with workspace variable context " , nargs = ' ? ' , default = False )
2019-11-13 12:04:41 +01:00
parser . add_argument ( " -e " , help = " Gather shared-creds from environment variables (Dont use this flag if you dont want your ~/.aws/credentials replaced.) This is for CI/CD " , action = ' store_true ' , default = False )
2019-04-17 11:44:14 +02:00
parser . add_argument ( " -m " , help = " Prevents workspace from changing with git branches automatically " , action = ' store_true ' , default = False )
2019-07-17 09:34:14 +02:00
parser . add_argument ( " -n " , help = " Atmos will not add -var-file or -var args to terraform " , action = ' store_true ' , default = False )
2019-07-17 10:15:21 +02:00
parser . add_argument ( " -p " , " --project " , help = " Add a project prefix for env vars " , nargs = ' ? ' , default = " " )
parser . add_argument ( " -v " , " --verbose " , help = " Debug mode " , action = " store_true " , default = False )
2019-04-10 12:00:01 +02:00
args , params = parser . parse_known_args ( )
if args . command :
determine_actions ( args , params )
def determine_actions ( args , params ) :
2019-07-16 17:12:34 +02:00
aws_creds_file = " $HOME/.aws/credentials "
2019-04-17 11:44:14 +02:00
if ( is_git_directory ( ) ) and not ( args . m ) :
2019-11-13 11:58:37 +01:00
# if (args.e):
# aws_creds_file = aws_creds_file + "-atmos"
2019-10-29 21:29:27 +01:00
workspaces . workspace_manager ( )
2019-04-17 11:44:14 +02:00
2019-10-29 21:29:27 +01:00
workspace = workspaces . get_env ( )
2019-08-01 11:01:45 +02:00
workspace_vars = workspace
2019-11-13 11:34:51 +01:00
if ( args . project ) and workspace != ' default ' :
2019-08-15 17:32:45 +02:00
workspace = args . project + " - " + workspace
2019-08-01 11:01:45 +02:00
env_actions = [ " init " , " plan " , " apply " , " destroy " ] # Commands that require env context
2019-04-10 12:00:01 +02:00
cmd = ' terraform {args} ' . format ( args = args . command )
2019-07-17 09:34:14 +02:00
if ( args . command in env_actions ) and not ( args . n ) : # Append with env context
2019-08-01 11:01:45 +02:00
cmd = cmd + ' -var-file=vars/ {env} .tfvars ' . format ( env = workspace_vars )
cmd = cmd + ' -var " workspace= {env} " ' . format ( env = workspace )
2019-07-17 10:15:21 +02:00
cmd = cmd + ' -var " shared_credentials_file= {aws_creds_file} " ' . format ( aws_creds_file = aws_creds_file )
2019-06-12 15:55:01 +02:00
2019-04-10 12:00:01 +02:00
for param in params : # Pass terraform params directly through
cmd = cmd + ' ' + param
2019-04-17 10:59:30 +02:00
if ( args . e ) :
2019-10-29 21:29:27 +01:00
credentials . generate ( args )
2019-07-17 10:15:21 +02:00
if ( args . verbose ) :
print ( " Atmos will run: " + cmd )
2019-08-01 11:01:45 +02:00
print ( ' Terraform {args} using env vars in {env} ' . format ( args = args . command , env = workspace_vars ) )
2019-10-29 21:29:27 +01:00
run_cmd ( cmd )
def run_cmd ( cmd ) :
2019-04-10 12:00:01 +02:00
with subprocess . Popen ( shlex . split ( cmd ) ) as proc :
exit # Start process but kill py program
2019-08-30 13:40:14 +02:00
def is_git_directory ( ) :
return subprocess . call ( [ ' git ' , ' branch ' ] , stderr = subprocess . STDOUT , stdout = open ( os . devnull , ' w ' ) ) == 0
2019-04-17 11:44:14 +02:00
2019-04-09 16:54:28 +02:00
if __name__ == " __main__ " :
main ( sys . argv )