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

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



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

def initialize(app, env)
  @app, @env = app, env
  @client = Helpers::Client.new
  @translator = Helpers::Translator.new("actions.create")
end

Instance Method Details

#call(env) ⇒ Object



15
16
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vagrant-digitalocean/actions/create.rb', line 15

def call(env)
  # if the machine state is created skip
  if env[:machine].state.id == :active
    env[:ui].info @translator.t("skip")
    return @app.call(env)
  end

  # TODO check the content of the key to see if it's changed
  # TODO use the directory / vm name to qualify the key name
  begin
    ssh_key_id = @client
      .request("/ssh_keys/")
      .find_id(:ssh_keys, :name => "Vagrant Insecure")
  rescue Errors::ResultMatchError
    env[:ui].info @translator.t("create_key")

    key = DigitalOcean.vagrant_key

    result = @client.request("/ssh_keys/new", {
      :name => "Vagrant Insecure",
      :ssh_pub_key => key
    })

    ssh_key_id = result["ssh_key"]["id"]
  end

  size_id = @client
    .request("/sizes")
    .find_id(:sizes, :name => env[:machine].provider_config.size)

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

  region_id = @client
    .request("/regions")
    .find_id(:regions, :name => env[:machine].provider_config.region)

  env[:ui].info @translator.t("create_droplet")

  result = @client.request("/droplets/new", {
    :size_id => size_id,
    :region_id => region_id,
    :image_id => image_id,
    # TODO use the current directory name as a post fix
    :name => "vagrant",
    :ssh_key_ids => ssh_key_id
  })

  # assign the machine id for reference in other commands
  env[:machine].id = result["droplet"]["id"]

  env[:ui].info @translator.t("wait_active")

  retryable(:tries => 30, :sleep => 10) do
    # If we're interrupted don't worry about waiting
    next if env[:interrupted]

    # Wait for the server to be ready
    raise "not ready" if env[:machine].state.id != :active
  end

  # signal that the machine has just been created, used in ReadState
  env[:machine_just_created] = true

  @app.call(env)
end

#recover(env) ⇒ Object

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



85
86
87
88
89
90
91
# File 'lib/vagrant-digitalocean/actions/create.rb', line 85

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

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

#terminate(env) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/vagrant-digitalocean/actions/create.rb', line 93

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