]> andersk Git - moira.git/blob - incremental/afs_create.pl
Mistake in the loop construct which didn't detect a successful creation fixed.
[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     break;
32 }
33 &fatal("Unable to create $vname in $cell") if ($code); # Too many create errors
34 push(@clean, "$vos remove $asrv $apart $vname -cell $cell >/dev/null");
35
36 # Create mountpoint and set quota
37 $path =~ s:^/afs/([^.]):/afs/.\1:;
38 system("$fs checkv >/dev/null; $fs mkm $path $vname");
39 &fatal("Unable to create $path") if ($?);
40 push(@clean, "$fs rmm $path");
41
42 # Obtain user/group information (uid >= 0, gid <= 0)
43 $uid = $gid = 0;
44 open(PTS, "$pts ex $user -cell $cell|");
45 chop($_ = <PTS>);
46 close(PTS);
47 ($uid,$uid,$uid,$uid) = split(/[:,] /, $_) unless ($?);
48
49 open(PTS, "$pts ex system:$group -cell $cell|");
50 chop($_ = <PTS>);
51 close(PTS);
52 ($gid,$gid,$gid,$gid) = split(/[:,] /, $_) unless ($?);
53
54 # Dispatch to the cell-specific creation routines
55 eval "&$proc{$cell}";
56 &fatal($@) if ($@);
57
58 # Set the filesystem quota
59 system("$fs sq $path $quota");
60 &fatal("Unable to set the quota on $path") if ($?);
61
62 # Release the parent volume
63 ($p = $path) =~ s:/[^/]+$::;
64 open(FS, "$fs lv $p|") || &fatal("Can't get information about $p");
65 chop($_ = <FS>);
66 close(FS);
67 &fatal("Can't get information about $p") if ($?);
68 @tmp = (split(/ /,$_));
69 system("$vos release $tmp[$#tmp] -cell $cell >/dev/null") &&
70     &fatal("Can't release $tmp[$#tmp] in cell $cell");
71
72 &afs_quota_adj($cell,$asrv,$apart,$quota);
73 exit(0);
74
75 sub fatal
76 {
77     local($cmd);
78     $_ = join(' ',@_);
79     s/\n$//;
80
81     while (@clean) {
82         $cmd = pop(@clean);
83         warn "$locker: Cleanup failed: $cmd\n" if (system("$cmd"));
84     }
85     die "$locker: $_\n";
86 }
87
88 # Cell specific procedures
89 sub athena_proc
90 {
91     # Default acls:
92     #
93     # ACTIVITY  <user> all <group> all system:anyuser rl
94     # APROJ     <user> all <group> all system:anyuser rl
95     # AREF      <user> all <group> rl
96     # CONTRIB   <user> all system:anyuser rl
97     # COURSE    <user> all <group> all system:facdev all system:authuser rl
98     # HOMEDIR   <user> all
99     # PROJECT   <user> all <group> all
100     # REF       <user> all system:anyuser rl
101     # SW        <user> all system:swmaint all system:authuser rl
102     # SYSTEM    system:administrators all system:anyuser rl
103     # UROP      <user> all <group> all system:facdev all system:authuser rl
104     #
105     # Notes:
106     # 1. All directories also have "system:expunge ld".
107
108     @acl=("system:expunge ld");
109     push(@acl,"system:facdev all") if ($type =~ /^(COURSE|UROP)/);
110     push(@acl,"system:swmaint all") if ($type =~ /^(SW)/);
111     push(@acl,"system:administrators all") if ($type =~ /^(SYSTEM)/);
112     push(@acl,"$user all")
113         if ($uid != 0 && $type =~ /^(ACTIVITY|APROJ|AREF|CONTRIB|COURSE|HOMEDIR|PROJECT|REF|SW|UROP)/);
114     push(@acl,"system:$group all")
115         if ($gid != 0 && $type =~ /^(ACTIVITY|APROJ|COURSE|PROJECT|UROP)/);
116     push(@acl,"system:$group rl") if ($gid != 0 && $type =~ /^(AREF)/);
117     push(@acl,"system:authuser rl")
118         if ($type =~ /^(COURSE|SW|UROP)/);
119     push(@acl,"system:anyuser rl")
120         if ($type =~ /^(ACTIVITY|APROJ|CONTRIB|REF|SYSTEM)/);
121
122     if ($type !~ /^(AREF|SYSTEM)/) {
123         system("$fs mkm $path/OldFiles $vname.backup");
124         warn "$locker: Unable to create OldFiles mountpoint\n" if ($?);
125     }
126
127     if ($type =~ /ACTIVITY|APROJ|PROJECT/) {
128         chown($gid,0,$path) ||
129             die "Unable to set volume ownership\n";
130     } elsif ($type =~ /HOMEDIR|UROP/) {
131         chown($uid,0,$path) ||
132             die "Unable to set volume ownership\n";
133     }
134
135     if ($type eq "HOMEDIR") {
136         die "Unable to get uid for user\n" unless ($uid);
137
138         mkdir("$path/Public",0755) && chown($uid,0,"$path/Public") &&
139             mkdir("$path/Private",0700) && mkdir("$path/Mail", 0700) &&
140                 chown($uid,0,"$path/Public","$path/Private","$path/Mail") ||
141                     die "Unable to create subdirectories\n";
142         system("$fs sa $path/Public @acl system:anyuser rl -clear") &&
143             die "Unable to set acl on Public directory";
144         system("$fs sa -dir $path/Private $path/Mail -acl @acl -clear") &&
145             die "Unable to set acl on Private and/or Mail directories\n";
146
147         opendir(DIR,$protodir) || die "Unable to open prototype directory\n";
148         @files=readdir(DIR);
149         closedir(DIR);
150
151         for $i (@files) {
152             next if ($i eq "." || $i eq "..");
153             next unless -f "$protodir/$i";
154             open(IN,"<$protodir/$i") || die "Unable to open $protodir/$i\n";
155             open(OUT,">$path/$i") || die "Unable to create $i\n";
156             while ($_=<IN>) { print OUT $_; };
157             close(OUT);
158             close(IN);
159             chown($uid,0,"$path/$i");
160         }
161         system("$fs sa $path @acl system:anyuser l -clear") &&
162             die "Unable to set acl on top-level directory\n";
163         return;
164     }
165
166     system("$fs sa $path @acl -clear") &&
167         die "Unable to set acl of $path\n";
168 }
This page took 0.053598 seconds and 5 git commands to generate.