Class: VagrantPlugins::TerraformProvider::Action::WaitForVmUp
- Inherits:
-
Object
- Object
- VagrantPlugins::TerraformProvider::Action::WaitForVmUp
- Includes:
- Util::TerraformExecute
- Defined in:
- lib/vagrant-terraform/action/wait_for_vm_up.rb
Overview
Wait till VM is started, till it obtains an IP address and is accessible via ssh.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ WaitForVmUp
constructor
A new instance of WaitForVmUp.
- #port_open?(ip, port, seconds = 10) ⇒ Boolean
- #recover(env) ⇒ Object
- #terminate(env) ⇒ Object
Methods included from Util::TerraformExecute
Constructor Details
#initialize(app, env) ⇒ WaitForVmUp
Returns a new instance of WaitForVmUp.
16 17 18 19 |
# File 'lib/vagrant-terraform/action/wait_for_vm_up.rb', line 16 def initialize(app, env) @logger = Log4r::Logger.new("vagrant_terraform::action::wait_for_vm_up") @app = app end |
Instance Method Details
#call(env) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vagrant-terraform/action/wait_for_vm_up.rb', line 38 def call(env) # Initialize metrics if they haven't been env[:metrics] ||= {} # Wait for VM to obtain an ip address. env[:metrics]["instance_ip_time"] = Util::Timer.time do env[:ui].info(I18n.t("vagrant_terraform.waiting_for_ip")) for attempt in 1..300 # If we're interrupted don't worry about waiting next if env[:interrupted] output = terraform_execute(env, "terraform refresh") ip_addr = output.match(/\b(?:\d{1,3}\.){3}\d{1,3}\b/)&.to_s unless ip_addr.nil? env[:ui].info("Got IP (attempt #{attempt}): #{ip_addr}") # Check if SSH-Server is up if port_open?(ip_addr, 22) env[:ip_address] = ip_addr @logger.debug("Got output #{env[:ip_address]}") break end end sleep 2 end end terminate(env) if env[:interrupted] if env[:ip_address].nil? env[:ui].error("failed to get IP: #{env[:metrics]["instance_ip_time"]}") raise Errors::NoIPError else @logger.info("Got IP address #{env[:ip_address]}") @logger.info("Time for getting IP: #{env[:metrics]["instance_ip_time"]}") # Booted and ready for use. env[:ui].info(I18n.t("vagrant_terraform.ready")) @app.call(env) end end |
#port_open?(ip, port, seconds = 10) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vagrant-terraform/action/wait_for_vm_up.rb', line 21 def port_open?(ip, port, seconds=10) # => checks if a port is open or not on a remote host Timeout::timeout(seconds) do begin TCPSocket.new(ip, port).close @logger.info("SSH Check OK for IP: #{ip}") true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError => e @logger.info("SSH Connection Failed for IP #{ip}: #{e}") false end end rescue Timeout::Error @logger.info("SSH Connection Failed: Timeout for IP: #{ip}" ) false end |
#recover(env) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/vagrant-terraform/action/wait_for_vm_up.rb', line 80 def recover(env) return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError) if env[:machine].provider.state.id != :not_created # Undo the import terminate(env) end end |
#terminate(env) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/vagrant-terraform/action/wait_for_vm_up.rb', line 89 def terminate(env) destroy_env = env.dup destroy_env.delete(:interrupted) destroy_env[:config_validate] = false destroy_env[:force_confirm_destroy] = true env[:action_runner].run(Action.action_destroy, destroy_env) end |