class YleTfPlugins::Backends::File::Backend

Public Instance Methods

configure() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 15
def configure
  if !encrypt?
    create_tfstate(tfstate_path)
    symlink_tfstate
  elsif tfstate_path.exist?
    decrypt_tfstate
  end
end
create_tfstate(path) click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 28
def create_tfstate(path)
  return if path.exist?

  YleTf::Logger.debug('Creating state file')
  path.write(tfstate_template, perm: 0o644)
end
decrypt_tfstate() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 44
def decrypt_tfstate
  YleTf::Logger.info("Decrypting state from '#{tfstate_path}'")

  cmd = config.fetch('backend', type, 'decrypt_command')
  cmd.gsub!('{{FROM}}', tfstate_path.to_s)
  cmd.gsub!('{{TO}}', local_tfstate_path.to_s)

  # Split the command to have nicer logs
  YleTf::System.cmd(*Shellwords.split(cmd))
end
encrypt?() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 40
def encrypt?
  config.fetch('backend', type, 'encrypt')
end
encrypt_tfstate() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 55
def encrypt_tfstate
  YleTf::Logger.info("Encrypting state to '#{tfstate_path}'")

  cmd = config.fetch('backend', type, 'encrypt_command')
  cmd.gsub!('{{FROM}}', local_tfstate_path.to_s)
  cmd.gsub!('{{TO}}', tfstate_path.to_s)

  YleTf::System.cmd(*Shellwords.split(cmd),
                    error_handler: method(:on_encrypt_error))
end
local_tfstate_path() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 79
def local_tfstate_path
  @local_tfstate_path ||= Pathname.pwd.join('terraform.tfstate')
end
on_encrypt_error(_exit_code, error) click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 66
def on_encrypt_error(_exit_code, error)
  plain_tfstate_path = "#{tfstate_path}.plaintext"

  YleTf::Logger.warn("Copying unencrypted state to '#{plain_tfstate_path}'")
  FileUtils.cp(local_tfstate_path.to_s, plain_tfstate_path)

  raise error
end
tear_down() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 24
def tear_down
  encrypt_tfstate if encrypt? && local_tfstate_path.exist?
end
tfstate_path() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 75
def tfstate_path
  @tfstate_path ||= config.module_dir.join(config.fetch('backend', type, 'path'))
end
tfstate_template() click to toggle source
# File lib/yle_tf_plugins/backends/file/backend.rb, line 83
def tfstate_template
  '{"version": 1}'
end