1 # cluster-software-lib.pl
2 # common functions for installing packages across a cluster
3 # XXX refresh all packages after installing
5 BEGIN { push(@INC, ".."); };
8 &foreign_require("servers", "servers-lib.pl");
9 &foreign_require("software", "software-lib.pl");
11 %access = &get_module_acl();
13 # list_software_hosts()
14 # Returns a list of all hosts whose software is being managed by this module
15 sub list_software_hosts
18 local %smap = map { $_->{'id'}, $_ } &list_servers();
19 local $hdir = "$module_config_directory/hosts";
21 foreach $h (readdir(DIR)) {
22 next if ($h =~ /\.host$/ || $h eq '.' || $h eq '..');
23 local %host = ( 'id', $h );
24 next if (!$smap{$h}); # underlying server was deleted
25 opendir(PDIR, "$hdir/$h") || next;
26 foreach $p (readdir(PDIR)) {
27 next if ($p eq "." || $p eq "..");
29 &read_file("$hdir/$h/$p", \%pkg);
30 push(@{$host{'packages'}}, \%pkg);
33 &read_file("$hdir/$h.host", \%host);
40 # save_software_host(&host)
41 # Add or update a managed host with it's package list
42 sub save_software_host
44 local $hdir = "$module_config_directory/hosts";
46 if (-d "$hdir/$_[0]->{'id'}") {
47 opendir(DIR, "$hdir/$_[0]->{'id'}");
48 foreach $f (readdir(DIR)) {
49 unlink("$hdir/$_[0]->{'id'}/$f");
54 mkdir("$hdir/$_[0]->{'id'}", 0700);
56 foreach $p (@{$_[0]->{'packages'}}) {
57 local $pname = $p->{'name'};
59 &write_file("$hdir/$_[0]->{'id'}/$pname", $p);
62 delete($h{'packages'});
63 &write_file("$hdir/$_[0]->{'id'}.host", \%h);
66 # delete_software_host(&host)
67 sub delete_software_host
69 &unlink_file("$module_config_directory/hosts/$_[0]->{'id'}.host");
70 &unlink_file("$module_config_directory/hosts/$_[0]->{'id'}");
74 # Returns a list of all servers from the webmin servers module that can be
75 # managed, plus this server
78 local @servers = &servers::list_servers_sorted();
79 return ( &servers::this_server(), grep { $_->{'user'} } @servers );
82 # host_to_server(&host|id)
85 local $id = ref($_[0]) ? $_[0]->{'id'} : $_[0];
86 local ($serv) = grep { $_->{'id'} eq $id } &list_servers();
90 # server_name(&server)
93 return $_[0]->{'desc'} || $_[0]->{'realhost'} || $_[0]->{'host'};
96 # get_heiropen(hostid)
97 # Returns an array of open categories
100 open(HEIROPEN, "$module_config_directory/heiropen.$_[0]");
101 local @heiropen = <HEIROPEN>;
107 # save_heiropen(&heir, hostid)
110 &open_tempfile(HEIR, ">$module_config_directory/heiropen.$_[1]");
111 foreach $h (@{$_[0]}) {
112 &print_tempfile(HEIR, $h,"\n");
114 &close_tempfile(HEIR);
117 # create_on_input(desc, [no-donthave], [no-have])
120 local @hosts = &list_software_hosts();
121 local @servers = &list_servers();
123 push(@opts, [ -1, $text{'edit_all'} ]);
124 push(@opts, [ -2, $text{'edit_donthave'} ]) if (!$_[1]);
125 push(@opts, [ -3, $text{'edit_have'} ]) if (!$_[2]);
126 local @groups = &servers::list_all_groups(\@servers);
128 foreach $h (@hosts) {
129 local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
131 push(@opts, [ $s->{'id'},
132 $s->{'desc'} || $s->{'realhost'} || $s->{'host'} ]);
133 $gothost{$s->{'host'}}++;
137 foreach $g (@groups) {
139 foreach $m (@{$g->{'members'}}) {
140 ($found++, last) if ($gothost{$m});
142 push(@opts, [ "group_$g->{'name'}",
143 &text('edit_group', $g->{'name'}) ]) if ($found);
145 local $sel = &ui_select("server", undef, \@opts);
147 print &ui_table_row($_[0], $sel);
154 # create_on_parse(prefix, &already, name)
157 local @hosts = &list_software_hosts();
158 local @servers = &list_servers();
159 if ($in{'server'} == -2) {
160 # Install on hosts that don't have it
161 local %already = map { $_->{'id'}, 1 } @{$_[1]};
162 @hosts = grep { !$already{$_->{'id'}} } @hosts;
163 print "<b>",&text($_[0].'3', $_[2]),"</b><p>\n";
165 elsif ($in{'server'} == -3) {
166 # Install on hosts that do have it
167 local %already = map { $_->{'id'}, 1 } @{$_[1]};
168 @hosts = grep { $already{$_->{'id'}} } @hosts;
169 print "<b>",&text($_[0].'6', $_[2]),"</b><p>\n";
171 elsif ($in{'server'} =~ /^group_(.*)/) {
172 # Install on members of some group
173 local ($group) = grep { $_->{'name'} eq $1 }
174 &servers::list_all_groups(\@servers);
175 @hosts = grep { local $hid = $_->{'id'};
176 local ($s) = grep { $_->{'id'} == $hid } @servers;
177 &indexof($s->{'host'}, @{$group->{'members'}}) >= 0 }
179 print "<b>",&text($_[0].'4', $_[2], $group->{'name'}),
182 elsif ($in{'server'} != -1) {
183 # Just install on one host
184 @hosts = grep { $_->{'id'} == $in{'server'} } @hosts;
185 local ($s) = grep { $_->{'id'} == $hosts[0]->{'id'} } @servers;
186 print "<b>",&text($_[0].'5', $_[2],
187 &server_name($s)),"</b><p>\n";
190 # Installing on every host
191 print "<b>",&text($_[0], join(" ", @names)),"</b><p>\n";
196 # Setup error handler for down hosts
199 $add_error_msg = join("", @_);
202 # add_managed_host(&server)
203 # Adds a new system to this module for management, and returns a status code
204 # (0 or 1) and error or information message
209 &remote_error_setup(\&add_error);
211 # Get the packages for each host
212 local %sconfig = &foreign_config("software");
213 $add_error_msg = undef;
214 local $host = { 'id' => $s->{'id'} };
215 local $soft = &remote_foreign_check($s->{'host'}, "software");
216 if ($add_error_msg) {
217 return (0, $add_error_msg);
220 return (0, &text('add_echeck', $s->{'host'}));
222 &remote_foreign_require($s->{'host'}, "software", "software-lib.pl");
223 local $rconfig = &remote_foreign_config($s->{'host'}, "software");
224 #if ($rconfig->{'package_system'} ne $sconfig{'package_system'}) {
225 # return (0, &text('add_esystem', $s->{'host'}));
227 $host->{'package_system'} = $rconfig->{'package_system'};
228 local $gconfig = &remote_foreign_config($s->{'host'}, undef);
229 foreach $g ('os_type', 'os_version',
230 'real_os_type', 'real_os_version') {
231 $host->{$g} = $gconfig->{$g};
233 local $n = &remote_foreign_call($s->{'host'}, "software",
235 local $packages = &remote_eval($s->{'host'}, "software", "\\%packages");
236 for(my $i=0; $i<$n; $i++) {
237 push(@{$host->{'packages'}},
238 { 'name' => $packages->{$i,'name'},
239 'class' => $packages->{$i,'class'},
240 'desc' => $packages->{$i,'desc'},
241 'version' => $packages->{$i,'version'},
242 'nouninstall' => $packages->{$i,'nouninstall'},
243 'nolist' => $packages->{$i,'nolist'}, });
245 &save_software_host($host);
246 return (1, &text('add_ok', &server_name($s), $n));
249 # refresh_packages(&hosts)
250 # Update the local cache with actual installed packages. Returns an array
251 # of either an error messages or two arrays of added and removed packages.
255 local @servers = &list_servers();
257 # Setup error handler for down hosts
260 $ref_error_msg = join("", @_);
262 &remote_error_setup(\&ref_error);
265 foreach my $h (@$hosts) {
266 local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
268 local ($rh = "READ$p", $wh = "WRITE$p");
274 &remote_foreign_require($s->{'host'}, "software",
276 if ($ref_error_msg) {
278 print $wh &serialise_variable($ref_error_msg);
281 local $gconfig = &remote_foreign_config($s->{'host'}, undef);
282 foreach $g ('os_type', 'os_version',
283 'real_os_type', 'real_os_version') {
284 $h->{$g} = $gconfig->{$g};
286 local @old = map { $_->{'name'} } @{$h->{'packages'}};
287 undef($h->{'packages'});
288 local $n = &remote_foreign_call($s->{'host'}, "software",
290 local $packages = &remote_eval($s->{'host'}, "software",
293 for($i=0; $i<$n; $i++) {
294 next if (!$packages->{$i,'name'});
295 push(@{$h->{'packages'}},
296 { 'name' => $packages->{$i,'name'},
297 'class' => $packages->{$i,'class'},
298 'desc' => $packages->{$i,'desc'},
299 'version' => $packages->{$i,'version'},
300 'nouninstall' => $packages->{$i,'nouninstall'},
301 'nolist' => $packages->{$i,'nolist'}, });
302 $idx = &indexof($packages->{$i,'name'}, @old);
304 push(@added, $packages->{$i,'name'});
307 splice(@old, $idx, 1);
310 &save_software_host($h);
311 $rv = [ \@added, \@old ];
314 # remove from managed list
315 &delete_software_host($h);
318 print $wh &serialise_variable($rv);
329 foreach my $h (@hosts) {
330 local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
331 local $rh = "READ$p";
333 local $rv = &unserialise_variable($line);
342 # same_package_system(&host)
343 # Returns 1 if some host is using the same package system as this master
344 sub same_package_system
347 return !$host->{'package_system'} ||
348 $host->{'package_system'} eq $software::config{'package_system'};