Class: Cloudspin::Stack::BackendConfiguration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(terraform_backend_configuration_values:, instance_identifier:, stack_name:, base_folder:) ⇒ BackendConfiguration

Returns a new instance of BackendConfiguration.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloudspin/stack/backend_configuration.rb', line 11

def initialize(
                terraform_backend_configuration_values:,
                instance_identifier:,
                stack_name:,
                base_folder:
              )
  @terraform_backend_configuration_values  = terraform_backend_configuration_values
  @instance_identifier  = instance_identifier
  @stack_name           = stack_name
  @base_folder          = base_folder

  @has_remote_state         = ! @terraform_backend_configuration_values['bucket'].nil?

  if @has_remote_state
    # puts "DEBUG: Using remote state"
    @local_state_folder = nil
    @local_statefile = nil
    @terraform_backend_configuration_values['key'] = default_state_key
    @migrate_state = initialize_migrate_flag
  else
    # puts "DEBUG: Not using remote state"
    @migrate_state = false
  end

  if !@has_remote_state || @migrate_state
    @local_state_folder = intialize_state_folder
    @local_statefile = "#{@local_state_folder}/#{@instance_identifier}.tfstate"
    # puts "DEBUG: Local statefile: #{@local_statefile}"
    # puts "DEBUG: Migrating? #{@migrate_state}"
  end
end

Instance Attribute Details

#local_state_folderObject (readonly)

Returns the value of attribute local_state_folder.



8
9
10
# File 'lib/cloudspin/stack/backend_configuration.rb', line 8

def local_state_folder
  @local_state_folder
end

#local_statefileObject (readonly)

Returns the value of attribute local_statefile.



9
10
11
# File 'lib/cloudspin/stack/backend_configuration.rb', line 9

def local_statefile
  @local_statefile
end

Instance Method Details

#default_state_keyObject



80
81
82
# File 'lib/cloudspin/stack/backend_configuration.rb', line 80

def default_state_key
  "#{@instance_identifier}.tfstate"
end

#initialize_migrate_flagObject



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

def initialize_migrate_flag
  if @terraform_backend_configuration_values['migrate'].nil?
    false
  else
    migrate_value = @terraform_backend_configuration_values.delete('migrate')
    migrate_value.to_s.downcase == 'true'
  end
end

#intialize_state_folderObject



65
66
67
68
69
# File 'lib/cloudspin/stack/backend_configuration.rb', line 65

def intialize_state_folder
  # TODO: Prefer to not actually create the folder, but seemed necessary to build the full path string.
  FileUtils.mkdir_p "#{@base_folder}/state"
  Pathname.new("#{@base_folder}/state/#{@instance_identifier}").realdirpath.to_s
end

#migrate_state?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/cloudspin/stack/backend_configuration.rb', line 84

def migrate_state?
  @migrate_state
end

#remote_state?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/cloudspin/stack/backend_configuration.rb', line 88

def remote_state?
  @has_remote_state
end

#terraform_command_parametersObject



55
56
57
58
59
60
61
62
63
# File 'lib/cloudspin/stack/backend_configuration.rb', line 55

def terraform_command_parameters
  if remote_state?
    {}
  else
    {
      :state => @local_statefile
    }
  end
end

#terraform_init_parametersObject



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

def terraform_init_parameters
  if remote_state?
    {
      backend: 'true',
      force_copy: migrate_state?,
      backend_config: @terraform_backend_configuration_values
    }
  else
    {}
  end
end