1 # cluster-usermin-lib.pl
2 # Common functions for managing usermin installs across a cluster
4 BEGIN { push(@INC, ".."); };
7 &foreign_require("servers", "servers-lib.pl");
8 &foreign_require("usermin", "usermin-lib.pl");
10 # list_usermin_hosts()
11 # Returns a list of all hosts whose usermin modules are being managed
12 sub list_usermin_hosts
14 local %smap = map { $_->{'id'}, $_ } &list_servers();
15 local $hdir = "$module_config_directory/hosts";
18 foreach $h (readdir(DIR)) {
19 next if ($h eq "." || $h eq ".." || !-d "$hdir/$h");
20 local %host = ( 'id', $h );
21 next if (!$smap{$h}); # underlying server was deleted
23 opendir(MDIR, "$hdir/$h");
24 foreach $f (readdir(MDIR)) {
25 if ($f =~ /^(\S+)\.mod$/) {
27 &read_file("$hdir/$h/$f", \%mod);
28 push(@{$host{'modules'}}, \%mod);
30 elsif ($f =~ /^(\S+)\.theme$/) {
32 &read_file("$hdir/$h/$f", \%theme);
33 push(@{$host{'themes'}}, \%theme);
35 elsif ($f eq "usermin") {
36 &read_file("$hdir/$h/$f", \%host);
46 # save_usermin_host(&host)
49 local $hdir = "$module_config_directory/hosts";
52 if (-d "$hdir/$_[0]->{'id'}") {
53 opendir(DIR, "$hdir/$_[0]->{'id'}");
54 map { $oldfile{$_}++ } readdir(DIR);
58 mkdir("$hdir/$_[0]->{'id'}", 0700);
61 foreach $m (@{$_[0]->{'modules'}}) {
62 &write_file("$hdir/$_[0]->{'id'}/$m->{'dir'}.mod", $m);
63 delete($oldfile{"$m->{'dir'}.mod"});
65 foreach $m (@{$_[0]->{'themes'}}) {
66 &write_file("$hdir/$_[0]->{'id'}/$m->{'dir'}.theme", $m);
67 delete($oldfile{"$m->{'dir'}.theme"});
69 local %usermin = %{$_[0]};
70 delete($usermin{'modules'});
71 delete($usermin{'themes'});
72 delete($usermin{'id'});
73 &write_file("$hdir/$_[0]->{'id'}/usermin", \%usermin);
74 delete($oldfile{"usermin"});
75 unlink(map { "$hdir/$_[0]->{'id'}/$_" } keys %oldfile);
78 # delete_usermin_host(&host)
79 sub delete_usermin_host
81 system("rm -rf '$module_config_directory/hosts/$_[0]->{'id'}'");
85 # Returns a list of all servers from the usermin servers module that can be
86 # managed, plus this server
89 local @servers = &servers::list_servers_sorted();
90 return ( &servers::this_server(), grep { $_->{'user'} } @servers );
93 # server_name(&server)
96 return $_[0]->{'desc'} ? $_[0]->{'desc'} : $_[0]->{'host'};
102 local (%done, $u, %descc);
103 local @uniq = grep { !$done{$_->{'dir'}}++ } map { @{$_->{'modules'}} } @{$_[0]};
104 map { $descc{$_->{'desc'}}++ } @uniq;
106 $u->{'desc'} .= " ($u->{'dir'})" if ($descc{$u->{'desc'}} > 1);
108 return sort { $a->{'desc'} cmp $b->{'desc'} } @uniq;
115 return sort { $a->{'desc'} cmp $b->{'desc'} }
116 grep { !$done{$_->{'dir'}}++ }
117 map { @{$_->{'themes'}} } @{$_[0]};
124 return sort { $a->{'name'} cmp $b->{'name'} }
125 grep { !$done{$_->{'name'}}++ }
126 map { @{$_->{'groups'}} } @{$_[0]};
133 return sort { $a->{'name'} cmp $b->{'name'} }
134 grep { !$done{$_->{'name'}}++ }
135 map { @{$_->{'users'}} } @{$_[0]};
138 # create_on_input(desc, [no-donthave], [no-have], [multiple])
141 local @hosts = &list_usermin_hosts();
142 local @servers = &list_servers();
144 print "<tr> <td><b>$_[0]</b></td>\n";
148 print "<select name=server size=5 multiple>\n";
151 print "<select name=server>\n";
153 print "<option value=-1>$text{'user_all'}\n";
154 print "<option value=-2>$text{'user_donthave'}\n" if (!$_[1]);
155 print "<option value=-3>$text{'user_have'}\n" if (!$_[2]);
156 local @groups = &servers::list_all_groups(\@servers);
158 foreach $h (@hosts) {
159 local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
161 print "<option value='$s->{'id'}'>",
162 $s->{'desc'} ? $s->{'desc'} : $s->{'host'},"\n";
163 $gothost{$s->{'host'}}++;
167 foreach $g (@groups) {
169 foreach $m (@{$g->{'members'}}) {
170 ($found++, last) if ($gothost{$m});
172 print "<option value='group_$g->{'name'}'>",
173 &text('user_ofgroup', $g->{'name'}),"\n" if ($found);
177 print "</td> </tr>\n";
181 # create_on_parse(prefix, &already, name, [no-print])
184 local @allhosts = &list_usermin_hosts();
185 local @servers = &list_servers();
188 foreach $server (split(/\0/, $in{'server'})) {
190 # Install on hosts that don't have it
191 local %already = map { $_->{'id'}, 1 } @{$_[1]};
192 push(@hosts, grep { !$already{$_->{'id'}} } @allhosts);
193 print "<b>",&text($_[0].'3', $_[2]),"</b><p>\n" if (!$_[3]);
195 elsif ($server == -3) {
196 # Install on hosts that do have it
197 local %already = map { $_->{'id'}, 1 } @{$_[1]};
198 push(@hosts, grep { $already{$_->{'id'}} } @allhosts);
199 print "<b>",&text($_[0].'6', $_[2]),"</b><p>\n" if (!$_[3]);
201 elsif ($server =~ /^group_(.*)/) {
202 # Install on members of some group
203 local ($group) = grep { $_->{'name'} eq $1 }
204 &servers::list_all_groups(\@servers);
205 push(@hosts, grep { local $hid = $_->{'id'};
206 local ($s) = grep { $_->{'id'} == $hid } @servers;
207 &indexof($s->{'host'}, @{$group->{'members'}}) >= 0 }
209 print "<b>",&text($_[0].'4', $_[2], $group->{'name'}),
210 "</b><p>\n" if (!$_[3]);
212 elsif ($server != -1) {
213 # Just install on one host
214 local ($onehost) = grep { $_->{'id'} == $server }
216 push(@hosts, $onehost);
217 local ($s) = grep { $_->{'id'} == $onehost->{'id'} } @servers;
218 print "<b>",&text($_[0].'5', $_[2],
219 &server_name($s)),"</b><p>\n" if (!$_[3]);
222 # Installing on every host
223 push(@hosts, @allhosts);
224 print "<b>",&text($_[0], join(" ", @names)),
225 "</b><p>\n" if (!$_[3]);
228 return &unique(@hosts);
232 # Returns the text hash for Usermin, on this system
237 &usermin::get_usermin_miniserv_config(\%miniserv);
238 local $root = $miniserv{'root'};
240 &usermin::get_usermin_config(\%ugconfig);
241 local $ol = $ugconfig{'overlang'};
243 local ($dir) = ($_[1] || "lang");
245 # Read global lang files
246 foreach $o (@lang_order_list) {
247 local $ok = &read_file_cached("$root/$dir/$o", \%text);
248 return () if (!$ok && $o eq $default_lang);
251 foreach $o (@lang_order_list) {
252 &read_file_cached("$root/$ol/$o", \%text);
255 &read_file_cached("$usermin::config{'usermin_dir'}/custom-lang", \%text);
258 # Read module's lang files
259 local $mdir = "$root/$_[0]";
260 foreach $o (@lang_order_list) {
261 &read_file_cached("$mdir/$dir/$o", \%text);
264 foreach $o (@lang_order_list) {
265 &read_file_cached("$mdir/$ol/$o", \%text);
268 &read_file_cached("$usermin::config{'usermin_dir'}/$_[0]/custom-lang", \%text);
270 foreach $k (keys %text) {
271 $text{$k} =~ s/\$(\{([^\}]+)\}|([A-Za-z0-9\.\-\_]+))/text_subs($2 || $3,\%text)/ge;