const path = require('path'); const os = require('os'); module.exports = { tags: async (tln) => [], options: async (tln, args) => { args .prefix('TLN_TERRAFORM') .option('file-name', { describe: 'File name to store backend configuration', default: 'backend.tf', type: 'string' }) .option('backend', { describe: 'Defines which backend provider should be used (cloud, pg, se)', default: null, type: 'string' }) .option('org', { describe: 'Organization name', default: null, type: 'string' }) .option('project', { describe: 'Project name', default: null, type: 'string' }) .option('provider', { describe: 'Cloud provider (aws, gcp, azure, do etc.)', default: null, type: 'string' }) .option('group', { describe: 'Group of environments (dev, prod etc.)', default: null, type: 'string' }) .option('env', { describe: 'Environment (ci, dev01, qa02, uat, prod etc.)', default: null, type: 'string' }) .option('layer', { describe: 'Infrastructure instance layer (provider, network, managed, appl, tenant)', default: null, type: 'string' }) .option('suffix', { describe: 'Custom string', default: null, type: 'string' }) // pg .option('pg-conn-str', { describe: 'Postgresql backend connection string', default: null, type: 'string' }) // s3 .option('s3-bucket', { describe: 'S3 backend bucket', default: null, type: 'string' }) .option('s3-dynamodb-table', { describe: 'S3 backend dynamodb table', default: null, type: 'string' }) .option('s3-region', { describe: 'S3 backend bucket region', default: null, type: 'string' }) ; }, env: async (tln, env) => { env.TERRAFORM_HOME = env.TLN_COMPONENT_HOME; env.PATH = [env.TLN_COMPONENT_HOME, env.PATH].join(path.delimiter); }, dotenvs: async (tln) => [], inherits: async (tln) => [], depends: async (tln) => [], steps: async (tln) => [ { id: 'version', builder: async (tln, script) => { const id = script.env.TLN_COMPONENT_ID; const home = script.env.TLN_COMPONENT_HOME; const {name, version} = tln.unpackId(id); if (version) { script.set([` echo [terraform] && terraform version `]); } }}, { id: 'install', filter: '', builder: async (tln, script) => { const id = script.env.TLN_COMPONENT_ID; const home = script.env.TLN_COMPONENT_HOME; const {name, version} = tln.unpackId(id); if (version && tln.canInstallComponent(tln, id, home)) { const prefix = `${name}_${version}`; const arch = os.arch() === 'x64' ? 'amd64' : os.arch(); script.set(tln.getDownloadScript(tln, { linux: { name: `${prefix}_linux_${arch}.zip`, opts: null, url: `https://releases.hashicorp.com/terraform/${version}/${prefix}_linux_${arch}.zip` }, darwin: { name: `${prefix}_darwin_${arch}.zip`, opts: null, url: `https://releases.hashicorp.com/terraform/${version}/${prefix}_darwin_${arch}.zip` }, win32: { name: `${prefix}_windows_${arch}.zip`, opts: null, url: `https://releases.hashicorp.com/terraform/${version}/${prefix}_windows_${arch}.zip` } } )); } } }, { id: 'generate-backend', builder: async (tln, script) => { const cmds = []; const fn = script.env.TLN_TERRAFORM_FILE_NAME; if (script.env.TLN_TERRAFORM_BACKEND) { const name = []; if (script.env.TLN_TERRAFORM_PROJECT) name.push(script.env.TLN_TERRAFORM_PROJECT); if (script.env.TLN_TERRAFORM_PROVIDER) name.push(script.env.TLN_TERRAFORM_PROVIDER); if (script.env.TLN_TERRAFORM_GROUP) name.push(script.env.TLN_TERRAFORM_GROUP); if (script.env.TLN_TERRAFORM_ENV) name.push(script.env.TLN_TERRAFORM_ENV); if (script.env.TLN_TERRAFORM_LAYER) name.push(script.env.TLN_TERRAFORM_LAYER); if (script.env.TLN_TERRAFORM_SUFFIX) name.push(script.env.TLN_TERRAFORM_SUFFIX); const wName = name.join('-'); const pName = name.join('/'); // cmds.push(`echo 'terraform {' > ${fn}`); switch (script.env.TLN_TERRAFORM_BACKEND) { case 'cloud': cmds.push(`echo ' cloud {' >> ${fn}`); cmds.push(`echo ' organization = "${script.env.TLN_TERRAFORM_ORG}"' >> ${fn}`); cmds.push(`echo ' workspaces {' >> ${fn}`); cmds.push(`echo ' project = "${script.env.TLN_TERRAFORM_PROJECT}" }' >> ${fn}`); cmds.push(`echo ' name = "${wName}"' >> ${fn}`); cmds.push(`echo ' }' >> ${fn}`); cmds.push(`echo ' }' >> ${fn}`); break; case 'remote': cmds.push(`echo ' backend "remote" {' >> ${fn}`); cmds.push(`echo ' organization = "${script.env.TLN_TERRAFORM_ORG}"' >> ${fn}`); cmds.push(`echo ' workspaces { name = "${wName}" }' >> ${fn}`); cmds.push(`echo ' }' >> ${fn}`); break; case 'pg': cmds.push(`echo ' backend "pg" {' >> ${fn}`); cmds.push(`echo ' conn_str = "${script.env.TLN_TERRAFORM_PG_CONN_STR}"' >> ${fn}`); cmds.push(`echo ' schema_name = "${wName}"' >> ${fn}`); cmds.push(`echo ' }' >> ${fn}`); break; case 's3': cmds.push(`echo ' backend "s3" {' >> ${fn}`); cmds.push(`echo ' bucket = "${script.env.TLN_TERRAFORM_S3_BUCKET}"' >> ${fn}`); cmds.push(`echo ' key = "tfenvs/${pName}/terraform.tfstate"' >> ${fn}`); cmds.push(`echo ' dynamodb_table = "${script.env.TLN_TERRAFORM_S3_DYNAMODB_TABLE}"' >> ${fn}`); cmds.push(`echo ' region = "${script.env.TLN_TERRAFORM_S3_REGION}"' >> ${fn}`); cmds.push(`echo ' }' >> ${fn}`); break; } cmds.push(`echo '}' >> ${fn}`); } else { cmds.push(`rm -f ${fn}`); } script.set(cmds); }}, ], components: async (tln) => require('./components.js') }