diff --git a/README.md b/README.md index 792c447..f8b74e0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ [![CircleCI](https://circleci.com/gh/Spengreb/atmos.svg?style=svg)](https://circleci.com/gh/Spengreb/atmos) # 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 @@ -67,4 +67,8 @@ QA_ACCESS_KEY_ID=id 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. \ No newline at end of file +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. \ No newline at end of file diff --git a/atmos.py b/atmos.py index 8a8b43d..0788475 100755 --- a/atmos.py +++ b/atmos.py @@ -6,13 +6,18 @@ 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) - 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() if args.command: 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() env_actions = ["plan", "apply", "destroy"] # Commands that require env context cmd = 'terraform {args}'.format(args=args.command) @@ -30,25 +35,21 @@ def determine_actions(args, params): with subprocess.Popen(shlex.split(cmd)) as proc: 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 = '.'): - if subprocess.call(["git", "branch"], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w')) != 0: - return(False) + return subprocess.call(['git', '-C', path, 'status'], stderr=subprocess.STDOUT, stdout = open(os.devnull, 'w')) == 0 + +def workspace_manager(): + branch = subprocess.getoutput("git rev-parse --abbrev-ref HEAD") + if branch == "master": + branch = "default" 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(): current_workspace = get_env()