Add override to turn off automatically switching workspace

This commit is contained in:
Conor.McManus 2019-04-17 11:44:14 +02:00
parent 731f14eb62
commit 0b432cd88a
2 changed files with 26 additions and 21 deletions

View file

@ -1,7 +1,7 @@
[![CircleCI](https://circleci.com/gh/Spengreb/atmos.svg?style=svg)](https://circleci.com/gh/Spengreb/atmos) [![CircleCI](https://circleci.com/gh/Spengreb/atmos.svg?style=svg)](https://circleci.com/gh/Spengreb/atmos)
# Terraform Atmosphere # Terraform Atmosphere
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 on directly. 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 on directly. Atmos will automatically switch workspaces per git branches if it discovers its in a git repository
# Quick Start # Quick Start
@ -68,3 +68,7 @@ QA_SECRET_ACCESS_KEY=key
``` ```
Note: Atmos will override your default credentials file as this functionality is for use in a docker container or in situations where you would rather use variables. Note: Atmos will override your default credentials file as this functionality is for use in a docker container or in situations where you would rather use variables.
# atmos -m
Adding `-m` flag will set to manual mode. It will not try to automatically switch workspace per branch. It will adhere to whatever you last set the workspace to.

View file

@ -6,13 +6,18 @@ def main(argv):
parser = argparse.ArgumentParser(description='Control Terraform Workspaces.') parser = argparse.ArgumentParser(description='Control Terraform Workspaces.')
g = parser.add_mutually_exclusive_group() 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("command", help="Send commands to terraform with workspace variable context", nargs='?', default=False)
parser.add_argument("-e", help="Template mode, 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) 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)
parser.add_argument("-m", help="Prevents workspace from changing with git branches automatically", action='store_true', default=False)
args, params = parser.parse_known_args() args, params = parser.parse_known_args()
if args.command: if args.command:
determine_actions(args, params) determine_actions(args, params)
def determine_actions(args, params): def determine_actions(args, params):
workspace_manager() if (is_git_directory()) and not (args.m):
print(args.m)
print(is_git_directory())
workspace_manager()
workspace = get_env() workspace = get_env()
env_actions = ["plan", "apply", "destroy"] # Commands that require env context env_actions = ["plan", "apply", "destroy"] # Commands that require env context
cmd = 'terraform {args}'.format(args=args.command) cmd = 'terraform {args}'.format(args=args.command)
@ -30,25 +35,21 @@ def determine_actions(args, params):
with subprocess.Popen(shlex.split(cmd)) as proc: with subprocess.Popen(shlex.split(cmd)) as proc:
exit # Start process but kill py program exit # Start process but kill py program
def workspace_manager():
if is_git_directory != False:
branch = subprocess.getoutput("git rev-parse --abbrev-ref HEAD")
if branch == "master":
branch = "default"
else:
if branch not in get_valid_envs():
branch = "qa"
if get_env() != branch:
print("WARNING: Terraform workspace & git branch have diverged. Changing workspace to git branch...")
subprocess.call(["terraform", "workspace", "new", branch], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w'))
subprocess.call(["terraform", "workspace", "select", branch], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w'))
def is_git_directory(path = '.'): def is_git_directory(path = '.'):
if subprocess.call(["git", "branch"], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w')) != 0: return subprocess.call(['git', '-C', path, 'status'], stderr=subprocess.STDOUT, stdout = open(os.devnull, 'w')) == 0
return(False)
def workspace_manager():
branch = subprocess.getoutput("git rev-parse --abbrev-ref HEAD")
if branch == "master":
branch = "default"
else: else:
return(True) if branch not in get_valid_envs():
branch = "qa"
if get_env() != branch:
print("[INFO]: Terraform workspace & git branch have diverged. Changing workspace to git branch...")
subprocess.call(["terraform", "workspace", "new", branch], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w'))
subprocess.call(["terraform", "workspace", "select", branch], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w'))
def generate_creds(): def generate_creds():
current_workspace = get_env() current_workspace = get_env()