Class: VagrantPlugins::Brightbox::Action::CreateServer
- Inherits:
-
Object
- Object
- VagrantPlugins::Brightbox::Action::CreateServer
- Includes:
- Vagrant::Util::Retryable
- Defined in:
- lib/vagrant-brightbox/action/create_server.rb
Overview
This creates the Brightbox Server
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ CreateServer
constructor
A new instance of CreateServer.
- #normalise_id(collection, element, pattern) ⇒ Object
-
#ready?(machine) ⇒ Boolean
Check if machine is ready, trapping only non-fatal errors.
- #recover(env) ⇒ Object
- #terminate(env) ⇒ Object
Constructor Details
#initialize(app, env) ⇒ CreateServer
Returns a new instance of CreateServer.
14 15 16 17 |
# File 'lib/vagrant-brightbox/action/create_server.rb', line 14 def initialize(app, env) @app = app @logger = Log4r::Logger.new("vagrant_brightbox::action::create_server") end |
Instance Method Details
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/vagrant-brightbox/action/create_server.rb', line 19 def call(env) # Initialize metrics if they haven't been env[:metrics] ||= {} # Get the region we're going to booting up in region = env[:machine].provider_config.region # Get the configs region_config = env[:machine].provider_config.get_region_config(region) image_id = region_config.image_id zone = region_config.zone server_name = region_config.server_name server_type = region_config.server_type server_groups = region_config.server_groups user_data = region_config.user_data zone_id = normalise_id( lambda {env[:brightbox_compute].zones}, zone, /^zon-/) server_type_id = normalise_id( lambda {env[:brightbox_compute].flavors}, server_type, /^typ-/) # Launch! env[:ui].info(I18n.t("vagrant_brightbox.launching_server")) env[:ui].info(I18n.t("vagrant_brightbox.supplied_user_data")) if user_data env[:ui].info(" -- Type: #{server_type}") if server_type env[:ui].info(" -- Image: #{image_id}") if image_id env[:ui].info(" -- Region: #{region}") env[:ui].info(" -- Name: #{server_name}") if server_name env[:ui].info(" -- Zone: #{zone}") if zone env[:ui].info(" -- Server Groups: #{server_groups.inspect}") if !server_groups.empty? @logger.info(" -- Zone ID: #{zone_id}") if zone_id @logger.info(" -- Type ID: #{server_type_id}") if server_type_id begin = { :image_id => image_id, :name => server_name, :flavor_id => server_type_id, :user_data => user_data, :zone_id => zone_id } [:server_groups] = server_groups unless server_groups.empty? server = env[:brightbox_compute].servers.create() rescue Excon::Errors::HTTPStatusError => e raise Errors::FogError, :message => e.response end # Immediately save the ID since it is created at this point. env[:machine].id = server.id # Wait for the server to build env[:metrics]["server_build_time"] = Util::Timer.time do tries = region_config.server_build_timeout / 2 env[:ui].info(I18n.t("vagrant_brightbox.waiting_for_build")) begin retryable(:on => Fog::Errors::TimeoutError, :tries => tries) do # If we're interrupted don't worry about waiting next if env[:interrupted] # Wait for the server to be ready server.wait_for(2) { ready? } end rescue Fog::Errors::TimeoutError # Delete the server terminate(env) # Notify the user raise Errors::ServerBuildTimeout, timeout: region_config.server_build_timeout end end @logger.info("Time for server to build: #{env[:metrics]["server_build_time"]}") if !env[:interrupted] @app.call(env) env[:metrics]["instance_ssh_time"] = Util::Timer.time do # Wait for SSH to be ready. env[:ui].info(I18n.t("vagrant_brightbox.waiting_for_ssh")) while true # If we're interrupted then just back out break if env[:interrupted] break if ready?(env[:machine]) sleep 2 end end @logger.info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}") # Ready and booted! env[:ui].info(I18n.t("vagrant_brightbox.ready")) end # Terminate the instance if we were interrupted terminate(env) if env[:interrupted] end |
#normalise_id(collection, element, pattern) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/vagrant-brightbox/action/create_server.rb', line 157 def normalise_id(collection, element, pattern) @logger.info("Normalising element #{element.inspect}") @logger.info("Against pattern #{pattern.inspect}") case element when pattern, nil element else result = collection.call.find { |f| f.handle == element } if result result.id else element end end end |
#ready?(machine) ⇒ Boolean
Check if machine is ready, trapping only non-fatal errors
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vagrant-brightbox/action/create_server.rb', line 124 def ready?(machine) @logger.info("Checking if SSH is ready or is permanently broken...") @logger.info("Connecting as '#{machine.ssh_info[:username]}'") if machine.ssh_info[:username] # Yes this is cheating. machine.communicate.send(:connect) @logger.info("SSH is ready") true # Fatal errors rescue Vagrant::Errors::SSHAuthenticationFailed raise # Transient errors rescue Vagrant::Errors::VagrantError => e @logger.info("SSH not up: #{e.inspect}") return false end |
#recover(env) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/vagrant-brightbox/action/create_server.rb', line 140 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
149 150 151 152 153 154 155 |
# File 'lib/vagrant-brightbox/action/create_server.rb', line 149 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 |