cloudspin-stack 0.1.14 → 0.1.15

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
  SHA1:
3
- metadata.gz: 3443b901ed3a2055a4b259373a55d90eb5bf59f0
4
- data.tar.gz: 3f511b6161a45c8adb0904f7069079460045c050
3
+ metadata.gz: 52f063b9cb944926767a548fc4e404c6943a5c4c
4
+ data.tar.gz: daf3e7c38fa7d9373541d7b3b68d2d618fede492
5
5
  SHA512:
6
- metadata.gz: 0e6428a7ac5751a3dcba61b8aea159e122f16df830785b19b87ba4e92dc6688ee0a4fcfb927ff916a0c97c9be7d5b57fe1fc1ca3838bcc2573db8d43db10a7e5
7
- data.tar.gz: 2613f8a1d6602d0d4eb45121f8659561052950b368058a9d4ce46753c0eab07edd1694703bec34b7476390b81a9018b54db8fc02c31610990ed0daa046850e08
6
+ metadata.gz: 135e7bb6999312d2028af2bee79348a154b0f0f65a5a256f30064fe24b7e73e0817e476aca0cb51e33ba5bad30df3fa3466313dcf5c2fc95823ecc7bb03ff661
7
+ data.tar.gz: cb2d6e3b39bb4fff1f69bf89aa32b41f291bc491f21b620fad0a944251b3c6a7bf56b42baefa37b0ab4ed0161aa0aba4d17e2e0cc59c21cb881eb590eedbc8a1
@@ -6,43 +6,33 @@ module Cloudspin
6
6
 
7
7
  attr_reader :name
8
8
  attr_reader :version
9
- attr_reader :parameter_names
10
- attr_reader :resource_names
11
- attr_reader :terraform_source_path
12
-
13
- def initialize(terraform_source_path: '',
14
- parameter_names: [],
15
- resource_names: [],
16
- stack: {})
17
- @terraform_source_path = terraform_source_path
18
- @parameter_names = parameter_names
19
- @resource_names = resource_names
20
- @name = stack[:name] || 'NO_NAME'
21
- @version = stack[:version] || '0'
9
+ attr_reader :source_path
10
+
11
+ def initialize(source_path:, stack_name:, stack_version: '0')
12
+ @source_path = source_path
13
+ @name = stack_name
14
+ @version = stack_version
22
15
  end
23
16
 
24
17
  def self.from_file(specfile)
25
- raise "Cloudspin definition file not found: #{specfile}" unless File.exists?(specfile)
18
+ raise NoStackDefinitionConfigurationFile unless File.exists?(specfile)
19
+ source_path = File.dirname(specfile)
26
20
  spec_hash = YAML.load_file(specfile)
27
- stack_spec = symbolize(spec_hash)
28
- terraform_source_path = File.dirname(specfile)
29
- self.new(terraform_source_path: terraform_source_path, **stack_spec)
21
+ self.new(
22
+ source_path: source_path,
23
+ stack_name: spec_hash.dig('stack', 'name'),
24
+ stack_version: spec_hash.dig('stack', 'version')
25
+ )
30
26
  end
31
27
 
32
- private
33
-
34
- def self.symbolize(obj)
35
- return obj.inject({}){ |memo, (k,v)|
36
- memo[k.to_sym] = symbolize(v)
37
- memo
38
- } if obj.is_a? Hash
39
- return obj.inject([]) { |memo, v|
40
- memo << symbolize(v)
41
- memo
42
- } if obj.is_a? Array
43
- return obj
28
+ def self.from_folder(definition_folder)
29
+ from_file("#{definition_folder}/stack-definition.yaml")
44
30
  end
45
31
 
46
32
  end
33
+
34
+
35
+ class NoStackDefinitionConfigurationFile < StandardError; end
36
+
47
37
  end
48
38
  end
@@ -11,33 +11,57 @@ module Cloudspin
11
11
  :working_folder,
12
12
  :backend_config,
13
13
  :statefile_folder,
14
- :parameter_values,
15
- :resource_values
16
-
17
- def initialize(id:,
18
- stack_definition:,
19
- backend_config:,
20
- working_folder:,
21
- statefile_folder:
22
- )
14
+ :configuration
15
+
16
+ def initialize(
17
+ id:,
18
+ stack_definition:,
19
+ backend_config:,
20
+ working_folder:,
21
+ statefile_folder:,
22
+ configuration:
23
+ )
23
24
  validate_id(id)
24
25
  @id = id
25
26
  @stack_definition = stack_definition
26
27
  @backend_config = backend_config
27
28
  @working_folder = working_folder
28
29
  @statefile_folder = Pathname.new(statefile_folder).realdirpath.to_s
29
- @parameter_values = {}
30
- @resource_values = {}
30
+ @configuration = configuration
31
31
  end
32
32
 
33
- def self.from_definition_folder(id:, definition_folder:, instance_folder: '.')
33
+ def self.from_folder(
34
+ *instance_configuration_files,
35
+ definition_folder:,
36
+ backend_config:,
37
+ working_folder:,
38
+ statefile_folder:
39
+ )
40
+ self.from_files(
41
+ instance_configuration_files,
42
+ stack_definition: Definition.from_folder(definition_folder),
43
+ backend_config: backend_config,
44
+ working_folder: working_folder,
45
+ statefile_folder: statefile_folder
46
+ )
47
+ end
48
+
49
+ def self.from_files(
50
+ *instance_configuration_files,
51
+ stack_definition:,
52
+ backend_config:,
53
+ working_folder:,
54
+ statefile_folder:
55
+ )
56
+ instance_configuration = InstanceConfiguration.from_files(stack_definition, instance_configuration_files)
34
57
  self.new(
35
- id: id,
36
- stack_definition: Definition.from_file(definition_folder + '/stack-definition.yaml'),
37
- backend_config: {},
38
- working_folder: instance_folder + '/work',
39
- statefile_folder: instance_folder + '/state'
40
- )
58
+ id: instance_configuration.instance_identifier,
59
+ stack_definition: stack_definition,
60
+ backend_config: backend_config,
61
+ working_folder: working_folder,
62
+ statefile_folder: statefile_folder,
63
+ configuration: instance_configuration
64
+ )
41
65
  end
42
66
 
43
67
  def validate_id(raw_id)
@@ -46,33 +70,18 @@ module Cloudspin
46
70
  raise "Stack instance ID '#{raw_id}' won't work. First character should be a letter." if /^[^A-Za-z]/ =~ raw_id
47
71
  end
48
72
 
49
- def add_parameter_values(new_parameter_values)
50
- @parameter_values.merge!(new_parameter_values)
51
- end
52
-
53
- def add_resource_values(new_resource_values)
54
- @resource_values.merge!(new_resource_values)
73
+ def parameter_values
74
+ configuration.parameter_values
55
75
  end
56
76
 
57
- def add_config_from_yaml(yaml_file)
58
- config = load_config_file(yaml_file)
59
- add_parameter_values(config['parameters']) if config['parameters']
60
- add_resource_values(config['resources']) if config['resources']
77
+ def resource_values
78
+ configuration.resource_values
61
79
  end
62
80
 
63
- def load_config_file(yaml_file)
64
- if File.exists?(yaml_file)
65
- YAML.load_file(yaml_file) || {}
66
- else
67
- {}
68
- end
69
- end
70
-
71
-
72
81
  def plan(plan_destroy: false)
73
82
  RubyTerraform.clean(directory: working_folder)
74
83
  mkdir_p File.dirname(working_folder)
75
- cp_r @stack_definition.terraform_source_path, working_folder
84
+ cp_r @stack_definition.source_path, working_folder
76
85
  Dir.chdir(working_folder) do
77
86
  RubyTerraform.init(backend_config: backend_config)
78
87
  RubyTerraform.plan(
@@ -98,7 +107,7 @@ module Cloudspin
98
107
  def up
99
108
  RubyTerraform.clean(directory: working_folder)
100
109
  mkdir_p File.dirname(working_folder)
101
- cp_r @stack_definition.terraform_source_path, working_folder
110
+ cp_r @stack_definition.source_path, working_folder
102
111
  Dir.chdir(working_folder) do
103
112
  RubyTerraform.init(backend_config: backend_config)
104
113
  RubyTerraform.apply(
@@ -123,7 +132,7 @@ module Cloudspin
123
132
  def down
124
133
  RubyTerraform.clean(directory: working_folder)
125
134
  mkdir_p File.dirname(working_folder)
126
- cp_r @stack_definition.terraform_source_path, working_folder
135
+ cp_r @stack_definition.source_path, working_folder
127
136
  Dir.chdir(working_folder) do
128
137
  RubyTerraform.init(backend_config: backend_config)
129
138
  RubyTerraform.destroy(
@@ -146,9 +155,9 @@ module Cloudspin
146
155
  end
147
156
 
148
157
  def terraform_variables
149
- @parameter_values.merge(@resource_values) { |key, oldval, newval|
158
+ parameter_values.merge(resource_values) { |key, oldval, newval|
150
159
  raise "Duplicate values for terraform variable '#{key}' ('#{oldval}' and '#{newval}')"
151
- }
160
+ }.merge({ 'instance_identifier' => id })
152
161
  end
153
162
 
154
163
  def terraform_statefile
@@ -0,0 +1,66 @@
1
+ require 'ruby_terraform'
2
+ require 'fileutils'
3
+
4
+ module Cloudspin
5
+ module Stack
6
+ class InstanceConfiguration
7
+
8
+ attr_reader :stack_definition
9
+ attr_reader :stack_name
10
+
11
+ attr_reader :instance_values
12
+ attr_reader :parameter_values
13
+ attr_reader :resource_values
14
+
15
+ def initialize(stack_definition)
16
+ @stack_definition = stack_definition
17
+ @stack_name = stack_definition.name
18
+ @instance_values = {}
19
+ @parameter_values = {}
20
+ @resource_values = {}
21
+ end
22
+
23
+ def self.from_files(stack_definition, *configuration_files)
24
+ config = self.new(stack_definition)
25
+ configuration_files.flatten.each { |config_file|
26
+ config.add_values(load_file(config_file))
27
+ }
28
+ config
29
+ end
30
+
31
+ def self.load_file(yaml_file)
32
+ if File.exists?(yaml_file)
33
+ YAML.load_file(yaml_file) || {}
34
+ else
35
+ {}
36
+ end
37
+ end
38
+
39
+ def add_values(values)
40
+ @instance_values.merge!(values['instance']) if values['instance']
41
+ @parameter_values.merge!(values['parameters']) if values['parameters']
42
+ @resource_values.merge!(values['resources']) if values['resources']
43
+ self
44
+ end
45
+
46
+ def instance_identifier
47
+ if instance_values['identifier']
48
+ instance_values['identifier']
49
+ else
50
+ stack_name
51
+ end
52
+ end
53
+
54
+ def to_s
55
+ {
56
+ 'instance' => instance_values,
57
+ 'parameters' => parameter_values,
58
+ 'resources' => resource_values
59
+ }.to_s
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+
@@ -1,5 +1,5 @@
1
1
  module Cloudspin
2
2
  module Stack
3
- VERSION = '0.1.14'
3
+ VERSION = '0.1.15'
4
4
  end
5
5
  end
@@ -1,3 +1,4 @@
1
1
  require 'cloudspin/stack/version'
2
2
  require 'cloudspin/stack/definition'
3
+ require 'cloudspin/stack/instance_configuration'
3
4
  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.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - 'kief '
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-06 00:00:00.000000000 Z
11
+ date: 2018-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-terraform
@@ -104,6 +104,7 @@ files:
104
104
  - lib/cloudspin/stack.rb
105
105
  - lib/cloudspin/stack/definition.rb
106
106
  - lib/cloudspin/stack/instance.rb
107
+ - lib/cloudspin/stack/instance_configuration.rb
107
108
  - lib/cloudspin/stack/version.rb
108
109
  homepage: https://github.com/cloudspinners
109
110
  licenses: