module Hobo class VM attr_reader :vm class < true) end def import HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..." @vm = VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base])) end def persist HOBO_LOGGER.info "Persisting the VM UUID (#{@vm.uuid})..." Env.persist_vm(@vm) end def setup_mac_address HOBO_LOGGER.info "Matching MAC addresses..." @vm.nics.first.macaddress = Hobo.config[:vm][:base_mac] @vm.save(true) end def forward_ssh HOBO_LOGGER.info "Forwarding SSH ports..." port = VirtualBox::ForwardedPort.new port.name = "ssh" port.hostport = Hobo.config[:ssh][:port] port.guestport = 22 @vm.forwarded_ports << port @vm.save(true) end def setup_shared_folder HOBO_LOGGER.info "Creating shared folders..." folder = VirtualBox::SharedFolder.new folder.name = "hobo-root-path" folder.hostpath = Env.root_path @vm.shared_folders << folder @vm.save(true) end def start HOBO_LOGGER.info "Booting VM..." @vm.start(:headless, true) # Now we have to wait for the boot to be successful HOBO_LOGGER.info "Waiting for VM to boot..." counter = 1 begin sleep 5 unless ENV['HOBO_ENV'] == 'test' HOBO_LOGGER.info "Trying to connect (attempt ##{counter} of #{Hobo.config[:ssh][:max_tries]})..." SSH.execute { |ssh| } rescue Errno::ECONNREFUSED if counter >= Hobo.config[:ssh][:max_tries].to_i HOBO_LOGGER.info "Failed to connect to VM! Failed to boot?" return false end counter += 1 retry end HOBO_LOGGER.info "VM booted and ready for use!" true end end end