terraform-wrapper 0.2.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/terraform-wrapper.rb +1 -1
- data/lib/terraform-wrapper/common.rb +12 -1
- data/lib/terraform-wrapper/shared/auths/azure.rb +44 -37
- data/lib/terraform-wrapper/shared/auths/common.rb +3 -7
- data/lib/terraform-wrapper/shared/backends/aws.rb +16 -16
- data/lib/terraform-wrapper/shared/backends/azure.rb +14 -14
- data/lib/terraform-wrapper/shared/backends/common.rb +3 -7
- data/lib/terraform-wrapper/shared/backends/local.rb +9 -8
- data/lib/terraform-wrapper/shared/binary.rb +2 -2
- data/lib/terraform-wrapper/shared/code.rb +2 -2
- data/lib/terraform-wrapper/shared/config.rb +19 -59
- data/lib/terraform-wrapper/shared/runner.rb +5 -10
- data/lib/terraform-wrapper/shared/variables.rb +73 -10
- data/lib/terraform-wrapper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5f6f6de51796b0d23045b3733faa6becaad7ad36abf07a653aa2bfc750623f6
|
4
|
+
data.tar.gz: 83250f035ea1f126fb74f51bd171dd72b359e58df9cc39f707c38b928612f25e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd8fda5cc8485316ccd447b2ab4d713d8b78e7cee01d01f29336c15b11684ee44d0a19009b3c5dfda1e2b21d863f01f9ead3551199089870593a2fd8dd0f7f82
|
7
|
+
data.tar.gz: 04bc0285f62cf3a6c8ff9e80cf38aea09a5d3e17ce78d3fe8464c16fc36eeebea5e11bd5e295629a5117a18a147f2cb0d3f6fa6596955072f969e333fd56ef2c
|
data/lib/terraform-wrapper.rb
CHANGED
@@ -22,7 +22,7 @@ module TerraformWrapper
|
|
22
22
|
|
23
23
|
###############################################################################
|
24
24
|
|
25
|
-
def self.
|
25
|
+
def self.deployment_tasks(component:, options: Hash.new, service:)
|
26
26
|
@logger.info("Building tasks for service: #{service}, component: #{component}...")
|
27
27
|
|
28
28
|
@logger.fatal("Options must be specified as a hash!") unless options.kind_of?(Hash)
|
@@ -4,7 +4,7 @@ module TerraformWrapper
|
|
4
4
|
|
5
5
|
###############################################################################
|
6
6
|
|
7
|
-
def self.create_directory(directory:,
|
7
|
+
def self.create_directory(directory:, exists: true, purpose: nil)
|
8
8
|
return exists if File.directory?(directory)
|
9
9
|
|
10
10
|
result = false
|
@@ -20,6 +20,17 @@ module TerraformWrapper
|
|
20
20
|
result = File.directory?(directory)
|
21
21
|
end
|
22
22
|
|
23
|
+
###############################################################################
|
24
|
+
|
25
|
+
def self.find(base:, name:, exts:, description: "File")
|
26
|
+
exts.each do |ext|
|
27
|
+
path = File.join(base, name + ext)
|
28
|
+
return path if File.file?(path)
|
29
|
+
end
|
30
|
+
|
31
|
+
@logger.fatal("#{description} name: #{name} not found in location: #{base}!")
|
32
|
+
end
|
33
|
+
|
23
34
|
###############################################################################
|
24
35
|
|
25
36
|
end
|
@@ -25,27 +25,34 @@ module TerraformWrapper
|
|
25
25
|
|
26
26
|
###############################################################################
|
27
27
|
|
28
|
-
@
|
28
|
+
@keyvault = nil
|
29
|
+
@secret_username = nil
|
30
|
+
@secret_password = nil
|
29
31
|
|
30
32
|
###############################################################################
|
31
33
|
|
32
|
-
|
33
|
-
attr_reader :tenant
|
34
|
-
attr_reader :username
|
34
|
+
@subscription
|
35
35
|
|
36
36
|
###############################################################################
|
37
37
|
|
38
|
-
def initialize(
|
39
|
-
construct(
|
38
|
+
def initialize(options:, variables:)
|
39
|
+
construct(options: options, variables: variables)
|
40
40
|
end
|
41
41
|
|
42
42
|
###############################################################################
|
43
43
|
|
44
44
|
def auth()
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
details = subscription_details(subscription: @subscription)
|
46
|
+
|
47
|
+
subscription = details["id"]
|
48
|
+
tenant = details["tenant"]
|
49
|
+
username = @keyvault.nil? ? nil : secret(vault: @keyvault, name: @secret_username)
|
50
|
+
password = @keyvault.nil? ? nil : secret(vault: @keyvault, name: @secret_password)
|
51
|
+
|
52
|
+
ENV["ARM_SUBSCRIPTION_ID"] = subscription
|
53
|
+
ENV["ARM_TENANT_ID"] = tenant
|
54
|
+
ENV["ARM_CLIENT_ID"] = username unless username.nil?
|
55
|
+
ENV["ARM_CLIENT_SECRET"] = password unless password.nil?
|
49
56
|
logger.success("Azure authenticator environment variables set!")
|
50
57
|
end
|
51
58
|
|
@@ -78,7 +85,7 @@ module TerraformWrapper
|
|
78
85
|
stdout = `#{cmdline}`
|
79
86
|
code = $?.exitstatus
|
80
87
|
|
81
|
-
logger.fatal("Failed to get secret: #{name} from key vault: #{vault}!") if (code != 0
|
88
|
+
logger.fatal("Failed to get secret: #{name} from key vault: #{vault}!") if (code != 0 or stdout.strip.empty?)
|
82
89
|
|
83
90
|
return(stdout.strip)
|
84
91
|
end
|
@@ -97,11 +104,11 @@ module TerraformWrapper
|
|
97
104
|
details = YAML.load(stdout.strip)
|
98
105
|
|
99
106
|
logger.fatal("Returned details did not include the subscription ID!") unless details.key?("id")
|
100
|
-
logger.fatal("Returned subscription ID is not a
|
107
|
+
logger.fatal("Returned subscription ID is not a string!") unless details["id"].kind_of?(String)
|
101
108
|
logger.fatal("Returned subscription ID is empty!") if details["id"].strip.empty?
|
102
109
|
|
103
110
|
logger.fatal("Returned details did not include the tenant ID!") unless details.key?("tenant")
|
104
|
-
logger.fatal("Returned tenant ID is not a
|
111
|
+
logger.fatal("Returned tenant ID is not a string!") unless details["tenant"].kind_of?(String)
|
105
112
|
logger.fatal("Returned tenant ID is empty!") if details["tenant"].strip.empty?
|
106
113
|
|
107
114
|
details.transform_values! { |value| value.strip }
|
@@ -120,44 +127,44 @@ module TerraformWrapper
|
|
120
127
|
logger.fatal("Azure CLI must be installed and accessible to use the Azure authenticator.") unless cli
|
121
128
|
|
122
129
|
logger.fatal("Azure authenticator mandatory option 'subscription' has not been set!") unless @options.key?("subscription")
|
130
|
+
logger.fatal("Azure authenticator subscription must be a string!") unless @options["subscription"].kind_of?(String)
|
131
|
+
logger.fatal("Azure authenticator subscription must not be blank!") if @options["subscription"].strip.empty?
|
123
132
|
|
124
133
|
subscription = @options["subscription"]
|
125
134
|
|
126
|
-
logger.fatal("Azure authenticator subscription must be a String!") unless subscription.kind_of?(String)
|
127
|
-
logger.fatal("Azure authenticator subscription must not be blank!") if subscription.strip.empty?
|
128
|
-
|
129
135
|
if @options.key?("keyvault") then
|
130
|
-
keyvault
|
136
|
+
logger.fatal("Azure authenticator keyvault name must be a string if specified!") unless @options["keyvault"].kind_of?(String)
|
137
|
+
logger.fatal("Azure authenticator keyvault name must not be blank if specified!") if @options["keyvault"].strip.empty?
|
131
138
|
|
132
|
-
|
133
|
-
logger.fatal("Azure authenticator keyvault name must not be blank if specified!") if keyvault.strip.empty?
|
139
|
+
keyvault = @options["keyvault"]
|
134
140
|
|
135
|
-
|
141
|
+
if @options.key?("username-secret") then
|
142
|
+
logger.fatal("Azure authenticator keyvault secret for username must be a string if keyvault name is specified!") unless @options["username-secret"].kind_of?(String)
|
143
|
+
logger.fatal("Azure authenticator keyvault secret for username must not be blank if keyvault name is specified!") if @options["username-secret"].strip.empty?
|
136
144
|
|
137
|
-
|
138
|
-
|
145
|
+
username = @options["username-secret"]
|
146
|
+
else
|
147
|
+
username = "terraform-username"
|
148
|
+
end
|
139
149
|
|
140
|
-
|
150
|
+
if @options.key?("password-secret") then
|
151
|
+
logger.fatal("Azure authenticator keyvault secret for password must be a string if keyvault name is specified!") unless @options["password-secret"].kind_of?(String)
|
152
|
+
logger.fatal("Azure authenticator keyvault secret for password must not be blank if keyvault name is specified!") if @options["password-secret"].strip.empty?
|
141
153
|
|
142
|
-
|
143
|
-
|
154
|
+
password = @options["password-secret"]
|
155
|
+
else
|
156
|
+
password = "terraform-password"
|
157
|
+
end
|
144
158
|
end
|
145
159
|
|
146
160
|
begin
|
147
|
-
subscription
|
148
|
-
keyvault
|
149
|
-
|
150
|
-
|
161
|
+
@subscription = subscription % @variables.identifiers
|
162
|
+
@keyvault = keyvault % @variables.identifiers unless keyvault.nil?
|
163
|
+
@secret_username = username % @variables.identifiers unless keyvault.nil?
|
164
|
+
@secret_password = password % @variables.identifiers unless keyvault.nil?
|
151
165
|
rescue
|
152
|
-
logger.fatal("Azure authenticator options contain
|
166
|
+
logger.fatal("Azure authenticator options contain identifiers that are not included in the configuration file!")
|
153
167
|
end
|
154
|
-
|
155
|
-
details = subscription_details(subscription: subscription)
|
156
|
-
|
157
|
-
@subscription = details["id"]
|
158
|
-
@tenant = details["tenant"]
|
159
|
-
@username = keyvault.nil? ? nil : secret(vault: keyvault, name: username)
|
160
|
-
@password = keyvault.nil? ? nil : secret(vault: keyvault, name: password)
|
161
168
|
end
|
162
169
|
|
163
170
|
###############################################################################
|
@@ -29,7 +29,7 @@ module TerraformWrapper
|
|
29
29
|
|
30
30
|
###############################################################################
|
31
31
|
|
32
|
-
def initialize(
|
32
|
+
def initialize(options:, variables:)
|
33
33
|
logger.fatal("This class should not be used directly! Please create an authenticator-specific class instead!")
|
34
34
|
end
|
35
35
|
|
@@ -59,13 +59,9 @@ module TerraformWrapper
|
|
59
59
|
|
60
60
|
###############################################################################
|
61
61
|
|
62
|
-
def construct(
|
62
|
+
def construct(options:, variables:)
|
63
63
|
@options = options
|
64
|
-
@variables = variables
|
65
|
-
component: code.name,
|
66
|
-
config: config,
|
67
|
-
service: service
|
68
|
-
})
|
64
|
+
@variables = variables
|
69
65
|
|
70
66
|
specific
|
71
67
|
end
|
@@ -35,8 +35,8 @@ module TerraformWrapper
|
|
35
35
|
|
36
36
|
###############################################################################
|
37
37
|
|
38
|
-
def initialize(
|
39
|
-
construct(
|
38
|
+
def initialize(options:, variables:)
|
39
|
+
construct(options: options, variables: variables)
|
40
40
|
end
|
41
41
|
|
42
42
|
###############################################################################
|
@@ -70,17 +70,17 @@ module TerraformWrapper
|
|
70
70
|
|
71
71
|
bucket = @options["bucket"]
|
72
72
|
|
73
|
-
logger.fatal("AWS backend S3 bucket name must be a
|
73
|
+
logger.fatal("AWS backend S3 bucket name must be a string!") unless bucket.kind_of?(String)
|
74
74
|
logger.fatal("AWS backend S3 bucket name must not be blank!") if bucket.strip.empty?
|
75
75
|
|
76
76
|
region = @options["region"]
|
77
77
|
|
78
|
-
logger.fatal("AWS backend S3 bucket region must be a
|
78
|
+
logger.fatal("AWS backend S3 bucket region must be a string!") unless region.kind_of?(String)
|
79
79
|
logger.fatal("AWS backend S3 bucket region must not be blank!") if region.strip.empty?
|
80
80
|
|
81
81
|
key = @options.key?("key") ? @options["key"] : File.join("%{service}", "%{config}", "%{component}" + @@ext)
|
82
82
|
|
83
|
-
logger.fatal("AWS backend S3 bucket key must be a
|
83
|
+
logger.fatal("AWS backend S3 bucket key must be a string!") unless key.kind_of?(String)
|
84
84
|
logger.fatal("AWS backend S3 bucket key must not be blank!") if key.strip.empty?
|
85
85
|
|
86
86
|
encrypt = @options.key?("encrypt") ? @options["encrypt"] : true
|
@@ -90,29 +90,29 @@ module TerraformWrapper
|
|
90
90
|
if @options.key?("kms") then
|
91
91
|
kms = @options["kms"]
|
92
92
|
|
93
|
-
logger.fatal("AWS backend S3 bucket encryption KMS key ARN must be a
|
93
|
+
logger.fatal("AWS backend S3 bucket encryption KMS key ARN must be a string if specified!") unless kms.kind_of?(String)
|
94
94
|
logger.fatal("AWS backend S3 bucket encryption KMS key ARN must not be blank if specified!") if kms.strip.empty?
|
95
95
|
end
|
96
96
|
|
97
97
|
if @options.key?("role") then
|
98
98
|
role = @options["role"]
|
99
99
|
|
100
|
-
logger.fatal("AWS backend role to assume ARN must be a
|
100
|
+
logger.fatal("AWS backend role to assume ARN must be a string if specified!") unless role.kind_of?(String)
|
101
101
|
logger.fatal("AWS backend role to assume ARN must not be blank if specified!") if role.strip.empty?
|
102
102
|
end
|
103
103
|
|
104
|
-
|
105
|
-
|
106
|
-
|
104
|
+
@variables.core.keys.map{ |sym| sym.to_s }.each do |core|
|
105
|
+
logger.fatal("AWS backend S3 bucket name or key must include %{#{core}}.") unless (bucket.include?("%{#{core}}") or key.include?("%{#{core}}"))
|
106
|
+
end
|
107
107
|
|
108
108
|
begin
|
109
|
-
bucket = bucket % @variables
|
110
|
-
region = region % @variables
|
111
|
-
key = key % @variables
|
112
|
-
kms = kms % @variables unless kms.nil?
|
113
|
-
role = role % @variables unless role.nil?
|
109
|
+
bucket = bucket % @variables.identifiers
|
110
|
+
region = region % @variables.identifiers
|
111
|
+
key = key % @variables.identifiers
|
112
|
+
kms = kms % @variables.identifiers unless kms.nil?
|
113
|
+
role = role % @variables.identifiers unless role.nil?
|
114
114
|
rescue
|
115
|
-
logger.fatal("AWS backend options contain
|
115
|
+
logger.fatal("AWS backend options contain identifiers that are not included in the configuration file!")
|
116
116
|
end
|
117
117
|
|
118
118
|
logger.fatal("Key: #{key} is too long for backend of type: #{@@type}") if key.length > 1024
|
@@ -31,8 +31,8 @@ module TerraformWrapper
|
|
31
31
|
|
32
32
|
###############################################################################
|
33
33
|
|
34
|
-
def initialize(
|
35
|
-
construct(
|
34
|
+
def initialize(options:, variables:)
|
35
|
+
construct(options: options, variables: variables)
|
36
36
|
end
|
37
37
|
|
38
38
|
###############################################################################
|
@@ -57,35 +57,35 @@ module TerraformWrapper
|
|
57
57
|
|
58
58
|
group = @options["group"]
|
59
59
|
|
60
|
-
logger.fatal("Azure backend group must be a
|
60
|
+
logger.fatal("Azure backend group must be a string!") unless group.kind_of?(String)
|
61
61
|
logger.fatal("Azure backend group must not be blank!") if group.strip.empty?
|
62
62
|
|
63
63
|
account = @options.key?("account") ? @options["account"] : group + "tf"
|
64
64
|
|
65
|
-
logger.fatal("Azure backend storage account must be a
|
65
|
+
logger.fatal("Azure backend storage account must be a string!") unless account.kind_of?(String)
|
66
66
|
logger.fatal("Azure backend storage account must not be blank!") if account.strip.empty?
|
67
67
|
|
68
68
|
container = @options.key?("container") ? @options["container"] : "default"
|
69
69
|
|
70
|
-
logger.fatal("Azure backend storage account container must be a
|
70
|
+
logger.fatal("Azure backend storage account container must be a string!") unless container.kind_of?(String)
|
71
71
|
logger.fatal("Azure backend storage account container must not be blank!") if container.strip.empty?
|
72
72
|
|
73
73
|
key = @options.key?("key") ? @options["key"] : File.join("%{service}", "%{config}", "%{component}" + @@ext)
|
74
74
|
|
75
|
-
logger.fatal("Azure backend storage account key must be a
|
75
|
+
logger.fatal("Azure backend storage account key must be a string!") unless key.kind_of?(String)
|
76
76
|
logger.fatal("Azure backend storage account key must not be blank!") if key.strip.empty?
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
@variables.core.keys.map{ |sym| sym.to_s }.each do |core|
|
79
|
+
logger.fatal("Azure backend container or key must include %{#{core}}.") unless (container.include?("%{#{core}}") or key.include?("%{#{core}}"))
|
80
|
+
end
|
81
81
|
|
82
82
|
begin
|
83
|
-
group = group % @variables
|
84
|
-
account = account % @variables
|
85
|
-
container = container % @variables
|
86
|
-
key = key % @variables
|
83
|
+
group = group % @variables.identifiers
|
84
|
+
account = account % @variables.identifiers
|
85
|
+
container = container % @variables.identifiers
|
86
|
+
key = key % @variables.identifiers
|
87
87
|
rescue
|
88
|
-
logger.fatal("Azure backend options contain
|
88
|
+
logger.fatal("Azure backend options contain identifiers that are not included in the configuration file!")
|
89
89
|
end
|
90
90
|
|
91
91
|
if key.length > 1024 then
|
@@ -30,7 +30,7 @@ module TerraformWrapper
|
|
30
30
|
|
31
31
|
###############################################################################
|
32
32
|
|
33
|
-
def initialize(
|
33
|
+
def initialize(options:, variables:)
|
34
34
|
logger.fatal("This class should not be used directly! Please create a backend-specific class instead!")
|
35
35
|
end
|
36
36
|
|
@@ -54,13 +54,9 @@ module TerraformWrapper
|
|
54
54
|
|
55
55
|
###############################################################################
|
56
56
|
|
57
|
-
def construct(
|
57
|
+
def construct(options:, variables:)
|
58
58
|
@options = options
|
59
|
-
@variables = variables
|
60
|
-
component: code.name,
|
61
|
-
config: config,
|
62
|
-
service: service
|
63
|
-
})
|
59
|
+
@variables = variables
|
64
60
|
|
65
61
|
specific
|
66
62
|
end
|
@@ -28,8 +28,8 @@ module TerraformWrapper
|
|
28
28
|
|
29
29
|
###############################################################################
|
30
30
|
|
31
|
-
def initialize(
|
32
|
-
construct(
|
31
|
+
def initialize(options:, variables:)
|
32
|
+
construct(options: options, variables: variables)
|
33
33
|
end
|
34
34
|
|
35
35
|
###############################################################################
|
@@ -49,17 +49,18 @@ module TerraformWrapper
|
|
49
49
|
def specific()
|
50
50
|
path = @options.key?("path") ? @options["path"] : File.join(Dir.pwd, "state", "terraform", "%{config}", "%{component}" + @@ext)
|
51
51
|
|
52
|
-
logger.fatal("Local backend path must be a
|
52
|
+
logger.fatal("Local backend path must be a string!") unless path.kind_of?(String)
|
53
53
|
logger.fatal("Local backend path must not be blank!") if path.strip.empty?
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
@variables.core.keys.map{ |sym| sym.to_s }.each do |core|
|
56
|
+
next if (core == "service") and (path.include?(Dir.pwd))
|
57
|
+
logger.fatal("Local backend path must include %{#{core}}.") unless path.include?("%{#{core}}")
|
58
|
+
end
|
58
59
|
|
59
60
|
begin
|
60
|
-
path = path % @variables
|
61
|
+
path = path % @variables.identifiers
|
61
62
|
rescue
|
62
|
-
logger.fatal("Local backend options contain
|
63
|
+
logger.fatal("Local backend options contain identifiers that are not included in the configuration file!")
|
63
64
|
end
|
64
65
|
|
65
66
|
directory = File.dirname(path)
|
@@ -28,12 +28,12 @@ module TerraformWrapper
|
|
28
28
|
###############################################################################
|
29
29
|
|
30
30
|
def initialize(options:)
|
31
|
-
logger.fatal("Binary base path must be a
|
31
|
+
logger.fatal("Binary base path must be a string!") unless options["base"].kind_of?(String)
|
32
32
|
logger.fatal("Binary base path must not be blank!") if options["base"].strip.empty?
|
33
33
|
|
34
34
|
@base = options["base"]
|
35
35
|
|
36
|
-
logger.fatal("Binary version must be a
|
36
|
+
logger.fatal("Binary version must be a string!") unless options["version"].kind_of?(String)
|
37
37
|
logger.fatal("Binary version must not be blank!") if options["version"].strip.empty?
|
38
38
|
|
39
39
|
@version = options["version"]
|
@@ -23,12 +23,12 @@ module TerraformWrapper
|
|
23
23
|
###############################################################################
|
24
24
|
|
25
25
|
def initialize(options:)
|
26
|
-
logger.fatal("Code base path must be a
|
26
|
+
logger.fatal("Code base path must be a string!") unless options["base"].kind_of?(String)
|
27
27
|
logger.fatal("Code base path must not be blank!") if options["base"].strip.empty?
|
28
28
|
|
29
29
|
@base = options["base"]
|
30
30
|
|
31
|
-
logger.fatal("Code component name must be a
|
31
|
+
logger.fatal("Code component name must be a string!") unless options["name"].kind_of?(String)
|
32
32
|
logger.fatal("Code component name must not be blank!") if options["name"].strip.empty?
|
33
33
|
|
34
34
|
@name = options["name"]
|
@@ -22,11 +22,6 @@ module TerraformWrapper
|
|
22
22
|
|
23
23
|
@@config_exts = [ "", ".yaml", ".yml" ]
|
24
24
|
|
25
|
-
###############################################################################
|
26
|
-
|
27
|
-
@@variable_files_name = "tfvars"
|
28
|
-
@@variable_files_ext = ".tfvars"
|
29
|
-
|
30
25
|
###############################################################################
|
31
26
|
|
32
27
|
attr_reader :auths
|
@@ -36,23 +31,22 @@ module TerraformWrapper
|
|
36
31
|
attr_reader :name
|
37
32
|
attr_reader :path
|
38
33
|
attr_reader :service
|
39
|
-
attr_reader :variable_files
|
40
34
|
attr_reader :variables
|
41
35
|
|
42
36
|
###############################################################################
|
43
37
|
|
44
38
|
def initialize(code:, options:)
|
45
|
-
logger.fatal("Configuration base path must be a
|
39
|
+
logger.fatal("Configuration base path must be a string!") unless options["base"].kind_of?(String)
|
46
40
|
logger.fatal("Configuration base path must not be blank!") if options["base"].strip.empty?
|
47
41
|
|
48
42
|
@base = options["base"]
|
49
43
|
|
50
|
-
logger.fatal("Configuration service name must be a
|
44
|
+
logger.fatal("Configuration service name must be a string!") unless options["service"].kind_of?(String)
|
51
45
|
logger.fatal("Configuration service name must not be blank!") if options["service"].strip.empty?
|
52
46
|
|
53
47
|
@service = options["service"]
|
54
48
|
|
55
|
-
logger.fatal("Configuration name must be a
|
49
|
+
logger.fatal("Configuration name must be a string!") unless options["name"].kind_of?(String)
|
56
50
|
logger.fatal("Configuration name must not be blank!") if options["name"].strip.empty?
|
57
51
|
|
58
52
|
@name = options["name"]
|
@@ -65,7 +59,7 @@ module TerraformWrapper
|
|
65
59
|
|
66
60
|
auth_azure_options = options["auth-azure-options"]
|
67
61
|
|
68
|
-
logger.fatal("Configuration backend name must be a
|
62
|
+
logger.fatal("Configuration backend name must be a string!") unless options["backend"].kind_of?(String)
|
69
63
|
logger.fatal("Configuration backend name must not be blank!") if options["backend"].strip.empty?
|
70
64
|
|
71
65
|
backend = options["backend"]
|
@@ -75,71 +69,37 @@ module TerraformWrapper
|
|
75
69
|
backend_options = options["backend-options"]
|
76
70
|
|
77
71
|
@code = code
|
78
|
-
@path = find
|
72
|
+
@path = ::TerraformWrapper.find(base: @base, name: @name, exts: @@config_exts, description: "Configuration")
|
79
73
|
|
80
74
|
yaml = YAML.load(File.read(@path))
|
81
|
-
|
82
75
|
logger.fatal("Invalid YAML in configuration file: #{@path}") unless yaml.kind_of?(Hash)
|
83
76
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
else
|
88
|
-
@variables = TerraformWrapper::Shared::Variables.new()
|
89
|
-
end
|
77
|
+
identifiers = yaml.key?("identifiers") ? yaml["identifiers"] : Hash.new
|
78
|
+
@variables = TerraformWrapper::Shared::Variables.new(config: @name, component: @code.name, service: @service, identifiers: identifiers)
|
79
|
+
@variables.add_variables(variables: yaml["globals"]) if yaml.key?("globals")
|
90
80
|
|
91
|
-
|
81
|
+
if yaml.key?("terraform") then
|
82
|
+
logger.fatal("Key 'terraform' is not a hash in configuration file: #{@path}") unless yaml["terraform"].kind_of?(Hash)
|
83
|
+
terraform = yaml["terraform"]
|
84
|
+
|
85
|
+
@variables.add_variables(variables: terraform["variables"]) if terraform.key?("variables")
|
86
|
+
@variables.add_files(base: @base, files: terraform["files"]) if terraform.key?("files")
|
87
|
+
end
|
92
88
|
|
93
89
|
@auths = Array.new
|
94
|
-
@auths.append(TerraformWrapper::Shared::Auths::Azure.new(
|
90
|
+
@auths.append(TerraformWrapper::Shared::Auths::Azure.new(options: auth_azure_options, variables: @variables)) if auth_azure
|
95
91
|
|
96
92
|
if backend == "local" then
|
97
|
-
@backend = TerraformWrapper::Shared::Backends::Local.new(
|
93
|
+
@backend = TerraformWrapper::Shared::Backends::Local.new(options: backend_options, variables: @variables)
|
98
94
|
elsif backend == "aws" then
|
99
|
-
@backend = TerraformWrapper::Shared::Backends::AWS.new(
|
95
|
+
@backend = TerraformWrapper::Shared::Backends::AWS.new(options: backend_options, variables: @variables)
|
100
96
|
elsif backend == "azure" then
|
101
|
-
@backend = TerraformWrapper::Shared::Backends::Azure.new(
|
97
|
+
@backend = TerraformWrapper::Shared::Backends::Azure.new(options: backend_options, variables: @variables)
|
102
98
|
else
|
103
99
|
logger.fatal("Backend: #{backend} is not valid!")
|
104
100
|
end
|
105
101
|
end
|
106
102
|
|
107
|
-
###############################################################################
|
108
|
-
|
109
|
-
private
|
110
|
-
|
111
|
-
###############################################################################
|
112
|
-
|
113
|
-
def find
|
114
|
-
@@config_exts.each do |config_ext|
|
115
|
-
path = File.join(@base, @name + config_ext)
|
116
|
-
return path if File.file?(path)
|
117
|
-
end
|
118
|
-
|
119
|
-
logger.fatal("Terraform configuration name: #{@name} not found in location: #{@base}!")
|
120
|
-
end
|
121
|
-
|
122
|
-
###############################################################################
|
123
|
-
|
124
|
-
def validate(variable_files:)
|
125
|
-
logger.fatal("Optional key 'variable_files' must be a list of strings!") unless variable_files.kind_of?(Array)
|
126
|
-
|
127
|
-
result = Array.new
|
128
|
-
|
129
|
-
variable_files.each do |variable_file|
|
130
|
-
logger.fatal("All elements of 'variable_files' must be strings!") unless variable_file.kind_of?(String)
|
131
|
-
logger.fatal("All elements of 'variable_files' must not be blank!") if variable_file.strip.empty?
|
132
|
-
path = File.join(@base, @@variable_files_name, variable_file.strip + @@variable_files_ext)
|
133
|
-
if File.file?(path) then
|
134
|
-
result.append(path)
|
135
|
-
else
|
136
|
-
logger.fatal("Terraform variables file: #{variable_file}, path: #{path} does not exist!")
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
return result
|
141
|
-
end
|
142
|
-
|
143
103
|
###############################################################################
|
144
104
|
|
145
105
|
end
|
@@ -29,7 +29,6 @@ module TerraformWrapper
|
|
29
29
|
@code = code
|
30
30
|
|
31
31
|
@initialised = false
|
32
|
-
@ready = false
|
33
32
|
end
|
34
33
|
|
35
34
|
###############################################################################
|
@@ -48,11 +47,11 @@ module TerraformWrapper
|
|
48
47
|
def import(address: nil, id: nil)
|
49
48
|
logger.fatal("Cannot Terraform import before initialising backend!") unless initialised
|
50
49
|
|
51
|
-
logger.fatal("Terraform state address for import must be a
|
52
|
-
logger.fatal("Terraform state address for import must be a
|
50
|
+
logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
|
51
|
+
logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
|
53
52
|
logger.fatal("Terraform state address for import must not be blank!") if address.strip.empty?
|
54
53
|
|
55
|
-
logger.fatal("Identification for infrastructure to import must be a
|
54
|
+
logger.fatal("Identification for infrastructure to import must be a string!") unless id.kind_of?(String)
|
56
55
|
logger.fatal("Identification for infrastructure to import must not be blank!") if id.strip.empty?
|
57
56
|
|
58
57
|
parameters = Array.new
|
@@ -152,8 +151,8 @@ module TerraformWrapper
|
|
152
151
|
|
153
152
|
result = Array.new
|
154
153
|
|
155
|
-
@config.
|
156
|
-
result.append("-var-file=\"#{
|
154
|
+
@config.variables.files.each do |file|
|
155
|
+
result.append("-var-file=\"#{file}\"")
|
157
156
|
end
|
158
157
|
|
159
158
|
return result
|
@@ -166,10 +165,6 @@ module TerraformWrapper
|
|
166
165
|
|
167
166
|
result = Array.new
|
168
167
|
|
169
|
-
result.append("-var=\"component=#{@code.name}\"")
|
170
|
-
result.append("-var=\"config=#{@config.name}\"")
|
171
|
-
result.append("-var=\"service=#{@config.service}\"")
|
172
|
-
|
173
168
|
@config.variables.values.each do |key, value|
|
174
169
|
result.append("-var=\"#{key.to_s}=#{value}\"")
|
175
170
|
end
|
@@ -16,17 +16,80 @@ module TerraformWrapper
|
|
16
16
|
|
17
17
|
###############################################################################
|
18
18
|
|
19
|
-
@@
|
19
|
+
@@variable_files_name = "tfvars"
|
20
|
+
@@variable_files_exts = [ ".tfvars" ]
|
20
21
|
|
21
22
|
###############################################################################
|
22
23
|
|
24
|
+
attr_reader :core
|
25
|
+
attr_reader :files
|
26
|
+
attr_reader :identifiers
|
23
27
|
attr_reader :values
|
24
28
|
|
25
29
|
###############################################################################
|
26
30
|
|
27
|
-
def initialize(
|
28
|
-
|
29
|
-
|
31
|
+
def initialize(component:, config:, service:, identifiers: Hash.new, sort: false)
|
32
|
+
logger.fatal("Identifiers provided must be a hash!") unless identifiers.kind_of?(Hash)
|
33
|
+
|
34
|
+
@core = Hash.new()
|
35
|
+
@core[:component] = component
|
36
|
+
@core[:config] = config
|
37
|
+
@core[:service] = service
|
38
|
+
|
39
|
+
user = cleanse(variables: identifiers, reserved: @core.keys)
|
40
|
+
merged = @core.merge(user)
|
41
|
+
|
42
|
+
@identifiers = sort ? merged.sort.to_h : merged
|
43
|
+
@values = @identifiers
|
44
|
+
@files = Array.new
|
45
|
+
end
|
46
|
+
|
47
|
+
###############################################################################
|
48
|
+
|
49
|
+
def add_files(base:, files:)
|
50
|
+
logger.fatal("Variable files provided must be an array!") unless files.kind_of?(Array)
|
51
|
+
|
52
|
+
files.each do |file|
|
53
|
+
logger.fatal("All provided variable file names must be strings!") unless file.kind_of?(String)
|
54
|
+
logger.fatal("All provided variable file names must not be blank!") if file.strip.empty?
|
55
|
+
|
56
|
+
path = ::TerraformWrapper.find(base: File.join(base, @@variable_files_name), name: file.strip, exts: @@variable_files_exts, description: "Terraform values file")
|
57
|
+
|
58
|
+
if @files.include?(path) then
|
59
|
+
logger.warn("Terraform variables file is included more than once: #{file.strip}")
|
60
|
+
else
|
61
|
+
@files.append(path)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
###############################################################################
|
67
|
+
|
68
|
+
def add_variables(variables:, sort: false)
|
69
|
+
logger.fatal("Variables provided must be a hash!") unless variables.kind_of?(Hash)
|
70
|
+
|
71
|
+
cleansed = cleanse(variables: variables, reserved: @values.keys)
|
72
|
+
|
73
|
+
begin
|
74
|
+
cleansed = cleansed.map{ |key, value| [ key, value % @identifiers ] }.to_h
|
75
|
+
rescue
|
76
|
+
logger.fatal("Variables contain identifiers that are not included in the configuration file!")
|
77
|
+
end
|
78
|
+
|
79
|
+
merged = @values.merge(cleansed)
|
80
|
+
@values = sort ? merged.sort.to_h : merged
|
81
|
+
end
|
82
|
+
|
83
|
+
###############################################################################
|
84
|
+
|
85
|
+
def clear_files()
|
86
|
+
@files = Array.new
|
87
|
+
end
|
88
|
+
|
89
|
+
###############################################################################
|
90
|
+
|
91
|
+
def clear_variables()
|
92
|
+
@values = @identifers
|
30
93
|
end
|
31
94
|
|
32
95
|
###############################################################################
|
@@ -35,17 +98,17 @@ module TerraformWrapper
|
|
35
98
|
|
36
99
|
###############################################################################
|
37
100
|
|
38
|
-
def cleanse(
|
101
|
+
def cleanse(variables:, reserved:)
|
39
102
|
result = Hash.new
|
40
103
|
|
41
|
-
|
104
|
+
variables.keys.each do |key|
|
42
105
|
logger.fatal("Could not clean variables hash. All keys MUST be strings!") unless key.kind_of?(String)
|
43
|
-
logger.fatal("Could not clean variables hash, key: #{key.downcase} is reserved and cannot be used!") if
|
106
|
+
logger.fatal("Could not clean variables hash, key: #{key.downcase} is reserved or already in use and cannot be used!") if reserved.include?(key.downcase.to_sym)
|
44
107
|
logger.fatal("Could not clean variables hash, duplicate key found: #{key.downcase}!") if result.key?(key.downcase.to_sym)
|
45
|
-
logger.fatal("Could not clean variables hash, value for: #{key.downcase} is not a string!") unless
|
46
|
-
logger.fatal("Could not clean variables hash, value for: #{key.downcase} is empty!") if
|
108
|
+
logger.fatal("Could not clean variables hash, value for: #{key.downcase} is not a string!") unless variables[key].kind_of?(String)
|
109
|
+
logger.fatal("Could not clean variables hash, value for: #{key.downcase} is empty!") if variables[key].strip.empty?
|
47
110
|
|
48
|
-
result[key.downcase.to_sym] =
|
111
|
+
result[key.downcase.to_sym] = variables[key].strip
|
49
112
|
end
|
50
113
|
|
51
114
|
return result
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terraform-wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Lees
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|