From 4e61b36891a6703c372f1f75946faea3f800371b Mon Sep 17 00:00:00 2001 From: Jean-Philippe Pialasse Date: Sat, 17 May 2025 00:44:43 -0400 Subject: [PATCH] * Tue Mar 18 2025 Jean-Philippe Pialasse 11.0.0-9.sme - update config for Nut 2.8.2 [SME: 12657] TODO review master/slave mode TODO convert master/slave to primary/secondary TODO convert to new config data + migrate TODO event and action --- createlinks | 5 ++ .../migrate/{nutModel => 10nutModel} | 0 .../migrate/{nutPass => 15nutPass} | 0 .../db/configuration/migrate/20nutMigrate | 22 ++++++ root/etc/e-smith/events/actions/nut-config | 52 +++++++++++++ .../e-smith/events/actions/nut-config-drivers | 43 +++++++---- .../e-smith/templates/etc/ups/ups.conf/UPS | 3 +- .../templates/etc/ups/upsmon.conf/MONITOR | 24 +++--- .../lib/SrvMngr/Controller/Nutups-Custom.pm | 76 ++++++++----------- 9 files changed, 154 insertions(+), 71 deletions(-) rename root/etc/e-smith/db/configuration/migrate/{nutModel => 10nutModel} (100%) rename root/etc/e-smith/db/configuration/migrate/{nutPass => 15nutPass} (100%) create mode 100644 root/etc/e-smith/db/configuration/migrate/20nutMigrate create mode 100644 root/etc/e-smith/events/actions/nut-config diff --git a/createlinks b/createlinks index 5739148..e480223 100644 --- a/createlinks +++ b/createlinks @@ -15,7 +15,11 @@ foreach (qw(bootstrap-console-save console-save smeserver-nutUPS-update nut-conf templates2events("/etc/ups/upsd.conf", $_); } +event_link("nut-config", "post-upgrade" , "4"); +event_link("nut-config-drivers", "post-upgrade", "50"); + my $event= "nut-conf"; +event_link("nut-config", $event, "4"); event_link("nut-config-drivers", $event, "50"); event_link("systemd-default", $event, "88"); safe_symlink("try-restart", "root/etc/e-smith/events/$event/services2adjust/nut-driver\@ups"); @@ -30,5 +34,6 @@ safe_symlink("try-restart", "root/etc/e-smith/events/$event/services2adjust/nut- safe_symlink("try-restart", "root/etc/e-smith/events/$event/services2adjust/nut-monitor"); safe_symlink("try-restart", "root/etc/e-smith/events/$event/services2adjust/nut-server"); templates2events("/etc/systemd/system-preset/49-koozali.preset", $event); +event_link("nut-config", $event, "4"); event_link("nut-config-drivers", $event, "50"); event_link("systemd-default", $event, "88"); diff --git a/root/etc/e-smith/db/configuration/migrate/nutModel b/root/etc/e-smith/db/configuration/migrate/10nutModel similarity index 100% rename from root/etc/e-smith/db/configuration/migrate/nutModel rename to root/etc/e-smith/db/configuration/migrate/10nutModel diff --git a/root/etc/e-smith/db/configuration/migrate/nutPass b/root/etc/e-smith/db/configuration/migrate/15nutPass similarity index 100% rename from root/etc/e-smith/db/configuration/migrate/nutPass rename to root/etc/e-smith/db/configuration/migrate/15nutPass diff --git a/root/etc/e-smith/db/configuration/migrate/20nutMigrate b/root/etc/e-smith/db/configuration/migrate/20nutMigrate new file mode 100644 index 0000000..ce91957 --- /dev/null +++ b/root/etc/e-smith/db/configuration/migrate/20nutMigrate @@ -0,0 +1,22 @@ +{ + # migrate to SME11 + # first we get ride of Master=no + # if Mode not set then set it according to Master + my $mnut = $DB->get('nut') || $DB->new_record('nut', {type => 'service'}); + my $nutMaster = $DB->get_prop_and_delete ('nut','Master'); + unless ( ${'nut'}{'Mode'} ) { + $mnut>set_prop('Mode','netserver') if $nutMaster eq "yes"; + } + + # if nut-driver@ups not existing create it + my $driverUPS = $DB->get('nut-driver@ups') || $DB->new_record('nut-driver@ups', {type => 'service'}); + # then migrate to nut-driver@ups (first UPS) the following properties + my $mdl = $DB->get_prop_and_delete ('nut','mdl'); + $driverUPS->set_prop('mdl',$mdl) unless ${'nut-driver@ups'}{'mdl'}; + my $mfr = $DB->get_prop_and_delete ('nut','mfr'); + $driverUPS->set_prop('mfr',$mfr) unless ${'nut-driver@ups'}{'mfr'}; + my $model = $DB->get_prop_and_delete ('nut','Model'); + $driverUPS->set_prop('Model',$model) unless ${'nut-driver@ups'}{'Model'}; + my $Type = $DB->get_prop_and_delete ('nut','Type'); + $driverUPS->set_prop('Type',$model) unless ${'nut-driver@ups'}{'Type'}; +} diff --git a/root/etc/e-smith/events/actions/nut-config b/root/etc/e-smith/events/actions/nut-config new file mode 100644 index 0000000..d0b7734 --- /dev/null +++ b/root/etc/e-smith/events/actions/nut-config @@ -0,0 +1,52 @@ +#!/bin/bash + +# this script is called before template-expand +# then another script is called to run /usr/libexec/nut-driver-enumerator.sh and start/stop drivers +# then traditionnal services2adjust + +# calling this script to configure unit and drivers +if [[ $(/sbin/e-smith/config getprop nut status || echo "disabled") == "disabled" ]] ; then + # disable server + /sbin/e-smith/config setprop nut-server status disabled + /sbin/e-smith/config setprop nut access localhost + # disable monitor + /sbin/e-smith/config setprop nut-monitor status disabled + # TODO get_all_by_prop filter nut-driver@ and foreach $UPS disabled + #config keys|grep nut-driver + for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver) + do + /sbin/e-smith/config setprop $OUTPUT status disabled + done + + exit 0; +fi +# nut is enabled + +# we set services depending on Mode +Mode=$(/sbin/e-smith/config getprop nut Mode || echo "standelone") +if [[ $Mode == "netserver" ]] ; then + /sbin/e-smith/config setprop nut-server status enabled + /sbin/e-smith/config setprop nut access private +elif [[ $Mode == "netclient" ]] ; then + /sbin/e-smith/config setprop nut-server status disabled + /sbin/e-smith/config setprop nut access localhost +else + /sbin/e-smith/config setprop nut-server status enabled + /sbin/e-smith/config setprop nut access localhost +fi +# enable nut-monitor +/sbin/e-smith/config setprop nut-monitor status enabled + +# get_all_by_prop filter nut-driver@ and foreach $UPS enable. +if [[ $Mode == "netclient" ]] ; then + for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver) + do + /sbin/e-smith/config setprop $OUTPUT status disabled + done +else + for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver) + do + /sbin/e-smith/config setprop $OUTPUT status enabled + done +fi + diff --git a/root/etc/e-smith/events/actions/nut-config-drivers b/root/etc/e-smith/events/actions/nut-config-drivers index 8747c88..550fa8e 100644 --- a/root/etc/e-smith/events/actions/nut-config-drivers +++ b/root/etc/e-smith/events/actions/nut-config-drivers @@ -1,18 +1,33 @@ #!/bin/bash -#if MODE = netserver then access should be private -#if MODE = netclient only enable monitor - -# calling this script to configure unit and drivers -if [[ $(/sbin/e-smith/config getprop nut status || echo "disabled") == "disabled" ]] ; then - # disable server - # leave monitor as is, we might want to monitor an external ups ? - # get_all_by_prop filter nut-driver@ and foreach $UPS disabled - exit 0; -fi -# nut is enabled -# enable nut-monitor -# enable nut-server "enable nut-server.service\n" if ($master eq 'yes'); -# get_all_by_prop filter nut-driver@ and foreach $UPS enable. +# another script is called before template-expand to set services +# this script is called to run /usr/libexec/nut-driver-enumerator.sh and start/stop drivers +# then traditionnal services2adjust +# enumerate / configure drivers /usr/libexec/nut-driver-enumerator.sh + +event=$1 + +if [[ $i == "post-upgrade" ]] ; then + exit 0 +fi + +if [[ $i == "bootstrap-console-save" ]] ; then + exit 0 +fi + +if [[ $(/sbin/e-smith/config getprop nut status || echo "disabled") == "disabled" ]] ; then + # if disabled stop them + for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver) + do + /usr/bin/systemctl stop $OUTPUT + done + exit 0 +fi + +# if we are there we want to restart / start them +for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver) + do + /usr/bin/systemctl restart $OUTPUT +done diff --git a/root/etc/e-smith/templates/etc/ups/ups.conf/UPS b/root/etc/e-smith/templates/etc/ups/ups.conf/UPS index 70a0294..f42a18c 100644 --- a/root/etc/e-smith/templates/etc/ups/ups.conf/UPS +++ b/root/etc/e-smith/templates/etc/ups/ups.conf/UPS @@ -3,12 +3,13 @@ foreach my $ups ( sort {$a cmp $b} @upses ) { my $nnut = ${$ups}; + my ($name )= $ups =~ /^nut-driver\@(.*)$/ ; my $model = $nnut{Model} || "usbhid-ups"; my $device = $nnut{Device} || "auto"; #"/var/lib/ups/hiddev0"; my $type = $nnut{Type}; my $mfr = $nnut{mfr}; my $mdl = $nnut{mdl}; - $OUT .= "[UPS]\n"; + $OUT .= "[$name]\n"; $OUT .= "\tdriver = $model\n"; if ($model eq 'genericups') { diff --git a/root/etc/e-smith/templates/etc/ups/upsmon.conf/MONITOR b/root/etc/e-smith/templates/etc/ups/upsmon.conf/MONITOR index f64f747..da6cd50 100644 --- a/root/etc/e-smith/templates/etc/ups/upsmon.conf/MONITOR +++ b/root/etc/e-smith/templates/etc/ups/upsmon.conf/MONITOR @@ -1,14 +1,14 @@ { - if ( ($nut{Mode} || 'standalone') eq 'netclient' ) { - $OUT = "MONITOR $nut{ClientUPS} 1 $nut{ClientUser} $nut{ClientPass} secondary"; - } else { - my @upses = grep(/^nut-driver\@[a-zA-Z0-9_-]+$/, $DB->keys); - my $size = scalar(@upses); - foreach my $ups ( sort {$a cmp $b} @upses ) - { - my ($upsname) = $ups =~ /^nut-driver\@([a-zA-Z0-9_-]+)$/ ; - $OUT .= "MONITOR $upsname\@localhost 1 upsprimary $nut{PrimaryPass} primary"; - $OUT .= "\n" if --$size >=1; - } - } + if ( ($nut{Mode} || 'standalone') eq 'netclient' ) { + $OUT = "MONITOR $nut{ClientUPS} 1 $nut{ClientUser} $nut{ClientPass} secondary"; + } else { + my @upses = grep(/^nut-driver\@[a-zA-Z0-9_-]+$/, $DB->keys); + my $size = scalar(@upses); + foreach my $ups ( sort {$a cmp $b} @upses ) + { + my ($upsname) = $ups =~ /^nut-driver\@([a-zA-Z0-9_-]+)$/ ; + $OUT .= "MONITOR $upsname\@localhost 1 upsprimary $nut{PrimaryPass} primary"; + $OUT .= "\n" if --$size >=1; + } + } } diff --git a/root/usr/share/smanager/lib/SrvMngr/Controller/Nutups-Custom.pm b/root/usr/share/smanager/lib/SrvMngr/Controller/Nutups-Custom.pm index be1e35b..53151dc 100644 --- a/root/usr/share/smanager/lib/SrvMngr/Controller/Nutups-Custom.pm +++ b/root/usr/share/smanager/lib/SrvMngr/Controller/Nutups-Custom.pm @@ -49,11 +49,11 @@ my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); {$ret .= 'Validation for status failed';} if (! TRUE) #validate $c->param('Nutmode') {$ret .= 'Validation for Nutmode failed';} -#do we want the Slave validated ? we should just display read only + #do we want the Slave validated ? we should just display read only if (! TRUE) #validate $c->param('SlaveUPS_Name') {$ret .= 'Validation for SlaveUPS_Name failed';} - if (! TRUE) #validate $c->param('ClientUPS_Name') - {$ret .= 'Validation for ClientUPS_Name failed';} + if (! TRUE) #validate $c->param('ClientUPS_Name') + {$ret .= 'Validation for ClientUPS_Name failed';} if (! TRUE) #validate $c->param('MasterUPS_Name') {$ret .= 'Validation for MasterUPS_Name failed';} if (! TRUE) #validate $c->param('UPS_Model') @@ -76,14 +76,14 @@ my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); sub get_data_for_panel_STATUS { # Return a hash with the fields required which will be loaded into the shared data my $c = shift; - $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); + $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); my %ret = ( #'Data1'=>'Data for STATUS', #Example # fields from Inputs in STATUS $fields['STATUS'] 'UPSStatus'=>$c->get_ups_status(), 'ups_data' => $c->get_ups_status_as_hash(), - 'Nutmode'=>$cdb->get_prop('nut', 'mode')||"none", - 'localip'=>$cdb->get_value('LocalIP'), + 'Nutmode'=>$cdb->get_prop('nut', 'Mode')||"none", + 'localip'=>$cdb->get_value('LocalIP'), 'SlaveUPS_Pass'=>$cdb->get_prop('nut', 'SecondaryPass'), ); return %ret; @@ -95,22 +95,21 @@ my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); my %ret = ( 'Data1'=>'Data for CONFIG', #Example - 'localip'=>$cdb->get_value('LocalIP'), + 'localip'=>$cdb->get_value('LocalIP'), # fields from Inputs in CONFIG $fields['CONFIG'] - 'status'=>$cdb->get_prop('nut', 'status', 'disabled'), - 'Nutmode'=>$cdb->get_prop('nut', 'mode', 'standalone'), - #'SlaveUPS_Name'=>$cdb->get_prop('nut', 'SlaveUPS'), - 'ClientUPS_Name'=>$cdb->get_prop('nut', 'ClientUPS'), - 'ClientUPS_User'=>$cdb->get_prop('nut', 'ClientUser'), - 'ClientUPS_Pass'=>$cdb->get_prop('nut', 'ClientPass'), - 'SlaveUPS_Pass'=>$cdb->get_prop('nut', 'SecondaryPass'), + 'status'=>$cdb->get_prop('nut', 'status') || 'disabled', + 'Nutmode'=>$cdb->get_prop('nut', 'Mode') || 'standalone', + 'ClientUPS_Name'=>$cdb->get_prop('nut', 'ClientUPS'), + 'ClientUPS_User'=>$cdb->get_prop('nut', 'ClientUser'), + 'ClientUPS_Pass'=>$cdb->get_prop('nut', 'ClientPass'), + 'SlaveUPS_Pass'=>$cdb->get_prop('nut', 'SecondaryPass'), 'MasterUPS_Name'=>$cdb->get_prop('nut', 'MasterUPS'), - # TODO migrate to nut-driver@ups entry - 'UPS_Model'=>$cdb->get_prop('nut', 'Model') || 'usbhid-ups', - 'UPS_Device'=>$cdb->get_prop('nut', 'Device')|| 'auto', - 'UPS_gen_Type'=>$cdb->get_prop('nut', 'Type', ''), - 'UPS_gen_Mfr'=>$cdb->get_prop('nut', 'mfr', ''), - 'UPS_gen_Model'=>$cdb->get_prop('nut', 'mdl', ''), + # nut-driver@ups entry + 'UPS_Model'=>$cdb->get_prop('nut-driver@ups', 'Model') || 'usbhid-ups', + 'UPS_Device'=>$cdb->get_prop('nut-driver@ups', 'Device')|| 'auto', + 'UPS_gen_Type'=>$cdb->get_prop('nut-driver@ups', 'Type'), + 'UPS_gen_Mfr'=>$cdb->get_prop('nut-driver@ups', 'mfr'), + 'UPS_gen_Model'=>$cdb->get_prop('nut-driver@ups', 'mdl'), ); return %ret; } @@ -163,35 +162,35 @@ my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); my $db = $cdb; #maybe one of the others my $dbkey = 'nut'; - my $driverkey = 'nut-driver@ups'; + my $driverkey = 'nut-driver@ups'; # To make it write to DB as comment, delete this (regex) string in each if statement "TRUE\) \#copy or perform with value: .* e.g." if (!$db->set_prop($dbkey,'status',$c->param('status'))) {$ret .= 'Perform/save failed for status';} - if (!$db->set_prop($dbkey,'mode',$c->param('Nutmode'))) + if (!$db->set_prop($dbkey,'Mode',$c->param('Nutmode'))) {$ret .= 'Perform/save failed for Nutmode';} - # let only save info regarding the current mode - # if client mode + # let only save info regarding the current mode + # if client mode if (! $db->set_prop($dbkey,'ClientUPS',$c->param('ClientUPS_Name'))) {$ret .= 'Perform/save failed for ClientUPS_Name';} if (! $db->set_prop($dbkey,'MasterUPS',$c->param('MasterUPS_Name'))) {$ret .= 'Perform/save failed for MasterUPS_Name';} - if (! $db->set_prop($dbkey,'ClientUser',$c->param('ClientUPS_User'))) - {$ret .= 'Perform/save failed for ClientUPS_USer';} + if (! $db->set_prop($dbkey,'ClientUser',$c->param('ClientUPS_User'))) + {$ret .= 'Perform/save failed for ClientUPS_USer';} if (! $db->set_prop($dbkey,'ClientPass',$c->param('ClientUPS_Pass'))) {$ret .= 'Perform/save failed for ClientUPS_Pass';} - # if not client mode - if (! $db->set_prop($dbkey,'Model',$c->param('UPS_Model'))) + # if not client mode + if (! $db->set_prop($driverkey,'Model',$c->param('UPS_Model'))) {$ret .= 'Perform/save failed for UPS_Model';} - if (! $db->set_prop($dbkey,'Device',$c->param('UPS_Device'))) + if (! $db->set_prop($driverkey,'Device',$c->param('UPS_Device'))) {$ret .= 'Perform/save failed for UPS_Device';} - if (! $db->set_prop($dbkey,'Type',$c->param('UPS_gen_Type'))) + if (! $db->set_prop($driverkey,'Type',$c->param('UPS_gen_Type'))) {$ret .= 'Perform/save failed for UPS_gen_Type';} - if (! $db->set_prop($dbkey,'mfr',$c->param('UPS_gen_Mfr'))) + if (! $db->set_prop($driverkey,'mfr',$c->param('UPS_gen_Mfr'))) {$ret .= 'Perform/save failed for UPS_gen_Mfr';} - if (! $db->set_prop($dbkey,'mdl',$c->param('UPS_gen_Model'))) + if (! $db->set_prop($driverkey,'mdl',$c->param('UPS_gen_Model'))) {$ret .= 'Perform/save failed for UPS_gen_Model';} #changes from sme10 to sme11 @@ -208,17 +207,6 @@ my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); if ($ret eq "") { $ret = 'ok'; - #and set Master and access properties according to values - if ($db->get_prop($dbkey,"mode",'standalone') eq 'netclient'){ - $db->set_prop($dbkey,'Master','no'); - } else { - $db->set_prop($dbkey,'Master','yes'); - } - if ($db->get_prop($dbkey,"mode",'standalone') eq 'netserver'){ - $db->set_prop($dbkey,'access','private'); - } else { - $db->set_prop($dbkey,'access','localhost'); - } #And run signal-event to apply templates for config files and start task. my @result = qx{/usr/sbin/e-smith/signal-event smeserver-nutUPS-update}; if ($? != 0) { @@ -259,7 +247,7 @@ sub get_model_options { sub get_ups_status { my $c = shift; $cdb = esmith::ConfigDB->open() || die("Couldn't open config db"); - my $nutmode = $cdb->get_prop('nut', 'mode','standalone'); + my $nutmode = $cdb->get_prop('nut', 'Mode','standalone'); if ($cdb->get_prop('nut', 'status', 'disabled') eq 'disabled'){ return $c->l('nut_status_is_disabled') } elsif ($nutmode eq 'netclient'){