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
# 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 = configure_for_remote_backend
  @has_local_state = configure_for_local_backend
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

#add_backend_terraform_file_to(working_folder) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloudspin/stack/backend_configuration.rb', line 74

def add_backend_terraform_file_to(working_folder)
  # puts "DEBUG: Creating file #{working_folder}/_cloudspin_backend.tf"
  File.open("#{working_folder}/_cloudspin_backend.tf", 'w') { |backend_file|
    backend_file.write(<<~TF_BACKEND_SOURCE
      terraform {
        backend "s3" {}
      }
    TF_BACKEND_SOURCE
    )
  }
end

#after(working_folder:) ⇒ Object



68
69
70
71
72
# File 'lib/cloudspin/stack/backend_configuration.rb', line 68

def after(working_folder:)
  if migrate_state?
    disable_local_statefile
  end
end

#configure_for_local_backendObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloudspin/stack/backend_configuration.rb', line 37

def configure_for_local_backend
  @local_state_folder = "#{@base_folder}/state/#{@instance_identifier}"
  @local_statefile = "#{@local_state_folder}/#{@instance_identifier}.tfstate"
  if @has_remote_state && ! File.exists?(@local_statefile)
    @local_state_folder = nil
    @local_statefile = nil
    # puts "DEBUG: Not using local state"
    false
  else
    # puts "DEBUG: Using local state"
    true
  end
end

#configure_for_remote_backendObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/cloudspin/stack/backend_configuration.rb', line 26

def configure_for_remote_backend
  if @terraform_backend_configuration_values['bucket'].nil?
    # puts "DEBUG: Not using remote state"
    false
  else
    # puts "DEBUG: Using remote state"
    @terraform_backend_configuration_values['key'] = default_state_key
    true
  end
end

#copy_statefile_to(working_folder) ⇒ Object



91
92
93
# File 'lib/cloudspin/stack/backend_configuration.rb', line 91

def copy_statefile_to(working_folder)
  FileUtils.copy(@local_statefile, "#{working_folder}/terraform.tfstate")
end

#create_local_state_folderObject



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

def create_local_state_folder
  # puts "DEBUG: backend_configuration.create_local_state_folder: #{@local_state_folder}"
  FileUtils.mkdir_p @local_state_folder
end

#default_state_keyObject



100
101
102
# File 'lib/cloudspin/stack/backend_configuration.rb', line 100

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

#disable_local_statefileObject



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

def disable_local_statefile
  # puts "DEBUG: Renaming local statefile from '#{@local_statefile}' to '#{@local_statefile}.migrated'"
  FileUtils.move(@local_statefile, "#{@local_statefile}.migrated")
end

#local_state?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/cloudspin/stack/backend_configuration.rb', line 130

def local_state?
  @has_local_state
end

#migrate_state?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/cloudspin/stack/backend_configuration.rb', line 134

def migrate_state?
  remote_state? && local_state?
end

#prepare(working_folder:) ⇒ Object



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

def prepare(working_folder:)
  if remote_state?
    add_backend_terraform_file_to(working_folder)
    # puts "DEBUG: Prepare for use of remote state"
  end

  if local_state?
    # puts "DEBUG: Prepare to use local state"
    create_local_state_folder
  end

  if migrate_state?
    # puts "DEBUG: Prepare to migrate state from local to remote"
    copy_statefile_to(working_folder)
  end
end

#remote_state?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/cloudspin/stack/backend_configuration.rb', line 126

def remote_state?
  @has_remote_state
end

#terraform_command_argumentsObject



116
117
118
119
120
121
122
123
124
# File 'lib/cloudspin/stack/backend_configuration.rb', line 116

def terraform_command_arguments
  if local_state? && !migrate_state?
    {
      :state => @local_statefile
    }
  else
    {}
  end
end

#terraform_init_argumentsObject



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

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