* Mon Mar 03 2025 Brian Read <brianr@koozali.org> 11.0.0-6.sme

- Add in SM2 NutUPS configuration and status panel [SME: 12949]
This commit is contained in:
2025-03-03 14:48:05 +00:00
parent 369074dbc3
commit 6592179f55
12 changed files with 1031 additions and 1 deletions

View File

@@ -0,0 +1,261 @@
#
# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-01-20 16:21:33
#
#
# Routines to be edited by the developer to provide content and validation for parameters
# and provison of the control data for table(s)
#
use esmith::util;
use esmith::util::network;
use esmith::ConfigDB;
use esmith::HostsDB;
use esmith::AccountsDB;
use esmith::NetworksDB;
use esmith::DomainsDB;
use constant FALSE => 0;
use constant TRUE => 1;
#The most common ones
my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
#our $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
#our $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
#our $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
#our $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
# Validation routines - parameters for each panel
sub validate_STATUS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('UPSStatus')
{$ret .= 'Validation for UPSStatus failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub validate_CONFIG {
#return "ok";
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('status')
{$ret .= 'Validation for status failed';}
if (! TRUE) #validate $c->param('Nutmode')
{$ret .= 'Validation for Nutmode failed';}
if (! TRUE) #validate $c->param('SlaveUPS_Name')
{$ret .= 'Validation for SlaveUPS_Name failed';}
if (! TRUE) #validate $c->param('MasterUPS_Name')
{$ret .= 'Validation for MasterUPS_Name failed';}
if (! TRUE) #validate $c->param('UPS_Model')
{$ret .= 'Validation for UPS_Model failed';}
if (! TRUE) #validate $c->param('UPS_Device')
{$ret .= 'Validation for UPS_Device failed';}
if (! TRUE) #validate $c->param('UPS_gen_Type')
{$ret .= 'Validation for UPS_gen_Type failed';}
if (! TRUE) #validate $c->param('UPS_gen_Mfr')
{$ret .= 'Validation for UPS_gen_Mfr failed';}
if (! TRUE) #validate $c->param('UPS_gen_Model')
{$ret .= 'Validation for UPS_gen_Model failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
# Get singleton data for each panel
sub get_data_for_panel_STATUS {
# Return a hash with the fields required which will be loaded into the shared data
my $c = shift;
my %ret = (
#'Data1'=>'Data for STATUS', #Example
# fields from Inputs in STATUS $fields['STATUS']
'UPSStatus'=>$c->get_ups_status(),
);
return %ret;
}
sub get_data_for_panel_CONFIG {
# 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");
my %ret = (
'Data1'=>'Data for CONFIG', #Example
# 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', 'ups@IPaddress'),
'MasterUPS_Name'=>$cdb->get_prop('nut', 'MasterUPS', 'ups@localhost'),
'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', ''),
);
return %ret;
}
# Get control data for table(s)
# Return hash with values from row in which link clicked on table
sub get_selected_STATUS {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
sub get_selected_CONFIG {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
#after sucessful modify or create or whatever and submit then perfom (if the params validate)
sub perform_STATUS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
my $db = $cdb; #maybe one of the others
my $dbkey = 'ChangeThis';
# 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 (! TRUE) #copy or perform with value: UPSStatus e.g. $db->set_prop($dbkey,'UPSStatus',$c->param('UPSStatus'),type=>'service'))
{$ret .= 'Perform/save failed for UPSStatus';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub perform_CONFIG {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
$cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
my $db = $cdb; #maybe one of the others
my $dbkey = 'nut';
# 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'),type=>'service'))
{$ret .= 'Perform/save failed for status';}
if (!$db->set_prop($dbkey,'mode',$c->param('Nutmode'),type=>'service'))
{$ret .= 'Perform/save failed for Nutmode';}
if (! $db->set_prop($dbkey,'SlaveUPS',$c->param('SlaveUPS_Name'),type=>'service'))
{$ret .= 'Perform/save failed for SlaveUPS_Name';}
if (! $db->set_prop($dbkey,'MasterUPS',$c->param('MasterUPS_Name'),type=>'service'))
{$ret .= 'Perform/save failed for MasterUPS_Name';}
if (! $db->set_prop($dbkey,'SlavePass',$c->param('SlaveUPS_Pass'),type=>'service'))
{$ret .= 'Perform/save failed for SlaveUPS_Pass';}
if (! $db->set_prop($dbkey,'MasterPass',$c->param('MasterUPS_Pass'),type=>'service'))
{$ret .= 'Perform/save failed for MasterUPS_Pass';}
if (! $db->set_prop($dbkey,'Model',$c->param('UPS_Model'),type=>'service'))
{$ret .= 'Perform/save failed for UPS_Model';}
if (! $db->set_prop($dbkey,'Device',$c->param('UPS_Device'),type=>'service'))
{$ret .= 'Perform/save failed for UPS_Device';}
if (! $db->set_prop($dbkey,'Type',$c->param('UPS_gen_Type'),type=>'service'))
{$ret .= 'Perform/save failed for UPS_gen_Type';}
if (! $db->set_prop($dbkey,'mfr',$c->param('UPS_gen_Mfr'),type=>'service'))
{$ret .= 'Perform/save failed for UPS_gen_Mfr';}
if (! $db->set_prop($dbkey,'mdl',$c->param('UPS_gen_Model'),type=>'service'))
{$ret .= 'Perform/save failed for UPS_gen_Model';}
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',type=>'service');
} else {
$db->set_prop($dbkey,'Master','yes',type=>'service');
}
if ($db->get_prop($dbkey,"mode",'standalone') eq 'netserver'){
$db->set_prop($dbkey,'access','private',type=>'service');
} else {
$db->set_prop($dbkey,'access','localhost',type=>'service');
}
#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) {
warn "Error executing signal-event: $!";
return "Signal-event Error occurred $!";
}
}
return $ret;
}
sub create_link{
# WIP
my ($c,$route, $panel, $index) = @_;
my $link = "$route?trt=$panel&Selected=$index";
return $link;
}
sub get_model_options {
# Execute the RPM command and capture the output
my @output = qx{rpm -ql nut | grep /usr/sbin};
# Check for errors
if ($? != 0) {
warn "Error executing command: $!";
return ['Error occurred'];
}
# Remove "/usr/sbin/" from the front of each line
s{^/usr/sbin/}{} for @output;
# Trim whitespace from each element and return the array
chomp(@output); # Remove newline characters from each line
return @output; # Return the array of modified output lines
}
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');
if ($cdb->get_prop('nut', 'status', 'disabled') eq 'disabled'){
return $c->l('nut_status_is_disabled')
} elsif ($nutmode eq 'standalone'){
return $c->get_status_from_device($cdb->get_prop('nut','MasterUPS','ups@localhost'));
} elsif ($nutmode eq 'netserver'){
return $c->get_status_from_device($cdb->get_prop('nut','MasterUPS','ups@localhost'));
} else {
return $c->get_status_from_device($cdb->get_prop('nut','SlaveUPS','apc@192.168.1.99'));
}
}
sub get_status_from_device {
my ($c,$device) = @_;
my $command = '/usr/bin/upsc';
# Check if the file exists and is executable
return ["$command Not allowed"] unless (-e $command && -x $command) ;
$result = qx{$command $device};
if ($? != 0) {
warn "Error executing $command command: $!";
return ["$command $device Error occurred $!"];
}
chomp($result);
$result = $result || 'No results';
$result = "Status from $device\n\n".$result;
return $result;
}
1;

View File

@@ -0,0 +1,312 @@
package SrvMngr::Controller::Nutups;
#
# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-01-20 16:21:33
#
#----------------------------------------------------------------------
# heading : System
# description : NutUPS configuration
# navigation : 4000 700
#
# name : nutups, method : get, url : /nutups, ctlact : Nutups#main
# name : nutupsu, method : post, url : /nutupsu, ctlact : Nutups#do_update
# name : nutupsd, method : get, url : /nutupsd, ctlact : Nutups#do_display
#
# routes : end
#
# Documentation: https://wiki.contribs.org/Nutups
#----------------------------------------------------------------------
#
# Scheme of things:
#
# TBA!!
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use constant FALSE => 0;
use constant TRUE => 1;
use Locale::gettext;
use SrvMngr::I18N;
use SrvMngr qw(theme_list init_session);
use Data::Dumper;
use esmith::util;
use esmith::util::network;
use esmith::ConfigDB;
use esmith::AccountsDB;
use esmith::NetworksDB;
use esmith::HostsDB;
use esmith::DomainsDB;
require '/usr/share/smanager/lib/SrvMngr/Controller/Nutups-Custom.pm'; #The code that is to be added by the developer
sub main {
#
# Initial entry - route is "/<whatever>"
#
#set initial panel
#for initial panel:
#Specifiy panel to enter
#load up _data hash with DB fields
#load up stash with pointer(s) to control fields hash(= get-))
#and a pointer to the prefix_data hash
#render initial panel
my $c = shift;
$c->app->log->info( $c->log_req );
#The most common ones
my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
my $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
my $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
my $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
my $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
my %nut_data = ();
my $title = $c->l('nut_NutUPS_configuration');
my $modul = '';
$nut_data{'trt'} = 'STATUS';
#Load any DB entries into the <prefix>_data area so as they are preset in the form
# which DB - this only really works if the initial panel is a PARAMS type panel and not a TABLE
my $db = $cdb; #pickup local or global db or Default to config
$c->do_display($nut_data{'trt'});
}
# Post request with params - submit from the form
sub do_update {
#
# Return after submit pushed on panel (this is a post) - route is "/<whatever>u"
# parameters in the params hash.
#
#load up all params into prefix_data hash:
#By panel (series of if statements - only one executed):
#call validate-PANEL() - return ret = ok or error message
#if validation not ok:
#render back to current panel with error message in stash
#otherwise:
#By panel (series of if statements - only one executed):
#do whatever is required: call perform-PANEL() - return "ok" or Error Message
#call signal-event for any global actions specified (check it exists - error and continue?)
#if action smeserver-<whatever>-update exists
#signal_event smeserver-<whatever>-update
#call signal-event for any specific actions for thids panel (check it exists first - error and continue)
#set success in stash
#if no "nextpanel" entry:
#set firstpanel
#else
#set nextpanel
#call render
my $c = shift;
$c->app->log->info($c->log_req);
my $modul = '';
#The most common ones - you might want to comment out any not used.
my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
my $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
my $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
my $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
my $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
my %nut_data = ();
my $title = $c->l('nut_NutUPS_configuration');
# Accessing all POST parameters
my %params = $c->req->params->to_hash;
# Get number of POST parameters
my $num_params = keys %params;
#Params are available in the hash "params" - copy to the prefix_data hash
#while (my ($key, $value) = each %{$c->req->params->to_hash}) {
# $nut_data{$key} = $value;
#}
# the value of trt will tell you which panel has returned
my $trt = $c->param('trt') || 'STATUS'; #hidden control on every form.
my $ret = 'ok';
#Validate the parameters in a custom sub one for each panel (although only one of these will be executed)
my $thispanel;
if ($trt eq 'STATUS'){
#Validate form parameters for panel STATUS
$ret = $c->validate_STATUS(\%nut_data);
$thispanel = 'STATUS';
}
if ($trt eq 'CONFIG'){
#Validate form parameters for panel CONFIG
$ret = $c->validate_CONFIG(\%nut_data);
$thispanel = 'CONFIG';
}
if ($ret ne "ok"){
$c->warn($ret);
$c->do_display($thispanel);
} else {
#Do whatever is needed, including writing values to the DB
if ($trt eq 'STATUS'){
#do whatever is required ...
$ret = $c->perform_STATUS(\%nut_data);
if ($ret ne "ok") {
# return to the panel with error message
$c->stash(error => $c->l($ret));
$c->stash(
title => $title,
modul => $modul,
nut_data => \%nut_data
);
$c->render(template => "nutups");
} else {
$c->stash( success => $c->l('nut_STATUS_panel_action_was_successful')); #A bit bland - edit it in the lex file
}
}
if ($trt eq 'CONFIG'){
#do whatever is required ...
$ret = $c->perform_CONFIG(\%nut_data);
if ($ret ne "ok") {
# return to the panel with error message
$c->stash(error => $c->l($ret));
$c->stash(
title => $title,
modul => $modul,
nut_data => \%nut_data
);
$c->render(template => "nutups");
} else {
$c->stash( success => $c->l('nut_CONFIG_panel_action_was_successful')); #A bit bland - edit it in the lex file
}
}
# and call any signal-events needed
#TBD
# Setup shared data and call panel
if ('none' eq 'none') {
$nut_data{'trt'} = 'STATUS';
} else {
$nut_data{'trt'} = 'none';
}
$c->do_display($nut_data{'trt'});
}
}
sub do_display {
#
# Return after link clicked in table (this is a get) - route is "/<whatever>d"
# Expects ?trt=PANEL&selected="TableRowName" plus any other required
#
# OR it maybe a post from the main panel to add a new record
#
#load up all supplied params into prefix_data hash
#call get-selected-PANEL() - returns hash of all relevent parameters
#load up returned hash into prefix_data
#render - to called panel
my ($c,$trt) = @_;
$c->app->log->info($c->log_req);
#The most common ones - you might want to comment out any not used.
my $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
my $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
my $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
my $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
my $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
my %nut_data = ();
my $title = $c->l('nut_NutUPS_configuration');
my $modul = "";
# Accessing all parameters
#my %params = $c->req->params->to_hash;
# Get number of parameters
#my $num_params = keys %params;
#Tag as Post or Get (ie. create new entry or edit existing one
my $is_new_record = ($c->req->method() eq 'POST');
#Params are available in the hash "params" - copy to the prefix_data hash
#while (my ($key, $value) = each %{$c->req->params->to_hash}) {
# $nut_data{$key} = $value;
#}
# the value of trt will tell you which panel has returned
if (! $trt){
$trt = $c->param('trt') || 'STATUS'; #Indicates where to go now
}
# Now add in the params from the selected row from the table
my %selectedrow;
if ($trt eq 'STATUS'){
#Validate Get selected row (if applicable) STATUS
%selectedrow = $c->get_selected_STATUS($nut_data{'Selected'},$is_new_record);
}
if ($trt eq 'CONFIG'){
#Validate Get selected row (if applicable) CONFIG
%selectedrow = $c->get_selected_CONFIG($nut_data{'Selected'},$is_new_record);
}
#Copy in the selected row params to the prefix_data hash to pass to the panel
while (my ($key, $value) = each %selectedrow){
$nut_data{$key} = $value;
}
# Where to go now
$nut_data{'trt'} = $trt;
# Set up other shared data according to the panel to go to
if ($trt eq 'STATUS'){
# pickup any other contents needed and load them into hash shared with panel
my %returned_hash;
# subroutine returns a hash directly
%returned_hash = $c->get_data_for_panel_STATUS();
# Copy each key-value pair from the returned hash to the prefix data hash
while (my ($key, $value) = each %returned_hash) {
$nut_data{$key} = $value;
}
}
if ($trt eq 'CONFIG'){
# pickup any other contents needed and load them into hash shared with panel
my %returned_hash;
# subroutine returns a hash directly
%returned_hash = $c->get_data_for_panel_CONFIG();
# Copy each key-value pair from the returned hash to the prefix data hash
while (my ($key, $value) = each %returned_hash) {
$nut_data{$key} = $value;
}
}
# and table control fields
# Data for panel
$c->stash(
title => $title,
modul => $modul,
nut_data => \%nut_data
);
$c->render(template => "nutups");
}
1;

View File

@@ -0,0 +1,30 @@
#
# Generated by SM2Gen version: SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-01-20 16:21:33
#
'nut_UPS_Generic_Model' => 'UPS Generic Model',
'nut_UPS_Model' => 'UPS Model',
'nut_Manage_Nutups-config_settings:' => 'Manage Nutups-config settings',
'nut_Descriptive_paragraph' => 'Descriptive paragraph',
'nut_MasterUPS_UPSNAME@IP' => 'UPS name@localhost',
'nut_SlaveUPS_UPSNAME@IP' => 'Slave->Master UPSNAME@IP',
'nut_MasterUPS_Password' => Local server password',
'nut_SlaveUPS_Password' => 'Slaves Password ->Server',
'nut_Nut_status' => 'Nut status',
'nut_Configure_NutUPS' => 'Configure NutUPS',
'nut_CONFIG_panel_action_was_successful' => 'CONFIG panel action was successful',
'nut_UPS_Generic_Type' => 'UPS Generic Type',
'nut_Status_(from_upsc)' => 'Status (from running upsc)',
'nut_Error_Status_Report' => 'Error Status Report',
'nut_if_Net_Server' => 'Net Server details',
'nut_if_genericups' => 'Generic UPS details',
'nut_if_Net_Client' => 'Net Client details',
'nut_STATUS_panel_action_was_successful' => 'STATUS panel action was successful',
'nut_Nut_mode' => 'Nut mode',
'nut_Status_Report' => 'Status Report',
'nut_NutUPS_configuration' => 'NutUPS configuration',
'nut_APPLY' => 'Apply',
'nut_UPS_Device' => 'UPS Device',
'nut_UPS_Generic_Manufacturer' => 'UPS Generic Manufacturer',
'nut_Status_of_the_controlled_UPS' => 'Status of the controlled UPS',
'nut_Save' => 'Save',
'nut_status_is_disabled' => 'Nut disabled',