-
Notifications
You must be signed in to change notification settings - Fork 341
/
terraform.js
143 lines (130 loc) · 4.22 KB
/
terraform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const fs = require('fs').promises;
const { ltr } = require('semver');
const winston = require('winston');
const { exec, tfCapture } = require('./utils');
const MIN_TF_VER = '0.14.0';
const version = async () => {
try {
const output = await exec('terraform', 'version', '-json');
const { terraform_version: ver } = JSON.parse(output);
return ver;
} catch (err) {
const output = await exec('terraform', 'version');
const matches = output.match(/Terraform v(\d{1,2}\.\d{1,2}\.\d{1,2})/);
if (matches.length < 2) throw new Error('Unable to get TF version');
return matches[1];
}
};
const loadTfState = async (opts = {}) => {
const { path } = opts;
const json = await fs.readFile(path, 'utf-8');
return JSON.parse(json);
};
const saveTfState = async (opts = {}) => {
const { path, tfstate } = opts;
await fs.writeFile(path, JSON.stringify(tfstate, null, '\t'));
};
const init = async (opts = {}) => {
const { dir = './' } = opts;
return await exec('terraform', `-chdir=${dir}`, 'init');
};
const apply = async (opts = {}) => {
const { dir = './' } = opts;
const { env } = process;
if (env.TF_LOG_PROVIDER === undefined) env.TF_LOG_PROVIDER = 'DEBUG';
try {
await tfCapture(
'terraform',
[`-chdir=${dir}`, 'apply', '-auto-approve', '-json'],
{
cwd: process.cwd(),
env,
shell: true
}
);
} catch (rejectionLogs) {
process.stdout.write(rejectionLogs);
throw new Error('terraform apply error');
}
};
const destroy = async (opts = {}) => {
const { dir = './', target } = opts;
return await exec(
'terraform',
`-chdir=${dir}`,
'destroy',
'-auto-approve',
...(target ? ['-target', target] : [])
);
};
const iterativeProviderTpl = ({ tpiVersion }) => ({
terraform: {
required_providers: {
iterative: {
source: 'iterative/iterative',
...(tpiVersion && { version: tpiVersion })
}
}
},
provider: {
iterative: {}
}
});
const iterativeCmlRunnerTpl = (opts = {}) => {
const tfObj = {
...iterativeProviderTpl(opts),
resource: {
iterative_cml_runner: {
runner: {
...(opts.image && { image: opts.image }),
...(opts.awsSecurityGroup && {
aws_security_group: opts.awsSecurityGroup
}),
...(opts.awsSubnet && { aws_subnet_id: opts.awsSubnet }),
...(opts.cloud && { cloud: opts.cloud }),
...(opts.cmlVersion && { cml_version: opts.cmlVersion }),
...(opts.dockerVolumes && { docker_volumes: opts.dockerVolumes }),
...(opts.driver && { driver: opts.driver }),
...(opts.gpu && { instance_gpu: opts.gpu }),
...(opts.hddSize && { instance_hdd_size: opts.hddSize }),
...(typeof opts.idleTimeout !== 'undefined' && {
idle_timeout: opts.idleTimeout
}),
...(opts.labels && { labels: opts.labels }),
...(opts.metadata && { metadata: opts.metadata }),
...(opts.name && { name: opts.name }),
...(opts.permissionSet && {
instance_permission_set: opts.permissionSet
}),
...(opts.region && { region: opts.region }),
...(opts.repo && { repo: opts.repo }),
...(opts.single && { single: opts.single }),
...(opts.spot && { spot: opts.spot }),
...(opts.spotPrice && { spot_price: opts.spotPrice }),
...(opts.sshPrivate && { ssh_private: opts.sshPrivate }),
...(opts.startupScript && { startup_script: opts.startupScript }),
...(opts.token && { token: opts.token }),
...(opts.type && { instance_type: opts.type })
}
}
}
};
winston.debug(`terraform data: ${JSON.stringify(tfObj)}`);
return tfObj;
};
const checkMinVersion = async () => {
const ver = await version();
if (ltr(ver, MIN_TF_VER))
throw new Error(
`Terraform version must be greater that 14: current ${ver}`
);
};
exports.version = version;
exports.loadTfState = loadTfState;
exports.saveTfState = saveTfState;
exports.init = init;
exports.apply = apply;
exports.destroy = destroy;
exports.iterativeProviderTpl = iterativeProviderTpl;
exports.iterativeCmlRunnerTpl = iterativeCmlRunnerTpl;
exports.checkMinVersion = checkMinVersion;