Statistics
| Branch: | Tag: | Revision:

one / src / onedb / vcenter_one54.rb @ 0c715f3a

History | View | Annotate | Download (5.64 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
if !ONE_LOCATION
18
    LOG_LOCATION = "/var/log/one"
19
else
20
    LOG_LOCATION = ONE_LOCATION + "/var"
21
end
22

    
23
LOG     = LOG_LOCATION + "/onedb-vcenter-one54.log"
24
TMP_DIR = "/var/tmp/vcenter_one54"
25

    
26
module One54Vcenter
27
    VERSION = "5.4.0"
28

    
29
    def check_db_version(ops)
30
        db_version = read_db_version()
31

    
32
        if ( db_version[:version] != VERSION )
33

    
34
            raise <<-EOT
35
Version mismatch: vcenter migration is for version #{VERSION}
36

37
Current database is version #{db_version[:version]}
38
EOT
39
        end
40
    end
41

    
42
    def migrate_templates(verbose)
43
        Dir["#{TMP_DIR}/one_migrate_template_*"].each do |template_filename|
44
            template_id = template_filename.split("_")[-1]
45
            template_xml = File.read(template_filename)
46
            @db.run("UPDATE template_pool SET body='#{template_xml}' WHERE oid='#{template_id}'")
47
            puts "    Template #{template_id} migrated!" if verbose
48
        end
49

    
50
        return true
51
    end
52

    
53
    def migrate_vms(verbose)
54
        Dir["#{TMP_DIR}/one_migrate_vm_*"].each do |vm_filename|
55
            vm_id = vm_filename.split("_")[-1]
56
            vm_xml = File.read(vm_filename)
57

    
58
            vm_xml_doc = REXML::Document.new(vm_xml).root
59

    
60
            seq_node  = vm_xml_doc.elements["HISTORY_RECORDS/HISTORY/SEQ"]
61
            dsid_node = vm_xml_doc.elements["HISTORY_RECORDS/HISTORY/DS_ID"]
62

    
63
            if seq_node && seq_node.has_text? && dsid_node && dsid_node.has_text?
64
                seq = seq_node.text
65
                dsid= dsid_node.text
66
                begin
67

    
68
                h_dataset = @db["SELECT body from history where vid=#{vm_id} and seq=#{seq}"]
69
                h_body    = h_dataset.map(:body)
70

    
71
                h_xml = REXML::Document.new(h_body[0]).root
72

    
73
                h_ds_node = h_xml.elements["DS_ID"]
74

    
75
                if h_ds_node && h_ds_node.has_text?
76
                    h_ds_node.text = dsid
77

    
78
                    h_xml.delete_element "TM_MAD"
79

    
80
                    tmmad_elem = REXML::Element.new("TM_MAD")
81
                    tmmad_elem.text = "vcenter"
82
                    h_xml.add_element(tmmad_elem)
83

    
84
                    @db.run("UPDATE history SET body='#{h_xml.to_s}' WHERE vid=#{vm_id} and seq=#{seq}")
85
                end
86

    
87
                rescue
88
                    puts "VM #{vm_id} cannot set datastore. Manual update needed"
89
                end
90
            end
91

    
92
            @db.run("UPDATE vm_pool SET body='#{vm_xml}' WHERE oid='#{vm_id}'")
93
            puts "    VM #{vm_id} migrated!" if verbose
94
        end
95

    
96
        return true
97
    end
98

    
99
    def migrate_hosts(verbose)
100
        Dir["#{TMP_DIR}/one_migrate_host_*"].each do |host_filename|
101
            host_id  = host_filename.split("_")[-1]
102
            host_xml = File.read(host_filename)
103
            @db.run("UPDATE host_pool SET body='#{host_xml}' WHERE oid='#{host_id}'")
104
            puts "    Host #{host_id} migrated!" if verbose
105
        end
106

    
107
        return true
108
    end
109

    
110
    def migrate_datastores(verbose)
111
        Dir["#{TMP_DIR}/one_migrate_ds_*"].each do |ds_filename|
112
            ds_id  = ds_filename.split("_")[-1]
113
            ds_xml =  File.read(ds_filename)
114
            @db.run("UPDATE datastore_pool SET body='#{ds_xml}' WHERE oid='#{ds_id}'")
115
            puts "    Datastore #{ds_id} migrated!" if verbose
116
        end
117

    
118
        return true
119
    end
120

    
121
    def migrate_vnets(verbose)
122
        Dir["#{TMP_DIR}/one_migrate_vnet_*"].each do |vnet_filename|
123
            vnet_id  = vnet_filename.split("_")[-1]
124
            vnet_xml = File.read(vnet_filename)
125
            @db.run("UPDATE network_pool SET body='#{vnet_xml}' WHERE oid='#{vnet_id}'")
126
            puts "    Network #{vnet_id} migrated!" if verbose
127
        end
128

    
129
        return true
130
    end
131

    
132
    def migrate_images(verbose)
133
        Dir["#{TMP_DIR}/one_migrate_image_*"].each do |image_filename|
134
            image_id  = image_filename.split("_")[-1]
135
            image_xml = File.read(image_filename)
136
            @db.run("UPDATE image_pool SET body='#{image_xml}' WHERE oid='#{image_id}'")
137
            puts "    Image #{image_id} migrated!" if verbose
138
        end
139

    
140
        return true
141
    end
142

    
143
    def migrate_history(verbose)
144
        Dir["#{TMP_DIR}/one_migrate_history_*"].each do |history_filename|
145
            vm_id  = history_filename.split("_")[-2]
146
            seq_id = history_filename.split("_")[-1]
147
            history_xml = File.read(history_filename)
148
            @db.run("UPDATE history SET body='#{history_xml}' WHERE vid='#{vm_id}' and seq='#{seq_id}'")
149
            puts "    History for VM #{vm_id} migrated!" if verbose
150
        end
151

    
152
        return true
153
    end
154
end