Statistics
| Branch: | Tag: | Revision:

one / src / onedb / local / 4.90.0_to_5.3.80.rb @ 031da2d3

History | View | Annotate | Download (8.42 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
        log_time()
53

    
54
        return true
55
    end
56

    
57
    private
58

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

    
68
    ############################################################################
69
    # Feature 5136. Improve ec2 keys_ids_security
70
    #
71
    ############################################################################
72
    def feature_5136
73
        ec2_driver_conf = "#{ETC_LOCATION}/ec2_driver.conf"
74
        token = File.read(VAR_LOCATION+'/.one/one_key')
75
        opts = {}
76

    
77
        begin
78
            ec2_conf = YAML::load(File.read(ec2_driver_conf))
79
        rescue Exception => e
80
            str_error="ec2_driver.conf invalid syntax!"
81
            raise str_error
82
        end
83

    
84
        regions = ec2_conf["regions"]
85
        @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;"
86
        create_table(:host_pool)
87

    
88
        @db.transaction do
89
            @db.fetch("SELECT * FROM old_host_pool") do |row|
90
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
91
                    c.default_xml.noblanks
92
                }
93
                template = doc.root.at_xpath("TEMPLATE")
94

    
95
                if xpath(doc, "TEMPLATE/HYPERVISOR").to_s == "ec2"
96

    
97
                    host_name = xpath(doc, "NAME").to_s
98
                    host_info = ( regions[host_name].nil? ? regions["default"] : regions[host_name] )
99

    
100
                    opts["EC2_ACCESS"]=host_info["access_key_id"]
101
                    opts["EC2_SECRET"]=host_info["secret_access_key"]
102

    
103
                    OpenNebula.encrypt(opts, token).each { |k, v|
104
                        template.add_child(doc.create_element k, v)
105
                    }
106
                end
107

    
108
                row[:body] = doc.root.to_s
109
                @db[:host_pool].insert(row)
110
            end
111
        end
112

    
113
        @db.run "DROP TABLE old_host_pool;"
114
    end
115

    
116
    ############################################################################
117
    # Feature 4921. Adds TOTAL_CPU and TOTAL_MEM to HOST/HOST_SHARE to compute
118
    # MAX_CPU and MAX_MEM when RESERVED_CPU/MEM is updated
119
    ############################################################################
120
    def feature_4901
121
        @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;"
122
        create_table(:host_pool)
123

    
124
        @db.transaction do
125
            @db.fetch("SELECT * FROM old_host_pool") do |row|
126
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
127
                    c.default_xml.noblanks
128
                }
129

    
130
                rcpu = xpath(doc, "TEMPLATE/RESERVED_CPU").to_i
131
                rmem = xpath(doc, "TEMPLATE/RESERVED_MEM").to_i
132

    
133
                total_cpu = xpath(doc, "HOST_SHARE/MAX_CPU").to_i + rcpu
134
                total_mem = xpath(doc, "HOST_SHARE/MAX_MEM").to_i + rmem
135

    
136
                total_cpu_e = doc.create_element "TOTAL_CPU", total_cpu
137
                total_mem_e = doc.create_element "TOTAL_MEM", total_mem
138

    
139
                host_share = doc.root.at_xpath("HOST_SHARE")
140
                host_share.add_child(total_cpu_e)
141
                host_share.add_child(total_mem_e)
142

    
143
                row[:body] = doc.root.to_s
144

    
145
                @db[:host_pool].insert(row)
146
            end
147
        end
148

    
149
        @db.run "DROP TABLE old_host_pool;"
150
    end
151

    
152
    ############################################################################
153
    # Feature 5005.
154
    # Adds UID, GID and REQUEST_ID to history records
155
    ############################################################################
156
    def feature_5005
157
        @db.run "ALTER TABLE vm_pool RENAME TO old_vm_pool;"
158
        create_table(:vm_pool)
159

    
160
        @db.transaction do
161
            @db.fetch("SELECT * FROM old_vm_pool") do |row|
162

    
163
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
164
                  c.default_xml.noblanks
165
                }
166

    
167
                doc.root.xpath("HISTORY_RECORDS/HISTORY").each do |h|
168
                    reason = h.xpath("REASON")
169
                    reason.unlink if !reason.nil?
170

    
171
                    uid = doc.create_element "UID", -1
172
                    gid = doc.create_element "GID", -1
173
                    rid = doc.create_element "REQUEST_ID", -1
174

    
175
                    h.add_child(uid)
176
                    h.add_child(gid)
177
                    h.add_child(rid)
178
                end
179

    
180
                row[:body] = doc.root.to_s
181

    
182
                @db[:vm_pool].insert(row)
183
            end
184
        end
185

    
186
        @db.run "DROP TABLE old_vm_pool;"
187

    
188
        @db.run "ALTER TABLE history RENAME TO old_history;"
189
        create_table(:history)
190

    
191
        @db.transaction do
192
            @db.fetch("SELECT * FROM old_history") do |row|
193
                doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c|
194
                    c.default_xml.noblanks
195
                }
196

    
197
                h = doc.root
198

    
199
                reason = h.xpath("REASON")
200
                reason.unlink if !reason.nil?
201

    
202
                uid = doc.create_element "UID", -1
203
                gid = doc.create_element "GID", -1
204
                rid = doc.create_element "REQUEST_ID", -1
205

    
206
                h.add_child(uid)
207
                h.add_child(gid)
208
                h.add_child(rid)
209

    
210
                row[:body] = doc.root.to_s
211

    
212
                @db[:history].insert(row)
213
            end
214
        end
215

    
216
        @db.run "DROP TABLE old_history;"
217
    end
218

    
219
    def feature_2347
220
        create_table(:vmgroup_pool)
221
    end
222

    
223
    ############################################################################
224
    # Bug 3705
225
    # Adds DRIVER to CEPH and LVM image datastores
226
    ############################################################################
227
    def bug_3705
228
        @db.run "ALTER TABLE datastore_pool RENAME TO old_datastore_pool;"
229
        create_table(:datastore_pool)
230

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

    
237
                type = xpath(doc, 'TYPE').to_i
238
                tm_mad = xpath(doc, 'TM_MAD')
239

    
240
                if (type == 0) && (["ceph", "fs_lvm"].include?(tm_mad))
241
                    doc.root.xpath("TEMPLATE/DRIVER").each do |d|
242
                        d.remove
243
                    end
244

    
245
                    driver = doc.create_element "DRIVER", "raw"
246
                    doc.root.at_xpath("TEMPLATE").add_child(driver)
247

    
248
                    row[:body] = doc.root.to_s
249
                end
250

    
251
                @db[:datastore_pool].insert(row)
252
            end
253
        end
254

    
255
        @db.run "DROP TABLE old_datastore_pool;"
256
    end
257

    
258
    ############################################################################
259
    # Feature 4809
260
    # Simplify HA management in OpenNebula
261
    ############################################################################
262
    def feature_4809
263
        create_table(:logdb)
264
    end
265
end