class Chef::Knife::Cloud::ServerCreateCommand

Attributes

create_options[RW]
server[RW]

Public Class Methods

new(argv = []) click to toggle source
Calls superclass method
# File lib/chef/knife/cloud/server/create_command.rb, line 28
def initialize(argv = [])
  super argv
  # columns_with_info is array of hash with label, key and attribute extraction callback, ex [{:label => "Label text", :key => 'key', value_callback => callback_method to extract/format the required value}, ...]
  @columns_with_info = []
end

Public Instance Methods

after_bootstrap() click to toggle source
# File lib/chef/knife/cloud/server/create_command.rb, line 111
def after_bootstrap
  service.server_summary(@server, @columns_with_info)
end
after_exec_command() click to toggle source

Derived classes can override after_exec_command and also call cleanup_on_failure if any exception occured.

# File lib/chef/knife/cloud/server/create_command.rb, line 77
def after_exec_command
  # bootstrap the server
  bootstrap
rescue CloudExceptions::BootstrapError => e
  ui.fatal(e.message)
  cleanup_on_failure
  raise e
rescue => e
  error_message = "Check if --connection-protocol and --image-os-type is correct. #{e.message}"
  ui.fatal(error_message)
  cleanup_on_failure
  raise e, error_message
end
before_bootstrap() click to toggle source

any cloud specific initializations/cleanup we want to do around bootstrap.

# File lib/chef/knife/cloud/server/create_command.rb, line 109
def before_bootstrap; end
before_exec_command() click to toggle source
# File lib/chef/knife/cloud/server/create_command.rb, line 55
def before_exec_command
  post_connection_validations
  service.create_server_dependencies
rescue CloudExceptions::ServerCreateDependenciesError => e
  ui.fatal(e.message)
  service.delete_server_dependencies
  raise e
end
bootstrap() click to toggle source

Bootstrap the server

# File lib/chef/knife/cloud/server/create_command.rb, line 99
def bootstrap
  before_bootstrap
  @bootstrapper = Bootstrapper.new(config)
  Chef::Log.debug("Bootstrapping the server...")
  ui.info("Bootstrapping the server by using #{ui.color("connection_protocol", :cyan)}: #{config[:connection_protocol]} and #{ui.color("image_os_type", :cyan)}: #{config[:image_os_type]}")
  @bootstrapper.bootstrap
  after_bootstrap
end
cleanup_on_failure() click to toggle source
# File lib/chef/knife/cloud/server/create_command.rb, line 91
def cleanup_on_failure
  if config[:delete_server_on_failure]
    service.delete_server_dependencies
    service.delete_server_on_failure(@server)
  end
end
execute_command() click to toggle source
# File lib/chef/knife/cloud/server/create_command.rb, line 64
def execute_command
  begin
    @server = service.create_server(create_options)
  rescue CloudExceptions::ServerCreateError => e
    ui.fatal(e.message)
    # server creation failed, so we need to rollback only dependencies.
    service.delete_server_dependencies
    raise e
  end
  service.server_summary(@server, @columns_with_info)
end
get_node_name(chef_node_name, prefix) click to toggle source

generate a random name if chef_node_name is empty

# File lib/chef/knife/cloud/server/create_command.rb, line 121
def get_node_name(chef_node_name, prefix)
  return chef_node_name unless chef_node_name.nil?

  # lazy uuids, 15 chars cause windows has limits
  ("#{prefix}-" + rand.to_s.split(".")[1]).slice(0, 14)
end
post_connection_validations() click to toggle source
# File lib/chef/knife/cloud/server/create_command.rb, line 128
def post_connection_validations; end
set_default_config() click to toggle source

knife-plugin can override set_default_config to set default config by using their own mechanism.

# File lib/chef/knife/cloud/server/create_command.rb, line 116
def set_default_config
  config[:image_os_type] = "windows" if config[:connection_protocol] == "winrm"
end
validate_params!() click to toggle source
# File lib/chef/knife/cloud/server/create_command.rb, line 34
def validate_params!
  # set param vm_name to a random value if the name is not set by the user (plugin)
  config[:chef_node_name] = get_node_name(config[:chef_node_name], config[:chef_node_name_prefix])

  # validate ssh_identity_file for connection protocol and connection_user, connection_password for both ssh bootstrap protocol and winrm bootstrap protocol
  errors = []
  if config[:connection_protocol] == "ssh"
    if config[:ssh_identity_file].nil? && config[:connection_password].nil?
      errors << "You must provide either SSH Identity file or Connection Password."
    end
  elsif config[:connection_protocol] == "winrm"
    if config[:connection_password].nil?
      errors << "You must provide Connection Password."
    end
  else
    errors << "You must provide a valid connection protocol. options [ssh/winrm]. For linux type images, options [ssh]"
  end
  error_message = ""
  raise CloudExceptions::ValidationError, error_message if errors.each { |e| ui.error(e); error_message = "#{error_message} #{e}." }.any?
end