Class: VagrantPlugins::Vmpooler::Action::CreateServer

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateServer

Returns a new instance of CreateServer.



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

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_vmpooler::action::create_server")
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
# File 'lib/vagrant-vmpooler/action/create_server.rb', line 18

def call(env)
  # Get the configs
  provider_config = env[:machine].provider_config

  token = provider_config.token
  url = provider_config.url
  os = provider_config.os
  ttl = provider_config.ttl
  disk = provider_config.disk
  verbose = provider_config.verbose

  if !url
    raise Errors::NoURLError,
      :message => "There was no provided url to connect to vmpooler with"
  end

  if !token
    env[:ui].warn("There was no token provided. All vms will only last 2 hours.")
  end

  if !os
    raise Errors::NoURLError,
      :message => "There was no operatingystem provided"
  end

  # Output the settings we're going to use to the user
  env[:ui].info(I18n.t("vagrant_vmpooler.launching_server"))
  env[:ui].info(" -- Vmpooler URL: #{url}")
  env[:ui].info(" -- Vmpooler Verbose Mode: #{verbose}")
  env[:ui].info(" -- Image: #{os}")
  env[:ui].info(" -- Additional TTL: #{ttl}") if ttl
  env[:ui].info(" -- Additional Disk: #{disk}") if disk

  # Create the server
  os_arr = {}
  os_arr[os] = 1
  server = Pooler.retrieve(verbose, os_arr, token, url)

  # Store the ID right away so we can track it
  # in this case it's the hostname
  if server['ok'] == false
    raise Errors::GetVMError,
      :message => "Could not retrieve vm from pooler:\n #{server}"
  end

  server_name = server[os]["hostname"]
  env[:machine].id = server_name

  tags = {}
  tags['vagrant-vmpooler'] = VagrantPlugins::Vmpooler::VERSION

  response_body = Pooler.modify(verbose, url, server_name, token, nil, tags)
  if response_body['ok'] == false
    env[:ui].warn(I18n.t("vagrant_vmpooler.errors.failed_tag"))
  end

  if ttl
    response_body = Pooler.modify(verbose, url, server_name, token, ttl, nil)
    if response_body['ok'] == false
      env[:ui].warn(I18n.t("vagrant_vmpooler.errors.failed_ttl"))
    end
  end

  if disk
    response_body = Pooler.disk(verbose, url, server_name, token, disk)
    if response_body['ok'] == false
      env[:ui].warn(I18n.t("vagrant_vmpooler.errors.failed_disk_extend"))
    end
  end

  if !env[:interrupted]
    # Clear the line one more time so the progress is removed
    env[:ui].clear_line

    # Wait for SSH to become available
    env[:ui].info(I18n.t("vagrant_vmpooler.waiting_for_ssh"))
    while true
      begin
        # If we're interrupted then just back out
        break if env[:interrupted]
        break if env[:machine].communicate.ready?
      rescue Errno::ENETUNREACH, Errno::EHOSTUNREACH
      end
      sleep 2
    end

    env[:ui].info(I18n.t("vagrant_vmpooler.ready"))
  end

  @app.call(env)
end