--- loncom/interface/domainprefs.pm 2012/09/25 20:38:47 1.160.6.6
+++ loncom/interface/domainprefs.pm 2012/09/25 23:20:42 1.160.6.7
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Handler to set domain-wide configuration settings
#
-# $Id: domainprefs.pm,v 1.160.6.6 2012/09/25 20:38:47 raeburn Exp $
+# $Id: domainprefs.pm,v 1.160.6.7 2012/09/25 23:20:42 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -217,7 +217,11 @@ sub handler {
'usercreation','usermodification','scantron',
'requestcourses','requestauthor','coursecategories',
'serverstatuses','usersessions');
- if (keys(%servers) > 1) {
+ my %existing;
+ if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
+ %existing = %{$domconfig{'loadbalancing'}};
+ }
+ if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
push(@prefs_order,'loadbalancing');
}
my %prefs = (
@@ -360,9 +364,9 @@ sub handler {
col2 => 'Rules'}],
},
'loadbalancing' =>
- {text => 'Dedicated Load Balancer',
+ {text => 'Dedicated Load Balancer(s)',
help => 'Domain_Configuration_Load_Balancing',
- header => [{col1 => 'Server',
+ header => [{col1 => 'Balancers',
col2 => 'Default destinations',
col3 => 'User affliation',
col4 => 'Overrides'},
@@ -390,10 +394,11 @@ sub handler {
&Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
} elsif ($phase eq 'display') {
my $js = &recaptcha_js();
- if (keys(%servers) > 1) {
+ if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
my ($othertitle,$usertypes,$types) =
&Apache::loncommon::sorted_inst_types($dom);
- $js .= &lonbalance_targets_js($dom,$types,\%servers).
+ $js .= &lonbalance_targets_js($dom,$types,\%servers,
+ $domconfig{'loadbalancing'}).
&new_spares_js().
&common_domprefs_js().
&Apache::loncommon::javascript_array_indexof();
@@ -2849,16 +2854,13 @@ sub print_loadbalancing {
my $numinrow = 1;
my $datatable;
my %servers = &Apache::lonnet::internet_dom_servers($dom);
- my ($currbalancer,$currtargets,$currrules);
- if (keys(%servers) > 1) {
- if (ref($settings) eq 'HASH') {
- $currbalancer = $settings->{'lonhost'};
- $currtargets = $settings->{'targets'};
- $currrules = $settings->{'rules'};
- } else {
- ($currbalancer,$currtargets) =
- &Apache::lonnet::get_lonbalancer_config(\%servers);
- }
+ my (%currbalancer,%currtargets,%currrules,%existing);
+ if (ref($settings) eq 'HASH') {
+ %existing = %{$settings};
+ }
+ if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
+ &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
+ \%currtargets,\%currrules);
} else {
return;
}
@@ -2868,104 +2870,191 @@ sub print_loadbalancing {
if (ref($types) eq 'ARRAY') {
$rownum += scalar(@{$types});
}
- my $css_class = ' class="LC_odd_row"';
- my $targets_div_style = 'display: none';
- my $disabled_div_style = 'display: block';
- my $homedom_div_style = 'display: none';
- $datatable = '
'.
- ''.
- ' | '.
- ' '.&mt('No dedicated Load Balancer').' '."\n".
- ''.&mt('Offloads to:').' ';
- my ($numspares,@spares) = &count_servers($currbalancer,%servers);
- my @sparestypes = ('primary','default');
- my %typetitles = &sparestype_titles();
- foreach my $sparetype (@sparestypes) {
- my $targettable;
- for (my $i=0; $i<$numspares; $i++) {
- my $checked;
- if (ref($currtargets) eq 'HASH') {
- if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
- if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
- $checked = ' checked="checked"';
+ my @css_class = ('LC_odd_row','LC_even_row');
+ my $balnum = 0;
+ my $islast;
+ my (@toshow,$disabledtext);
+ if (keys(%currbalancer) > 0) {
+ @toshow = sort(keys(%currbalancer));
+ if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
+ push(@toshow,'');
+ }
+ } else {
+ @toshow = ('');
+ $disabledtext = &mt('No existing load balancer');
+ }
+ foreach my $lonhost (@toshow) {
+ if ($balnum == scalar(@toshow)-1) {
+ $islast = 1;
+ } else {
+ $islast = 0;
+ }
+ my $cssidx = $balnum%2;
+ my $targets_div_style = 'display: none';
+ my $disabled_div_style = 'display: block';
+ my $homedom_div_style = 'display: none';
+ $datatable .= ' '.
+ ''.
+ ' ';
+ if ($lonhost eq '') {
+ $datatable .= '';
+ if (keys(%currbalancer) > 0) {
+ $datatable .= &mt('Add balancer:');
+ } else {
+ $datatable .= &mt('Enable balancer:');
+ }
+ $datatable .= ' '.
+ ''."\n".
+ ' '."\n";
+ } else {
+ $datatable .= ''.$lonhost.' '.
+ ''.
+ '';
+ $targets_div_style = 'display: block';
+ $disabled_div_style = 'display: none';
+ if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
+ $homedom_div_style = 'display: block';
+ }
+ }
+ $datatable .= ' | '.
+ ' '.$disabledtext.' '."\n".
+ ''.&mt('Offloads to:').' ';
+ my ($numspares,@spares) = &count_servers($lonhost,%servers);
+ my @sparestypes = ('primary','default');
+ my %typetitles = &sparestype_titles();
+ foreach my $sparetype (@sparestypes) {
+ my $targettable;
+ for (my $i=0; $i<$numspares; $i++) {
+ my $checked;
+ if (ref($currtargets{$lonhost}) eq 'HASH') {
+ if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
+ if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
+ $checked = ' checked="checked"';
+ }
+ }
+ }
+ my ($chkboxval,$disabled);
+ if (($lonhost ne '') && (exists($servers{$lonhost}))) {
+ $chkboxval = $spares[$i];
+ }
+ if (exists($currbalancer{$spares[$i]})) {
+ $disabled = ' disabled="disabled"';
+ }
+ $targettable .=
+ ' | | ';
+ my $rem = $i%($numinrow);
+ if ($rem == 0) {
+ if (($i > 0) && ($i < $numspares-1)) {
+ $targettable .= ' ';
+ }
+ if ($i < $numspares-1) {
+ $targettable .= ' ';
}
}
}
- my $chkboxval;
- if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
- $chkboxval = $spares[$i];
- }
- $targettable .= ' | ';
- my $rem = $i%($numinrow);
- if ($rem == 0) {
- if ($i > 0) {
- $targettable .= ' ';
- }
- $targettable .= ' ';
+ if ($targettable ne '') {
+ my $rem = $numspares%($numinrow);
+ my $colsleft = $numinrow - $rem;
+ if ($colsleft > 1 ) {
+ $targettable .= ''.
+ ' | ';
+ } elsif ($colsleft == 1) {
+ $targettable .= ' | ';
+ }
+ $datatable .= ''.$typetitles{$sparetype}.' '.
+ ' ';
+ }
+ }
+ my $cssidx = $balnum%2;
+ $datatable .= ' '.
+ &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
+ $othertitle,$usertypes,$types,\%servers,
+ \%currbalancer,$lonhost,
+ $targets_div_style,$homedom_div_style,
+ $css_class[$cssidx],$balnum,$islast);
+ $$rowtotal += $rownum;
+ $balnum ++;
+ }
+ $datatable .= ' ';
+ return $datatable;
+}
+
+sub get_loadbalancers_config {
+ my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
+ return unless ((ref($servers) eq 'HASH') &&
+ (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
+ (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
+ if (keys(%{$existing}) > 0) {
+ my $oldlonhost;
+ foreach my $key (sort(keys(%{$existing}))) {
+ if ($key eq 'lonhost') {
+ $oldlonhost = $existing->{'lonhost'};
+ $currbalancer->{$oldlonhost} = 1;
+ } elsif ($key eq 'targets') {
+ if ($oldlonhost) {
+ $currtargets->{$oldlonhost} = $existing->{'targets'};
+ }
+ } elsif ($key eq 'rules') {
+ if ($oldlonhost) {
+ $currrules->{$oldlonhost} = $existing->{'rules'};
+ }
+ } elsif (ref($existing->{$key}) eq 'HASH') {
+ $currbalancer->{$key} = 1;
+ $currtargets->{$key} = $existing->{$key}{'targets'};
+ $currrules->{$key} = $existing->{$key}{'rules'};
}
}
- if ($targettable ne '') {
- my $rem = $numspares%($numinrow);
- my $colsleft = $numinrow - $rem;
- if ($colsleft > 1 ) {
- $targettable .= ' '.
- ' | ';
- } elsif ($colsleft == 1) {
- $targettable .= ' | ';
+ } else {
+ my ($balancerref,$targetsref) =
+ &Apache::lonnet::get_lonbalancer_config($servers);
+ if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
+ foreach my $server (sort(keys(%{$balancerref}))) {
+ $currbalancer->{$server} = 1;
+ $currtargets->{$server} = $targetsref->{$server};
}
- $datatable .= ' '.$typetitles{$sparetype}.''.
- ' ';
}
}
- $datatable .= ' |
'.
- &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
- $usertypes,$types,\%servers,$currbalancer,
- $targets_div_style,$homedom_div_style,$css_class);
- $$rowtotal += $rownum;
- return $datatable;
+ return;
}
sub loadbalancing_rules {
my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
- $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
+ $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
+ $css_class,$balnum,$islast) = @_;
my $output;
- my ($alltypes,$othertypes,$titles) =
+ my $num = 0;
+ my ($alltypes,$othertypes,$titles) =
&loadbalancing_titles($dom,$intdom,$usertypes,$types);
if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
foreach my $type (@{$alltypes}) {
+ $num ++;
my $current;
if (ref($currrules) eq 'HASH') {
$current = $currrules->{$type};
}
if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
- if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
+ if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
$current = '';
}
}
$output .= &loadbalance_rule_row($type,$titles->{$type},$current,
- $servers,$currbalancer,$dom,
- $targets_div_style,$homedom_div_style,$css_class);
+ $servers,$currbalancer,$lonhost,$dom,
+ $targets_div_style,$homedom_div_style,
+ $css_class,$balnum,$num,$islast);
}
}
return $output;
@@ -3002,8 +3091,8 @@ sub loadbalancing_titles {
}
sub loadbalance_rule_row {
- my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
- $homedom_div_style,$css_class) = @_;
+ my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
+ $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
my @rulenames = ('default','homeserver');
my %ruletitles = &offloadtype_text();
if ($type eq '_LC_external') {
@@ -3016,9 +3105,15 @@ sub loadbalance_rule_row {
if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
$style = $homedom_div_style;
}
- my $output =
- ''.$title.' | '."\n".
- ''."\n";
+ my $space;
+ if ($islast && $num == 1) {
+ $space = ' ';
+ }
+ my $output =
+ ' '.$space.
+ ' '.$title.' | '."\n".
+ ''.$space.
+ ''."\n";
for (my $i=0; $i<@rulenames; $i++) {
my $rule = $rulenames[$i];
my ($checked,$extra);
@@ -3034,17 +3129,20 @@ sub loadbalance_rule_row {
unless ($checked) {
$default = ' selected="selected"';
}
- $extra = ': | |