Skip to content

Commit

Permalink
Drop support for Terraform 0.8 and older
Browse files Browse the repository at this point in the history
The support has been actually broken for year now, since PR #9.
Nobody has complained, so clean up the code base a bit.

Anyway, add a check and clear error message for old Terraform versions.
  • Loading branch information
tmatilai committed Dec 31, 2018
1 parent 8b5b5f9 commit 0010702
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 47 deletions.
2 changes: 2 additions & 0 deletions lib/yle_tf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 5 additions & 21 deletions lib/yle_tf/action/terraform_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
23 changes: 17 additions & 6 deletions lib/yle_tf/action/verify_terraform_version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'yle_tf'
require 'yle_tf/error'
require 'yle_tf/logger'
require 'yle_tf/system'
Expand All @@ -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(?<version>[^\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
Expand Down
10 changes: 0 additions & 10 deletions lib/yle_tf/backend_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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+
Expand Down
5 changes: 0 additions & 5 deletions lib/yle_tf/version_requirement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions lib/yle_tf_plugins/backends/file/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0010702

Please sign in to comment.