-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_helper.rb
59 lines (45 loc) · 1.5 KB
/
tf_helper.rb
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
#! ruby
# NOTE: this is a temporary file, this will be baked into the cli down the road.
require 'json'
terraform_cmd = ARGV[0]
bundle_name = File.basename(__dir__)
schema_params = File.read("./schema-params.json")
params = JSON.parse(schema_params)
examples = params["examples"]
template_params_file = File.read("./src/dev.params.tfvars.json")
$all_cmds = []
examples.each do |example|
name = example.delete("__name")
slug_safe_name = name.downcase.gsub(/[^a-z0-9]/, "")
outfile = "/tmp/#{rand(10000000)}.params.tfvars.json"
example_dev_params_file = template_params_file.gsub("PLACEHOLDER", slug_safe_name)
example_dev_base_params = JSON.parse(example_dev_params_file)
example_params = example_dev_base_params.merge(example)
this_run_tfvars_json = File.open(outfile, "w+") do |f|
f.puts JSON.pretty_generate(example_params)
end
var_files = "-var-file ./dev.connections.tfvars.json -var-file #{outfile}"
cmd = [
%Q{echo Example: #{name} Params: #{outfile} Workspace: #{slug_safe_name}},
"pushd ./src",
"terraform init",
"terraform workspace new #{slug_safe_name} || terraform workspace select #{slug_safe_name}"
]
addl_cmds = {
"plan" => [
"terraform plan #{var_files}"
],
"destroy" => [
"terraform destroy #{var_files} -auto-approve"
],
"apply" => [
"terraform apply #{var_files} -auto-approve"
]
}
final_cmds = [
"popd"
]
cmd += (addl_cmds[terraform_cmd] + final_cmds)
$all_cmds += (cmd)
end
puts $all_cmds.join(' && ')