Class: VagrantPlugins::Sakura::Action::RunInstance

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RunInstance

Returns a new instance of RunInstance.



13
14
15
16
# File 'lib/vagrant-sakura/action/run_instance.rb', line 13

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_sakura::action::run_instance")
end

Instance Method Details

#call(env) ⇒ Object



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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vagrant-sakura/action/run_instance.rb', line 18

def call(env)
  server_name = env[:machine].provider_config.server_name
  server_name ||= env[:machine].name
  server_plan = env[:machine].provider_config.server_plan
  disk_plan = env[:machine].provider_config.disk_plan
  disk_source_archive = env[:machine].provider_config.disk_source_archive
  sshkey_id = env[:machine].provider_config.sshkey_id
  public_key_path = env[:machine].provider_config.public_key_path
  use_insecure_key = env[:machine].provider_config.use_insecure_key

  env[:ui].info(I18n.t("vagrant_sakura.creating_instance"))
  env[:ui].info(" -- Server Name: #{server_name}")
  env[:ui].info(" -- Server Plan: #{server_plan}")
  env[:ui].info(" -- Disk Plan: #{disk_plan}")
  env[:ui].info(" -- Disk Source Archive: #{disk_source_archive}")

  api = env[:sakura_api]

  if env[:machine].provider_config.disk_id
    diskid = env[:machine].provider_config.disk_id

  else
    data = {
      "Disk" => {
        "Name" => server_name,
        "Plan" => { "ID" => disk_plan },
        "Connection" => "virtio",
        "SourceArchive" => {
          "ID" => disk_source_archive
        }
      }
    }
    response = api.post("/disk", data)
    unless response["Disk"]["ID"]
      raise 'no Disk ID returned'
    end
    diskid = response["Disk"]["ID"]

    while true
      response = api.get("/disk/#{diskid}")
      case response["Disk"]["Availability"]
      when "available"
        break
      when "migrating"
        migrated = response["Disk"]["MigratedMB"]
        size = response["Disk"]["SizeMB"]
        env[:ui].info("Disk #{diskid} is migrating (#{migrated}/#{size})")
      else
        status = presponse["Disk"]["Availability"]
        env[:ui].info("Disk #{diskid} is #{status}")
      end
      sleep 3
    end
  end

  data = {
    "Server" => {
      "Name" => server_name,
      "ServerPlan" => { "ID" => server_plan },
      "ConnectedSwitches" => [
        { "Scope" => "shared", "BandWidthMbps" => 100 }
      ]
    }
  }
  response = api.post("/server", data)
  unless response["Server"]["ID"]
    raise 'no Server ID returned'
  end
  env[:machine].id = serverid = response["Server"]["ID"]
  # Server Created

  begin
    response = api.put("/disk/#{diskid}/to/server/#{serverid}")
    # Disk mounted to Server
  rescue VagrantPlugins::Sakura::Driver::NotFoundError
    terminate(env)
    raise
  end

  data = {
    "UserSubnet" => {}
  }
  if sshkey_id
    data["SSHKey"] = { "ID" => sshkey_id }
  elsif public_key_path
    data["SSHKey"] = { "PublicKey" => File.read(public_key_path) }
  elsif use_insecure_key
    pubkey = Vagrant.source_root.join("keys", "vagrant.pub").read.chomp
    data["SSHKey"] = { "PublicKey" => pubkey }
  else
    raise 'failsafe'
  end
  response = api.put("/disk/#{diskid}/config", data)
  # Config

  response = api.put("/server/#{serverid}/power")
  # Power On

  if !env[:interrupted]
    # Wait for SSH to be ready.
    env[:ui].info(I18n.t("vagrant_sakura.waiting_for_ssh"))
    while true
      break if env[:interrupted]
      break if env[:machine].communicate.ready?
      sleep 2
    end

    #@logger.info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}")

    # Ready and booted!
    env[:ui].info(I18n.t("vagrant_sakura.ready"))
  end

  # Terminate the instance if we were interrupted
  terminate(env) if env[:interrupted]

  @app.call(env)
end

#recover(env) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/vagrant-sakura/action/run_instance.rb', line 137

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



146
147
148
149
150
151
152
# File 'lib/vagrant-sakura/action/run_instance.rb', line 146

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