Class: Terraspace::CLI::CheckSetup

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/terraspace/cli/check_setup.rb

Constant Summary collapse

REQUIRED_TERRAFORM_VERSION =

Terraspace requires at least this version of terraform

"0.12"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CheckSetup

Returns a new instance of CheckSetup.



8
9
10
# File 'lib/terraspace/cli/check_setup.rb', line 8

def initialize(options={})
  @options = options
end

Class Method Details

.check!Object

Used as library call



91
92
93
94
95
96
# File 'lib/terraspace/cli/check_setup.rb', line 91

def check!
  setup = new
  return if setup.ok?
  # run meth designed for CLI and will puts out informative messages about installed version and exit 1 when version is not ok
  setup.run
end

Instance Method Details

#check_command?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/terraspace/cli/check_setup.rb', line 37

def check_command?
  ARGV[0] == "check_setup"
end

#check_required_version!Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/terraspace/cli/check_setup.rb', line 25

def check_required_version!
  puts "Terraspace requires Terraform v#{REQUIRED_TERRAFORM_VERSION}.x and above"
  if ok?
    puts "You're all set!"
  else
    puts "The installed version of terraform may not work with terraspace."
    puts "Recommend using at least terraform v#{REQUIRED_TERRAFORM_VERSION}.x"
    puts "If you would like to bypass this check. Use TS_VERSION_CHECK=0" unless check_command?
    exit 1 unless ENV['TS_VERSION_CHECK'] == '0'
  end
end

#ok?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/terraspace/cli/check_setup.rb', line 48

def ok?
  unless terraform_bin
    puts terraform_is_not_installed
    exit 1
  end

  version = terraform_version_message.sub(/.*v/,'') # => 0.12.24
  unless version.match(/\d+\.\d+\.\d+/) # just parse did not find the version number
    puts "WARN: Unable to get the terraform version".color(:yellow)
    return true
  end
  major, minor, _ = version.split('.')
  required_major, required_minor = REQUIRED_TERRAFORM_VERSION.split('.')
  x = major.to_i >= required_major.to_i
  y = minor.to_i >= required_minor.to_i
  x && y
end

#runObject

Used for the CLI



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/terraspace/cli/check_setup.rb', line 13

def run
  puts "Detected Terrspace version: #{Terraspace::VERSION}"
  if terraform_bin
    puts "Detected Terraform bin: #{terraform_bin}"
    puts "Detected #{terraform_version_message}"
    check_required_version!
  else
    puts terraform_is_not_installed
    exit 1
  end
end

#terraform_binObject



66
67
68
69
70
71
# File 'lib/terraspace/cli/check_setup.rb', line 66

def terraform_bin
  out = `type terraform 2>&1`.strip
  return unless $?.success?
  md = out.match(/is (.*)/)
  md[1] if md
end

#terraform_is_not_installedObject



41
42
43
44
45
46
# File 'lib/terraspace/cli/check_setup.rb', line 41

def terraform_is_not_installed
  <<~EOL
    Terraform not installed. Unable to detect a terraform command. Please double check that terraform is installed.
    See: https://terraspace.cloud/docs/install/terraform/
  EOL
end

#terraform_version_messageObject

Sometimes Terraform shows the version info on the first line and sometimes on the bottom line. Account for that by finding the line.

$ terraform --version
Terraform v0.12.24

Your version of Terraform is out of date! The latest version
is 0.12.26. You can update by downloading from https://www.terraform.io/downloads.html

Note: The -json option is only available in v0.13+



84
85
86
# File 'lib/terraspace/cli/check_setup.rb', line 84

def terraform_version_message
  `terraform --version`.split("\n").find { |l| l =~ /^Terraform / }.strip
end