Statistics
| Branch: | Tag: | Revision:

one / src / onedb / local / 4.90.0_to_5.3.80.rb @ 2911648e

History | View | Annotate | Download (10.9 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

    
18
require 'set'
19
require 'base64'
20
require 'zlib'
21
require 'pathname'
22
require 'yaml'
23
require 'opennebula'
24

    
25
$: << File.dirname(__FILE__)
26

    
27
include OpenNebula
28

    
29
module Migrator
30
    def db_version
31
        "5.3.80"
32
    end
33

    
34
    def one_version
35
        "OpenNebula 5.3.80"
36
    end
37

    
38
    def up
39
        init_log_time()
40

    
41
        feature_5136()
42

    
43
        feature_4901()
44

    
45
        feature_5005()
46

    
47
        feature_2347()
48

    
49
        bug_3705()
50

    
51
        feature_4809()
52

    
53
        log_time()
54

    
55
        return true
56
    end
57

    
58
    private
59

    
60
    def xpath(doc, sxpath)
61
        element = doc.root.at_xpath(sxpath)
62
        if !element.nil?
63
            element.text
64
        else
65
            ""
66
        end
67
    end
68

    
69
    def delete_element(doc, element)
70
        doc.search("//#{element}").each do |node|
71
            node.remove
72
        end
73
    end
74

    
75
    ############################################################################
76
    # Feature 5136. Improve ec2 keys_ids_security
77
    #
78
    ############################################################################
79
    def feature_5136
80
        ec2_driver_conf = "#{ETC_LOCATION}/ec2_driver.conf.old"
81
        token = File.read(VAR_LOCATION+'/.one/one_key')
82
        to_encrypt = {}
83

    
84
        if !File.exist?(ec2_driver_conf)
85
            STDERR.puts "  > Old EC2 file not found, skipping EC2 host migration"
86
            return
87
        end
88

    
89
        begin
90
            ec2_conf = YAML::load(File.read(ec2_driver_conf))
91
        rescue Exception => e
92
            str_error="ec2_driver.conf invalid syntax!"
93
            raise str_error
94
        end
95

    
96
        regions = ec2_conf["regions"]
97

    
98
        if !regions
99
            STDERR.puts "  > Regions not found in EC2 config file, skipping migration"
100
            return
101
        end
102

    
103
        @db.run "DROP TABLE IF EXISTS old_host_pool;"
104
        @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;"
105
        create_table(:host_pool)
106

    
107
        @db.transaction do
108
            @db.fetch("SELECT * FROM old_host_pool") do |row|
109
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
110
                    c.default_xml.noblanks
111
                }
112

    
113
                template = doc.root.at_xpath("TEMPLATE")
114

    
115
                if xpath(doc, "TEMPLATE/HYPERVISOR").to_s == "ec2"
116
                    host_name = xpath(doc, "NAME").to_s
117
                    host_info = ( regions[host_name].nil? ? regions["default"] : regions[host_name] )
118

    
119
                    to_encrypt["EC2_ACCESS"]=host_info["access_key_id"]
120
                    to_encrypt["EC2_SECRET"]=host_info["secret_access_key"]
121

    
122
                    OpenNebula.encrypt(to_encrypt, token).each { |k, v|
123
                        delete_element(template, k)
124
                        template.add_child(doc.create_element(k, v))
125
                    }
126

    
127
                    capacity = doc.create_element("CAPACITY")
128
                    host_info["capacity"].each { |k, v|
129
                        name = k.gsub(".", "_")
130
                        capacity.add_child(doc.create_element(name.upcase, v))
131
                    }
132

    
133
                    delete_element(template, "CAPACITY")
134
                    template.add_child(capacity)
135

    
136
                    delete_element(template, "REGION_NAME")
137
                    template.add_child(doc.create_element "REGION_NAME", host_info["region_name"])
138
                end
139

    
140
                row[:body] = doc.root.to_s
141
                @db[:host_pool].insert(row)
142
            end
143
        end
144

    
145
        @db.run "DROP TABLE old_host_pool;"
146

    
147
        STDERR.puts "  > You can now delete #{ec2_driver_conf} file"
148
    end
149

    
150
    ############################################################################
151
    # Feature 4921. Adds TOTAL_CPU and TOTAL_MEM to HOST/HOST_SHARE to compute
152
    # MAX_CPU and MAX_MEM when RESERVED_CPU/MEM is updated
153
    ############################################################################
154
    def feature_4901
155
        @db.run "DROP TABLE IF EXISTS old_host_pool;"
156
        @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;"
157
        create_table(:host_pool)
158

    
159
        @db.transaction do
160
            @db.fetch("SELECT * FROM old_host_pool") do |row|
161
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
162
                    c.default_xml.noblanks
163
                }
164

    
165
                rcpu = xpath(doc, "TEMPLATE/RESERVED_CPU").to_i
166
                rmem = xpath(doc, "TEMPLATE/RESERVED_MEM").to_i
167

    
168
                total_cpu = xpath(doc, "HOST_SHARE/MAX_CPU").to_i + rcpu
169
                total_mem = xpath(doc, "HOST_SHARE/MAX_MEM").to_i + rmem
170

    
171
                total_cpu_e = doc.create_element "TOTAL_CPU", total_cpu
172
                total_mem_e = doc.create_element "TOTAL_MEM", total_mem
173

    
174
                host_share = doc.root.at_xpath("HOST_SHARE")
175
                host_share.add_child(total_cpu_e)
176
                host_share.add_child(total_mem_e)
177

    
178
                row[:body] = doc.root.to_s
179

    
180
                @db[:host_pool].insert(row)
181
            end
182
        end
183

    
184
        @db.run "DROP TABLE old_host_pool;"
185
    end
186

    
187
    ############################################################################
188
    # Feature 5005.
189
    # Adds UID, GID and REQUEST_ID to history records
190
    # It also changes the old naming for mads from 4.x to 5.x
191
    ############################################################################
192
    def feature_5005
193
        @db.run "DROP TABLE IF EXISTS old_vm_pool;"
194
        @db.run "ALTER TABLE vm_pool RENAME TO old_vm_pool;"
195
        create_table(:vm_pool)
196

    
197
        @db.transaction do
198
            @db.fetch("SELECT * FROM old_vm_pool") do |row|
199

    
200
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
201
                  c.default_xml.noblanks
202
                }
203

    
204
                doc.root.xpath("HISTORY_RECORDS/HISTORY").each do |h|
205
                    reason = h.xpath("REASON")
206
                    reason.unlink if !reason.nil?
207

    
208
                    uid = doc.create_element "UID", -1
209
                    gid = doc.create_element "GID", -1
210
                    rid = doc.create_element "REQUEST_ID", -1
211

    
212
                    h.add_child(uid)
213
                    h.add_child(gid)
214
                    h.add_child(rid)
215
                end
216

    
217
                row[:body] = doc.root.to_s
218

    
219
                @db[:vm_pool].insert(row)
220
            end
221
        end
222

    
223
        @db.run "DROP TABLE old_vm_pool;"
224

    
225
        @db.run "DROP TABLE IF EXISTS old_history;"
226
        @db.run "ALTER TABLE history RENAME TO old_history;"
227
        create_table(:history)
228

    
229
        @db.transaction do
230
            @db.fetch("SELECT * FROM old_history") do |row|
231
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
232
                    c.default_xml.noblanks
233
                }
234

    
235
                h = doc.root
236

    
237
                reason = h.xpath("REASON")
238
                reason.unlink if !reason.nil?
239

    
240
                uid = doc.create_element "UID", -1
241
                gid = doc.create_element "GID", -1
242
                rid = doc.create_element "REQUEST_ID", -1
243

    
244
                h.add_child(uid)
245
                h.add_child(gid)
246
                h.add_child(rid)
247

    
248
                # This section is unrelated to Feature 5005. It renames
249
                # attributes in the history with the  nomenclature that was in
250
                # use before OpenNebula 5.0.
251
                vm_mad = h.at_xpath("VMMMAD")
252
                vm_mad.node_name = "VM_MAD" if vm_mad
253

    
254
                tm_mad = h.at_xpath("TMMAD")
255
                tm_mad.node_name = "TM_MAD" if tm_mad
256

    
257
                vn_mad = h.at_xpath("VNMMAD")
258
                vn_mad.remove if vn_mad
259

    
260
                row[:body] = doc.root.to_s
261

    
262
                @db[:history].insert(row)
263
            end
264
        end
265

    
266
        @db.run "DROP TABLE old_history;"
267
    end
268

    
269
    def feature_2347
270
        create_table(:vmgroup_pool)
271
    end
272

    
273
    ############################################################################
274
    # Bug 3705
275
    # Adds DRIVER to CEPH and LVM image datastores
276
    ############################################################################
277
    def bug_3705
278
        @db.run "DROP TABLE IF EXISTS old_datastore_pool;"
279
        @db.run "ALTER TABLE datastore_pool RENAME TO old_datastore_pool;"
280
        create_table(:datastore_pool)
281

    
282
        @db.transaction do
283
            @db.fetch("SELECT * FROM old_datastore_pool") do |row|
284
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
285
                    c.default_xml.noblanks
286
                }
287

    
288
                type = xpath(doc, 'TYPE').to_i
289
                tm_mad = xpath(doc, 'TM_MAD')
290

    
291
                if (type == 0) && (["ceph", "fs_lvm"].include?(tm_mad))
292
                    doc.root.xpath("TEMPLATE/DRIVER").each do |d|
293
                        d.remove
294
                    end
295

    
296
                    driver = doc.create_element "DRIVER", "raw"
297
                    doc.root.at_xpath("TEMPLATE").add_child(driver)
298

    
299
                    row[:body] = doc.root.to_s
300
                end
301

    
302
                @db[:datastore_pool].insert(row)
303
            end
304
        end
305

    
306
        @db.run "DROP TABLE old_datastore_pool;"
307
    end
308

    
309
    ############################################################################
310
    # Feature 4809
311
    # Simplify HA management in OpenNebula
312
    ############################################################################
313
    def feature_4809
314
        create_table(:logdb)
315

    
316
        @db.run "DROP TABLE IF EXISTS old_zone_pool;"
317
        @db.run "ALTER TABLE zone_pool RENAME TO old_zone_pool;"
318
        create_table(:zone_pool)
319

    
320
        @db.transaction do
321
            @db.fetch("SELECT * FROM old_zone_pool") do |row|
322
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
323
                    c.default_xml.noblanks
324
                }
325

    
326
                server_pool = doc.create_element "SERVER_POOL"
327
                doc.root.add_child(server_pool)
328

    
329
                row[:body] = doc.root.to_s
330

    
331
                @db[:zone_pool].insert(row)
332
            end
333
        end
334

    
335
        @db.run "DROP TABLE old_zone_pool;"
336
    end
337
end