Class: YleTfPlugins::Backends::File::Backend
Constant Summary
YleTf::Backend::BACKEND_CONFIG_FILE
Instance Attribute Summary
#config
Instance Method Summary
collapse
#backend_specific_config, #initialize, #to_h, #to_s, #type
Constructor Details
This class inherits a constructor from YleTf::Backend
Instance Method Details
15
16
17
18
19
20
21
22
|
# 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) ⇒ Object
28
29
30
31
32
33
|
# 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 ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# 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)
YleTf::System.cmd(*Shellwords.split(cmd))
end
|
#encrypt? ⇒ Boolean
40
41
42
|
# File 'lib/yle_tf_plugins/backends/file/backend.rb', line 40
def encrypt?
config.fetch('backend', type, 'encrypt')
end
|
#encrypt_tfstate ⇒ Object
55
56
57
58
59
60
61
62
63
64
|
# 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 ⇒ Object
79
80
81
|
# 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) ⇒ Object
66
67
68
69
70
71
72
73
|
# 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
|
#symlink_tfstate ⇒ Object
35
36
37
38
|
# File 'lib/yle_tf_plugins/backends/file/backend.rb', line 35
def symlink_tfstate
YleTf::Logger.info("Symlinking state to '#{tfstate_path}'")
local_tfstate_path.make_symlink(tfstate_path)
end
|
#tear_down ⇒ Object
24
25
26
|
# 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 ⇒ Object
75
76
77
|
# 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 ⇒ Object
83
84
85
|
# File 'lib/yle_tf_plugins/backends/file/backend.rb', line 83
def tfstate_template
'{"version": 1}'
end
|