diff --git a/src/onedb/1.rb b/src/onedb/1.rb index b2722aebea..29239ee339 100644 --- a/src/onedb/1.rb +++ b/src/onedb/1.rb @@ -36,13 +36,22 @@ class Migrator < MigratorBase # Create new user_pool @db.run "CREATE TABLE user_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name));" + user_group_ids = "" + # Read each entry in the old user_pool, and insert into new user_pool @db.fetch("SELECT * FROM old_user_pool") do |row| oid = row[:oid] - gid = (oid == 0) ? 0 : 1 + + if( oid == 0 ) + gid = 0 + else + gid = 1 + user_group_ids += "#{oid}" + end + name = row[:user_name] - body = "#{oid}#{name}#{gid}#{row[:password]}#{row[:enabled]}" + body = "#{oid}#{gid}#{name}#{row[:password]}#{row[:enabled]}#{gid}" @db.run "INSERT INTO user_pool VALUES(#{oid},'#{name}','#{body}');" end @@ -50,6 +59,54 @@ class Migrator < MigratorBase # Delete old user_pool @db.run "DROP TABLE old_user_pool" + ######################################################################## + # Hosts + ######################################################################## + + # 2.2 Schema + # 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)); + # 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); + + # Move table + @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;" + + # Create new table + @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));" + + cluster_host_ids = Hash.new + + # Read each entry in the old table, and insert into new table + @db.fetch("SELECT * FROM old_host_pool") do |row| + oid = row[:oid] + name = row[:host_name] + state = row[:state] + last_mon_time = row[:last_mon_time] + cluster = row[:cluster] + + # OpenNebula 2.X stored the cluster name, we need the cluster ID + cluster_id = 0 + @db.fetch("SELECT oid FROM cluster_pool WHERE cluster_name='#{cluster}'") do |cluster_row| + cluster_id = cluster_row[:oid] + + cluster_host_ids[cluster_id] = "" if cluster_host_ids[cluster_id] == nil + cluster_host_ids[cluster_id] += "#{oid}" + end + + # There is one host share for each host + host_share = "" + @db.fetch("SELECT * FROM host_shares WHERE hid=#{oid}") do |share| + host_share = "#{share[:disk_usage]}#{share[:mem_usage]}#{share[:cpu_usage]}#{share[:max_disk]}#{share[:max_mem]}#{share[:max_cpu]}#{share[:free_disk]}#{share[:free_mem]}#{share[:free_cpu]}#{share[:used_disk]}#{share[:used_mem]}#{share[:used_cpu]}#{share[:running_vms]}" + end + + body = "#{oid}#{name}#{state}#{row[:im_mad]}#{row[:vm_mad]}#{row[:tm_mad]}#{last_mon_time}#{cluster_id}#{host_share}#{row[:template]}" + + @db.run "INSERT INTO host_pool VALUES(#{oid},'#{name}','#{body}', #{state}, #{last_mon_time}, #{cluster_id});" + end + + # Delete old table + @db.run "DROP TABLE old_host_pool" + @db.run "DROP TABLE host_shares" + ######################################################################## # Clusters ######################################################################## @@ -68,7 +125,12 @@ class Migrator < MigratorBase oid = row[:oid] name = row[:cluster_name] - body = "#{oid}#{name}" + hids = "" + if cluster_host_ids[oid] != nil + hids = cluster_host_ids[oid] + end + + body = "#{oid}#{name}#{hids}" @db.run "INSERT INTO cluster_pool VALUES(#{oid},'#{name}','#{body}');" end @@ -76,49 +138,6 @@ class Migrator < MigratorBase # Delete old table @db.run "DROP TABLE old_cluster_pool" - ######################################################################## - # Hosts - ######################################################################## - - # 2.2 Schema - # 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)); - # 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); - - # Move table - @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;" - - # Create new table - @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));" - - # Read each entry in the old table, and insert into new table - @db.fetch("SELECT * FROM old_host_pool") do |row| - oid = row[:oid] - name = row[:host_name] - state = row[:state] - last_mon_time = row[:last_mon_time] - cluster = row[:cluster] - - # OpenNebula 2.X stored the cluster name, we need the cluster ID - cluster_id = 0 - @db.fetch("SELECT oid FROM cluster_pool WHERE name='#{cluster}'") do |cluster_row| - cluster_id = cluster_row[:oid] - end - - # There is one host share for each host - host_share = "" - @db.fetch("SELECT * FROM host_shares WHERE hid=#{oid}") do |share| - host_share = "#{share[:disk_usage]}#{share[:mem_usage]}#{share[:cpu_usage]}#{share[:max_disk]}#{share[:max_mem]}#{share[:max_cpu]}#{share[:free_disk]}#{share[:free_mem]}#{share[:free_cpu]}#{share[:used_disk]}#{share[:used_mem]}#{share[:used_cpu]}#{share[:running_vms]}" - end - - body = "#{oid}#{name}#{state}#{row[:im_mad]}#{row[:vm_mad]}#{row[:tm_mad]}#{last_mon_time}#{cluster_id}#{host_share}#{row[:template]}" - - @db.run "INSERT INTO host_pool VALUES(#{oid},'#{name}','#{body}', #{state}, #{last_mon_time}, '#{cluster}');" - end - - # Delete old table - @db.run "DROP TABLE old_host_pool" - @db.run "DROP TABLE host_shares" - ######################################################################## # Images ######################################################################## @@ -261,8 +280,8 @@ class Migrator < MigratorBase # The group pool has two default ones @db.run "CREATE TABLE group_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, UNIQUE(name));" - @db.run "INSERT INTO group_pool VALUES(0,'oneadmin','00oneadmin',0);" - @db.run "INSERT INTO group_pool VALUES(1,'users','10users',0);" + @db.run "INSERT INTO group_pool VALUES(0,'oneadmin','00oneadmin0',0);" + @db.run "INSERT INTO group_pool VALUES(1,'users','10users#{user_group_ids}',0);" # New pool_control table contains the last_oid used, must be rebuilt @db.run "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, last_oid BIGINT UNSIGNED)"