in tools/jenkins-slave-creation-unix/scripts/create_slave.py [0:0]
def main():
logging.getLogger().setLevel(logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--configdir',
help='Deployment configuration directory',
default='conf-ubuntu-cpu',
type=str)
parser.add_argument('-tf', '--terraformdir',
help='Directory containing the terraform scripts',
default='.',
type=str)
args = parser.parse_args()
terraform_dir_abs = os.path.abspath(args.terraformdir)
if not os.path.isfile(os.path.join(terraform_dir_abs, TERRAFORM_SCRIPT_NAME)):
raise FileNotFoundError('Unable to find terraform script. Did you specify "--terraformdir"? {}'.
format(os.path.join(terraform_dir_abs, TERRAFORM_SCRIPT_NAME)))
# Copy configuration to temp dir
with TemporaryDirectory() as temp_dir:
temp_archive_path = os.path.join(temp_dir, "config.tar.bz2")
with tarfile.open(temp_archive_path, "w:bz2") as tar:
scripts_dir = os.path.join(args.terraformdir, DEPLOYED_SCRIPTS_DIR_NAME)
for file in os.listdir(scripts_dir):
logging.debug('Archiving {}'.format(file))
tar.add(os.path.join(scripts_dir, file), arcname=os.path.basename(file))
# Trigger terraform
logging.info('Running terraform...')
logging.debug('Switching current work dir to {}'.format(terraform_dir_abs))
os.chdir(terraform_dir_abs)
# Setting up the terraform S3 backend requires to have AWS credentials in the env vars - it's not able
# to access the variables file due interpolation in terraform being enabled after initialization of
# the s3 backend
env_vars = os.environ.copy()
subprocess.check_call('~/bin/terraform init -backend-config={}'.format(os.path.join(args.configdir, TERRAFORM_BACKEND_VARFILE_NAME)), cwd=terraform_dir_abs, env=env_vars, shell=True)
command = input("Terraform apply or plan? [apply]: ") or "apply"
subprocess.check_call('~/bin/terraform {} -var-file="{}" -var "slave_config_tar_path={}"'.format(command, os.path.join(args.configdir, TERRAFORM_VARFILE_NAME), temp_archive_path), cwd=terraform_dir_abs, env=env_vars, shell=True)
logging.debug('Deployment finished')