Class: Chef::Knife::ProfitbricksCompositeServerCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
ProfitbricksBase
Defined in:
lib/chef/knife/profitbricks_composite_server_create.rb

Instance Method Summary collapse

Methods included from ProfitbricksBase

#connection, #error_and_exit, #get_image, included, #msg_pair, #validate_required_params

Instance Method Details

#runObject



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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/chef/knife/profitbricks_composite_server_create.rb', line 118

def run
  $stdout.sync = true

  validate_required_params(%i[datacenter_id name cores ram size type dhcp lan], Chef::Config[:knife])

  if !Chef::Config[:knife][:image] && !Chef::Config[:knife][:imagealias]
    ui.error("Either '--image' or '--image-alias' parameter must be provided")
    exit(1)
  end

  if !Chef::Config[:knife][:sshkeys] && !Chef::Config[:knife][:imagepassword]
    ui.error("Either '--image-password' or '--ssh-keys' parameter must be provided")
    exit(1)
  end

  print ui.color('Creating composite server...', :magenta).to_s
  volume_params = {
    name: Chef::Config[:knife][:volumename],
    size: Chef::Config[:knife][:size],
    bus: Chef::Config[:knife][:bus] || 'VIRTIO',
    image: Chef::Config[:knife][:image],
    type: Chef::Config[:knife][:type],
    licenceType: Chef::Config[:knife][:licencetype]
  }

  if Chef::Config[:knife][:image]
    volume_params['image'] = Chef::Config[:knife][:image]
  end

  if Chef::Config[:knife][:imagealias]
    volume_params['imageAlias'] = Chef::Config[:knife][:imagealias]
  end

  if Chef::Config[:knife][:sshkeys]
    volume_params[:sshKeys] = Chef::Config[:knife][:sshkeys]
  end

  if Chef::Config[:knife][:imagepassword]
    volume_params[:imagePassword] = Chef::Config[:knife][:imagepassword]
  end

  if config[:volume_availability_zone]
    volume_params[:availabilityZone] = Chef::Config[:knife][:volume_availability_zone]
  end

  nic_params = {
    name: Chef::Config[:knife][:nicname],
    ips: Chef::Config[:knife][:ips],
    dhcp: Chef::Config[:knife][:dhcp],
    lan: Chef::Config[:knife][:lan]
  }

  nic_params[:nat] = Chef::Config[:knife][:nat] if config[:nat]

  params = {
    name: Chef::Config[:knife][:name],
    cores: Chef::Config[:knife][:cores],
    cpuFamily: Chef::Config[:knife][:cpufamily],
    ram: Chef::Config[:knife][:ram],
    availabilityZone: Chef::Config[:knife][:availabilityzone],
    volumes: [volume_params],
    nics: [nic_params]
  }

  connection

  server = ProfitBricks::Server.create(
    Chef::Config[:knife][:datacenter_id],
    params.compact
  )

  dot = ui.color('.', :magenta)
  server.wait_for(300) { print dot; ready? }

  puts "\n"
  puts "#{ui.color('ID', :cyan)}: #{server.id}"
  puts "#{ui.color('Name', :cyan)}: #{server.properties['name']}"
  puts "#{ui.color('Cores', :cyan)}: #{server.properties['cores']}"
  puts "#{ui.color('CPU Family', :cyan)}: #{server.properties['cpuFamily']}"
  puts "#{ui.color('Ram', :cyan)}: #{server.properties['ram']}"
  puts "#{ui.color('Availability Zone', :cyan)}: #{server.properties['availabilityZone']}"

  puts 'done'
end