Class: Bcome::Orchestration::InteractiveTerraform

Inherits:
Base
  • Object
show all
Defined in:
lib/objects/orchestration/interactive_terraform.rb

Constant Summary collapse

QUIT =
"\\q"
COMMAND_PROMPT =
"enter command or '#{QUIT}' to quit: " + "terraform".informational + "\s"

Instance Method Summary collapse

Methods inherited from Base

#do_execute, #initialize

Constructor Details

This class inherits a constructor from Bcome::Orchestration::Base

Instance Method Details

#command(raw_command) ⇒ Object

Formulate a terraform command



75
76
77
# File 'lib/objects/orchestration/interactive_terraform.rb', line 75

def command(raw_command)
  "cd #{path_to_env_config} ; terraform #{raw_command} #{var_string}"
end

#executeObject



7
8
9
10
# File 'lib/objects/orchestration/interactive_terraform.rb', line 7

def execute
  show_intro_text
  wait_for_command_input
end

#form_var_stringObject

Get the terraform variables for this stack, and merge in with our EC2 access keys



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/objects/orchestration/interactive_terraform.rb', line 49

def form_var_string
  terraform_vars = 
  ec2_credentials = @node.network_driver.raw_fog_credentials
 
  cleaned_data = terraform_vars.select{|k,v|
    !v.is_a?(Hash) && !v.is_a?(Array)
  } # we can't yet handle nested terraform metadata on the command line so no arrays or hashes

  all_vars = cleaned_data.merge({
    :access_key => ec2_credentials["aws_access_key_id"],
    :secret_key => ec2_credentials["aws_secret_access_key"]
  })

  all_vars.collect{|key, value| "-var #{key}=\"#{value}\""}.join("\s")
end

#path_to_env_configObject

Retrieve the path to the terraform configurations for this stack



70
71
72
# File 'lib/objects/orchestration/interactive_terraform.rb', line 70

def path_to_env_config
  @path_to_env_config ||= "terraform/environments/#{@node.namespace.gsub(":","_")}"
end

#process_command(raw_command) ⇒ Object

PROCESSING INTERACTIVE COMMANDS



24
25
26
27
28
# File 'lib/objects/orchestration/interactive_terraform.rb', line 24

def process_command(raw_command)
  full_command = command(raw_command)
  @node.execute_local(full_command)
  wait_for_command_input
end

#show_intro_textObject



12
13
14
15
16
17
18
19
20
# File 'lib/objects/orchestration/interactive_terraform.rb', line 12

def show_intro_text
  puts "\n"
  puts "INTERACTIVE TERRAFORM\n".underline
  puts "Namespace:\s" + "#{@node.namespace}".informational
  puts "Configuration Path:\s" + "#{path_to_env_config}/*".informational
  puts "\nConfigured metadata:\s" + .inspect.informational

  puts "\nAny commands you enter here will be passed directly to Terraform in your configuration path scope."
end

#terraform_metadataObject

COMMAND PROCESSING



44
45
46
# File 'lib/objects/orchestration/interactive_terraform.rb', line 44

def 
  @terraform_metadata ||= @node..fetch("terraform", @node..fetch(:terraform, {}))
end

#var_stringObject



65
66
67
# File 'lib/objects/orchestration/interactive_terraform.rb', line 65

def var_string
  @var_string ||= form_var_string
end

#wait_for_command_inputObject

HANDLING USER INPUT



32
33
34
35
36
37
# File 'lib/objects/orchestration/interactive_terraform.rb', line 32

def wait_for_command_input
  raw_command = wait_for_input
  unless raw_command == QUIT
    process_command(raw_command)
  end
end

#wait_for_input(message = COMMAND_PROMPT) ⇒ Object



39
40
41
# File 'lib/objects/orchestration/interactive_terraform.rb', line 39

def wait_for_input(message = COMMAND_PROMPT)
  ::Readline.readline("\n#{message}", true).squeeze('').to_s
end