]> andersk Git - moira.git/blob - incremental/afs_rename.pl
333d769b986c20ca075aae8aea1c1992fcaf840b
[moira.git] / incremental / afs_rename.pl
1 #!/usr/athena/bin/perl
2 # Usage: afs_change \
3 #               oldname oldcell oldtype oldltype oldpath \
4 #               newname newcell newtype newltype newpath
5
6 require "/moira/bin/afs_utils.pl";
7
8 die "$0: Incorrect number of arguments\n" if (@ARGV != 10);
9
10 ($oldname, $oldcell, $oldtype, $oldltype, $oldpath,
11  $newname, $newcell, $newtype, $newltype, $newpath) =
12     @ARGV;
13
14 # Modify the paths, as only the read-write paths are used.
15 $oldpath =~ s:^/afs/([^.]):/afs/.\1:;   # use read-write path
16 $newpath =~ s:^/afs/([^.]):/afs/.\1:;   # use read-write path
17
18 die "Cannot change cells\n" if ($oldcell ne $newcell);
19 die "Can only handle AFS and ERR lockers\n"
20     if (($oldtype !~ /^(AFS|ERR)$/) ||
21         ($newtype !~ /^(AFS|ERR)$/));
22
23 # Lookup volume type
24 ($c = $newcell) =~ s/\./_/g;
25 $vtype = eval "\$vtypes_${c}{$newltype}";
26 die "Cannot handle $newltype volumes\n" unless $vtype;
27 $newvname = $vtype . "." . $newname;
28
29 if ($oldtype eq "ERR") {
30     # If volume was never unmounted, we don't need to do anything.
31     system("$fs lsm $oldpath > /dev/null");
32     exit(0) if ($? == 0);
33
34     # Lookup volume type for old locker
35     ($c = $oldcell) =~ s/\./_/g;
36     $vtype = eval "\$vtypes_${c}{$oldltype}";
37     die "Cannot handle $oldltype volumes\n" unless $vtype;
38     $oldvname = $vtype . "." . $oldname;
39     $oldvname =~ s/[^-A-Za-z0-9_.]//g;  # strip out illegal characters
40
41     if (&check("X" . $oldvname) && &check("Xn." . $oldvname)) {
42         print STDERR "Cannot locate deactivated locker $oldname\n";
43         exit(1) if ($newtype eq "AFS");
44         exit(0);
45     }
46     $newvname = "n." . $newvname if ($oldvname =~ /^Xn\./);
47 } else {
48     $prefix = "";
49
50     open(FS, "$fs lsm $oldpath|");
51     chop($_ = <FS>);
52     close(FS);
53     die "Unable to locate locker $oldname\n" if ($?);
54     ($oldvname = $_) =~ s/^.* volume '.(.*)'$/\1/;
55     die "Unusual mountpoint encountered: $oldpath\n" if ($oldvname =~ /[ :]/);
56     $newvname = "n." . $newvname if ($oldvname =~ /^n\./);
57 }
58
59 $newvname = "X" . $newvname if ($newtype eq "ERR");
60 $newvname =~ s/[^-A-Za-z0-9_.]//g;      # strip out illegal characters
61
62 if ($oldbackup && $newvname =~ /^n\./) {
63     system("$vos remove $oldbackup $oldvname.backup -cell $oldcell");
64 }
65 if ($oldvname ne $newvname) {
66     &run("$vos rename $oldvname $newvname -cell $newcell");
67     push(@clean, "$vos rename $newvname $oldvname -cell $newcell");
68 }
69
70 if ($oldtype eq "AFS") {
71     &run("$fs rmm $oldpath");
72     push(@clean, "$fs mkm $oldpath $oldvname");
73     &release_parent($oldpath)
74         if ($newtype ne "AFS" || $oldpath ne $newpath);
75 }
76 if ($newtype eq "AFS") {
77     &run("$fs mkm $newpath $newvname");
78     push(@clean, "$fs rmm $newpath");
79     &release_parent($newpath);
80 }
81
82 &do_releases;
83 exit;
84
85
86 sub run
87 {
88     local(@cmd) = @_;
89
90     system("@cmd >/dev/null");
91     &fatal("@cmd: FAILED") if ($?);
92     return 0;
93 }
94
95
96 sub fatal
97 {
98     local($cmd);
99     $_ = join(' ',@_);
100     s/\n$//;
101
102     while (@clean) {
103         $cmd = shift(@clean);
104         warn "$newname: Cleanup failed: $cmd\n" if (system("$cmd"));
105     }
106     die "$newname: $_\n";
107 }
108
109
110 sub check
111 {
112     local($vname) = @_;
113     local(@vos, @a);
114
115     open (VOS, "$vos listvldb -name $vname -cell $oldcell 2>/dev/null|");
116     chop(@vos = <VOS>);
117     close(VOS);
118     return 1 if ($?);
119
120     $oldvname = $vname;
121     @a = split(/\s+/, $vos[$[ + 4]);
122     if (($_ = pop @a) eq "valid") {
123         splice(@vos, 0, 6);
124         for (@vos) {
125             ($oldbackup=$_) =~ s/^.*server (.*) partition (.*) RW .*$/\1 \2/
126                 if (/RW Site/);
127         }
128     }
129     return 0;
130 }
131
132 sub release_parent
133 {
134     local($p) = @_;
135
136     $p =~ s:/[^/]+$::;
137     open(FS, "$fs lv $p|") || &fatal("Can't get information about $p");
138     chop($_ = <FS>);
139     close(FS);
140     return if ($?);
141
142     local(@tmp) = (split(/ /,$_));
143     push(@vrelease, "$tmp[$#tmp] -cell $newcell");
144 }
145
146
147 sub do_releases
148 {
149     local($lastv) = "";
150     local(@volumes) = sort @vrelease;
151     while (@volumes) {
152         $_ = shift(@volumes);
153         next if ($_ eq $lastv);
154         system("$vos release $_");
155     }
156 }
This page took 0.067189 seconds and 3 git commands to generate.