diff --git a/src/cli/cli_helper.rb b/src/cli/cli_helper.rb index 67b40ce6fc..570ffd1a24 100644 --- a/src/cli/cli_helper.rb +++ b/src/cli/cli_helper.rb @@ -82,6 +82,36 @@ module CLIHelper print "\33[#{x};#{y}H" end + def CLIHelper.scr_red + print "\33[31m" + end + + def CLIHelper.scr_green + print "\33[32m" + end + + ANSI_RED="\33[31m" + ANSI_GREEN="\33[32m" + ANSI_RESET="\33[0m" + + OK_STATES=%w{runn rdy on} + BAD_STATES=%w{fail err err} + + def CLIHelper.color_state(stat) + if $stdout.tty? + case stat.strip + when *OK_STATES + ANSI_GREEN+stat+ANSI_RESET + when *BAD_STATES + ANSI_RED+stat+ANSI_RESET + else + stat + end + else + stat + end + end + # Print header def CLIHelper.print_header(str, underline=true) if $stdout.tty? @@ -177,12 +207,23 @@ module CLIHelper ncolumns=@default_columns.length res_data=data_array(data, options) + if options[:stat_column] + stat_column=@default_columns.index( + options[:stat_column].upcase.to_sym) + else + stat_column=@default_columns.index(:STAT) + end + begin print res_data.collect{|l| (0..ncolumns-1).collect{ |i| dat=l[i] col=@default_columns[i] - format_str(col, dat) + + str=format_str(col, dat) + str=CLIHelper.color_state(str) if i==stat_column + + str }.join(' ') }.join("\n") rescue Errno::EPIPE diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 3fcf53f35d..8857c889c7 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -67,6 +67,60 @@ EOT :description => "Describe list columns" } + # Command line VM template options + TEMPLATE_OPTIONS=[ + { + :name => 'name', + :large => '--name name', + :description => + 'Name for the VM', + :format => String + }, + { + :name => 'cpu', + :large => '--cpu cpu', + :description => + 'CPU percentage reserved for the VM (1=100% one CPU)', + :format => Float + }, + { + :name => 'memory', + :large => '--memory memory', + :description => 'Memory ammount given to the VM', + :format => String, + :proc => lambda do |o,options| + m=o.strip.match(/^(\d+(?:\.\d+)?)(m|mb|g|gb)?$/i) + + if !m + [-1, 'Memory value malformed'] + else + multiplier=case m[2] + when /(g|gb)/i + 1024 + else + 1 + end + + value=m[1].to_f*multiplier + + [0, value.floor] + end + end + }, + { + :name => 'disk', + :large => '--disk disk0,disk1', + :description => 'Disks to attach. To use a disk owned by other user use user[disk]', + :format => Array + }, + { + :name => 'network', + :large => '--network network0,network1', + :description => 'Networks to attach. To use a network owned by other user use user[network]', + :format => Array + } + ] + OPTIONS = XML, NUMERIC, KILOBYTES class OneHelper @@ -458,4 +512,64 @@ EOT str = File.read(path) str end + + def self.parse_user_object(user_object) + reg=/^([^\[]+)(?:\[([^\]]+)\])?$/ + + m=user_object.match(reg) + + return nil if !m + + user=nil + if m[2] + user=m[1] + object=m[2] + else + object=m[1] + end + + [user, object] + end + + def self.create_disk_net(objects, section, name) + template='' + + objects.each do |obj| + res=parse_user_object(obj) + return [-1, "#{section.capitalize} \"#{obj}\" malformed"] if !res + user, object=*res + + template<<"#{section.upcase}=[\n" + template<<" #{name.upcase}_UNAME=\"#{user}\",\n" if user + template<<" #{name.upcase}=\"#{object}\"\n" + template<<"]\n" + end if objects + + [0, template] + end + + def self.create_template(options) + template='' + + template<<"NAME=\"#{options[:name]}\"\n" if options[:name] + template<<"CPU=#{options[:cpu]}\n" if options[:cpu] + template<<"MEMORY=#{options[:memory]}\n" if options[:memory] + + if options[:disk] + res=create_disk_net(options[:disk], 'DISK', 'IMAGE') + return res if res.first!=0 + + template<OpenNebulaHelper::TEMPLATE_OPTIONS do + res=OpenNebulaHelper.create_template(options) + + if res.first!=0 + STDERR.puts res.last + next -1 + end + helper.create_resource(options) do |t| - template=File.read(args[0]) + if args[0] + template=File.read(args[0]) + else + template='' + end + + template<[OneVMHelper::MULTIPLE] do + command :create, create_desc, [:file, nil], + :options=>[OneVMHelper::MULTIPLE]+ + OpenNebulaHelper::TEMPLATE_OPTIONS do number = options[:multiple] || 1 exit_code=nil + res=OpenNebulaHelper.create_template(options) + + if res.first!=0 + STDERR.puts res.last + next -1 + end + + if args[0] + begin + template=File.read(args[0]) + rescue + STDERR.puts "Error reading template." + next -1 + end + else + template='' + end + + template<