cloudspin-stack 0.1.32 → 0.1.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5068a21e5f23f92d3b1b193e2b7c7f59f95488643a3c3720508501f2fb473172
4
- data.tar.gz: 549543ec361272038346e41cf139b4f94f5d9dc044c3b039c978dff116654e26
3
+ metadata.gz: acb694792d307f529c684659d7bcc71bdc9680ca296df9b24e454503f5edfc96
4
+ data.tar.gz: dfd9a5111b9fcf63e810efe4e91669b8662dbc63c57e97708ea87a8b16694e25
5
5
  SHA512:
6
- metadata.gz: ff1b4c4f407821ab6529a63af72e5947160ae91a62978a7558ddc85c63b5a219fa62ec02885188fcac04a95c7fb4586e2456074f18318b3195771370ce13f53d
7
- data.tar.gz: f482061aed3d5b2daab47ceeabf20f80eb0fca74a8ff2950693313b0fa7ed515a143a94977d2d36af7c3f8cb39a692743cc02c871fee427007120f3b9f464a93
6
+ metadata.gz: ce5a6a97bae9ecd7b8ad9e5fbf38efff3ccb0fd47499925c58450802204b03d6dc4a1decb818496136a98ce3ab352a7d37b12ca45102bc2187e29f6407a30d00
7
+ data.tar.gz: 16ae531fd2c2c8243efe85f38cca95d8279516823180a00db6292d4f198846cde51e7d013ced7736e08dad390e8ba2415b047161b1eba4e0526263819b93e0d6
data/lib/cloudspin/cli.rb CHANGED
@@ -95,7 +95,8 @@ module Cloudspin
95
95
  Cloudspin::Stack::Terraform.new(
96
96
  working_folder: instance.working_folder,
97
97
  terraform_variables: instance.terraform_variables,
98
- terraform_init_arguments: instance.terraform_init_arguments
98
+ terraform_init_arguments: instance.terraform_init_arguments,
99
+ terraform_command_arguments: instance.terraform_command_arguments
99
100
  )
100
101
  end
101
102
 
@@ -19,73 +19,53 @@ module Cloudspin
19
19
  @stack_name = stack_name
20
20
  @base_folder = base_folder
21
21
 
22
- configure_for_remote_backend
23
- configure_to_migrate_backend
24
- configure_for_local_backend
22
+ @has_remote_state = configure_for_remote_backend
23
+ @has_local_state = configure_for_local_backend
25
24
  end
26
25
 
27
26
  def configure_for_remote_backend
28
- @has_remote_state = if @terraform_backend_configuration_values['bucket'].nil?
27
+ if @terraform_backend_configuration_values['bucket'].nil?
28
+ # puts "DEBUG: Not using remote state"
29
29
  false
30
30
  else
31
31
  # puts "DEBUG: Using remote state"
32
- @local_state_folder = nil
33
- @local_statefile = nil
34
32
  @terraform_backend_configuration_values['key'] = default_state_key
35
33
  true
36
34
  end
37
35
  end
38
36
 
39
- def configure_to_migrate_backend
40
- @migrate_state = if @terraform_backend_configuration_values['migrate'].nil?
37
+ def configure_for_local_backend
38
+ @local_state_folder = "#{@base_folder}/state/#{@instance_identifier}"
39
+ @local_statefile = "#{@local_state_folder}/#{@instance_identifier}.tfstate"
40
+ if @has_remote_state && ! File.exists?(@local_statefile)
41
+ @local_state_folder = nil
42
+ @local_statefile = nil
43
+ # puts "DEBUG: Not using local state"
41
44
  false
42
45
  else
43
- migrate_value = @terraform_backend_configuration_values.delete('migrate')
44
- migrate_value.to_s.downcase == 'true'
45
- end
46
- end
47
-
48
- def configure_for_local_backend
49
- if !@has_remote_state || @migrate_state
50
- # puts "DEBUG: Not using remote state, or else is migrating state"
51
- @local_state_folder = "#{@base_folder}/state/#{@instance_identifier}"
52
- @local_statefile = "#{@local_state_folder}/#{@instance_identifier}.tfstate"
46
+ # puts "DEBUG: Using local state"
47
+ true
53
48
  end
54
49
  end
55
50
 
56
- def terraform_init_parameters
51
+ def prepare(working_folder:)
57
52
  if remote_state?
58
- {
59
- backend: 'true',
60
- force_copy: migrate_state?,
61
- backend_config: @terraform_backend_configuration_values
62
- }
63
- else
64
- {}
53
+ add_backend_terraform_file_to(working_folder)
54
+ # puts "DEBUG: Prepare for use of remote state"
65
55
  end
66
- end
67
56
 
68
- def terraform_command_parameters
69
- if remote_state?
70
- {}
71
- else
72
- {
73
- :state => @local_statefile
74
- }
57
+ if local_state?
58
+ # puts "DEBUG: Prepare to use local state"
59
+ create_local_state_folder
75
60
  end
76
- end
77
61
 
78
- def prepare(working_folder:)
79
- if remote_state?
80
- add_backend_terraform_file(working_folder)
81
- # puts "DEBUG: Prepare for use of remote state"
82
- else
83
- # puts "DEBUG: Prepare for use of local state"
84
- create_local_state_folder
62
+ if migrate_state?
63
+ # puts "DEBUG: Prepare to migrate state from local to remote"
64
+ copy_statefile_to(working_folder)
85
65
  end
86
66
  end
87
67
 
88
- def add_backend_terraform_file(working_folder)
68
+ def add_backend_terraform_file_to(working_folder)
89
69
  # puts "DEBUG: Creating file #{working_folder}/_cloudspin_backend.tf"
90
70
  File.open("#{working_folder}/_cloudspin_backend.tf", 'w') { |backend_file|
91
71
  backend_file.write(<<~TF_BACKEND_SOURCE
@@ -103,18 +83,48 @@ module Cloudspin
103
83
  # Pathname.new(@local_state_folder).realdirpath.to_s
104
84
  end
105
85
 
86
+ def copy_statefile_to(working_folder)
87
+ FileUtils.copy(@local_statefile, "#{working_folder}/terraform.tfstate")
88
+ end
89
+
106
90
  def default_state_key
107
91
  "#{@instance_identifier}.tfstate"
108
92
  end
109
93
 
110
- def migrate_state?
111
- @migrate_state
94
+ def terraform_init_arguments
95
+ if remote_state?
96
+ {
97
+ backend: 'true',
98
+ force_copy: migrate_state?,
99
+ backend_config: @terraform_backend_configuration_values
100
+ }
101
+ else
102
+ {}
103
+ end
104
+ end
105
+
106
+ def terraform_command_arguments
107
+ if local_state? && !migrate_state?
108
+ {
109
+ :state => @local_statefile
110
+ }
111
+ else
112
+ {}
113
+ end
112
114
  end
113
115
 
114
116
  def remote_state?
115
117
  @has_remote_state
116
118
  end
117
119
 
120
+ def local_state?
121
+ @has_local_state
122
+ end
123
+
124
+ def migrate_state?
125
+ remote_state? && local_state?
126
+ end
127
+
118
128
  end
119
129
  end
120
130
  end
@@ -9,8 +9,7 @@ module Cloudspin
9
9
  attr_reader :id,
10
10
  :configuration,
11
11
  :working_folder,
12
- :backend_configuration,
13
- :terraform_command_arguments
12
+ :backend_configuration
14
13
 
15
14
  def initialize(
16
15
  id:,
@@ -24,7 +23,6 @@ module Cloudspin
24
23
  @working_folder = "#{base_working_folder}/#{id}"
25
24
  @configuration = configuration
26
25
  @backend_configuration = configuration.backend_configuration
27
- @terraform_command_arguments = {}
28
26
  # puts "DEBUG: instance working_folder: #{@working_folder}"
29
27
  end
30
28
 
@@ -88,7 +86,7 @@ module Cloudspin
88
86
  end
89
87
 
90
88
  def ensure_folder(folder)
91
- FileUtils.mkdir_p folder
89
+ mkdir_p folder
92
90
  Pathname.new(folder).realdirpath.to_s
93
91
  end
94
92
 
@@ -100,11 +98,31 @@ module Cloudspin
100
98
  # puts "DEBUG: Creating file #{working_folder}/_cloudspin-#{id}.auto.tfvars"
101
99
  File.open("#{working_folder}/_cloudspin-#{id}.auto.tfvars", 'w') { |tfvars_file|
102
100
  tfvars_file.write("# Automatically generated by cloudspin\n")
103
- tfvars_file.write(terraform_variables.map { |name,value| "#{name} = \"#{value}\"" }.join("\n"))
101
+ tfvars_file.write(terraform_variables.map { |name,value| "#{name} = #{format_tfvar(value)}" }.join("\n"))
104
102
  tfvars_file.write("\n")
105
103
  }
106
104
  end
107
105
 
106
+ def format_tfvar(raw_value)
107
+ if [true,false].include? raw_value
108
+ "#{raw_value}"
109
+ elsif raw_value.is_a?(Array)
110
+ '[' +
111
+ raw_value.map { |raw_item|
112
+ format_tfvar(raw_item)
113
+ }.join(', ') +
114
+ ']'
115
+ elsif raw_value.is_a?(Hash)
116
+ '{ ' +
117
+ raw_value.map { |key,val|
118
+ "\"#{key}\": #{format_tfvar(val)}"
119
+ }.join(', ') +
120
+ ' }'
121
+ else
122
+ "\"#{raw_value}\""
123
+ end
124
+ end
125
+
108
126
  def validate_id(raw_id)
109
127
  raise "Stack instance ID '#{raw_id}' won't work. It needs to work as a filename." if /[^0-9A-Za-z.\-\_]/ =~ raw_id
110
128
  raise "Stack instance ID '#{raw_id}' won't work. No double dots allowed." if /\.\./ =~ raw_id
@@ -126,35 +144,12 @@ module Cloudspin
126
144
  end
127
145
 
128
146
  def terraform_init_arguments
129
- @backend_configuration.terraform_init_parameters
147
+ @backend_configuration.terraform_init_arguments
130
148
  end
131
149
 
132
150
  def terraform_command_arguments
133
- @backend_configuration.terraform_command_parameters
134
- end
135
-
136
- # def migrate
137
- # RubyTerraform.clean(directory: working_folder)
138
- # mkdir_p File.dirname(working_folder)
139
- # cp_r @stack_definition.source_path, working_folder
140
- # Dir.chdir(working_folder) do
141
- # # cp @backend_configuration.local_state_folder
142
- # terraform_init
143
- # # terraform_state_push()
144
- # RubyTerraform.plan(terraform_command_parameters)
145
- # end
146
- # end
147
-
148
- # def init
149
- # if @backend_configuration.migrate_state?
150
- # prepare_state_for_migration
151
- # end
152
- # end
153
-
154
- # def prepare_state_for_migration
155
- # # puts "DEBUG: Preparing to migrate state from #{@backend_configuration.local_statefile}"
156
- # cp @backend_configuration.local_statefile, "#{working_folder}/terraform.tfstate"
157
- # end
151
+ @backend_configuration.terraform_command_arguments
152
+ end
158
153
 
159
154
  end
160
155
  end
@@ -24,8 +24,7 @@ module Cloudspin
24
24
  )
25
25
  # puts "DEBUG: InstanceConfiguration configuration_values: #{configuration_values}"
26
26
  @stack_definition = stack_definition
27
- @base_folder = base_folder
28
-
27
+ @base_folder = Pathname.new(base_folder).realdirpath.to_s
29
28
  @stack_values = configuration_values['stack'] || {}
30
29
  @instance_values = configuration_values['instance'] || {}
31
30
  @parameter_values = configuration_values['parameters'] || {}
@@ -36,7 +35,7 @@ module Cloudspin
36
35
  terraform_backend_configuration_values: configuration_values['terraform_backend'] || {},
37
36
  instance_identifier: instance_identifier,
38
37
  stack_name: stack_name,
39
- base_folder: base_folder
38
+ base_folder: @base_folder
40
39
  )
41
40
  end
42
41
 
@@ -12,18 +12,20 @@ module Cloudspin
12
12
  def initialize(
13
13
  working_folder: '.',
14
14
  terraform_variables: {},
15
- terraform_init_arguments: {}
15
+ terraform_init_arguments: {},
16
+ terraform_command_arguments: {}
16
17
  )
17
18
  @working_folder = working_folder
18
19
  # @terraform_variables = terraform_variables
19
20
  @terraform_variables = {}
20
21
  @terraform_init_arguments = terraform_init_arguments
22
+ @base_terraform_command_arguments = terraform_command_arguments
21
23
  end
22
24
 
23
25
  def plan(plan_destroy: false)
24
26
  Dir.chdir(@working_folder) do
25
27
  terraform_init
26
- RubyTerraform.plan(terraform_command_parameters(destroy: plan_destroy))
28
+ RubyTerraform.plan(terraform_command_arguments(destroy: plan_destroy))
27
29
  end
28
30
  end
29
31
 
@@ -32,58 +34,46 @@ module Cloudspin
32
34
  command_line_builder = plan_command.instantiate_builder
33
35
  configured_command = plan_command.configure_command(
34
36
  command_line_builder,
35
- terraform_command_parameters(:destroy => plan_destroy)
37
+ terraform_command_arguments(:destroy => plan_destroy)
36
38
  )
37
39
  built_command = configured_command.build
38
40
  "cd #{@working_folder} && #{built_command.to_s}"
39
41
  end
40
42
 
41
43
  def up
42
- # RubyTerraform.clean(directory: @working_folder)
43
- # mkdir_p File.dirname(@working_folder)
44
- # cp_r @stack_definition.source_path, @working_folder
45
- # ensure_state_folder
46
44
  Dir.chdir(@working_folder) do
47
45
  terraform_init
48
- RubyTerraform.apply(terraform_command_parameters(auto_approve: true))
46
+ RubyTerraform.apply(terraform_command_arguments(auto_approve: true))
49
47
  end
50
48
  end
51
49
 
52
50
  def up_dry
53
51
  up_command = RubyTerraform::Commands::Apply.new
54
52
  command_line_builder = up_command.instantiate_builder
55
- configured_command = up_command.configure_command(command_line_builder, terraform_command_parameters)
53
+ configured_command = up_command.configure_command(command_line_builder, terraform_command_arguments)
56
54
  built_command = configured_command.build
57
55
  "cd #{@working_folder} && #{built_command.to_s}"
58
56
  end
59
57
 
60
58
  def down
61
- # RubyTerraform.clean(directory: @working_folder)
62
- # mkdir_p File.dirname(@working_folder)
63
- # cp_r @stack_definition.source_path, @working_folder
64
- # ensure_state_folder
65
59
  Dir.chdir(@working_folder) do
66
60
  terraform_init
67
- RubyTerraform.destroy(terraform_command_parameters(force: true))
61
+ RubyTerraform.destroy(terraform_command_arguments(force: true))
68
62
  end
69
63
  end
70
64
 
71
65
  def down_dry
72
66
  down_command = RubyTerraform::Commands::Destroy.new
73
67
  command_line_builder = down_command.instantiate_builder
74
- configured_command = down_command.configure_command(command_line_builder, terraform_command_parameters)
68
+ configured_command = down_command.configure_command(command_line_builder, terraform_command_arguments)
75
69
  built_command = configured_command.build
76
70
  "cd #{@working_folder} && #{built_command.to_s}"
77
71
  end
78
72
 
79
73
  def refresh
80
- # RubyTerraform.clean(directory: @working_folder)
81
- # mkdir_p File.dirname(@working_folder)
82
- # cp_r @stack_definition.source_path, @working_folder
83
- # ensure_state_folder
84
74
  Dir.chdir(@working_folder) do
85
75
  terraform_init
86
- RubyTerraform.refresh(terraform_command_parameters(force: true))
76
+ RubyTerraform.refresh(terraform_command_arguments(force: true))
87
77
  end
88
78
  end
89
79
 
@@ -98,9 +88,6 @@ module Cloudspin
98
88
  end
99
89
 
100
90
  def init_dry
101
- # if configuration.backend_configuration.migrate_state?
102
- # "cp #{configuration.backend_configuration.local_statefile} -> #{@working_folder}/terraform.tfstate"
103
- # end
104
91
  init_command = RubyTerraform::Commands::Init.new
105
92
  command_line_builder = init_command.instantiate_builder
106
93
  configured_command = init_command.configure_command(
@@ -116,10 +103,10 @@ module Cloudspin
116
103
  # For that matter, should we just call each of the actions on this class as static
117
104
  # methods? Does this class really need to hold any kind of state?
118
105
 
119
- def terraform_command_parameters(added_parameters = {})
120
- {
106
+ def terraform_command_arguments(added_parameters = {})
107
+ @base_terraform_command_arguments.merge({
121
108
  vars: @terraform_variables
122
- }.merge(added_parameters)
109
+ }).merge(added_parameters)
123
110
  end
124
111
 
125
112
  end
@@ -1,5 +1,5 @@
1
1
  module Cloudspin
2
2
  module Stack
3
- VERSION = '0.1.32'
3
+ VERSION = '0.1.33'
4
4
  end
5
5
  end
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.32
4
+ version: 0.1.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - 'kief '
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-03 00:00:00.000000000 Z
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-terraform