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

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

  # submit new droplet request
  result = @client.post('/v2/droplets', {
    :size => @machine.provider_config.size,
    :region => @machine.provider_config.region,
    :image => @machine.provider_config.image,
    :name => @machine.config.vm.hostname || @machine.name,
    :ssh_keys => ssh_key_id,
    :private_networking => @machine.provider_config.private_networking,
    :backups => @machine.provider_config.backups_enabled,
    :ipv6 => @machine.provider_config.ipv6,
    :user_data => @machine.provider_config.user_data
  }.delete_if { |k, v| v.nil? })

  # wait for request to complete
  env[:ui].info I18n.t('vagrant_digital_ocean.info.creating')
  @client.wait_for_event(env, result['links']['actions'].first['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)
  public_network = droplet['networks']['v4'].find { |network| network['type'] == 'public' }
  private_network = droplet['networks']['v4'].find { |network| network['type'] == 'private' }
  env[:ui].info I18n.t('vagrant_digital_ocean.info.droplet_ip', {
    :ip => public_network['ip_address']
  })
  if private_network
    env[:ui].info I18n.t('vagrant_digital_ocean.info.droplet_private_ip', {
      :ip => private_network['ip_address']
    })
  end

  # wait for ssh to be ready
  switch_user = @machine.provider_config.setup?
  user = @machine.config.ssh.username
  @machine.config.ssh.username = 'root' if switch_user

  retryable(:tries => 120, :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



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

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



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

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