Class: VagrantPlugins::ArubaCloud::Action::CreateServer

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

Overview

Create a new server

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateServer

Returns a new instance of CreateServer.



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

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_arubacloud::action::create_server')
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-arubacloud/action/create_server.rb', line 19

def call(env)
  config = env[:machine].provider_config
  arubacloud_dc = config.endpoint
  tb_package_id = {'small' => 'CPU: 1, Ram(GB): 1, DiskSize(GB): 20',
                   'medium' => 'CPU: 1, Ram(GB): 2, DiskSize(GB): 40',
                   'large' => 'CPU: 2, Ram(GB): 4, DiskSize(GB): 80',
                   'extra large' => 'CPU: 4, Ram(GB): 8, DiskSize(GB): 160'
        }
  tb_package_id.default = " *unknow package_id* "

  sshpwd = env[:machine].config.ssh.password

  # Set Server Name
  server_name = config.server_name || env[:machine].name

  # Output the settings we're going to use to the user
  env[:ui].info('Creating a server with the following settings...')
  env[:ui].info(" -- Datacenter:    #{arubacloud_dc}")
  env[:ui].info(" -- Name:          #{server_name}")
  env[:ui].info(" -- Root Password: #{sshpwd}")
  # Build the config hash according to the service type
  if config.service_type.eql? 4
    options = {
        :name => server_name,
        :vm_type => 'smart',
        :admin_passwd => sshpwd,
        :cpu => 1,
        :memory => 1,
        :template_id => config.template_id,
        :package_id => config.package_id
    }
    env[:ui].info(" -- Package:       #{config.package_id}  config as:  #{tb_package_id[config.package_id]}")

  else
    # Processing hds
    disks = []
    accum = "["
    config.hds.each do |disk_spec|
      disks << env[:arubacloud_compute].disks.create({
          :size => disk_spec[:size],
          :virtual_disk_type => disk_spec[:type]}).get_hash
      accum += "(hd#{disk_spec[:type]}: #{disk_spec[:size]})"
    end
    accum += "]"
    options = {
        :name => server_name,
        :vm_type => 'pro',
        :admin_passwd => sshpwd,
        :cpu => config.cpu_number,
        :memory => config.ram_qty,
        :template_id => config.template_id,
        :disks => disks
    }
    env[:ui].info(" -- Config as:   CPU: #{config.cpu_number}, Ram(GB): #{config.ram_qty}, Disk(GB): #{accum} ")
  end

  env[:ui].info(" -- OS Template:   #{config.template_id}")
  env[:ui].info(" -- Service Type:  #{config.service_type} (#{options[:vm_type]}) ")

  # Create the server
  begin
    server = env[:arubacloud_compute].servers.create(options)
  rescue Fog::ArubaCloud::Errors::RequestError => e
    message = ''
    error = nil
    @logger.debug(e.inspect.to_yaml)
    if e.response['ResultCode'].eql? 16
      message = "Virtual machine with name: #{options[:name]}, already present. Bailout!"
      error = Errors::MachineAlreadyPresent
    elsif e.response['ResultCode'].eql?(-500)
      message = 'Server returned an unexpected response. Bailout!'
      error = Errors::BadServerResponse
    end
    env[:ui].warn("Response message: #{e.response.to_yaml}")
    env[:ui].warn(message)
    raise error
  end

  # Store id of the machine
  env[:machine].id = server.id

  # Wait for ssh to be ready
  env[:ui].info(" [#{arubacloud_dc}] " + 'Waiting until server is ready...')

  retryable(:tries => 20, :sleep => 45) do
    next if env[:interrupted]
    server.wait_for(5) { ready? }
  end

  env[:ui].info(" [#{arubacloud_dc}] " + 'The server is ready!')

  @app.call(env)
end