Class: Cloudspin::Stack::Instance

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/cloudspin/stack/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, stack_definition:, backend_config:, working_folder:, statefile_folder:) ⇒ Instance

Returns a new instance of Instance.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloudspin/stack/instance.rb', line 17

def initialize(id:,
               stack_definition:,
               backend_config:,
               working_folder:,
               statefile_folder:
              )
  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
  @parameter_values = {}
  @resource_values = {}
end

Instance Attribute Details

#backend_configObject (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

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/cloudspin/stack/instance.rb', line 10

def id
  @id
end

#parameter_valuesObject (readonly)

Returns the value of attribute parameter_values.



10
11
12
# File 'lib/cloudspin/stack/instance.rb', line 10

def parameter_values
  @parameter_values
end

#resource_valuesObject (readonly)

Returns the value of attribute resource_values.



10
11
12
# File 'lib/cloudspin/stack/instance.rb', line 10

def resource_values
  @resource_values
end

#statefile_folderObject (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_folderObject (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

Instance Method Details

#add_config_from_yaml(yaml_file) ⇒ Object



47
48
49
50
51
# File 'lib/cloudspin/stack/instance.rb', line 47

def add_config_from_yaml(yaml_file)
  config = YAML.load_file(yaml_file) || {}
  add_parameter_values(config['parameters']) if config['parameters']
  add_resource_values(config['resources']) if config['resources']
end

#add_parameter_values(new_parameter_values) ⇒ Object



39
40
41
# File 'lib/cloudspin/stack/instance.rb', line 39

def add_parameter_values(new_parameter_values)
  @parameter_values.merge!(new_parameter_values)
end

#add_resource_values(new_resource_values) ⇒ Object



43
44
45
# File 'lib/cloudspin/stack/instance.rb', line 43

def add_resource_values(new_resource_values)
  @resource_values.merge!(new_resource_values)
end

#downObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cloudspin/stack/instance.rb', line 104

def down
  RubyTerraform.clean(directory: working_folder)
  mkdir_p File.dirname(working_folder)
  cp_r @stack_definition.terraform_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_dryObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/cloudspin/stack/instance.rb', line 118

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

#plan(plan_destroy: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cloudspin/stack/instance.rb', line 53

def plan(plan_destroy: false)
  RubyTerraform.clean(directory: working_folder)
  mkdir_p File.dirname(working_folder)
  cp_r @stack_definition.terraform_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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cloudspin/stack/instance.rb', line 67

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

#terraform_statefileObject



135
136
137
# File 'lib/cloudspin/stack/instance.rb', line 135

def terraform_statefile
  statefile_folder + "/stack-#{id}.tfstate"
end

#terraform_variablesObject



129
130
131
132
133
# File 'lib/cloudspin/stack/instance.rb', line 129

def terraform_variables
  @parameter_values.merge(@resource_values) { |key, oldval, newval|
    raise "Duplicate values for terraform variable '#{key}' ('#{oldval}' and '#{newval}')"
  }
end

#upObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cloudspin/stack/instance.rb', line 79

def up
  RubyTerraform.clean(directory: working_folder)
  mkdir_p File.dirname(working_folder)
  cp_r @stack_definition.terraform_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_dryObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/cloudspin/stack/instance.rb', line 93

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



33
34
35
36
37
# File 'lib/cloudspin/stack/instance.rb', line 33

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