Class: Cloudspin::Stack::Instance
- Inherits:
-
Object
- Object
- Cloudspin::Stack::Instance
- Includes:
- FileUtils
- Defined in:
- lib/cloudspin/stack/instance.rb
Instance Attribute Summary collapse
-
#backend_config ⇒ Object
readonly
Returns the value of attribute backend_config.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#statefile_folder ⇒ Object
readonly
Returns the value of attribute statefile_folder.
-
#working_folder ⇒ Object
readonly
Returns the value of attribute working_folder.
Class Method Summary collapse
- .from_files(*instance_configuration_files, stack_definition:, backend_config:, working_folder:, statefile_folder:) ⇒ Object
- .from_folder(*instance_configuration_files, definition_folder:, backend_config:, working_folder:, statefile_folder:) ⇒ Object
Instance Method Summary collapse
- #down ⇒ Object
- #down_dry ⇒ Object
-
#initialize(id:, stack_definition:, backend_config:, working_folder:, statefile_folder:, configuration:) ⇒ Instance
constructor
A new instance of Instance.
- #parameter_values ⇒ Object
- #plan(plan_destroy: false) ⇒ Object
- #plan_dry(plan_destroy: false) ⇒ Object
- #resource_values ⇒ Object
- #terraform_statefile ⇒ Object
- #terraform_variables ⇒ Object
- #up ⇒ Object
- #up_dry ⇒ Object
- #validate_id(raw_id) ⇒ Object
Constructor Details
#initialize(id:, stack_definition:, backend_config:, working_folder:, statefile_folder:, configuration:) ⇒ Instance
Returns a new instance of Instance.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cloudspin/stack/instance.rb', line 16 def initialize( id:, stack_definition:, backend_config:, working_folder:, statefile_folder:, configuration: ) validate_id(id) @id = id @stack_definition = stack_definition @backend_config = backend_config @working_folder = working_folder @statefile_folder = Pathname.new(statefile_folder).realdirpath.to_s @configuration = configuration end |
Instance Attribute Details
#backend_config ⇒ Object (readonly)
Returns the value of attribute backend_config.
10 11 12 |
# File 'lib/cloudspin/stack/instance.rb', line 10 def backend_config @backend_config end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/cloudspin/stack/instance.rb', line 10 def configuration @configuration end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/cloudspin/stack/instance.rb', line 10 def id @id end |
#statefile_folder ⇒ Object (readonly)
Returns the value of attribute statefile_folder.
10 11 12 |
# File 'lib/cloudspin/stack/instance.rb', line 10 def statefile_folder @statefile_folder end |
#working_folder ⇒ Object (readonly)
Returns the value of attribute working_folder.
10 11 12 |
# File 'lib/cloudspin/stack/instance.rb', line 10 def working_folder @working_folder end |
Class Method Details
.from_files(*instance_configuration_files, stack_definition:, backend_config:, working_folder:, statefile_folder:) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cloudspin/stack/instance.rb', line 49 def self.from_files( *instance_configuration_files, stack_definition:, backend_config:, working_folder:, statefile_folder: ) instance_configuration = InstanceConfiguration.from_files(stack_definition, instance_configuration_files) self.new( id: instance_configuration.instance_identifier, stack_definition: stack_definition, backend_config: backend_config, working_folder: working_folder, statefile_folder: statefile_folder, configuration: instance_configuration ) end |
.from_folder(*instance_configuration_files, definition_folder:, backend_config:, working_folder:, statefile_folder:) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cloudspin/stack/instance.rb', line 33 def self.from_folder( *instance_configuration_files, definition_folder:, backend_config:, working_folder:, statefile_folder: ) self.from_files( instance_configuration_files, stack_definition: Definition.from_folder(definition_folder), backend_config: backend_config, working_folder: working_folder, statefile_folder: statefile_folder ) end |
Instance Method Details
#down ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cloudspin/stack/instance.rb', line 132 def down RubyTerraform.clean(directory: working_folder) mkdir_p File.dirname(working_folder) cp_r @stack_definition.source_path, working_folder Dir.chdir(working_folder) do RubyTerraform.init(backend_config: backend_config) RubyTerraform.destroy( force: true, state: terraform_statefile, vars: terraform_variables ) end end |
#down_dry ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cloudspin/stack/instance.rb', line 146 def down_dry down_command = RubyTerraform::Commands::Destroy.new command_line_builder = down_command.instantiate_builder configured_command = down_command.configure_command(command_line_builder, { :state => terraform_statefile, :vars => terraform_variables }) built_command = configured_command.build "cd #{working_folder} && #{built_command.to_s}" end |
#parameter_values ⇒ Object
73 74 75 |
# File 'lib/cloudspin/stack/instance.rb', line 73 def parameter_values configuration.parameter_values end |
#plan(plan_destroy: false) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cloudspin/stack/instance.rb', line 81 def plan(plan_destroy: false) RubyTerraform.clean(directory: working_folder) mkdir_p File.dirname(working_folder) cp_r @stack_definition.source_path, working_folder Dir.chdir(working_folder) do RubyTerraform.init(backend_config: backend_config) RubyTerraform.plan( destroy: plan_destroy, state: terraform_statefile, vars: terraform_variables ) end end |
#plan_dry(plan_destroy: false) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cloudspin/stack/instance.rb', line 95 def plan_dry(plan_destroy: false) plan_command = RubyTerraform::Commands::Plan.new command_line_builder = plan_command.instantiate_builder configured_command = plan_command.configure_command(command_line_builder, { :destroy => plan_destroy, :state => terraform_statefile, :vars => terraform_variables }) built_command = configured_command.build "cd #{working_folder} && #{built_command.to_s}" end |
#resource_values ⇒ Object
77 78 79 |
# File 'lib/cloudspin/stack/instance.rb', line 77 def resource_values configuration.resource_values end |
#terraform_statefile ⇒ Object
163 164 165 |
# File 'lib/cloudspin/stack/instance.rb', line 163 def terraform_statefile statefile_folder + "/stack-#{id}.tfstate" end |
#terraform_variables ⇒ Object
157 158 159 160 161 |
# File 'lib/cloudspin/stack/instance.rb', line 157 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 |
#up ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cloudspin/stack/instance.rb', line 107 def up RubyTerraform.clean(directory: working_folder) mkdir_p File.dirname(working_folder) cp_r @stack_definition.source_path, working_folder Dir.chdir(working_folder) do RubyTerraform.init(backend_config: backend_config) RubyTerraform.apply( auto_approve: true, state: terraform_statefile, vars: terraform_variables ) end end |
#up_dry ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cloudspin/stack/instance.rb', line 121 def up_dry up_command = RubyTerraform::Commands::Apply.new command_line_builder = up_command.instantiate_builder configured_command = up_command.configure_command(command_line_builder, { :state => terraform_statefile, :vars => terraform_variables }) built_command = configured_command.build "cd #{working_folder} && #{built_command.to_s}" end |
#validate_id(raw_id) ⇒ Object
67 68 69 70 71 |
# File 'lib/cloudspin/stack/instance.rb', line 67 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 |