#!/usr/bin/env ruby # -------------------------------------------------------------------------- # # Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # # # # Licensed under the Apache License, Version 2.0 (the "License"); you may # # not use this file except in compliance with the License. You may obtain # # a copy of the License at # # # # http://www.apache.org/licenses/LICENSE-2.0 # # # # Unless required by applicable law or agreed to in writing, software # # distributed under the License is distributed on an "AS IS" BASIS, # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # ONE_LOCATION=ENV["ONE_LOCATION"] if !ONE_LOCATION RUBY_LIB_LOCATION="/usr/lib/one/ruby" else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cli" require 'command_parser' require 'one_helper/onevm_helper' cmd=CommandParser::CmdParser.new(ARGV) do usage "onevm COMMAND [args..] [options..]" version OpenNebulaHelper::ONE_VERSION helper = OneVMHelper.new ######################################################################## # Global Options ######################################################################## set :option, CommandParser::OPTIONS ######################################################################## # Formatters for arguments ######################################################################## set :format, :vmid, OneVMHelper.to_id_desc do |arg| helper.to_id(arg) end set :format, :vmid_list, OneVMHelper.list_to_id_desc do |arg| helper.list_to_id(arg) end set :format, :filterflag, OneVMHelper.filterflag_to_i_desc do |arg| helper.filterflag_to_i(arg) end ######################################################################## # Commands ######################################################################## command :cancel, 'Cancel a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"canceling") do |vm| vm.cancel end end command :chgrp, 'Changes the Virtual Machine group',[:range, :vmid_list], :text do helper.perform_actions(args[0],options,"User/Group changed") do |vm| vm.chown(-1, args[1].to_i) end end command :chown, 'Changes the Virtual Machine owner and group', [:range, :vmid_list], :text, [:text,nil] do gid = args[2].nil? ? -1 : args[2].to_id helper.perform_actions(args[0],options,"Group changed") do |vm| vm.chown(args[1].to_i, gid) end end command :create, 'Create a new Virtual Machine', :file do helper.create_resource(options) do |vm| template=File.read(args[0]) vm.allocate(template) end end command :delete, 'Removes a Virtual Machine', [:range, :vmid_list] do helper.perform_actions(args[0],options,"deleted") do |vm| vm.delete end end # TBD hostid instead of text in the second argument command :deploy, 'Deploy a Virtual Machine', [:range,:vmid_list], :text do host_id = args[1] verbose = "deploying in host #{host_id}" helper.perform_actions(args[0],options,verbose) do |vm| vm.deploy(host_id) end end command :hold, 'Hold a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"holding") do |vm| vm.hold end end command :list, 'Lists Virtual Machine in the pool', [:filterflag, nil], :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do helper.list_pool(options) end # TBD hostid instead of text in the second argument command :livemigrate, 'Livemigrate a Virtual Machine', [:range,:vmid_list], :text do host_id = args[1] verbose = "live migrating to #{host_id}" helper.perform_actions(args[0],options,verbose) do |vm| vm.livemigrate(host_id) end end # TBD hostid instead of text in the second argument command :migrate, 'Migrate a Virtual Machine', [:range,:vmid_list], :text do host_id = args[1] verbose = "migrating to #{host_id}" helper.perform_actions(args[0],options,verbose) do |vm| vm.migrate(host_id) end end command :release, 'Release a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"releasing") do |vm| vm.release end end command :restart, 'Restart a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"restarting") do |vm| vm.restart end end command :resubmit, 'Resubmit a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"resubmiting") do |vm| vm.restart end end command :resume, 'Resume a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"resuming") do |vm| vm.resume end end command :saveas, 'Save as a Virtual Machine', :vmid, :text do disk_id = args[1].to_i image_name = args[2] verbose = "disk #{disk_id} prepared to be saved in " << "the image #{image_name}" helper.perform_action(args[0],options,verbose) do |vm| vm.save_as(disk_id, image_name) end end command :show, 'Gets info from a Virtual Machine', :vmid, :options=>OpenNebulaHelper::XML do helper.show_resource(args[0],options) end command :stop, 'Stop a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"stopping") do |vm| vm.stop end end command :suspend, 'Suspend a Virtual Machine', [:range,:vmid_list] do helper.perform_actions(args[0],options,"suspending") do |vm| vm.suspend end end command :top, 'Tops Virtual Machine in the pool', [:filterflag, nil], :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do helper.list_pool(options, true) end end