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

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

Instance Attribute Summary collapse

Attributes inherited from BootstrapCommand

#custom_arguments, #service

Instance Method Summary collapse

Methods inherited from BootstrapCommand

#run

Methods included from Helpers

#create_service_instance, #msg_pair, #pretty_key, #validate!

Constructor Details

#initialize(argv = []) ⇒ ServerCreateCommand

Returns a new instance of ServerCreateCommand.



28
29
30
31
32
# 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

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



111
112
113
# File 'lib/chef/knife/cloud/server/create_command.rb', line 111

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
# 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_bootstrapObject

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



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

def before_bootstrap; end

#before_exec_commandObject



55
56
57
58
59
60
61
62
# 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

#bootstrapObject

Bootstrap the server



99
100
101
102
103
104
105
106
# 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_failureObject



91
92
93
94
95
96
# 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_commandObject



64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

generate a random name if chef_node_name is empty



121
122
123
124
125
126
# 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_validationsObject



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

def post_connection_validations; end

#set_default_configObject

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



116
117
118
# 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!Object



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 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