Statistics
| Branch: | Tag: | Revision:

one / src / onedb / 1.rb @ 6329d8b3

History | View | Annotate | Download (15.2 KB)

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

    
16
class Migrator < MigratorBase
17

    
18
    def initialize(db, verbose)
19
        super(db, verbose)
20
        @db_version  = 1
21
        @one_version = "OpenNebula 2.3.0"
22
    end
23

    
24
    def up
25

    
26
        ########################################################################
27
        # Users
28
        ########################################################################
29

    
30
        # 2.2 Schema
31
        # CREATE TABLE user_pool (oid INTEGER PRIMARY KEY, user_name VARCHAR(256), password TEXT,enabled INTEGER, UNIQUE(user_name));
32

    
33
        # Move table user_pool
34
        @db.run "ALTER TABLE user_pool RENAME TO old_user_pool;"
35

    
36
        # Create new user_pool
37
        @db.run "CREATE TABLE user_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name));"
38

    
39
        # Read each entry in the old user_pool, and insert into new user_pool
40
        @db.fetch("SELECT * FROM old_user_pool") do |row|
41
            oid  = row[:oid]
42
            gid  = (oid == 0) ? 0 : 1
43
            name = row[:user_name]
44

    
45
            body = "<USER><ID>#{oid}</ID><NAME>#{name}</NAME><GID>#{gid}</GID><PASSWORD>#{row[:password]}</PASSWORD><ENABLED>#{row[:enabled]}</ENABLED></USER>"
46

    
47
            @db.run "INSERT INTO user_pool VALUES(#{oid},'#{name}','#{body}');"
48
        end
49

    
50
        # Delete old user_pool
51
        @db.run "DROP TABLE old_user_pool"
52

    
53
        ########################################################################
54
        # Clusters
55
        ########################################################################
56

    
57
        # 2.2 Schema
58
        # CREATE TABLE cluster_pool (oid INTEGER PRIMARY KEY, cluster_name VARCHAR(128), UNIQUE(cluster_name) );
59

    
60
        # Move table
61
        @db.run "ALTER TABLE cluster_pool RENAME TO old_cluster_pool;"
62

    
63
        # Create new table
64
        @db.run "CREATE TABLE cluster_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name));"
65

    
66
        # Read each entry in the old table, and insert into new table
67
        @db.fetch("SELECT * FROM old_cluster_pool") do |row|
68
            oid  = row[:oid]
69
            name = row[:cluster_name]
70

    
71
            body = "<CLUSTER><ID>#{oid}</ID><NAME>#{name}</NAME></CLUSTER>"
72

    
73
            @db.run "INSERT INTO cluster_pool VALUES(#{oid},'#{name}','#{body}');"
74
        end
75

    
76
        # Delete old table
77
        @db.run "DROP TABLE old_cluster_pool"
78

    
79
        ########################################################################
80
        # Hosts
81
        ########################################################################
82

    
83
        # 2.2 Schema
84
        # CREATE TABLE host_pool (oid INTEGER PRIMARY KEY,host_name VARCHAR(256), state INTEGER,im_mad VARCHAR(128),vm_mad VARCHAR(128),tm_mad VARCHAR(128),last_mon_time INTEGER, cluster VARCHAR(128), template TEXT, UNIQUE(host_name));
85
        # CREATE TABLE host_shares(hid INTEGER PRIMARY KEY,disk_usage INTEGER, mem_usage INTEGER, cpu_usage INTEGER,max_disk  INTEGER,  max_mem   INTEGER, max_cpu   INTEGER,free_disk INTEGER,  free_mem  INTEGER, free_cpu  INTEGER,used_disk INTEGER,  used_mem  INTEGER, used_cpu  INTEGER,running_vms INTEGER);
86

    
87
        # Move table
88
        @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;"
89

    
90
        # Create new table
91
        @db.run "CREATE TABLE host_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, last_mon_time INTEGER, cid INTEGER, UNIQUE(name));"
92

    
93
        # Read each entry in the old table, and insert into new table
94
        @db.fetch("SELECT * FROM old_host_pool") do |row|
95
            oid             = row[:oid]
96
            name            = row[:host_name]
97
            state           = row[:state]
98
            last_mon_time   = row[:last_mon_time]
99
            cluster         = row[:cluster]
100

    
101
            # OpenNebula 2.X stored the cluster name, we need the cluster ID
102
            cluster_id = 0
103
            @db.fetch("SELECT oid FROM cluster_pool WHERE name='#{cluster}'") do |cluster_row|
104
                cluster_id = cluster_row[:oid]
105
            end
106

    
107
            # There is one host share for each host
108
            host_share = ""
109
            @db.fetch("SELECT * FROM host_shares WHERE hid=#{oid}") do |share|
110
                host_share = "<HOST_SHARE><DISK_USAGE>#{share[:disk_usage]}</DISK_USAGE><MEM_USAGE>#{share[:mem_usage]}</MEM_USAGE><CPU_USAGE>#{share[:cpu_usage]}</CPU_USAGE><MAX_DISK>#{share[:max_disk]}</MAX_DISK><MAX_MEM>#{share[:max_mem]}</MAX_MEM><MAX_CPU>#{share[:max_cpu]}</MAX_CPU><FREE_DISK>#{share[:free_disk]}</FREE_DISK><FREE_MEM>#{share[:free_mem]}</FREE_MEM><FREE_CPU>#{share[:free_cpu]}</FREE_CPU><USED_DISK>#{share[:used_disk]}</USED_DISK><USED_MEM>#{share[:used_mem]}</USED_MEM><USED_CPU>#{share[:used_cpu]}</USED_CPU><RUNNING_VMS>#{share[:running_vms]}</RUNNING_VMS></HOST_SHARE>"
111
            end
112

    
113
            body = "<HOST><ID>#{oid}</ID><NAME>#{name}</NAME><STATE>#{state}</STATE><IM_MAD>#{row[:im_mad]}</IM_MAD><VM_MAD>#{row[:vm_mad]}</VM_MAD><TM_MAD>#{row[:tm_mad]}</TM_MAD><LAST_MON_TIME>#{last_mon_time}</LAST_MON_TIME><CID>#{cluster_id}</CID>#{host_share}#{row[:template]}</HOST>"
114

    
115
            @db.run "INSERT INTO host_pool VALUES(#{oid},'#{name}','#{body}', #{state}, #{last_mon_time}, '#{cluster}');"
116
        end
117

    
118
        # Delete old table
119
        @db.run "DROP TABLE old_host_pool"
120
        @db.run "DROP TABLE host_shares"
121

    
122
        ########################################################################
123
        # Images
124
        ########################################################################
125

    
126
        # 2.2 Schema
127
        # CREATE TABLE image_pool (oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(128), type INTEGER, public INTEGER, persistent INTEGER, regtime INTEGER, source TEXT, state INTEGER, running_vms INTEGER, template TEXT, UNIQUE(name) );
128

    
129
        # Move table
130
        @db.run "ALTER TABLE image_pool RENAME TO old_image_pool;"
131

    
132
        # Create new table
133
        @db.run "CREATE TABLE image_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, gid INTEGER, public INTEGER, UNIQUE(name,uid) );"
134

    
135
        # Read each entry in the old table, and insert into new table
136
        @db.fetch("SELECT * FROM old_image_pool") do |row|
137
            oid    = row[:oid]
138
            name   = row[:name]
139
            uid    = row[:uid]
140
            gid    = (uid == 0) ? 0 : 1
141
            public = row[:public]
142

    
143
            # In OpenNebula 2.0 Image States go from 0 to 3, in 3.0 go
144
            # from 0 to 5, but the meaning is the same for states 0 to 3
145
            body = "<IMAGE><ID>#{oid}</ID><UID>#{row[:uid]}</UID><GID>#{gid}</GID><NAME>#{name}</NAME><TYPE>#{row[:type]}</TYPE><PUBLIC>#{public}</PUBLIC><PERSISTENT>#{row[:persistent]}</PERSISTENT><REGTIME>#{row[:regtime]}</REGTIME><SOURCE>#{row[:source]}</SOURCE><STATE>#{row[:state]}</STATE><RUNNING_VMS>#{row[:running_vms]}</RUNNING_VMS>#{row[:template]}</IMAGE>"
146

    
147
            @db.run "INSERT INTO image_pool VALUES(#{oid},'#{name}','#{body}', #{uid}, #{gid}, #{public});"
148
        end
149

    
150
        # Delete old table
151
        @db.run "DROP TABLE old_image_pool"
152

    
153
        ########################################################################
154
        # VMs
155
        ########################################################################
156

    
157
        # 2.2 Schema
158
        # CREATE TABLE vm_pool (oid INTEGER PRIMARY KEY,uid INTEGER,name TEXT,last_poll INTEGER, state INTEGER,lcm_state INTEGER,stime INTEGER,etime INTEGER,deploy_id TEXT,memory INTEGER,cpu INTEGER,net_tx INTEGER,net_rx INTEGER, last_seq INTEGER, template TEXT);
159
        # CREATE TABLE history (vid INTEGER,seq INTEGER,host_name TEXT,vm_dir TEXT,hid INTEGER,vm_mad TEXT,tm_mad TEXT,stime INTEGER,etime INTEGER,pstime INTEGER,petime INTEGER,rstime INTEGER,retime INTEGER,estime INTEGER,eetime INTEGER,reason INTEGER,PRIMARY KEY(vid,seq));
160

    
161
        # Move tables
162
        @db.run "ALTER TABLE vm_pool RENAME TO old_vm_pool;"
163
        @db.run "ALTER TABLE history RENAME TO old_history;"
164

    
165
        # Create new tables
166
        @db.run "CREATE TABLE vm_pool (oid INTEGER PRIMARY KEY, name TEXT, body TEXT, uid INTEGER, gid INTEGER, last_poll INTEGER, state INTEGER, lcm_state INTEGER);"
167
        @db.run "CREATE TABLE history (vid INTEGER, seq INTEGER, body TEXT, PRIMARY KEY(vid,seq));"
168

    
169

    
170
        # Read each entry in the old history table, and insert into new table
171
        @db.fetch("SELECT * FROM old_history") do |row|
172
            vid = row[:vid]
173
            seq = row[:seq]
174

    
175
            body = "<HISTORY><SEQ>#{seq}</SEQ><HOSTNAME>#{row[:host_name]}</HOSTNAME><VM_DIR>#{row[:vm_dir]}</VM_DIR><HID>#{row[:hid]}</HID><STIME>#{row[:stime]}</STIME><ETIME>#{row[:etime]}</ETIME><VMMMAD>#{row[:vm_mad]}</VMMMAD><TMMAD>#{row[:tm_mad]}</TMMAD><PSTIME>#{row[:pstime]}</PSTIME><PETIME>#{row[:petime]}</PETIME><RSTIME>#{row[:rstime]}</RSTIME><RETIME>#{row[:retime]}</RETIME><ESTIME>#{row[:estime]}</ESTIME><EETIME>#{row[:eetime]}</EETIME><REASON>#{row[:reason]}</REASON></HISTORY>"
176

    
177
            @db.run "INSERT INTO history VALUES(#{vid},'#{seq}','#{body}');"
178
        end
179

    
180

    
181
        # Read each entry in the old vm table, and insert into new table
182
        @db.fetch("SELECT * FROM old_vm_pool") do |row|
183
            oid       = row[:oid]
184
            name      = row[:name]
185
            uid       = row[:uid]
186
            gid       = (uid == 0) ? 0 : 1
187
            last_poll = row[:last_poll]
188
            state     = row[:state]
189
            lcm_state = row[:lcm_state]
190

    
191
            # If the VM has History items, the last one is included in the XML
192
            history = ""
193
            @db.fetch("SELECT body FROM history WHERE vid=#{oid} AND seq=(SELECT MAX(seq) FROM history WHERE vid=#{oid})") do |history_row|
194
                history = history_row[:body]
195
            end
196

    
197
            body = "<VM><ID>#{oid}</ID><UID>#{uid}</UID><GID>#{gid}</GID><NAME>#{name}</NAME><LAST_POLL>#{last_poll}</LAST_POLL><STATE>#{state}</STATE><LCM_STATE>#{lcm_state}</LCM_STATE><STIME>#{row[:stime]}</STIME><ETIME>#{row[:etime]}</ETIME><DEPLOY_ID>#{row[:deploy_id]}</DEPLOY_ID><MEMORY>#{row[:memory]}</MEMORY><CPU>#{row[:cpu]}</CPU><NET_TX>#{row[:net_tx]}</NET_TX><NET_RX>#{row[:net_rx]}</NET_RX>#{row[:template]}#{history}</VM>"
198

    
199
            @db.run "INSERT INTO vm_pool VALUES(#{oid},'#{name}','#{body}', #{uid}, #{gid}, #{last_poll}, #{state}, #{lcm_state});"
200
        end
201

    
202

    
203
        # Delete old tables
204
        @db.run "DROP TABLE old_vm_pool"
205
        @db.run "DROP TABLE old_history"
206

    
207

    
208
        ########################################################################
209
        # Virtual Networks
210
        ########################################################################
211

    
212
        # 2.2 Schema
213
        # CREATE TABLE network_pool (oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(256), type INTEGER, bridge TEXT, public INTEGER, template TEXT, UNIQUE(name));
214
        # CREATE TABLE leases (oid INTEGER, ip BIGINT, mac_prefix BIGINT, mac_suffix BIGINT,vid INTEGER, used INTEGER, PRIMARY KEY(oid,ip));
215

    
216
        # Move tables
217
        @db.run "ALTER TABLE network_pool RENAME TO old_network_pool;"
218
        @db.run "ALTER TABLE leases RENAME TO old_leases;"
219

    
220
        # Create new tables
221
        @db.run "CREATE TABLE network_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, uid INTEGER, gid INTEGER, public INTEGER, UNIQUE(name,uid));"
222
        @db.run "CREATE TABLE leases (oid INTEGER, ip BIGINT, body TEXT, PRIMARY KEY(oid,ip));"
223

    
224
        # Read each entry in the old table, and insert into new table
225
        @db.fetch("SELECT * FROM old_network_pool") do |row|
226
            oid    = row[:oid]
227
            name   = row[:name]
228
            uid    = row[:uid]
229
            gid    = (uid == 0) ? 0 : 1
230
            public = row[:public]
231

    
232
            # <TOTAL_LEASES> is stored in the DB, but it is not used to rebuild
233
            # the VirtualNetwork object, and it is generated each time the
234
            # network is listed. So setting it to 0 is safe
235
            body = "<VNET><ID>#{oid}</ID><UID>#{uid}</UID><GID>#{gid}</GID><NAME>#{name}</NAME><TYPE>#{row[:type]}</TYPE><BRIDGE>#{row[:bridge]}</BRIDGE><PUBLIC>#{public}</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES>#{row[:template]}</VNET>"
236

    
237
            @db.run "INSERT INTO network_pool VALUES(#{oid},'#{name}','#{body}', #{uid}, #{gid}, #{public});"
238
        end
239

    
240
        # Read each entry in the old table, and insert into new table
241
        @db.fetch("SELECT * FROM old_leases") do |row|
242
            oid = row[:oid]
243
            ip  = row[:ip]
244

    
245
            body = "<LEASE><IP>#{ip}</IP><MAC_PREFIX>#{row[:mac_prefix]}</MAC_PREFIX><MAC_SUFFIX>#{row[:mac_suffix]}</MAC_SUFFIX><USED>#{row[:used]}</USED><VID>#{row[:vid]}</VID></LEASE>"
246

    
247
            @db.run "INSERT INTO leases VALUES(#{oid}, #{ip}, '#{body}');"
248
        end
249

    
250
        # Delete old tables
251
        @db.run "DROP TABLE old_network_pool"
252
        @db.run "DROP TABLE old_leases"
253

    
254

    
255
        ########################################################################
256
        # New tables in DB version 1
257
        ########################################################################
258

    
259
        @db.run "CREATE TABLE db_versioning (oid INTEGER PRIMARY KEY, version INTEGER, timestamp INTEGER, comment VARCHAR(256));"
260
        @db.run "CREATE TABLE template_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, public INTEGER);"
261

    
262
        # The group pool has two default ones
263
        @db.run "CREATE TABLE group_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, UNIQUE(name));"
264
        @db.run "INSERT INTO group_pool VALUES(0,'oneadmin','<GROUP><ID>0</ID><UID>0</UID><NAME>oneadmin</NAME></GROUP>',0);"
265
        @db.run "INSERT INTO group_pool VALUES(1,'users','<GROUP><ID>1</ID><UID>0</UID><NAME>users</NAME></GROUP>',0);"
266

    
267
        # New pool_control table contains the last_oid used, must be rebuilt
268
        @db.run "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, last_oid BIGINT UNSIGNED)"
269

    
270
        for table in ["user_pool", "cluster_pool", "host_pool", "image_pool", "vm_pool", "network_pool"] do
271
            @db.fetch("SELECT MAX(oid) FROM #{table}") do |row|
272
                if( row[:"MAX(oid)"] != nil )
273
                    @db.run "INSERT INTO pool_control (tablename, last_oid) VALUES ('#{table}', #{row[:"MAX(oid)"]});"
274
                end
275
            end
276
        end
277

    
278
        # First 100 group Ids are reserved for system groups.
279
        # Regular ones start from ID 100
280
        @db.run "INSERT INTO pool_control (tablename, last_oid) VALUES ('group_pool', 99);"
281

    
282
        return true
283
    end
284
end