]> andersk Git - moira.git/blob - incremental/afs_create.pl
If a problem is encountered trying to create a volume on a server, fallback
[moira.git] / incremental / afs_create.pl
1 #!/usr/athena/bin/perl
2 # Usage: afs_create locker type cell path quota user group
3
4 require "/moira/bin/afs_utils.pl";
5
6 $protodir="/moira/dotfiles";
7 $quota=1;
8
9 %proc =
10     ("ATHENA.MIT.EDU", 'athena_proc' );
11
12 umask(0);
13
14 die "Usage: $0 locker type cell path user group\n" if (@ARGV != 6);
15 ($locker,$type,$cell,$path,$user,$group) = @ARGV;
16
17 # Lookup volume type
18 ($c = $cell) =~ s/\./_/g;
19 $vtype = eval "\$vtypes_$c{$type}";
20 die "Cannot create $type volumes in $cell\n" unless $vtype;
21 $vname = $vtype . "." . $locker;
22 $vname =~ s/[^-A-Za-z0-9_.]//g;         # strip out illegal characters
23
24 # Find free space/Create volume
25 foreach $tries (1..3) {
26     ($asrv,$apart) = &afs_find($cell,$type,$quota,@except);
27     die "Unable to find space to create $vname in $cell\n" unless ($asrv&&$apart);
28     $code = system("$vos create $asrv $apart $vname -cell $cell >/dev/null");
29     push(@except, $asrv);
30     next if ($code);
31 }
32 &fatal("Unable to create $vname in $cell") if ($code); # Too many create errors
33 push(@clean, "$vos remove $asrv $apart $vname -cell $cell >/dev/null");
34
35 # Create mountpoint and set quota
36 $path =~ s:^/afs/([^.]):/afs/.\1:;
37 system("$fs checkv >/dev/null; $fs mkm $path $vname");
38 &fatal("Unable to create $path") if ($?);
39 push(@clean, "$fs rmm $path");
40
41 # Obtain user/group information (uid >= 0, gid <= 0)
42 $uid = $gid = 0;
43 open(PTS, "$pts ex $user -cell $cell|");
44 chop($_ = <PTS>);
45 close(PTS);
46 ($uid,$uid,$uid,$uid) = split(/[:,] /, $_) unless ($?);
47
48 open(PTS, "$pts ex system:$group -cell $cell|");
49 chop($_ = <PTS>);
50 close(PTS);
51 ($gid,$gid,$gid,$gid) = split(/[:,] /, $_) unless ($?);
52
53 # Dispatch to the cell-specific creation routines
54 eval "&$proc{$cell}";
55 &fatal($@) if ($@);
56
57 # Set the filesystem quota
58 system("$fs sq $path $quota");
59 &fatal("Unable to set the quota on $path") if ($?);
60
61 # Release the parent volume
62 ($p = $path) =~ s:/[^/]+$::;
63 open(FS, "$fs lv $p|") || &fatal("Can't get information about $p");
64 chop($_ = <FS>);
65 close(FS);
66 &fatal("Can't get information about $p") if ($?);
67 @tmp = (split(/ /,$_));
68 system("$vos release $tmp[$#tmp] -cell $cell >/dev/null") &&
69     &fatal("Can't release $tmp[$#tmp] in cell $cell");
70
71 &afs_quota_adj($cell,$asrv,$apart,$quota);
72 exit(0);
73
74 sub fatal
75 {
76     local($cmd);
77     $_ = join(' ',@_);
78     s/\n$//;
79
80     while (@clean) {
81         $cmd = pop(@clean);
82         warn "$locker: Cleanup failed: $cmd\n" if (system("$cmd"));
83     }
84     die "$locker: $_\n";
85 }
86
87 # Cell specific procedures
88 sub athena_proc
89 {
90     # Default acls:
91     #
92     # ACTIVITY  <user> all <group> all system:anyuser rl
93     # APROJ     <user> all <group> all system:anyuser rl
94     # AREF      <user> all <group> rl
95     # CONTRIB   <user> all system:anyuser rl
96     # COURSE    <user> all <group> all system:facdev all system:authuser rl
97     # HOMEDIR   <user> all
98     # PROJECT   <user> all <group> all
99     # REF       <user> all system:anyuser rl
100     # SW        <user> all system:swmaint all system:authuser rl
101     # SYSTEM    system:administrators all system:anyuser rl
102     # UROP      <user> all <group> all system:facdev all system:authuser rl
103     #
104     # Notes:
105     # 1. All directories also have "system:expunge ld".
106
107     @acl=("system:expunge ld");
108     push(@acl,"system:facdev all") if ($type =~ /^(COURSE|UROP)/);
109     push(@acl,"system:swmaint all") if ($type =~ /^(SW)/);
110     push(@acl,"system:administrators all") if ($type =~ /^(SYSTEM)/);
111     push(@acl,"$user all")
112         if ($uid != 0 && $type =~ /^(ACTIVITY|APROJ|AREF|CONTRIB|COURSE|HOMEDIR|PROJECT|REF|SW|UROP)/);
113     push(@acl,"system:$group all")
114         if ($gid != 0 && $type =~ /^(ACTIVITY|APROJ|COURSE|PROJECT|UROP)/);
115     push(@acl,"system:$group rl") if ($gid != 0 && $type =~ /^(AREF)/);
116     push(@acl,"system:authuser rl")
117         if ($type =~ /^(COURSE|SW|UROP)/);
118     push(@acl,"system:anyuser rl")
119         if ($type =~ /^(ACTIVITY|APROJ|CONTRIB|REF|SYSTEM)/);
120
121     if ($type !~ /^(AREF|SYSTEM)/) {
122         system("$fs mkm $path/OldFiles $vname.backup");
123         warn "$locker: Unable to create OldFiles mountpoint\n" if ($?);
124     }
125
126     if ($type =~ /ACTIVITY|APROJ|PROJECT/) {
127         chown($gid,0,$path) ||
128             die "Unable to set volume ownership\n";
129     } elsif ($type =~ /HOMEDIR|UROP/) {
130         chown($uid,0,$path) ||
131             die "Unable to set volume ownership\n";
132     }
133
134     if ($type eq "HOMEDIR") {
135         die "Unable to get uid for user\n" unless ($uid);
136
137         mkdir("$path/Public",0755) && chown($uid,0,"$path/Public") &&
138             mkdir("$path/Private",0700) && mkdir("$path/Mail", 0700) &&
139                 chown($uid,0,"$path/Public","$path/Private","$path/Mail") ||
140                     die "Unable to create subdirectories\n";
141         system("$fs sa $path/Public @acl system:anyuser rl -clear") &&
142             die "Unable to set acl on Public directory";
143         system("$fs sa -dir $path/Private $path/Mail -acl @acl -clear") &&
144             die "Unable to set acl on Private and/or Mail directories\n";
145
146         opendir(DIR,$protodir) || die "Unable to open prototype directory\n";
147         @files=readdir(DIR);
148         closedir(DIR);
149
150         for $i (@files) {
151             next if ($i eq "." || $i eq "..");
152             next unless -f "$protodir/$i";
153             open(IN,"<$protodir/$i") || die "Unable to open $protodir/$i\n";
154             open(OUT,">$path/$i") || die "Unable to create $i\n";
155             while ($_=<IN>) { print OUT $_; };
156             close(OUT);
157             close(IN);
158             chown($uid,0,"$path/$i");
159         }
160         system("$fs sa $path @acl system:anyuser l -clear") &&
161             die "Unable to set acl on top-level directory\n";
162         return;
163     }
164
165     system("$fs sa $path @acl -clear") &&
166         die "Unable to set acl of $path\n";
167 }
This page took 0.0496 seconds and 5 git commands to generate.