Add workspace manager to lock git branch with workspace
This commit is contained in:
parent
360e946f6c
commit
5985e6ffc9
1 changed files with 22 additions and 1 deletions
21
atmos.py
21
atmos.py
|
|
@ -12,6 +12,7 @@ def main(argv):
|
||||||
determine_actions(args, params)
|
determine_actions(args, params)
|
||||||
|
|
||||||
def determine_actions(args, params):
|
def determine_actions(args, params):
|
||||||
|
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)
|
||||||
|
|
@ -29,6 +30,26 @@ 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 = '.'):
|
||||||
|
if subprocess.call(["git", "branch"], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w')) != 0:
|
||||||
|
return(False)
|
||||||
|
else:
|
||||||
|
return(True)
|
||||||
|
|
||||||
def generate_creds():
|
def generate_creds():
|
||||||
current_workspace = get_env()
|
current_workspace = get_env()
|
||||||
workspaces = ['default']
|
workspaces = ['default']
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue