Created
November 3, 2017 22:47
-
-
Save atodorov-storpool/298fcf1b684ce34adb450ca76b38eab1 to your computer and use it in GitHub Desktop.
patch tool to convert VM disk snapshots from tree to flat ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -------------------------------------------------------------------------- # | |
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems # | |
# # | |
# 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. # | |
#--------------------------------------------------------------------------- # | |
if !ONE_LOCATION | |
LOG_LOCATION = "/var/log/one" | |
else | |
LOG_LOCATION = ONE_LOCATION + "/var" | |
end | |
LOG = LOG_LOCATION + "/onedb-allow_orphans.log" | |
require 'nokogiri' | |
module OneDBPatch | |
VERSION_MIN = "5.0.0" | |
VERSION_MAX = "5.5.0" | |
def is_hot_patch(ops) | |
return false | |
end | |
def check_db_version(ops) | |
db_version = read_db_version() | |
version = Gem::Version.new(db_version[:version]) | |
if (version < Gem::Version.new(VERSION_MIN)) || | |
(version >= Gem::Version.new(VERSION_MAX)) | |
then | |
raise <<-EOT | |
Version mismatch: patch file is for version | |
Shared: #{VERSION_MIN} to #{VERSION_MAX} (excluding) | |
Current database is version | |
Shared: #{db_version[:version]} | |
EOT | |
end | |
end | |
def add_element(elem, name) | |
return elem.add_child(elem.document.create_element(name)) | |
end | |
def add_cdata(elem, name, text) | |
# The cleaner doc.create_cdata(txt) is not supported in | |
# old versions of nokogiri | |
return add_element(elem, name).add_child( | |
Nokogiri::XML::CDATA.new(elem.document(), text)) | |
end | |
def add_allow_orphans(ele, val, type) | |
did = ele.xpath("DISK_ID").text | |
if (did) then | |
ele.search("ALLOW_ORPHANS").remove | |
add_cdata(ele, "ALLOW_ORPHANS", val) | |
elem = ele.search("ALLOW_ORPHANS") | |
puts " #{type} DISK_ID:#{did} :: #{elem}" | |
end | |
end | |
def patch(ops) | |
init_log_time() | |
puts "This tool will convert the tree of disk snapshots to orphans." | |
@db.transaction do | |
@db.fetch("SELECT * FROM vm_pool") do |row| | |
doc = Nokogiri::XML(row[:body],nil,NOKOGIRI_ENCODING){|c| c.default_xml.noblanks} | |
next if row[:state] == 6 # skip VMs in state DONE | |
puts " VM_ID:#{row[:oid]} state:#{row[:state]} lcm_state:#{row[:lcm_state]}" | |
doc.xpath("/VM/TEMPLATE/DISK").each do |disk| | |
add_allow_orphans(disk, "YES", "DISKS") | |
end | |
doc.xpath("/VM/SNAPSHOTS").each do |snaps| | |
add_allow_orphans(snaps, "YES", "SNAPS") | |
snaps.xpath("SNAPSHOT").each do |snap| | |
snap.search("CHILDREN").remove | |
snap.search("PARENT").remove | |
add_cdata(snap, "PARENT", "-1") | |
parent_ele = snap.search("PARENT") | |
sid = snap.xpath("ID").text | |
puts " SNAPSHOT_ID:#{sid} :: #{parent_ele}" | |
end | |
end | |
body = doc.root.to_s | |
@db[:vm_pool].where(oid: row[:oid]).update(body: body) | |
end | |
log_time() | |
end | |
return true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment