Class: YleTf::Action::TerraformInit

Inherits:
Object
  • Object
show all
Defined in:
lib/yle_tf/action/terraform_init.rb

Constant Summary collapse

TF_CMD_ARGS =
%w[-input=false -no-color].freeze
TF_CMD_OPTS =
{
  env: { 'TF_IN_AUTOMATION' => 'true' }, # Reduces some output
  stdout: :debug                         # Hide the output to the debug level
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TerraformInit

Returns a new instance of TerraformInit.



17
18
19
# File 'lib/yle_tf/action/terraform_init.rb', line 17

def initialize(app)
  @app = app
end

Instance Method Details

#backend_config(config) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/yle_tf/action/terraform_init.rb', line 55

def backend_config(config)
  backend_type = config.fetch('backend', 'type').downcase
  backend_proc = backend_proc(backend_type)

  klass = backend_proc.call
  klass.new.backend_config(config)
end

#backend_proc(backend_type) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/yle_tf/action/terraform_init.rb', line 63

def backend_proc(backend_type)
  backends = Plugin.manager.backends
  backends.fetch(backend_type.to_sym) do
    raise Error, "Unknown backend type '#{backend_type}'. " \
      "Supported backends: #{backends.keys.join(', ')}"
  end
end

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yle_tf/action/terraform_init.rb', line 21

def call(env)
  config = env[:config]
  backend = backend_config(config)

  Logger.info('Initializing Terraform')
  Logger.debug("Backend configuration: #{backend}")

  if VersionRequirement.pre_0_9?(env[:terraform_version])
    init_pre_0_9(backend)
  else
    init(backend)
  end

  @app.call(env)
end

#init(backend) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/yle_tf/action/terraform_init.rb', line 47

def init(backend)
  Logger.debug('Generating the backend configuration')
  backend.generate_config do
    Logger.debug('Initializing Terraform')
    YleTf::System.cmd('terraform', 'init', *TF_CMD_ARGS, TF_CMD_OPTS)
  end
end

#init_pre_0_9(backend) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/yle_tf/action/terraform_init.rb', line 37

def init_pre_0_9(backend)
  cli_args = backend.cli_args
  if cli_args
    YleTf::System.cmd('terraform', 'remote', 'config', *TF_CMD_ARGS, *cli_args, TF_CMD_OPTS)
  end

  Logger.debug('Fetching Terraform modules')
  YleTf::System.cmd('terraform', 'get', *TF_CMD_ARGS, TF_CMD_OPTS)
end