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(stack_definition:, backend_config:, working_folder:, statefile_folder:) ⇒ Instance

Returns a new instance of Instance.



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

def initialize(stack_definition:,
               backend_config:,
               working_folder:,
               statefile_folder:
              )
  @stack_definition = stack_definition
  @backend_config = backend_config
  @working_folder = working_folder
  @statefile_folder = statefile_folder
  @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

#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



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

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



29
30
31
# File 'lib/cloudspin/stack/instance.rb', line 29

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

#add_resource_values(new_resource_values) ⇒ Object



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

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

#downObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cloudspin/stack/instance.rb', line 82

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

#planObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloudspin/stack/instance.rb', line 43

def plan
  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(
      state: terraform_statefile,
      vars: terraform_variables
    )
  end
end

#plan_commandObject



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

def plan_command
  options = {
    :state => terraform_statefile,
    :vars => terraform_variables
  }
  plan_command = RubyTerraform::Commands::Plan.new
  command_line_builder = plan_command.instantiate_builder
  configured_command = plan_command.configure_command(command_line_builder, options)
  built_command = configured_command.build
  built_command.to_s
end

#terraform_statefileObject



102
103
104
# File 'lib/cloudspin/stack/instance.rb', line 102

def terraform_statefile
  statefile_folder + "/default_name.tfstate"
end

#terraform_variablesObject



96
97
98
99
100
# File 'lib/cloudspin/stack/instance.rb', line 96

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

#upObject



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

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