Class: VagrantPlugins::Brightbox::Action::CreateServer

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-brightbox/action/create_server.rb

Overview

This creates the Brightbox Server

Instance Method Summary collapse

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
# 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

  # Launch!
  env[:ui].info(I18n.t("vagrant_brightbox.launching_server"))
  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?

  begin
    options = {
      :image_id           => image_id,
	      :name		  => server_name,
      :flavor_id          => server_type,
      :zone_id 		  => zone
    }

    if !server_groups.empty?
      options[:server_groups] = server_groups
    end

    server = env[:brightbox_compute].servers.create(options)
  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
    env[:ui].info(I18n.t("vagrant_brightbox.waiting_for_build"))
    retryable(:on => Fog::Errors::TimeoutError, :tries => 30) 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
  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

#ready?(machine) ⇒ Boolean

Check if machine is ready, trapping only non-fatal errors

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-brightbox/action/create_server.rb', line 102

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



118
119
120
121
122
123
124
125
# File 'lib/vagrant-brightbox/action/create_server.rb', line 118

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



127
128
129
130
131
132
133
# File 'lib/vagrant-brightbox/action/create_server.rb', line 127

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