Class: Cloudspin::Stack::Terraform

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudspin/stack/terraform.rb

Instance Method Summary collapse

Constructor Details

#initialize(working_folder: '.', terraform_variables: {}, terraform_init_arguments: {}) ⇒ Terraform

KSM: Maybe this should be a static class - pass in the working directory and the arguments, and call them. All the logic of assembling the command line arguments should be in the caller?? Since I want the caller to be able to spit out the things in a variables file, for instance.



12
13
14
15
16
17
18
19
20
21
# File 'lib/cloudspin/stack/terraform.rb', line 12

def initialize(
  working_folder: '.',
  terraform_variables: {},
  terraform_init_arguments: {}
)
  @working_folder = working_folder
  # @terraform_variables = terraform_variables
  @terraform_variables = {}
  @terraform_init_arguments = terraform_init_arguments
end

Instance Method Details

#downObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/cloudspin/stack/terraform.rb', line 60

def down
  # RubyTerraform.clean(directory: @working_folder)
  # mkdir_p File.dirname(@working_folder)
  # cp_r @stack_definition.source_path, @working_folder
  # ensure_state_folder
  Dir.chdir(@working_folder) do
    terraform_init
    RubyTerraform.destroy(terraform_command_parameters(force: true))
  end
end

#down_dryObject



71
72
73
74
75
76
77
# File 'lib/cloudspin/stack/terraform.rb', line 71

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, terraform_command_parameters)
  built_command = configured_command.build
  "cd #{@working_folder} && #{built_command.to_s}"
end

#initObject



94
95
96
97
98
# File 'lib/cloudspin/stack/terraform.rb', line 94

def init
  Dir.chdir(@working_folder) do
    terraform_init
  end
end

#init_dryObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cloudspin/stack/terraform.rb', line 100

def init_dry
  # if configuration.backend_configuration.migrate_state?
  #   "cp #{configuration.backend_configuration.local_statefile} -> #{@working_folder}/terraform.tfstate"
  # end
  init_command = RubyTerraform::Commands::Init.new
  command_line_builder = init_command.instantiate_builder
  configured_command = init_command.configure_command(
    command_line_builder,
    @terraform_init_arguments
  )
  built_command = configured_command.build
  "cd #{@working_folder} && #{built_command.to_s}"
end

#plan(plan_destroy: false) ⇒ Object



23
24
25
26
27
28
# File 'lib/cloudspin/stack/terraform.rb', line 23

def plan(plan_destroy: false)
  Dir.chdir(@working_folder) do
    terraform_init
    RubyTerraform.plan(terraform_command_parameters(destroy: plan_destroy))
  end
end

#plan_dry(plan_destroy: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/cloudspin/stack/terraform.rb', line 30

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,
    terraform_command_parameters(:destroy => plan_destroy)
  )
  built_command = configured_command.build
  "cd #{@working_folder} && #{built_command.to_s}"
end

#refreshObject



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

def refresh
  # RubyTerraform.clean(directory: @working_folder)
  # mkdir_p File.dirname(@working_folder)
  # cp_r @stack_definition.source_path, @working_folder
  # ensure_state_folder
  Dir.chdir(@working_folder) do
    terraform_init
    RubyTerraform.refresh(terraform_command_parameters(force: true))
  end
end

#terraform_command_parameters(added_parameters = {}) ⇒ Object

KSM: Do we have this here, or do we munge all this up in the calling method and just pass in the processed list of arguments? For that matter, should we just call each of the actions on this class as static methods? Does this class really need to hold any kind of state?



119
120
121
122
123
# File 'lib/cloudspin/stack/terraform.rb', line 119

def terraform_command_parameters(added_parameters = {})
  {
    vars: @terraform_variables
  }.merge(added_parameters)
end

#terraform_initObject



90
91
92
# File 'lib/cloudspin/stack/terraform.rb', line 90

def terraform_init
  RubyTerraform.init(@terraform_init_arguments)
end

#upObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/cloudspin/stack/terraform.rb', line 41

def up
  # RubyTerraform.clean(directory: @working_folder)
  # mkdir_p File.dirname(@working_folder)
  # cp_r @stack_definition.source_path, @working_folder
  # ensure_state_folder
  Dir.chdir(@working_folder) do
    terraform_init
    RubyTerraform.apply(terraform_command_parameters(auto_approve: true))
  end
end

#up_dryObject



52
53
54
55
56
57
58
# File 'lib/cloudspin/stack/terraform.rb', line 52

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, terraform_command_parameters)
  built_command = configured_command.build
  "cd #{@working_folder} && #{built_command.to_s}"
end