cloudspin-stack 0.1.30 → 0.1.31

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a0cac1862741e42e43595507f3013533717bbfd7927c449fd3dd61428008040
4
- data.tar.gz: b3bcc54327a0a33d64185d4c9441f96a995fc7ca16bd735c7b3501e8f4abf0a1
3
+ metadata.gz: 678301e9078d489fdeb291e89c89b211d00331c49602667ff731ed8b4e890854
4
+ data.tar.gz: a08a9749c18e6a6ad20f12c4bbb229c32d2c6b6fab8453fd9f078377ccdab2cc
5
5
  SHA512:
6
- metadata.gz: f5d3fd779e9faf9c3a7e05023147006974a41b28978c792dc24fb831b9890e344cddbec786e2a06b7f16b724b70dcb10ac72a06e20abb902a311c309da8a1250
7
- data.tar.gz: 9eb450340c002f38f675542952359bfbe8611c7cd53b78623c5f0e60bc5af3e7677d6c780cca667162fd6214d59f59d3c1ccd107cdb228818efb09a2ecd7712b
6
+ metadata.gz: 76f814c40a4775c4018ea824c56ace5a39711c336fcd26cf04c9d992f320ea059c5f44cdd5f868fd13086bfe52168b15f4a2c728f48b5e8785f1be156252bd54
7
+ data.tar.gz: 3942f2712d44d275b154c2f234132e56e124e3cd0583b3693132669aefd9873eee9eed5d4b76a94b7b7ba9690e10f86c89318bb0ea556eb1f73f3ada80ff5965
@@ -0,0 +1,95 @@
1
+ require 'ruby_terraform'
2
+ require 'fileutils'
3
+
4
+ module Cloudspin
5
+ module Stack
6
+ class BackendConfiguration
7
+
8
+ attr_reader :local_state_folder
9
+ attr_reader :local_statefile
10
+
11
+ def initialize(
12
+ terraform_backend_configuration_values:,
13
+ instance_identifier:,
14
+ stack_name:,
15
+ base_folder:
16
+ )
17
+ @terraform_backend_configuration_values = terraform_backend_configuration_values
18
+ @instance_identifier = instance_identifier
19
+ @stack_name = stack_name
20
+ @base_folder = base_folder
21
+
22
+ @has_remote_state = ! @terraform_backend_configuration_values['bucket'].nil?
23
+
24
+ if @has_remote_state
25
+ # puts "DEBUG: Using remote state"
26
+ @local_state_folder = nil
27
+ @local_statefile = nil
28
+ @terraform_backend_configuration_values['key'] = default_state_key
29
+ @migrate_state = initialize_migrate_flag
30
+ else
31
+ # puts "DEBUG: Not using remote state"
32
+ @migrate_state = false
33
+ end
34
+
35
+ if !@has_remote_state || @migrate_state
36
+ @local_state_folder = intialize_state_folder
37
+ @local_statefile = "#{@local_state_folder}/#{@instance_identifier}.tfstate"
38
+ # puts "DEBUG: Local statefile: #{@local_statefile}"
39
+ # puts "DEBUG: Migrating? #{@migrate_state}"
40
+ end
41
+ end
42
+
43
+ def terraform_init_parameters
44
+ if remote_state?
45
+ {
46
+ backend: 'true',
47
+ force_copy: migrate_state?,
48
+ backend_config: @terraform_backend_configuration_values
49
+ }
50
+ else
51
+ {}
52
+ end
53
+ end
54
+
55
+ def terraform_command_parameters
56
+ if remote_state?
57
+ {}
58
+ else
59
+ {
60
+ :state => @local_statefile
61
+ }
62
+ end
63
+ end
64
+
65
+ def intialize_state_folder
66
+ # TODO: Prefer to not actually create the folder, but seemed necessary to build the full path string.
67
+ FileUtils.mkdir_p "#{@base_folder}/state"
68
+ Pathname.new("#{@base_folder}/state/#{@instance_identifier}").realdirpath.to_s
69
+ end
70
+
71
+ def initialize_migrate_flag
72
+ if @terraform_backend_configuration_values['migrate'].nil?
73
+ false
74
+ else
75
+ migrate_value = @terraform_backend_configuration_values.delete('migrate')
76
+ migrate_value.to_s.downcase == 'true'
77
+ end
78
+ end
79
+
80
+ def default_state_key
81
+ "#{@instance_identifier}.tfstate"
82
+ end
83
+
84
+ def migrate_state?
85
+ @migrate_state
86
+ end
87
+
88
+ def remote_state?
89
+ @has_remote_state
90
+ end
91
+
92
+ end
93
+ end
94
+ end
95
+
@@ -55,7 +55,10 @@ module Cloudspin
55
55
  )
56
56
 
57
57
  if instance_configuration.has_remote_state_configuration? && stack_definition.is_from_remote?
58
- add_terraform_backend_source(stack_definition.source_path)
58
+ # puts "DEBUG: Stack instance is configured to use remote terraform state AND remote stack definition code"
59
+ add_backend_configuration_source(stack_definition.source_path)
60
+ # else
61
+ # puts "DEBUG: Stack instance is configured to use local terraform state AND/OR local stack definition code"
59
62
  end
60
63
 
61
64
  self.new(
@@ -71,7 +74,7 @@ module Cloudspin
71
74
  Pathname.new(folder).realdirpath.to_s
72
75
  end
73
76
 
74
- def self.add_terraform_backend_source(terraform_source_folder)
77
+ def self.add_backend_configuration_source(terraform_source_folder)
75
78
  # puts "DEBUG: Creating file #{terraform_source_folder}/_cloudspin_created_backend.tf"
76
79
  File.open("#{terraform_source_folder}/_cloudspin_created_backend.tf", 'w') { |backend_file|
77
80
  backend_file.write(<<~TF_BACKEND_SOURCE
@@ -97,6 +100,18 @@ module Cloudspin
97
100
  configuration.resource_values
98
101
  end
99
102
 
103
+ # def migrate
104
+ # RubyTerraform.clean(directory: working_folder)
105
+ # mkdir_p File.dirname(working_folder)
106
+ # cp_r @stack_definition.source_path, working_folder
107
+ # Dir.chdir(working_folder) do
108
+ # # cp configuration.backend_configuration.local_state_folder
109
+ # terraform_init
110
+ # # terraform_state_push()
111
+ # RubyTerraform.plan(terraform_command_parameters)
112
+ # end
113
+ # end
114
+
100
115
  def plan(plan_destroy: false)
101
116
  RubyTerraform.clean(directory: working_folder)
102
117
  mkdir_p File.dirname(working_folder)
@@ -168,12 +183,22 @@ module Cloudspin
168
183
  end
169
184
  end
170
185
 
171
-
172
186
  def terraform_init
187
+ if configuration.backend_configuration.migrate_state?
188
+ prepare_state_for_migration
189
+ end
173
190
  RubyTerraform.init(terraform_init_params)
174
191
  end
175
192
 
193
+ def prepare_state_for_migration
194
+ # puts "DEBUG: Preparing to migrate state from #{configuration.backend_configuration.local_statefile}"
195
+ cp configuration.backend_configuration.local_statefile, "#{working_folder}/terraform.tfstate"
196
+ end
197
+
176
198
  def init_dry
199
+ if configuration.backend_configuration.migrate_state?
200
+ "cp #{configuration.backend_configuration.local_statefile} -> #{working_folder}/terraform.tfstate"
201
+ end
177
202
  init_command = RubyTerraform::Commands::Init.new
178
203
  command_line_builder = init_command.instantiate_builder
179
204
  configured_command = init_command.configure_command(
@@ -185,26 +210,21 @@ module Cloudspin
185
210
  end
186
211
 
187
212
  def terraform_init_params
188
- if configuration.has_remote_state_configuration?
189
- {
190
- backend: 'true',
191
- backend_config: backend_parameters
192
- }
193
- else
194
- {}
195
- end
213
+ configuration.backend_configuration.terraform_init_parameters
196
214
  end
197
215
 
216
+ # TODO: Redundant? The folder is created in the BackendConfiguration class ...
198
217
  def ensure_state_folder
199
218
  if configuration.has_local_state_configuration?
200
- Instance.ensure_folder(configuration.terraform_backend['statefile_folder'])
219
+ Instance.ensure_folder(configuration.backend_configuration.local_state_folder)
201
220
  end
202
221
  end
203
222
 
204
223
  def terraform_command_parameters(added_parameters = {})
205
224
  {
206
225
  vars: terraform_variables
207
- }.merge(local_state_parameters).merge(added_parameters)
226
+ }.merge(configuration.backend_configuration.terraform_command_parameters)
227
+ .merge(added_parameters)
208
228
  end
209
229
 
210
230
  def terraform_variables
@@ -213,22 +233,6 @@ module Cloudspin
213
233
  }.merge({ 'instance_identifier' => id })
214
234
  end
215
235
 
216
- def local_state_parameters
217
- if configuration.has_local_state_configuration?
218
- { state: configuration.local_statefile }
219
- else
220
- {}
221
- end
222
- end
223
-
224
- def backend_parameters
225
- if configuration.has_remote_state_configuration?
226
- configuration.terraform_backend
227
- else
228
- {}
229
- end
230
- end
231
-
232
236
  end
233
237
  end
234
238
  end
@@ -15,13 +15,17 @@ module Cloudspin
15
15
 
16
16
  attr_reader :stack_name
17
17
  attr_reader :instance_identifier
18
- attr_reader :terraform_backend
18
+ attr_reader :backend_configuration
19
+
20
+
21
+ attr_reader :Xterraform_backend
19
22
 
20
23
  def initialize(
21
24
  configuration_values: {},
22
25
  stack_definition:,
23
26
  base_folder: '.'
24
27
  )
28
+ # puts "DEBUG: InstanceConfiguration configuration_values: #{configuration_values}"
25
29
  @stack_definition = stack_definition
26
30
  @base_folder = base_folder
27
31
 
@@ -29,21 +33,23 @@ module Cloudspin
29
33
  @instance_values = configuration_values['instance'] || {}
30
34
  @parameter_values = configuration_values['parameters'] || {}
31
35
  @resource_values = configuration_values['resources'] || {}
32
-
33
36
  @stack_name = @stack_values['name'] || stack_definition.name
37
+ init_instance_identifier
38
+ @backend_configuration = BackendConfiguration.new(
39
+ terraform_backend_configuration_values: configuration_values['terraform_backend'] || {},
40
+ instance_identifier: instance_identifier,
41
+ stack_name: stack_name,
42
+ base_folder: base_folder
43
+ )
44
+ end
45
+
46
+ def init_instance_identifier
34
47
  @instance_identifier = if @instance_values['identifier']
35
- instance_values['identifier']
48
+ @instance_values['identifier']
36
49
  elsif @instance_values['group']
37
- stack_name + '-' + @instance_values['group']
50
+ @stack_name + '-' + @instance_values['group']
38
51
  else
39
- stack_name
40
- end
41
-
42
- @terraform_backend = configuration_values['terraform_backend'] || {}
43
- if @terraform_backend.empty?
44
- @terraform_backend['statefile_folder'] = default_state_folder
45
- else
46
- @terraform_backend['key'] = default_state_key
52
+ @stack_name
47
53
  end
48
54
  end
49
55
 
@@ -73,30 +79,17 @@ module Cloudspin
73
79
  if File.exists?(yaml_file)
74
80
  YAML.load_file(yaml_file) || {}
75
81
  else
76
- puts "No configuration file: #{yaml_file}"
82
+ puts "WARNING: No configuration file: #{yaml_file}"
77
83
  {}
78
84
  end
79
85
  end
80
86
 
81
87
  def has_local_state_configuration?
82
- ! @terraform_backend['statefile_folder'].nil?
83
- end
84
-
85
- def local_statefile
86
- "#{@terraform_backend['statefile_folder']}/#{instance_identifier}.tfstate"
88
+ ! @backend_configuration.remote_state?
87
89
  end
88
90
 
89
91
  def has_remote_state_configuration?
90
- ! @terraform_backend['key'].nil?
91
- end
92
-
93
- def default_state_folder
94
- FileUtils.mkdir_p "#{base_folder}/state"
95
- Pathname.new("#{base_folder}/state/#{instance_identifier}").realdirpath.to_s
96
- end
97
-
98
- def default_state_key
99
- "#{instance_identifier}.tfstate"
92
+ @backend_configuration.remote_state?
100
93
  end
101
94
 
102
95
  def to_s
@@ -105,7 +98,7 @@ module Cloudspin
105
98
  'instance' => instance_values,
106
99
  'parameters' => parameter_values,
107
100
  'resources' => resource_values,
108
- 'terraform_backend' => terraform_backend
101
+ 'backend_configuration' => backend_configuration
109
102
  }.to_s
110
103
  end
111
104
 
@@ -1,5 +1,5 @@
1
1
  module Cloudspin
2
2
  module Stack
3
- VERSION = '0.1.30'
3
+ VERSION = '0.1.31'
4
4
  end
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require 'cloudspin/stack/version'
2
2
  require 'cloudspin/stack/remote_definition'
3
3
  require 'cloudspin/stack/definition'
4
+ require 'cloudspin/stack/backend_configuration'
4
5
  require 'cloudspin/stack/instance_configuration'
5
6
  require 'cloudspin/stack/instance'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudspin-stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.30
4
+ version: 0.1.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - 'kief '
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-13 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-terraform
@@ -131,6 +131,7 @@ files:
131
131
  - exe/stack
132
132
  - lib/cloudspin/cli.rb
133
133
  - lib/cloudspin/stack.rb
134
+ - lib/cloudspin/stack/backend_configuration.rb
134
135
  - lib/cloudspin/stack/definition.rb
135
136
  - lib/cloudspin/stack/instance.rb
136
137
  - lib/cloudspin/stack/instance_configuration.rb