Class: Cloudspin::Stack::Instance
- Inherits:
-
Object
- Object
- Cloudspin::Stack::Instance
- Includes:
- FileUtils
- Defined in:
- lib/cloudspin/stack/instance.rb
Instance Attribute Summary collapse
-
#backend_configuration ⇒ Object
readonly
Returns the value of attribute backend_configuration.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#working_folder ⇒ Object
readonly
Returns the value of attribute working_folder.
Class Method Summary collapse
- .from_files(*instance_configuration_files, stack_definition:, base_folder: '.', base_working_folder:) ⇒ Object
- .from_folder(*instance_configuration_files, definition_location:, base_folder: '.', base_working_folder:) ⇒ Object
Instance Method Summary collapse
- #after ⇒ Object
- #clean_working_folder ⇒ Object
- #copy_instance_source ⇒ Object
- #create_variables_file ⇒ Object
- #create_working_folder ⇒ Object
- #ensure_folder(folder) ⇒ Object
- #format_tfvar(raw_value) ⇒ Object
-
#initialize(id:, stack_definition:, base_working_folder:, configuration:) ⇒ Instance
constructor
A new instance of Instance.
- #parameter_values ⇒ Object
- #prepare ⇒ Object
- #prepare_state ⇒ Object
- #resource_values ⇒ Object
- #terraform_command_arguments ⇒ Object
- #terraform_init_arguments ⇒ Object
- #terraform_variables ⇒ Object
- #validate_id(raw_id) ⇒ Object
Constructor Details
#initialize(id:, stack_definition:, base_working_folder:, configuration:) ⇒ Instance
Returns a new instance of Instance.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cloudspin/stack/instance.rb', line 14 def initialize( id:, stack_definition:, base_working_folder:, configuration: ) validate_id(id) @id = id @stack_definition = stack_definition @working_folder = "#{base_working_folder}/#{id}" @configuration = configuration @backend_configuration = configuration.backend_configuration # puts "DEBUG: instance working_folder: #{@working_folder}" end |
Instance Attribute Details
#backend_configuration ⇒ Object (readonly)
Returns the value of attribute backend_configuration.
9 10 11 |
# File 'lib/cloudspin/stack/instance.rb', line 9 def backend_configuration @backend_configuration end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
9 10 11 |
# File 'lib/cloudspin/stack/instance.rb', line 9 def configuration @configuration end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/cloudspin/stack/instance.rb', line 9 def id @id end |
#working_folder ⇒ Object (readonly)
Returns the value of attribute working_folder.
9 10 11 |
# File 'lib/cloudspin/stack/instance.rb', line 9 def working_folder @working_folder end |
Class Method Details
.from_files(*instance_configuration_files, stack_definition:, base_folder: '.', base_working_folder:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cloudspin/stack/instance.rb', line 47 def self.from_files( *instance_configuration_files, stack_definition:, base_folder: '.', base_working_folder: ) instance_configuration = InstanceConfiguration.from_files( instance_configuration_files, stack_definition: stack_definition, base_folder: base_folder ) self.new( id: instance_configuration.instance_identifier, stack_definition: stack_definition, base_working_folder: base_working_folder, configuration: instance_configuration ) end |
.from_folder(*instance_configuration_files, definition_location:, base_folder: '.', base_working_folder:) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cloudspin/stack/instance.rb', line 29 def self.from_folder( *instance_configuration_files, definition_location:, base_folder: '.', base_working_folder: ) self.from_files( instance_configuration_files, stack_definition: Definition.from_location( definition_location, definition_cache_folder: "#{base_folder}/.cloudspin/definitions", stack_configuration: InstanceConfiguration.load_configuration_values(instance_configuration_files)['stack'] ), base_folder: base_folder, base_working_folder: base_working_folder ) end |
Instance Method Details
#after ⇒ Object
76 77 78 |
# File 'lib/cloudspin/stack/instance.rb', line 76 def after @backend_configuration.after(working_folder: working_folder) end |
#clean_working_folder ⇒ Object
80 81 82 |
# File 'lib/cloudspin/stack/instance.rb', line 80 def clean_working_folder FileUtils.rm_rf(working_folder) end |
#copy_instance_source ⇒ Object
88 89 90 |
# File 'lib/cloudspin/stack/instance.rb', line 88 def copy_instance_source cp_r @stack_definition.source_path, working_folder end |
#create_variables_file ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/cloudspin/stack/instance.rb', line 101 def create_variables_file # puts "DEBUG: Creating file #{working_folder}/_cloudspin-#{id}.auto.tfvars" File.open("#{working_folder}/_cloudspin-#{id}.auto.tfvars", 'w') { |tfvars_file| tfvars_file.write("# Automatically generated by cloudspin\n") tfvars_file.write(terraform_variables.map { |name,value| "#{name} = #{format_tfvar(value)}" }.join("\n")) tfvars_file.write("\n") } end |
#create_working_folder ⇒ Object
84 85 86 |
# File 'lib/cloudspin/stack/instance.rb', line 84 def create_working_folder mkdir_p File.dirname(working_folder) end |
#ensure_folder(folder) ⇒ Object
92 93 94 95 |
# File 'lib/cloudspin/stack/instance.rb', line 92 def ensure_folder(folder) mkdir_p folder Pathname.new(folder).realdirpath.to_s end |
#format_tfvar(raw_value) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cloudspin/stack/instance.rb', line 110 def format_tfvar(raw_value) if [true,false].include? raw_value "#{raw_value}" elsif raw_value.is_a?(Array) '[' + raw_value.map { |raw_item| format_tfvar(raw_item) }.join(', ') + ']' elsif raw_value.is_a?(Hash) '{ ' + raw_value.map { |key,val| "\"#{key}\": #{format_tfvar(val)}" }.join(', ') + ' }' else "\"#{raw_value}\"" end end |
#parameter_values ⇒ Object
136 137 138 |
# File 'lib/cloudspin/stack/instance.rb', line 136 def parameter_values configuration.parameter_values end |
#prepare ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/cloudspin/stack/instance.rb', line 67 def prepare clean_working_folder create_working_folder copy_instance_source prepare_state create_variables_file @working_folder end |
#prepare_state ⇒ Object
97 98 99 |
# File 'lib/cloudspin/stack/instance.rb', line 97 def prepare_state @backend_configuration.prepare(working_folder: working_folder) end |
#resource_values ⇒ Object
140 141 142 |
# File 'lib/cloudspin/stack/instance.rb', line 140 def resource_values configuration.resource_values end |
#terraform_command_arguments ⇒ Object
154 155 156 |
# File 'lib/cloudspin/stack/instance.rb', line 154 def terraform_command_arguments @backend_configuration.terraform_command_arguments end |
#terraform_init_arguments ⇒ Object
150 151 152 |
# File 'lib/cloudspin/stack/instance.rb', line 150 def terraform_init_arguments @backend_configuration.terraform_init_arguments end |
#terraform_variables ⇒ Object
144 145 146 147 148 |
# File 'lib/cloudspin/stack/instance.rb', line 144 def terraform_variables parameter_values.merge(resource_values) { |key, oldval, newval| raise "Duplicate values for terraform variable '#{key}' ('#{oldval}' and '#{newval}')" }.merge({ 'instance_identifier' => id }) end |
#validate_id(raw_id) ⇒ Object
130 131 132 133 134 |
# File 'lib/cloudspin/stack/instance.rb', line 130 def validate_id(raw_id) raise "Stack instance ID '#{raw_id}' won't work. It needs to work as a filename." if /[^0-9A-Za-z.\-\_]/ =~ raw_id raise "Stack instance ID '#{raw_id}' won't work. No double dots allowed." if /\.\./ =~ raw_id raise "Stack instance ID '#{raw_id}' won't work. First character should be a letter." if /^[^A-Za-z]/ =~ raw_id end |