diff --git a/lib/yle_tf.rb b/lib/yle_tf.rb index 778103e..27634a5 100644 --- a/lib/yle_tf.rb +++ b/lib/yle_tf.rb @@ -4,6 +4,8 @@ require 'yle_tf/version' class YleTf + TERRAFORM_VERSION_REQUIREMENT = '>= 0.9' + autoload :Action, 'yle_tf/action' autoload :Error, 'yle_tf/error' autoload :Plugin, 'yle_tf/plugin' diff --git a/lib/yle_tf/action/terraform_init.rb b/lib/yle_tf/action/terraform_init.rb index 12310ea..b5c15b1 100644 --- a/lib/yle_tf/action/terraform_init.rb +++ b/lib/yle_tf/action/terraform_init.rb @@ -4,7 +4,6 @@ require 'yle_tf/logger' require 'yle_tf/plugin' require 'yle_tf/system' -require 'yle_tf/version_requirement' class YleTf module Action @@ -27,31 +26,16 @@ def call(env) 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 + init(backend) @app.call(env) end - 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 - 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 + Logger.debug('Configuring the backend') + backend.generate_config + Logger.debug('Initializing Terraform') + YleTf::System.cmd('terraform', 'init', *TF_CMD_ARGS, TF_CMD_OPTS) end def backend_config(config) diff --git a/lib/yle_tf/action/verify_terraform_version.rb b/lib/yle_tf/action/verify_terraform_version.rb index ce57c67..0dc7c92 100644 --- a/lib/yle_tf/action/verify_terraform_version.rb +++ b/lib/yle_tf/action/verify_terraform_version.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'yle_tf' require 'yle_tf/error' require 'yle_tf/logger' require 'yle_tf/system' @@ -19,22 +20,32 @@ def call(env) raise(Error, 'Terraform not found') if !version Logger.debug("Terraform version: #{version}") - verify_version(env) + + verify_version(version, requirement_by_yletf, required_by: 'YleTf') + verify_version(version, requirement_by_config(env), required_by: 'config') @app.call(env) end def terraform_version v = YleTf::System.read_cmd('terraform', 'version', error_handler: proc {}) - Regexp.last_match(1) if v =~ /^Terraform v([^\s]+)/ + m = /^Terraform v(?[^\s]+)/.match(v) + m && m[:version] end - def verify_version(env) - version = env[:terraform_version] + def requirement_by_config(env) requirement = env[:config].fetch('terraform', 'version_requirement') { nil } + VersionRequirement.new(requirement) + end + + def requirement_by_yletf + VersionRequirement.new(YleTf::TERRAFORM_VERSION_REQUIREMENT) + end - if !VersionRequirement.new(requirement).satisfied_by?(version) - raise Error, "Terraform version '#{requirement}' required, '#{version}' found" + def verify_version(version, requirement, **opts) + if !requirement.satisfied_by?(version) + raise Error, "Terraform version '#{requirement}' required by #{opts[:required_by]}, " \ + "'#{version}' found" end end end diff --git a/lib/yle_tf/backend_config.rb b/lib/yle_tf/backend_config.rb index 790791b..64201c9 100644 --- a/lib/yle_tf/backend_config.rb +++ b/lib/yle_tf/backend_config.rb @@ -13,15 +13,6 @@ def initialize(type, config) @config = config end - # Returns an `Array` of CLI args for Terraform pre 0.9 `init` command - def cli_args - args = ["-backend=#{type}"] - config.each do |key, value| - args << "-backend-config=#{key}=#{value}" - end - args - end - # Generate backend configuration file for Terraform v0.9+ def generate_config data = { @@ -30,7 +21,6 @@ def generate_config }] } File.write(BACKEND_CONFIG_FILE, JSON.pretty_generate(data)) - yield if block_given? end # Returns the backend configuration as a `Hash` for Terraform v0.9+ diff --git a/lib/yle_tf/version_requirement.rb b/lib/yle_tf/version_requirement.rb index d24cff1..995bef6 100644 --- a/lib/yle_tf/version_requirement.rb +++ b/lib/yle_tf/version_requirement.rb @@ -5,11 +5,6 @@ class YleTf # Helper class for comparing versions class VersionRequirement - # Checks if the specified Terrform version is older than 0.9 - def self.pre_0_9?(terraform_version) - new('< 0.9.0-beta').satisfied_by?(terraform_version) - end - attr_reader :requirement def initialize(requirement) diff --git a/lib/yle_tf_plugins/backends/file/config.rb b/lib/yle_tf_plugins/backends/file/config.rb index 37ed38b..39c6a35 100644 --- a/lib/yle_tf_plugins/backends/file/config.rb +++ b/lib/yle_tf_plugins/backends/file/config.rb @@ -7,11 +7,7 @@ module Backends module File class Config < YleTf::BackendConfig def generate_config - yield if block_given? - end - - def cli_args - nil + # Do nothing, as the state file is just symlinked end end end