diff --git a/lib/chef/knife/cs_server_create.rb b/lib/chef/knife/cs_server_create.rb index e5c379f..394de6f 100644 --- a/lib/chef/knife/cs_server_create.rb +++ b/lib/chef/knife/cs_server_create.rb @@ -81,6 +81,11 @@ class CsServerCreate < Chef::Knife :proc => lambda { |n| n.split(',').map {|sn| sn.strip}} , :default => [] + option :cloudstack_disk, + :short => "-D DISK", + :long => "--disk DISK", + :description => "The name of CloudStack disk oferring" + option :cloudstack_hypervisor, :long => '--cloudstack-hypervisor HYPERVISOR', :description => "The CloudStack hypervisor type for the server" @@ -224,6 +229,7 @@ def run template : #{locate_config_value(:cloudstack_template)} zone : #{locate_config_value(:cloudstack_zone)} project: #{locate_config_value(:cloudstack_project)} + disk: #{locate_config_value(:cloudstack_disk)} network: #{locate_config_value(:cloudstack_networks)}") print "\n#{ui.color("Waiting for Server to be created", :magenta)}" @@ -236,6 +242,7 @@ def run locate_config_value(:cloudstack_template), locate_config_value(:cloudstack_zone), locate_config_value(:cloudstack_networks), + locate_config_value(:cloudstack_disk), params ) diff --git a/lib/knife-cloudstack/connection.rb b/lib/knife-cloudstack/connection.rb index d54758f..aaeae20 100644 --- a/lib/knife-cloudstack/connection.rb +++ b/lib/knife-cloudstack/connection.rb @@ -155,7 +155,7 @@ def list_servers ## # Deploys a new server using the specified parameters. - def create_server(host_name, service_name, template_name, zone_name=nil, network_names=[], extra_params) + def create_server(host_name, service_name, template_name, zone_name=nil, network_names=[], disk_name=nil, extra_params) if host_name then if get_server(host_name) then @@ -176,6 +176,23 @@ def create_server(host_name, service_name, template_name, zone_name=nil, network exit 1 end + if (!!disk_name) then + if ( disk_name =~ /(\D+):(\d+)/) then + disk_size = $2 + disk_name = $1 + end + disk = get_disk(disk_name) + if !disk then + puts "Error: Disk '#{disk_name}' is invalid" + exit 1 + end + if !disk_size.empty? && !disk['iscustomized'] then + puts "Error: You may not provide size for disk '#{disk_name}'" + exit 1 + end + end + + zone = zone_name ? get_zone(zone_name) : get_default_zone if !zone then msg = zone_name ? "Zone '#{zone_name}' is invalid" : "No default zone found" @@ -229,6 +246,9 @@ def create_server(host_name, service_name, template_name, zone_name=nil, network params['name'] = host_name if host_name + params['diskOfferingId'] = disk['id'] if !!disk && !disk.empty? + params['size'] = disk_size if !!disk_size && !disk_size.empty? + json = send_async_request(params) json['virtualmachine'] end @@ -360,6 +380,22 @@ def list_security_groups end + def get_disk(name) + params = { + 'command' => 'listDiskOfferings', + 'name' => name + } + json = send_request(params) + disk = json['diskoffering'] + + if !disk || disk.empty? then + return nil + end + + disk.first + end + + ## # Finds the template with the specified name.