Class: Cloudspin::Stack::InstanceConfiguration
- Inherits:
-
Object
- Object
- Cloudspin::Stack::InstanceConfiguration
- Defined in:
- lib/cloudspin/stack/instance_configuration.rb
Instance Attribute Summary collapse
-
#base_folder ⇒ Object
readonly
Returns the value of attribute base_folder.
-
#instance_identifier ⇒ Object
readonly
Returns the value of attribute instance_identifier.
-
#instance_values ⇒ Object
readonly
Returns the value of attribute instance_values.
-
#parameter_values ⇒ Object
readonly
Returns the value of attribute parameter_values.
-
#resource_values ⇒ Object
readonly
Returns the value of attribute resource_values.
-
#stack_definition ⇒ Object
readonly
Returns the value of attribute stack_definition.
-
#stack_name ⇒ Object
readonly
Returns the value of attribute stack_name.
-
#stack_values ⇒ Object
readonly
Returns the value of attribute stack_values.
-
#terraform_backend ⇒ Object
readonly
Returns the value of attribute terraform_backend.
Class Method Summary collapse
- .from_files(*configuration_files, stack_definition:, base_folder: '.') ⇒ Object
- .yaml_file_to_hash(yaml_file) ⇒ Object
Instance Method Summary collapse
- #default_state_folder ⇒ Object
- #default_state_key ⇒ Object
- #has_local_state_configuration? ⇒ Boolean
- #has_remote_state_configuration? ⇒ Boolean
-
#initialize(configuration_values: {}, stack_definition:, base_folder: '.') ⇒ InstanceConfiguration
constructor
A new instance of InstanceConfiguration.
- #local_statefile ⇒ Object
-
#to_s ⇒ Object
def instance_identifier if instance_values instance_values elsif instance_values stack_name + ‘-’ + instance_values else stack_name end end.
Constructor Details
#initialize(configuration_values: {}, stack_definition:, base_folder: '.') ⇒ InstanceConfiguration
Returns a new instance of InstanceConfiguration.
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 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 20 def initialize( configuration_values: {}, stack_definition:, base_folder: '.' ) @stack_definition = stack_definition @base_folder = base_folder @stack_values = configuration_values['stack'] || {} @instance_values = configuration_values['instance'] || {} @parameter_values = configuration_values['parameters'] || {} @resource_values = configuration_values['resources'] || {} @stack_name = @stack_values['name'] || stack_definition.name @instance_identifier = if @instance_values['identifier'] instance_values['identifier'] elsif @instance_values['group'] stack_name + '-' + @instance_values['group'] else stack_name end @terraform_backend = configuration_values['terraform_backend'] || {} if @terraform_backend.empty? @terraform_backend['statefile_folder'] = default_state_folder else @terraform_backend['key'] = default_state_key end end |
Instance Attribute Details
#base_folder ⇒ Object (readonly)
Returns the value of attribute base_folder.
9 10 11 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 9 def base_folder @base_folder end |
#instance_identifier ⇒ Object (readonly)
Returns the value of attribute instance_identifier.
17 18 19 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 17 def instance_identifier @instance_identifier end |
#instance_values ⇒ Object (readonly)
Returns the value of attribute instance_values.
11 12 13 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 11 def instance_values @instance_values end |
#parameter_values ⇒ Object (readonly)
Returns the value of attribute parameter_values.
12 13 14 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 12 def parameter_values @parameter_values end |
#resource_values ⇒ Object (readonly)
Returns the value of attribute resource_values.
13 14 15 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 13 def resource_values @resource_values end |
#stack_definition ⇒ Object (readonly)
Returns the value of attribute stack_definition.
8 9 10 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 8 def stack_definition @stack_definition end |
#stack_name ⇒ Object (readonly)
Returns the value of attribute stack_name.
16 17 18 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 16 def stack_name @stack_name end |
#stack_values ⇒ Object (readonly)
Returns the value of attribute stack_values.
14 15 16 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 14 def stack_values @stack_values end |
#terraform_backend ⇒ Object (readonly)
Returns the value of attribute terraform_backend.
18 19 20 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 18 def terraform_backend @terraform_backend end |
Class Method Details
.from_files(*configuration_files, stack_definition:, base_folder: '.') ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 50 def self.from_files( *configuration_files, stack_definition:, base_folder: '.' ) configuration_values = {} configuration_files.flatten.each { |config_file| # puts "DEBUG: Reading configuration file: #{config_file}" configuration_values = configuration_values.deep_merge(yaml_file_to_hash(config_file)) } self.new( stack_definition: stack_definition, base_folder: base_folder, configuration_values: configuration_values ) end |
.yaml_file_to_hash(yaml_file) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 67 def self.yaml_file_to_hash(yaml_file) if File.exists?(yaml_file) YAML.load_file(yaml_file) || {} else puts "No configuration file: #{yaml_file}" {} end end |
Instance Method Details
#default_state_folder ⇒ Object
88 89 90 91 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 88 def default_state_folder FileUtils.mkdir_p "#{base_folder}/state" Pathname.new("#{base_folder}/state/#{instance_identifier}").realdirpath.to_s end |
#default_state_key ⇒ Object
93 94 95 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 93 def default_state_key "#{instance_identifier}.tfstate" end |
#has_local_state_configuration? ⇒ Boolean
76 77 78 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 76 def has_local_state_configuration? ! @terraform_backend['statefile_folder'].nil? end |
#has_remote_state_configuration? ⇒ Boolean
84 85 86 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 84 def has_remote_state_configuration? ! @terraform_backend['key'].nil? end |
#local_statefile ⇒ Object
80 81 82 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 80 def local_statefile "#{@terraform_backend['statefile_folder']}/#{instance_identifier}.tfstate" end |
#to_s ⇒ Object
def instance_identifier
if instance_values['identifier']
instance_values['identifier']
elsif instance_values['group']
stack_name + '-' + instance_values['group']
else
stack_name
end
end
107 108 109 110 111 112 113 114 115 |
# File 'lib/cloudspin/stack/instance_configuration.rb', line 107 def to_s { 'instance_identifier' => instance_identifier, 'instance' => instance_values, 'parameters' => parameter_values, 'resources' => resource_values, 'terraform_backend' => terraform_backend }.to_s end |