mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-22 18:50:08 +03:00
Feature #407: Update onedb for new group-user and cluster-host relations
This commit is contained in:
parent
3db7376b5e
commit
91b37ed72d
115
src/onedb/1.rb
115
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 += "<ID>#{oid}</ID>"
|
||||
end
|
||||
|
||||
name = row[:user_name]
|
||||
|
||||
body = "<USER><ID>#{oid}</ID><NAME>#{name}</NAME><GID>#{gid}</GID><PASSWORD>#{row[:password]}</PASSWORD><ENABLED>#{row[:enabled]}</ENABLED></USER>"
|
||||
body = "<USER><ID>#{oid}</ID><GID>#{gid}</GID><NAME>#{name}</NAME><PASSWORD>#{row[:password]}</PASSWORD><ENABLED>#{row[:enabled]}</ENABLED><GROUPS><ID>#{gid}</ID></GROUPS></USER>"
|
||||
|
||||
@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] += "<ID>#{oid}</ID>"
|
||||
end
|
||||
|
||||
# There is one host share for each host
|
||||
host_share = ""
|
||||
@db.fetch("SELECT * FROM host_shares WHERE hid=#{oid}") do |share|
|
||||
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>"
|
||||
end
|
||||
|
||||
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>"
|
||||
|
||||
@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 = "<CLUSTER><ID>#{oid}</ID><NAME>#{name}</NAME></CLUSTER>"
|
||||
hids = ""
|
||||
if cluster_host_ids[oid] != nil
|
||||
hids = cluster_host_ids[oid]
|
||||
end
|
||||
|
||||
body = "<CLUSTER><ID>#{oid}</ID><NAME>#{name}</NAME><HOSTS>#{hids}</HOSTS></CLUSTER>"
|
||||
|
||||
@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 = "<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>"
|
||||
end
|
||||
|
||||
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>"
|
||||
|
||||
@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','<GROUP><ID>0</ID><UID>0</UID><NAME>oneadmin</NAME></GROUP>',0);"
|
||||
@db.run "INSERT INTO group_pool VALUES(1,'users','<GROUP><ID>1</ID><UID>0</UID><NAME>users</NAME></GROUP>',0);"
|
||||
@db.run "INSERT INTO group_pool VALUES(0,'oneadmin','<GROUP><ID>0</ID><UID>0</UID><NAME>oneadmin</NAME><USERS><ID>0</ID></USERS></GROUP>',0);"
|
||||
@db.run "INSERT INTO group_pool VALUES(1,'users','<GROUP><ID>1</ID><UID>0</UID><NAME>users</NAME><USERS>#{user_group_ids}</USERS></GROUP>',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)"
|
||||
|
Loading…
x
Reference in New Issue
Block a user