class Chef::Knife::OneandoneServerCreate

Public Instance Methods

run() click to toggle source
# File lib/chef/knife/oneandone_server_create.rb, line 115
def run
  $stdout.sync = true

  validate(config[:name], '-n NAME')
  validate(config[:appliance_id], '-I APPLIANCE_ID')

  init_client

  size_id = config[:fixed_size_id]
  hdds = nil

  if size_id.nil? || size_id.empty?
    hdds = [
      {
        'size' => config[:hdd_size],
        'is_main' => true
      }
    ]
  end

  pkeys_config = config[:public_key]
  pkeys = nil

  if !pkeys_config.nil? && !pkeys_config.empty?
    pkeys_config = pkeys_config.split(',')
    pkeys = []
    pkeys_config.each do |key|
      pkeys << key.strip
    end
  end

  server = OneAndOne::Server.new
  response = server.create(
    name: config[:name],
    description: config[:description],
    datacenter_id: config[:datacenter_id],
    fixed_instance_id: size_id,
    appliance_id: config[:appliance_id],
    vcore: config[:cpu],
    cores_per_processor: config[:cores],
    ram: config[:ram],
    hdds: hdds,
    power_on: config[:power_on],
    password: config[:password],
    rsa_key: config[:rsa_key],
    firewall_id: config[:firewall_id],
    ip_id: config[:ip_id],
    load_balancer_id: config[:load_balancer_id],
    monitoring_policy_id: config[:monitoring_policy_id],
    public_key: pkeys,
    server_type: config[:server_type],
    baremetal_model_id: config[:baremetal_model_id]
  )

  if config[:wait]
    puts ui.color('Deploying, wait for the operation to complete...', :cyan).to_s

    # wait for provisioning 30m max
    server.wait_for(timeout: 30, interval: 15)

    formated_output(server.get, true)

    first_password = server.first_password.nil? ? config[:password] : server.first_password
    first_ip = !server.specs['ips'].empty? ? server.specs['ips'][0]['ip'] : ''

    puts "\t#{ui.color('ID', :cyan)}: #{server.id}"
    puts "\t#{ui.color('Name', :cyan)}: #{server.specs['name']}"
    puts "\t#{ui.color('First IP', :cyan)}: #{first_ip}"
    puts "\t#{ui.color('First Password', :cyan)}: #{first_password}\n"

    puts ui.color('done', :bold).to_s
  else
    formated_output(response, true)
    puts "Server #{response['id']} is #{ui.color('being deployed', :bold)}"
  end
end