Class: Chef::Knife::Cloud::ServerCreateCommand
- Inherits:
-
Command
- Object
- Chef::Knife
- Command
- Chef::Knife::Cloud::ServerCreateCommand
- Defined in:
- lib/chef/knife/cloud/server/create_command.rb
Instance Attribute Summary collapse
-
#create_options ⇒ Object
Returns the value of attribute create_options.
-
#server ⇒ Object
Returns the value of attribute server.
Attributes inherited from Command
Instance Method Summary collapse
- #after_bootstrap ⇒ Object
-
#after_exec_command ⇒ Object
Derived classes can override after_exec_command and also call cleanup_on_failure if any exception occured.
-
#before_bootstrap ⇒ Object
any cloud specific initializations/cleanup we want to do around bootstrap.
- #before_exec_command ⇒ Object
-
#bootstrap ⇒ Object
Bootstrap the server.
- #cleanup_on_failure ⇒ Object
- #execute_command ⇒ Object
-
#get_node_name(chef_node_name, prefix) ⇒ Object
generate a random name if chef_node_name is empty.
-
#initialize(argv = []) ⇒ ServerCreateCommand
constructor
A new instance of ServerCreateCommand.
- #post_connection_validations ⇒ Object
-
#set_default_config ⇒ Object
knife-plugin can override set_default_config to set default config by using their own mechanism.
- #validate_params! ⇒ Object
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_options ⇒ Object
Returns the value of attribute create_options.
25 26 27 |
# File 'lib/chef/knife/cloud/server/create_command.rb', line 25 def @create_options end |
#server ⇒ Object
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_bootstrap ⇒ Object
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_command ⇒ Object
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.) cleanup_on_failure raise e rescue => e = "Check if --bootstrap-protocol and --image-os-type is correct. #{e.}" ui.fatal() cleanup_on_failure raise e, end end |
#before_bootstrap ⇒ Object
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_command ⇒ Object
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.) service.delete_server_dependencies raise e end end |
#bootstrap ⇒ Object
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_failure ⇒ Object
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_command ⇒ Object
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() rescue CloudExceptions::ServerCreateError => e ui.fatal(e.) # 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_validations ⇒ Object
133 134 |
# File 'lib/chef/knife/cloud/server/create_command.rb', line 133 def post_connection_validations end |
#set_default_config ⇒ Object
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 = "" raise CloudExceptions::ValidationError, if errors.each{|e| ui.error(e); = "#{} #{e}."}.any? end |