Class: Chef::Knife::Cloud::ServerCreateCommand

Inherits:
Command show all
Defined in:
lib/chef/knife/cloud/server/create_command.rb

Instance Attribute Summary collapse

Attributes inherited from Command

#custom_arguments, #service

Instance Method Summary collapse

Methods inherited from Command

#create_service_instance, #pretty_key, #run, #validate!

Methods included from Helpers

#locate_config_value, #msg_pair

Constructor Details

#initialize(argv = []) ⇒ ServerCreateCommand

Returns a new instance of ServerCreateCommand.



27
28
29
30
31
# File 'lib/chef/knife/cloud/server/create_command.rb', line 27

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

Instance Attribute Details

#create_optionsObject

Returns the value of attribute create_options.



25
26
27
# File 'lib/chef/knife/cloud/server/create_command.rb', line 25

def create_options
  @create_options
end

#serverObject

Returns the value of attribute server.



25
26
27
# File 'lib/chef/knife/cloud/server/create_command.rb', line 25

def server
  @server
end

Instance Method Details

#after_bootstrapObject



117
118
119
# File 'lib/chef/knife/cloud/server/create_command.rb', line 117

def after_bootstrap
  service.server_summary(@server, @columns_with_info)
end

#after_exec_commandObject

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/knife/cloud/server/create_command.rb', line 79

def after_exec_command
  begin
    # bootstrap the server
    bootstrap
  rescue CloudExceptions::BootstrapError => e
    ui.fatal(e.message)
    cleanup_on_failure
    raise e
  rescue => e
    error_message = "Check if --bootstrap-protocol and --image-os-type is correct. #{e.message}"
    ui.fatal(error_message)
    cleanup_on_failure
    raise e, error_message
  end
end

#before_bootstrapObject

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



113
114
115
# File 'lib/chef/knife/cloud/server/create_command.rb', line 113

def before_bootstrap
  ssh_override_winrm if locate_config_value(:bootstrap_protocol) == 'ssh'
end

#before_exec_commandObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/knife/cloud/server/create_command.rb', line 55

def before_exec_command
  begin
    post_connection_validations
    service.create_server_dependencies
  rescue CloudExceptions::ServerCreateDependenciesError => e
    ui.fatal(e.message)
    service.delete_server_dependencies
    raise e
  end
end

#bootstrapObject

Bootstrap the server



103
104
105
106
107
108
109
110
# File 'lib/chef/knife/cloud/server/create_command.rb', line 103

def bootstrap
  before_bootstrap
  @bootstrapper = Bootstrapper.new(config)
  Chef::Log.debug("Bootstrapping the server...")
  ui.info("Bootstrapping the server by using #{ui.color("bootstrap_protocol", :cyan)}: #{config[:bootstrap_protocol]} and #{ui.color("image_os_type", :cyan)}: #{config[:image_os_type]}")
  @bootstrapper.bootstrap
  after_bootstrap
end

#cleanup_on_failureObject



95
96
97
98
99
100
# File 'lib/chef/knife/cloud/server/create_command.rb', line 95

def cleanup_on_failure
  if config[:delete_server_on_failure]
    service.delete_server_dependencies
    service.delete_server_on_failure(@server)
  end
end

#execute_commandObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/knife/cloud/server/create_command.rb', line 66

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) ⇒ Object

generate a random name if chef_node_name is empty



127
128
129
130
131
# File 'lib/chef/knife/cloud/server/create_command.rb', line 127

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
  chef_node_name = ("#{prefix}-"+rand.to_s.split('.')[1]).slice(0,14)
end

#post_connection_validationsObject



133
134
# File 'lib/chef/knife/cloud/server/create_command.rb', line 133

def post_connection_validations
end

#set_default_configObject

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



122
123
124
# File 'lib/chef/knife/cloud/server/create_command.rb', line 122

def set_default_config
  config[:image_os_type] = 'windows' if config[:bootstrap_protocol] == 'winrm'
end

#validate_params!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/knife/cloud/server/create_command.rb', line 33

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(locate_config_value(:chef_node_name), locate_config_value(:chef_node_name_prefix))

  # validate ssh_user, ssh_password, identity_file for ssh bootstrap protocol and winrm_password for winrm bootstrap protocol
  errors = []

  if locate_config_value(:bootstrap_protocol) == 'ssh'
    if locate_config_value(:identity_file).nil? && locate_config_value(:ssh_password).nil?
      errors << "You must provide either Identity file or SSH Password."
    end
  elsif locate_config_value(:bootstrap_protocol) == 'winrm'
    if locate_config_value(:winrm_password).nil?
      errors << "You must provide Winrm Password."
    end
  else
    errors << "You must provide a valid bootstrap 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