-
Notifications
You must be signed in to change notification settings - Fork 341
/
terraform.js
125 lines (109 loc) · 3.03 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
const fs = require('fs').promises;
const { ltr } = require('semver');
const { exec } = require('./utils');
const MIN_TF_VER = '0.14.0';
const version = async () => {
try {
const output = await exec('terraform version -json');
const { terraform_version } = JSON.parse(output);
return terraform_version;
} 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 load_tfstate = async (opts = {}) => {
const { path } = opts;
const json = await fs.readFile(path, 'utf-8');
return JSON.parse(json);
};
const save_tfstate = 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;
return await exec(`terraform -chdir='${dir}' apply -auto-approve`);
};
const destroy = async (opts = {}) => {
const { dir = './', target } = opts;
const targetop = target ? `-target=${target}` : '';
return await exec(
`terraform -chdir='${dir}' destroy -auto-approve ${targetop}`
);
};
const iterative_provider_tpl = () => {
return `
terraform {
required_providers {
iterative = {
source = "iterative/iterative"
}
}
}
provider "iterative" {}
`;
};
const iterative_cml_runner_tpl = (opts = {}) => {
const {
repo,
token,
driver,
labels,
idle_timeout,
cloud,
region,
name,
type,
gpu,
hdd_size,
ssh_private,
spot,
spot_price
} = opts;
return `
${iterative_provider_tpl()}
resource "iterative_cml_runner" "runner" {
${repo ? `repo = "${repo}"` : ''}
${token ? `token = "${token}"` : ''}
${driver ? `driver = "${driver}"` : ''}
${labels ? `labels = "${labels}"` : ''}
${
typeof idle_timeout !== 'undefined' && idle_timeout >= 0
? `idle_timeout = ${idle_timeout}`
: ''
}
${name ? `name = "${name}"` : ''}
${cloud ? `cloud = "${cloud}"` : ''}
${region ? `region = "${region}"` : ''}
${type ? `instance_type = "${type}"` : ''}
${gpu ? `instance_gpu = "${gpu}"` : ''}
${hdd_size ? `instance_hdd_size = ${hdd_size}` : ''}
${ssh_private ? `ssh_private = "${ssh_private}"` : ''}
${spot ? `spot = ${spot}` : ''}
${spot_price ? `spot_price = ${spot_price}` : ''}
}
`;
};
const check_min_version = 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.load_tfstate = load_tfstate;
exports.save_tfstate = save_tfstate;
exports.init = init;
exports.apply = apply;
exports.destroy = destroy;
exports.iterative_provider_tpl = iterative_provider_tpl;
exports.iterative_cml_runner_tpl = iterative_cml_runner_tpl;
exports.check_min_version = check_min_version;