Last active
December 18, 2019 00:54
-
-
Save kendricktan/fbb8b49604bc820a283d938b389022ef to your computer and use it in GitHub Desktop.
Terraform Deployment Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import subprocess | |
import sys | |
# Python 2 and Python 3 compatability | |
try: | |
subprocess_run = subprocess.run | |
except AttributeError: | |
def run(*popenargs, **kwargs): | |
input = kwargs.pop("input", None) | |
check = kwargs.pop("handle", False) | |
if input is not None: | |
if 'stdin' in kwargs: | |
raise ValueError('stdin and input arguments may not both be used.') | |
kwargs['stdin'] = subprocess.PIPE | |
process = subprocess.Popen(*popenargs, **kwargs) | |
try: | |
stdout, stderr = process.communicate(input) | |
except: | |
process.kill() | |
process.wait() | |
raise | |
retcode = process.poll() | |
if check and retcode: | |
raise subprocess.CalledProcessError( | |
retcode, process.args, output=stdout, stderr=stderr) | |
# Make sure the API is the same | |
# with Python 2 and 3 | |
ret_obj = lambda: () | |
setattr(ret_obj, 'stderr', stderr) | |
setattr(ret_obj, 'stdout', stdout) | |
setattr(ret_obj, 'returncode', retcode) | |
return ret_obj | |
subprocess_run = run | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='Terraform-Development Deployment Helper for OmiseGO.' | |
) | |
parser.add_argument( | |
'-f', '--template-dir', required=True, | |
help='Reference dir with an existing configuration template' | |
) | |
parser.add_argument( | |
'-p', '--prefix', required=True, | |
help='Prefixes: [dev|prod|audit]' | |
) | |
parser.add_argument( | |
'-t', '--tag', required=True, | |
help='Plasma contract SHA (only first 7 characters)' | |
) | |
parser.add_argument( | |
'-n', '--network', required=True, | |
help='Ethereum network' | |
) | |
parser.add_argument( | |
'-N', '--number', required=True, | |
help='Deployment number' | |
) | |
args = parser.parse_args() | |
# Pad our number | |
new_number = args.number.rjust(2, '0') | |
# Gets root directory (of where script is executed) | |
cwd = os.getcwd() | |
old_template_dir = args.template_dir.split('/')[0] | |
old_prefix = old_template_dir.split('-')[0] | |
old_tag = old_template_dir.split('-')[1] | |
old_network = old_template_dir.split('-')[2] | |
old_number = old_template_dir.split('-')[3].replace('/', '') | |
new_dir_name = args.prefix + '-' \ | |
+ args.tag + '-' \ | |
+ args.network + '-' \ | |
+ new_number | |
new_dir_cwd = os.path.join(cwd, new_dir_name) | |
# Copy over existing dir to new dir | |
cp_result = subprocess_run( | |
['cp', '-r', args.template_dir, new_dir_name], | |
cwd=cwd | |
) | |
if cp_result.returncode != 0: | |
sys.exit(1) | |
# Edit backend.tf | |
with open(os.path.join(new_dir_cwd, 'backend.tf'), 'r') as f: | |
backend_tf_content = f.readlines() | |
backend_tf_content = list(map( | |
lambda x: x.replace(old_template_dir, new_dir_name), | |
backend_tf_content | |
)) | |
with open(os.path.join(new_dir_cwd, 'backend.tf'), 'w') as f: | |
f.writelines(backend_tf_content) | |
# Edit terraform.tfvars | |
with open(os.path.join(new_dir_cwd, 'terraform.tfvars'), 'r') as f: | |
tf_vars_content = f.readlines() | |
def modify_tf_vars_content(s): | |
# Don't wanna modify project id at this stage | |
if 'omisego-development' in s: | |
return s | |
s = s.replace(old_prefix, args.prefix) | |
s = s.replace(old_tag, args.tag) | |
s = s.replace(old_network, args.network) | |
s = s.replace(old_number, new_number) | |
return s | |
tf_vars_content = list(map( | |
modify_tf_vars_content, | |
tf_vars_content | |
)) | |
with open(os.path.join(new_dir_cwd, 'terraform.tfvars'), 'w') as f: | |
f.writelines(tf_vars_content) | |
print('Using directory ' + old_template_dir + ' as base template') | |
print('Created new directory: ' + new_dir_name) | |
print('Configuration files written ' + new_dir_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment