F #1884: Bug Fixes, minor changes

=================================

F #1884: show header (CLI)

F #1884: new list format for vCenter importer

F #1884: Bug fixex for importing networks

F #1884: fixing network opts importation

F #1884: import_bulk added! solve rollback bug

F #1884: improve rollback
This commit is contained in:
semedi committed 2018-04-18 11:04:08 +02:00
1 parent 29ebb3f191
commit dfcebe882c
8 files changed
+137 -84

No files matched your search

+40 -51
View File
@@ -29,19 +29,19 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
TABLE = {
VOBJECT::DATASTORE => {
:struct => ["DATASTORE_LIST", "DATASTORE"],
:columns => {:IMID => 10, :REF => 10, :VCENTER => 10, :NAME => 10, :CLUSTERS => 10},
:columns => {:IMID => 5, :REF => 15, :NAME => 25, :CLUSTERS => 10},
:cli => [:host],
:dialogue => ->(arg){}
},
VOBJECT::TEMPLATE => {
:struct => ["TEMPLATE_LIST", "TEMPLATE"],
:columns => {:IMID => 10, :REF => 10, :VCENTER => 10, :NAME => 10 , :CLUSTERS => 10},
:columns => {:IMID => 5, :REF => 10, :NAME => 35},
:cli => [:host],
:dialogue => ->(arg){ OneVcenterHelper.template_dialogue(arg) }
},
VOBJECT::NETWORK => {
:struct => ["NETWORK_LIST", "NETWORK"],
:columns => {:IMID => 10, :REF => 10, :VCENTER => 10 , :NAME => 10},
:columns => {:IMID => 5, :REF => 15, :NAME => 10},
:cli => [:host],
:dialogue => ->(arg){ OneVcenterHelper.network_dialogue(arg) }
},
@@ -55,7 +55,7 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
#######################
# CLI
# CLI ARGS
########################
def datastore(arg)
ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, arg)
@@ -70,6 +70,16 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
end
########################
def show_header(vcenter_host)
CLIHelper.scr_bold
CLIHelper.scr_underline
puts "# vCenter: #{vcenter_host}".ljust(50)
CLIHelper.scr_restore
puts
end
def set_object(type)
type = type.downcase
if (type == "datastores")
@@ -104,8 +114,12 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
end
def list_object(options, list)
list = cli_format(list)
vcenter_host = list.keys[0]
list = cli_format(list.values.first)
table = format_list()
show_header(vcenter_host)
table.show(list)
end
@@ -117,12 +131,12 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
set_object(opts[:object])
res = {}
TABLE[@vobject][:cli].each do |arg|
raise "#{arg} it's mandadory for this op" if opts[arg].nil?
res[arg] = self.method(arg).call(opts[arg])
end
return res
end
@@ -137,10 +151,6 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
d[:ref]
end
column :VCENTER, "vCenter", :left, :size=>config[:VCENTER] || 20 do |d|
d[:vcenter]
end
column :NAME, "Name", :left, :size=>config[:NAME] || 20 do |d|
d[:name] || d[:simple_name]
end
@@ -264,79 +274,58 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
return answer
}
size="255"
ar_type="ether"
first_ip=""
first_mac=""
slaac=""
global_prefix=""
ula_prefix=""
ip6_address = ""
prefix_length = ""
opts = { size: "255", type: "ether" }
question = " How many VMs are you planning"\
" to fit into this network [255]? "
size = ask.call(question, "255")
opts[:size] = ask.call(question, "255")
question = " What type of Virtual Network"\
" do you want to create (IPv[4],IPv[6], [E]thernet)?"
type_answer = ask.call(question, "ether")
if ["4","6","ether", "e", "ip4", "ip6" ].include?(type_answer.downcase)
ar_type = type_answer.downcase
else
ar_type = "ether"
supported_types = ["4","6","ether", "e", "ip4", "ip6" ]
if !supported_types.include?(type_answer)
type_answer = 'e'
STDOUT.puts " Type [#{type_answer}] not supported,"\
" defaulting to Ethernet."
end
end
question_ip = " Please input the first IP in the range: "
question_mac = " Please input the first MAC in the range [Enter for default]: "
case ar_type.downcase
case type_answer.downcase
when "4", "ip4"
first_ip = ask.call(question_ip)
firts_mac = ask.call(question_mac)
opts[:ip] = ask.call(question_ip)
opts[:mac] = ask.call(question_mac)
opts[:type] = "ip"
when "6", "ip6"
firts_mac = ask.call(question_mac)
opts[:mac] = ask.call(question_mac)
question = " Do you want to use SLAAC "\
"Stateless Address Autoconfiguration? ([y]/n): "
slaac_answer = ask.call(question)
slaac_answer = ask.call(question, 'y').downcase
if slaac_answer == 'n'
slaac = false
question = " Please input the IPv6 address (cannot be empty): "
ip6_address = ask.call(question)
opts[:ip6] = ask.call(question)
question = " Please input the Prefix length (cannot be empty): "
prefix_length = ask.call(question)
ar_type = "ip6"
opts[:prefix_length] = ask.call(question)
opts[:type] = 'ip6_static'
else
slaac = true
question = " Please input the GLOBAL PREFIX "\
"[Enter for default]: "
gp_answer = ask.call(question)
opts[:global_prefix] = ask.call(question)
question= " Please input the ULA PREFIX "\
"[Enter for default]: "
ula_answer = ask.call(question)
ar_type = "ip6_static"
opts[:ula_prefix] = ask.call(question)
opts[:type] = 'ip6'
end
when "e", "ether"
first_mac = ask.call(question_mac)
opts[:mac] = ask.call(question_mac)
end
opts = {
size: size,
type: ar_type,
ip: first_ip,
mac: first_mac,
slaac: slaac,
global_prefix: global_prefix,
ula_refix: ula_prefix,
ip6: ip6_address,
prefix_length: prefix_length
}
return opts
end
end
+1 -1
View File
@@ -154,4 +154,4 @@ cmd = CommandParser::CmdParser.new(ARGV) do
exit_code 0
end
end
end
end
+27 -1
View File
@@ -98,6 +98,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do
:format => String
}
ALL = {
:name => "all",
:large => "--all",
:description => "Import all list",
:format => String
}
############################################################################
# Global Options
############################################################################
@@ -123,6 +130,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
importer = VCenterDriver::VcImporter.new_child(helper.client, vi_client, options[:object])
list = importer.get_list(args)
helper.list_object(options, list)
else
puts "for the moment is not supported without the proper authentication"
@@ -134,7 +142,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Import the the desired vCenter object
EOT
command :import, import_desc, :oid, :options=>[ OBJECT, HOST, DATASTORE, VCENTER, USER, PASS, USE_DEFAULTS ] do
command :import, import_desc, :oid, :options=>[ OBJECT, HOST, DATASTORE ] do
indexes = args.first
if options[:host] && options[:object]
@@ -155,6 +163,24 @@ cmd=CommandParser::CmdParser.new(ARGV) do
exit 0
end
command :import_defaults, import_desc, :options=>[ OBJECT, HOST, DATASTORE ] do
if options[:host] && options[:object]
args = helper.parse_opts(options)
vi_client = VCenterDriver::VIClient.new_from_host(options[:host])
importer = VCenterDriver::VcImporter.new_child(helper.client, vi_client, options[:object])
importer.get_list(args)
importer.import_bulk
importer.stdout
else
puts "for the moment is not supported without the proper authentication"
end
exit 0
end
############################################################################
# Import clusters
############################################################################
@@ -149,7 +149,6 @@ class DatacenterFolder
ds_objects[ds_ref][:ref] = ds_ref
ds_objects[ds_ref][:import_id] = import_id
ds_objects[ds_ref][:datacenter] = dc_name
ds_objects[ds_ref][:vcenter] = vcenter_instance_name
ds_objects[ds_ref][:simple_name] = "#{ds_name}"
ds_objects[ds_ref][:total_mb] = ds_total_mb
ds_objects[ds_ref][:free_mb] = ds_free_mb
@@ -235,7 +234,8 @@ class DatacenterFolder
end
end
ds_objects
{ vcenter_instance_name => ds_objects }
end
def get_unimported_templates(vi_client, tpool)
@@ -316,7 +316,7 @@ class DatacenterFolder
end
end
template_objects
{ vcenter_instance_name => template_objects }
end
def get_unimported_networks(npool,vcenter_instance_name, hpool)
@@ -410,7 +410,7 @@ class DatacenterFolder
browser = r.obj.environmentBrowser || nil
if browser
browser.QueryConfigTarget.distributedVirtualPortgroup.each do |s|
next if networks[s.portgroupKey][:processed]
next if !networks.key?(s.portgroupKey) || networks[s.portgroupKey][:processed]
networks[s.portgroupKey][:uplink] = s.uplinkPortgroup
networks[s.portgroupKey][:processed] = true
@@ -427,7 +427,6 @@ class DatacenterFolder
opts[:vcenter_instance_name] = vcenter_instance_name
opts[:dc_name] = dc_name
imid = 0
clusters.each do |ref, info|
one_host = VCenterDriver::VIHelper.find_by_ref(OpenNebula::HostPool,
"TEMPLATE/VCENTER_CCR_REF",
@@ -454,9 +453,6 @@ class DatacenterFolder
if !networks[network_ref][:uplink]
networks[network_ref][:import_id] = imid
imid += 1
# network can belong to more than 1 cluster
networks[network_ref][:clusters][:refs] << ref
networks[network_ref][:clusters][:one_ids] << cluster_id
@@ -478,7 +474,10 @@ class DatacenterFolder
end #clusters loop
end # datacenters loop
return networks
imid = -1
networks.map{ |k,v| v[:import_id] = imid += 1 }
{ vcenter_instance_name => networks }
end
end # class DatatacenterFolder
@@ -292,6 +292,7 @@ class Datastore < Storage
"Got '#{item.class} instead."
end
@vi_client = vi_client
@item = item
@one_item = {}
end
@@ -655,8 +656,9 @@ class Datastore < Storage
rescue Exception => e
raise "Could not find images. Reason: #{e.message}/#{e.backtrace}"
end
vname = @vi_client.vc_name || ""
return images
{ vname => images }
end
# This is never cached
@@ -672,7 +674,7 @@ class DsImporter < VCenterDriver::VcImporter
@one_class = OpenNebula::Datastore
end
def get_list(&block)
def get_list(args = {})
dc_folder = VCenterDriver::DatacenterFolder.new(@vi_client)
# one pool creation
@@ -746,6 +748,7 @@ class ImageImporter < VCenterDriver::VcImporter
ds_ref = args[:datastore][:ds_ref]
one_ds = args[:datastore][:one_item]
raise "can't retrieve ref info from openNebula datastore" unless ds_ref
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, @vi_client).tap do |spawn|
@@ -324,25 +324,22 @@ class NetImporter < VCenterDriver::VcImporter
type = opts[:type].downcase
case type
when "4", "ip4"
when "4", "ip4", "ip"
str << "IP4\""
str << ",IP=" + opts[:ip] if opts[:ip]
str << ",MAC=" + opts[:mac] if opts[:mac]
str << ",IP=\"#{opts[:ip]}\"" if opts[:ip]
when 'ip6'
str << "IP6\""
str << ",MAC=" + opts[:mac] if opts[:mac]
str << ",GLOBAL_PREFIX=" + opts[:global_prefix] if opts[:global_prefix]
str << ",ULA_PREFIX=" + ula_prefix if ula_prefix
str << ",GLOBAL_PREFIX=\"#{opts[:global_prefix]}\"" if opts[:global_prefix]
str << ",ULA_PREFIX=\"#{opts[:ula_prefix]}\"" if opts[:ula_prefix]
when 'ether', 'e'
str << "ETHER\""
str << ",MAC=" + opts[:mac] if opts[:mac]
when 'ip6_static'
str << "IP6_STATIC\""
str << ",MAC=" + opts[:mac] if opts[:mac]
str << ",IP6=" + opts[:ip6] if opts[:ip6]
str << ",PREFIX_LENGTH=" + prefix_length if prefix_length
str << ",IP6=\"#{opts[:ip6]}\"" if opts[:ip6]
str << ",PREFIX_LENGTH=\"#{opts[:prefix_length]}\"" if opts[:prefix_length]
end
str << ",MAC=\"#{opts[:mac]}\"" if opts[:mac]
str << ",SIZE = \"#{opts[:size]}\"]"
return str
@@ -373,6 +370,10 @@ class NetImporter < VCenterDriver::VcImporter
return res
end
def defaults
{ size: "255", type: "ether" }
end
end
end # module VCenterDriver
@@ -5,13 +5,14 @@ module VCenterDriver
protected
class Raction
def initialize(object, method)
def initialize(object, method, args = [])
@object = object
@action = method
@args = args
end
def apply
@object.method(@action).call
@object.method(@action).call(*@args)
end
end
@@ -35,13 +36,15 @@ module VCenterDriver
@one_client = one_client
@list = {}
@info = {}
@rollback = []
@info[:clusters] = {}
@info[:success] = []
@info[:error] = []
end
def self.new_child(one_client, vi_client, type)
case type
case type.downcase
when "datastores"
VCenterDriver::DsImporter.new(one_client, vi_client)
when "templates"
@@ -80,19 +83,33 @@ module VCenterDriver
{ success: @info[:success], error: @info[:error] }
end
def import_bulk(range = nil)
raise "the list is empty" if list_empty?
list = @list.values[0]
indexes = list.keys.join(',')
process_import(indexes)
end
def process_import(indexes, opts = {}, &block)
raise "the list is empty" if list_empty?
indexes = indexes.gsub(/\s+/, "").split(",")
@info[:success] = []
@info[:error] = []
indexes.each do |index|
begin
@rollback = []
@info[index] = {}
selected = get_element(index)
@info[index][:opts] = block_given? ? block.call(selected) : opts[index]
if block_given?
@info[index][:opts] = block.call(selected)
elsif opts[index]
@info[index][:opts] = opts[index]
else
@info[index][:opts] = defaults
end
# import the object
@info[:success] << import(selected)
@@ -126,9 +143,11 @@ module VCenterDriver
end
def get_element(ref)
raise "the list is empty" if list_empty?
raise "the list is empty" if list_empty? || list.values[0].empty?
return @list[ref] if @list[ref]
list = @list.values[0]
return list[ref] if list[ref]
raise "#{ref} not found!"
end
@@ -149,6 +168,10 @@ module VCenterDriver
end
end
end
def defaults
{}
end
end
end
@@ -3339,6 +3339,7 @@ class VmImporter < VCenterDriver::VcImporter
@rollback.unshift(Raction.new(n, :delete))
end
raise error if !error.empty?
working_template[:one] << template_nics
working_template[:one] << rp_opts(opts[:type], opts[:resourcepool])
@@ -3348,5 +3349,16 @@ class VmImporter < VCenterDriver::VcImporter
return res
end
def defaults
opts = {
linked_clone: '0',
copy: '0',
name: '',
folder: '',
resourcepool: [],
type: ''
}
end
end
end # module VCenterDriver