step5_newids.pl 6.73 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2006, 2007, 2009 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#
use English;

use lib "/usr/testbed/lib";
use libdb;
use libtestbed;

#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# See if a table has been changed by looking for the existence of a field.
#
sub TableChanged($$)
{
    my ($table, $slot) = @_;

    my $describe_result =
	DBQueryFatal("describe $table $slot");

    return $describe_result->numrows;
}

# users
DBQueryFatal("lock tables users write, user_stats write");
if (! TableChanged("users", "uid_uuid")) {
    DBQueryFatal("alter table users ".
	"add `uid_uuid` varchar(40) NOT NULL default '' after uid_idx, ".
	"add KEY uid_uuid (`uid_uuid`)");
}
if (! TableChanged("user_stats", "uid_uuid")) {
    DBQueryFatal("alter table user_stats ".
	"add `uid_uuid` varchar(40) NOT NULL default '' after uid_idx, ".
	"add KEY uid_uuid (`uid_uuid`)");
}
my $query_result = DBQueryFatal("select uid_idx from users ".
				"where uid_uuid=''");
while (my ($uid_idx) = $query_result->fetchrow_array()) {
64
    my $uuid = NewUUID();
65 66 67 68 69 70 71 72 73 74 75 76 77

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for user: $uid_idx\n");
    }
    DBQueryFatal("update users set uid_uuid='$uuid' where uid_idx='$uid_idx'");
    DBQueryFatal("update user_stats set uid_uuid='$uuid' ".
		 "where uid_idx='$uid_idx'");
    select(undef, undef, undef, 0.02);
}
$query_result = DBQueryFatal("select uid_idx from user_stats ".
			     "where uid_uuid=''");
while (my ($uid_idx) = $query_result->fetchrow_array()) {
78
    my $uuid = NewUUID();
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for user: $uid_idx\n");
    }
    DBQueryFatal("update user_stats set uid_uuid='$uuid' ".
		 "where uid_idx='$uid_idx'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# groups (no point in doing the project).
DBQueryFatal("lock tables groups write, group_stats write");
if (! TableChanged("groups", "gid_uuid")) {
    DBQueryFatal("alter table groups ".
	"add `gid_uuid` varchar(40) NOT NULL default '' after gid_idx, ".
	"add KEY gid_uuid (`gid_uuid`)");
}
if (! TableChanged("group_stats", "gid_uuid")) {
    DBQueryFatal("alter table group_stats ".
	"add `gid_uuid` varchar(40) NOT NULL default '' after gid_idx, ".
	"add KEY gid_uuid (`gid_uuid`)");
}
$query_result = DBQueryFatal("select gid_idx from groups ".
			     "where gid_uuid=''");
while (my ($gid_idx) = $query_result->fetchrow_array()) {
105
    my $uuid = NewUUID();
106 107 108 109 110 111 112 113 114 115 116 117 118 119

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for group: $gid_idx\n");
    }
    DBQueryFatal("update groups set gid_uuid='$uuid' ".
		 "where gid_idx='$gid_idx'");
    DBQueryFatal("update group_stats set gid_uuid='$uuid' ".
		 "where gid_idx='$gid_idx'");
    select(undef, undef, undef, 0.02);
}
$query_result = DBQueryFatal("select gid_idx from group_stats ".
			     "where gid_uuid=''");
while (my ($gid_idx) = $query_result->fetchrow_array()) {
120
    my $uuid = NewUUID();
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for group: $gid_idx\n");
    }
    DBQueryFatal("update group_stats set gid_uuid='$uuid' ".
		 "where gid_idx='$gid_idx'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# experiments 
DBQueryFatal("lock tables experiments write, experiment_stats write");
if (! TableChanged("experiments", "eid_uuid")) {
    DBQueryFatal("alter table experiments ".
	"add `eid_uuid` varchar(40) NOT NULL default '' after eid, ".
	"add KEY eid_uuid (`eid_uuid`)");
}
if (! TableChanged("experiment_stats", "eid_uuid")) {
    DBQueryFatal("alter table experiment_stats ".
	"add `eid_uuid` varchar(40) NOT NULL default '' after eid, ".
	"add KEY eid_uuid (`eid_uuid`)");
}
$query_result = DBQueryFatal("select idx from experiments ".
			     "where eid_uuid=''");
while (my ($exptidx) = $query_result->fetchrow_array()) {
147
    my $uuid = NewUUID();
148 149 150 151 152 153 154 155 156 157 158 159 160 161

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for exptidx: $exptidx\n");
    }
    DBQueryFatal("update experiments set eid_uuid='$uuid' ".
		 "where idx='$exptidx'");
    DBQueryFatal("update experiment_stats set eid_uuid='$uuid' ".
		 "where exptidx='$exptidx'");
    select(undef, undef, undef, 0.02);
}
$query_result = DBQueryFatal("select exptidx from experiment_stats ".
			     "where eid_uuid=''");
while (my ($exptidx) = $query_result->fetchrow_array()) {
162
    my $uuid = NewUUID();
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for exptidx: $exptidx\n");
    }
    DBQueryFatal("update experiment_stats set eid_uuid='$uuid' ".
		 "where exptidx='$exptidx'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# images
DBQueryFatal("lock tables images write");
if (! TableChanged("images", "uuid")) {
    DBQueryFatal("alter table images ".
	"add `uuid` varchar(40) NOT NULL default '' after imageid, ".
	"add KEY uuid (`uuid`)");
}
$query_result = DBQueryFatal("select imageid from images where uuid=''");
while (my ($imageid) = $query_result->fetchrow_array()) {
183
    my $uuid = NewUUID();
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for imageid: $imageid\n");
    }
    DBQueryFatal("update images set uuid='$uuid' ".
		 "where imageid='$imageid'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# os_info
DBQueryFatal("lock tables os_info write");
if (! TableChanged("os_info", "uuid")) {
    DBQueryFatal("alter table os_info ".
	"add `uuid` varchar(40) NOT NULL default '' after osid, ".
	"add KEY uuid (`uuid`)");
}
$query_result = DBQueryFatal("select osid from os_info where uuid=''");
while (my ($osid) = $query_result->fetchrow_array()) {
204
    my $uuid = NewUUID();
205 206 207 208 209 210 211 212 213 214 215

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for osid: $osid\n");
    }
    DBQueryFatal("update os_info set uuid='$uuid' ".
		 "where osid='$osid'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");