Class: MCollective::Util::Playbook::Nodes::TerraformNodes
- Inherits:
-
Object
- Object
- MCollective::Util::Playbook::Nodes::TerraformNodes
- Defined in:
- lib/mcollective/util/playbook/nodes/terraform_nodes.rb
Instance Method Summary collapse
- #choria ⇒ Object
- #discover ⇒ Object
- #from_hash(data) ⇒ Object
- #output_data ⇒ Object
- #prepare ⇒ Object
- #tf_output ⇒ Object
- #valid_hostname?(host) ⇒ Boolean
- #validate_configuration! ⇒ Object
Instance Method Details
#choria ⇒ Object
54 55 56 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 54 def choria @_choria ||= Util::Choria.new(false) end |
#discover ⇒ Object
58 59 60 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 58 def discover output_data end |
#from_hash(data) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 19 def from_hash(data) @state = data["statefile"] @output = data["output"] @terraform = data.fetch("terraform", choria.which("terraform")) self end |
#output_data ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 40 def output_data return @_data if @_data data = JSON.parse(tf_output) raise("Only terraform outputs of type list is supported") unless data["type"] == "list" data["value"].each do |result| raise("%s is not a valid hostname" % result) unless valid_hostname?(result) end @_data = data["value"] end |
#prepare ⇒ Object
6 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 6 def prepare; end |
#tf_output ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 31 def tf_output shell = Shell.new("%s output -state %s -json %s 2>&1" % [@terraform, @state, @output]) shell.runcommand raise("Terraform exited with code %d: %s" % [shell.status.exitstatus, shell.stdout]) unless shell.status.exitstatus == 0 shell.stdout end |
#valid_hostname?(host) ⇒ Boolean
27 28 29 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 27 def valid_hostname?(host) host =~ /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/ end |
#validate_configuration! ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/mcollective/util/playbook/nodes/terraform_nodes.rb', line 8 def validate_configuration! raise("The supplied terraform path %s is not executable" % @terraform) if @terraform && !File.executable?(@terraform) raise("A terraform state file is needed") unless @state raise("The terraform statefile %s is not readable" % @state) unless File.readable?(@state) raise("An output name is needed") unless @output Validator.validate(@terraform, :shellsafe) Validator.validate(@state, :shellsafe) Validator.validate(@output, :shellsafe) end |