Class: VagrantPlugins::DigitalOcean::Actions::Create

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable, Helpers::Client
Defined in:
lib/vagrant-digitalocean/actions/create.rb

Instance Method Summary collapse

Methods included from Helpers::Client

#client

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



10
11
12
13
14
15
# File 'lib/vagrant-digitalocean/actions/create.rb', line 10

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @client = client
  @logger = Log4r::Logger.new('vagrant::digitalocean::create')
end

Instance Method Details

#call(env) ⇒ Object



17
18
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
# File 'lib/vagrant-digitalocean/actions/create.rb', line 17

def call(env)
  ssh_key_id = env[:ssh_key_id]

  size_id = @client
    .request('/sizes')
    .find_id(:sizes, :name => @machine.provider_config.size)

  image_id = @client
    .request('/images', { :filter => 'global' })
    .find_id(:images, :name => @machine.provider_config.image)

  region_id = @client
    .request('/regions')
    .find_id(:regions, :name => @machine.provider_config.region)

  # submit new droplet request
  result = @client.request('/droplets/new', {
    :size_id => size_id,
    :region_id => region_id,
    :image_id => image_id,
    :name => @machine.config.vm.hostname || @machine.name,
    :ssh_key_ids => ssh_key_id
  })

  # wait for request to complete
  env[:ui].info I18n.t('vagrant_digital_ocean.info.creating') 
  @client.wait_for_event(env, result['droplet']['event_id'])

  # assign the machine id for reference in other commands
  @machine.id = result['droplet']['id'].to_s

  # refresh droplet state with provider and output ip address
  droplet = Provider.droplet(@machine, :refresh => true)
  env[:ui].info I18n.t('vagrant_digital_ocean.info.droplet_ip', {
    :ip => droplet['ip_address']
  })

  # wait for ssh to be ready using the root user account
  user = @machine.config.ssh.username
  @machine.config.ssh.username = 'root'
  retryable(:tries => 30, :sleep => 10) do
    next if env[:interrupted]
    raise 'not ready' if !@machine.communicate.ready?
  end
  @machine.config.ssh.username = user

  @app.call(env)
end

#recover(env) ⇒ Object

Both the recover and terminate are stolen almost verbatim from the Vagrant AWS provider up action



68
69
70
71
72
73
74
# File 'lib/vagrant-digitalocean/actions/create.rb', line 68

def recover(env)
  return if env['vagrant.error'].is_a?(Vagrant::Errors::VagrantError)

  if @machine.state.id != :not_created
    terminate(env)
  end
end

#terminate(env) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/vagrant-digitalocean/actions/create.rb', line 76

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(Actions.destroy, destroy_env)
end