init_newids.pl 4.42 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2006 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 147 148 149 150 151 152 153
#
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'};

# We want to change the group_membership table later, so save the ids.
my %uids = ();
my %gids = ();

# Nice place to start.
my $next_gid = 10000;

# Locals
my $TBOPSPID = TBOPSPID();

#
# Generate new unique indexes for all existing users. 
#
my $query_result =
    DBQueryFatal("select uid,unix_uid from users ".
		 "order by usr_created");

while (my ($uid,$unix_uid) = $query_result->fetchrow_array()) {
    #print "$uid -> $unix_uid\n";
    
    DBQueryFatal("update users set uid_idx=$unix_uid ".
		 "where uid='$uid'");
    
    # These were left out in initial testbed setup.
    if ($uid eq "elabckup" || $uid eq "elabman" || $uid eq "operator") {
	DBQueryFatal("replace into user_stats set ".
		     " uid='$uid',uid_idx=$unix_uid");
    }

    $uids{$uid} = $unix_uid;
}
DBQuery("alter table users drop primary key");
DBQueryFatal("alter table users add PRIMARY KEY (uid_idx)");

DBQuery("alter table projects drop PRIMARY KEY");
DBQuery("alter table project_stats drop PRIMARY KEY");
DBQuery("alter table groups drop PRIMARY KEY");
DBQuery("alter table group_stats drop PRIMARY KEY");
DBQuery("alter table group_membership drop PRIMARY KEY");

# These were left out in initial testbed setup.
DBQueryFatal("replace into group_stats set ".
	     " pid='$TBOPSPID',gid='$TBOPSPID',gid_idx=0");
DBQueryFatal("replace into project_stats set ".
	     " pid='$TBOPSPID',pid_idx=0");

#
# Get the current list of pid/gid pairs. Generate new indexes for
# all pids and gids.
#
$query_result =
    DBQueryFatal("select pid from projects order by pid");

while (my ($pid) = $query_result->fetchrow_array()) {
    DBQueryFatal("update projects set pid_idx=$next_gid ".
		 "where pid='$pid'");
    DBQueryFatal("update project_stats set pid_idx=$next_gid ".
		 "where pid='$pid'");
    
    $gids{"$pid:$pid"} = $next_gid;
    $next_gid++;
}

$query_result =
    DBQueryFatal("select pid,gid from groups order by pid,gid");

while (my ($pid,$gid) = $query_result->fetchrow_array()) {
    my $pid_idx = $gids{"$pid:$pid"};
    my $gid_idx;

    if ($pid eq $gid) {
	$gid_idx = $pid_idx;
    }
    else {
	$gids{"$pid:$gid"} = $gid_idx = $next_gid;
	$next_gid++;
    }
    
    DBQueryFatal("update groups set gid_idx=$gid_idx,pid_idx=$pid_idx ".
		 "where pid='$pid' and gid='$gid'");
    DBQueryFatal("update group_stats set gid_idx=$gid_idx ".
		 "where pid='$pid' and gid='$gid'");
}
DBQueryFatal("replace into emulab_indicies set ".
	     " name='next_gid', idx='$next_gid'");

#
# Now patch up the group_membership table.
#
$query_result =
    DBQueryFatal("select uid,pid,gid from group_membership");

while (my ($uid, $pid, $gid) = $query_result->fetchrow_array()) {
    my $uid_idx = $uids{$uid};
    my $pid_idx = $gids{"$pid:$pid"};
    my $gid_idx = $gids{"$pid:$gid"};

    if (!defined($uid_idx)) {
	print "*** WARNING: No idx for $uid\n";
	next;
    }
    if (!defined($pid_idx)) {
	die("No idx for $pid:$pid\n");
    }
    if (!defined($gid_idx)) {
	die("No idx for $pid:$gid\n");
    }

    DBQueryFatal("update group_membership set ".
		 "  uid_idx=$uid_idx, pid_idx=$pid_idx, gid_idx=$gid_idx ".
		 "where uid='$uid' and pid='$pid' and gid='$gid'");
}

DBQueryFatal("alter table projects add PRIMARY KEY (pid_idx)");
DBQueryFatal("alter table project_stats add PRIMARY KEY (pid_idx)");
DBQueryFatal("alter table groups add PRIMARY KEY (gid_idx)");
DBQueryFatal("alter table group_stats add PRIMARY KEY (gid_idx)");
DBQueryFatal("alter table group_membership add PRIMARY KEY (uid_idx,gid_idx)");