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