Method: TFWrapper::RakeTasks#initialize

Defined in:
lib/tfwrapper/raketasks.rb

#initialize(tf_dir, opts = {}) ⇒ RakeTasks

Generate Rake tasks for working with Terraform.

Parameters:

  • tf_dir (String)

    Terraform config directory, relative to Rakefile. Set to ‘.’ if the Rakefile is in the same directory as the .tf configuration files.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :backend_config (Hash)

    hash of Terraform remote state backend configuration options, to override or supplement those in the terraform configuration. See the [Remote State](www.terraform.io/docs/state/remote.html) documentation for further information.

  • :namespace_prefix (String)

    if specified and not nil, this will put all tasks in a “#namespace_prefix_tf:” namespace instead of “tf:”. This allows using manheim_helpers for multiple terraform configurations in the same Rakefile.

  • :tf_vars_from_env (Hash)

    hash of Terraform variables to the (required) environment variables to populate their values from

  • :allowed_empty_vars (Hash)

    array of environment variable names (specified in :tf_vars_from_env) to allow to be empty or missing.

  • :tf_extra_vars (Hash)

    hash of Terraform variables to their values; overrides any same-named keys in tf_vars_from_env

  • :tf_sensitive_vars (Array)

    list of Terraform variables which should not be printed

  • :consul_url (String)

    URL to access Consul at, for the :consul_env_vars_prefix option.

  • :consul_env_vars_prefix (String)

    if specified and not nil, write the environment variables used from tf_vars_from_env and their values to JSON at this path in Consul. This should have the same naming constraints as consul_prefix.

  • :before_proc (Proc)

    Proc instance to call before executing the body of each task. Called with two arguments, the String full (namespaced) name of the task being executed, and tf_dir. Returning or breaking from this Proc will cause the task to not execute; to exit the Proc early, use next.

  • :after_proc (Proc)

    Proc instance to call after executing the body of each task. Called with two arguments, the String full (namespaced) name of the task being executed, and tf_dir. This will not execute if the body of the task fails.

  • :disable_landscape (Bool)

    By default, if the terraform_landscape gem can be loaded, it will be used to reformat the output of “terraform plan“. If this is not desired, set to true to disable landscale. Default: false.

  • :landscape_progress (Symbol, nil)

    The terraform_landscape code used to reformat plan output requires the full output of the complete plan execution. By default, this means that when landscape is used, no output will appear from the time “terraform plan“ begins until the command is complete. If progress output is desired, this option can be set to one of the following: :dots to print a dot to STDOUT for every line of “terraform plan“ output, :lines to print a dot followed by a newline (e.g. for systems like Jenkins that line buffer) for every line of “terraform plan“ output, or :stream to stream the raw “terraform plan“ output (which will then be followed by the reformatted landscape output). Default is nil to show no progress output.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tfwrapper/raketasks.rb', line 95

def initialize(tf_dir, opts = {})
  # find the directory that contains the Rakefile
  rakedir = File.realpath(Rake.application.rakefile)
  rakedir = File.dirname(rakedir) if File.file?(rakedir)
  @tf_dir = File.realpath(File.join(rakedir, tf_dir))
  @ns_prefix = opts.fetch(:namespace_prefix, nil)
  @consul_env_vars_prefix = opts.fetch(:consul_env_vars_prefix, nil)
  @tf_vars_from_env = opts.fetch(:tf_vars_from_env, {})
  @allowed_empty_vars = opts.fetch(:allowed_empty_vars, [])
  @tf_sensitive_vars = opts.fetch(:tf_sensitive_vars, [])
  @tf_extra_vars = opts.fetch(:tf_extra_vars, {})
  @backend_config = opts.fetch(:backend_config, {})
  @consul_url = opts.fetch(:consul_url, nil)
  @disable_landscape = opts.fetch(:disable_landscape, false)
  @landscape_progress = opts.fetch(:landscape_progress, nil)
  unless [:dots, :lines, :stream, nil].include?(@landscape_progress)
    raise(
      ArgumentError,
      'landscape_progress option must be one of: ' \
      '[:dots, :lines, :stream, nil]'
    )
  end
  @before_proc = opts.fetch(:before_proc, nil)
  if !@before_proc.nil? && !@before_proc.is_a?(Proc)
    raise(
      TypeError,
      'TFWrapper::RakeTasks.initialize option :before_proc must be a ' \
      'Proc instance, not a ' + @before_proc.class.name
    )
  end
  @after_proc = opts.fetch(:after_proc, nil)
  if !@after_proc.nil? && !@after_proc.is_a?(Proc)
    raise(
      TypeError,
      'TFWrapper::RakeTasks.initialize option :after_proc must be a Proc ' \
      'instance, not a ' + @after_proc.class.name
    )
  end
  # default to lowest possible version; this is set in the 'init' task
  @tf_version = Gem::Version.new('0.0.0')
  # rubocop:disable Style/GuardClause
  if @consul_url.nil? && !@consul_env_vars_prefix.nil?
    raise StandardError, 'Cannot set env vars in Consul when consul_url ' \
      'option is nil.'
  end
  # rubocop:enable Style/GuardClause
end