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.
-
#terraform_command_arguments ⇒ Object
readonly
Returns the value of attribute terraform_command_arguments.
-
#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
- #clean_working_folder ⇒ Object
- #copy_instance_source ⇒ Object
- #create_variables_file ⇒ Object
- #create_working_folder ⇒ Object
- #ensure_folder(folder) ⇒ 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_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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cloudspin/stack/instance.rb', line 15 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 @terraform_command_arguments = {} # 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 |
#terraform_command_arguments ⇒ Object (readonly)
Returns the value of attribute terraform_command_arguments.
9 10 11 |
# File 'lib/cloudspin/stack/instance.rb', line 9 def terraform_command_arguments @terraform_command_arguments 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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cloudspin/stack/instance.rb', line 49 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cloudspin/stack/instance.rb', line 31 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
#clean_working_folder ⇒ Object
78 79 80 |
# File 'lib/cloudspin/stack/instance.rb', line 78 def clean_working_folder FileUtils.rm_rf(working_folder) end |
#copy_instance_source ⇒ Object
86 87 88 |
# File 'lib/cloudspin/stack/instance.rb', line 86 def copy_instance_source cp_r @stack_definition.source_path, working_folder end |
#create_variables_file ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/cloudspin/stack/instance.rb', line 99 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} = \"#{value}\"" }.join("\n")) tfvars_file.write("\n") } end |
#create_working_folder ⇒ Object
82 83 84 |
# File 'lib/cloudspin/stack/instance.rb', line 82 def create_working_folder mkdir_p File.dirname(working_folder) end |
#ensure_folder(folder) ⇒ Object
90 91 92 93 |
# File 'lib/cloudspin/stack/instance.rb', line 90 def ensure_folder(folder) FileUtils.mkdir_p folder Pathname.new(folder).realdirpath.to_s end |
#parameter_values ⇒ Object
114 115 116 |
# File 'lib/cloudspin/stack/instance.rb', line 114 def parameter_values configuration.parameter_values end |
#prepare ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/cloudspin/stack/instance.rb', line 69 def prepare clean_working_folder create_working_folder copy_instance_source prepare_state create_variables_file @working_folder end |
#prepare_state ⇒ Object
95 96 97 |
# File 'lib/cloudspin/stack/instance.rb', line 95 def prepare_state @backend_configuration.prepare(working_folder: working_folder) end |
#resource_values ⇒ Object
118 119 120 |
# File 'lib/cloudspin/stack/instance.rb', line 118 def resource_values configuration.resource_values end |
#terraform_init_arguments ⇒ Object
128 129 130 |
# File 'lib/cloudspin/stack/instance.rb', line 128 def terraform_init_arguments @backend_configuration.terraform_init_parameters end |
#terraform_variables ⇒ Object
122 123 124 125 126 |
# File 'lib/cloudspin/stack/instance.rb', line 122 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
108 109 110 111 112 |
# File 'lib/cloudspin/stack/instance.rb', line 108 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 |