Add override to turn off automatically switching workspace
This commit is contained in:
parent
731f14eb62
commit
0b432cd88a
2 changed files with 26 additions and 21 deletions
|
|
@ -1,7 +1,7 @@
|
|||
[](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.
|
||||
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.
|
||||
39
atmos.py
39
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()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue