Class: VagrantPlugins::ProfitBricks::Action::CreateServer

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

Overview

This creates the ProfitBricks server.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateServer

Returns a new instance of CreateServer.



15
16
17
18
19
# File 'lib/vagrant-profitbricks/action/create_server.rb', line 15

def initialize(app, env)
  @app     = app
  @compute = env[:profitbricks_compute]
  @logger  = Log4r::Logger.new('vagrant_profitbricks::action::create_server')
end

Instance Method Details

#call(env) ⇒ Object



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vagrant-profitbricks/action/create_server.rb', line 21

def call(env)
  uuid_regex = /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/
  # Get the ProfitBricks configs
  config           = env[:machine].provider_config
  machine_config   = env[:machine].config
  begin
    communicator = machine_config.vm.communicator ||= :ssh
  rescue NoMethodError
    communicator = :ssh
  end

  # Figure out the Data Center ID,
  if config.datacenter_id.match uuid_regex
    datacenter = find_matching(@compute.datacenters, config.datacenter_id)
  else
    raise Errors::NoDatacenterID
  end

  # Find the flavor
  if config.flavor
    env[:ui].info(I18n.t('vagrant_profitbricks.finding_flavor'))
    flavor = find_matching(@compute.flavors, config.flavor)
    raise Errors::NoMatchingFlavor unless flavor
  end

  if !config.image && !config.image_alias
    raise Errors::ImageOrLicenceTypeMustBeProvided
  end

  if config.image
    images = @compute.images.all
    image = find_matching_image(images, config.image, datacenter.location)
  end

  if config.image_alias
    # Figure out the name for the server
    server_name = config.server_name || env[:machine].name
  end
  # Prepare volume values
  vol_name = server_name.to_s + '_volume' if server_name

  vol_size = config.volume_size ? config.volume_size : 5
  vol_type = config.volume_type
  vol_licence_type = config.volume_licence_type if config.volume_licence_type
  vol_ssh_keys = config.public_ssh_keys if config.public_ssh_keys
  vol_availability_zone = config.volume_availability_zone
  vol_image_password = config.image_password if config.image_password

  volume = {
    name: vol_name,
    size: vol_size,
    type: vol_type,
    availability_zone: vol_availability_zone
  }

  if image
    volume[:image] = image.id
  elsif config.image_alias
    volume[:image_alias] = config.image_alias
  elsif vol_licence_type
    volume[:licence_type] = vol_licence_type
  else
    raise Errors::ImageOrLicenceTypeMustBeProvided
  end

  if vol_ssh_keys
    volume[:ssh_keys] = vol_ssh_keys
  elsif vol_image_password
    volume[:image_password] = vol_image_password
  else
    raise Errors::ImageOrLicenceTypeMustBeProvided
  end

  # create LAN if it does not exist, and create a NIC on that LAN
  lan_id = config.lan_id
  begin
    lan = @compute.lans.get(datacenter.id, config.lan_id)
  rescue Excon::Error::NotFound
    raise Errors::NoLan
  end

  if !lan || lan.id.end_with?('/lans')
    lan_name = server_name.to_s + '_lan' if server_name
    lan = @compute.lans.create(datacenter_id: datacenter.id, name: lan_name, public: true)
    lan_id = lan.id
  end

  nic_name = server_name.to_s + '_nic' if server_name
  nic = {
    name: nic_name,
    lan: lan_id,
    dhcp: true,
    nat: config.nat
  }

  # Output the settings we're going to use to the user
  env[:ui].info(I18n.t('vagrant_profitbricks.launching_server'))
  env[:ui].info(" -- Data Center ID: #{datacenter.id}")
  env[:ui].info(" -- Location: #{datacenter.location}")
  env[:ui].info(" -- Name: #{server_name}")
  env[:ui].info(" -- Volume Name: #{vol_name}")
  env[:ui].info(" -- Image: #{image.name}") if image
  env[:ui].info(" -- Image: #{config.image_alias}") if config.image_alias

  server_cores = nil
  server_ram = nil

  if flavor
    env[:ui].info(" -- Flavor: #{flavor.name}")
    server_cores = flavor.cores
    server_ram = flavor.ram
  elsif config.ram && config.cores
    env[:ui].info(" -- Server cores: #{config.cores}")
    env[:ui].info(" -- Server ram: #{config.ram} MB")
    server_cores = config.cores
    server_ram = config.ram
  else
    raise Errors::NoMatchingFlavor
  end

  # Create the server
  env[:ui].info(I18n.t('vagrant_profitbricks.creating_server'))
  server = @compute.servers.create(datacenter_id: datacenter.id,
                                   name: server_name,
                                   cores: server_cores,
                                   ram: server_ram,
                                   cpu_family: config.cpu_family,
                                   volumes: [volume],
                                   nics: [nic])
  server.wait_for { ready? }

  # Store the ID right away so we can track it
  env[:machine].id = server.id

  env[:ui].info(I18n.t('vagrant_profitbricks.ready'))
  @app.call(env)
end