Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cockpit. added ui & api #10

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.rpm
*.tar.gz
/ui/node_modules
/ui/dist

3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -23,6 +23,9 @@ env:
-e ENDPOINTS_PACK
"

install:
- bash prep-sources &> /dev/null

script: >
docker run -ti --name makerpms ${EVARS}
--hostname b${TRAVIS_BUILD_NUMBER}.nethserver.org
Expand Down
121 changes: 121 additions & 0 deletions api/dashboard/read
@@ -0,0 +1,121 @@
#!/usr/bin/python

#
# Copyright (C) 2019 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see COPYING.
#

import simplejson
import sys
import subprocess
import fileinput

def read_config(config_name):
bash_command = "/sbin/e-smith/config getjson %s" % config_name
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
config_json = simplejson.loads(output)
return config_json

def read_clients():
nut_monitor_config = read_config('nut-monitor')
master = nut_monitor_config['props']['Master']

if master:
return []

nut_server_config = read_config('nut-server')
ups_name = nut_server_config['props']['Ups']

if not ups_name:
ups_name = 'UPS'

bash_command = "timeout 2 /usr/bin/upsc -c %s" % ups_name
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if error:
raise Exception("Error invoking /usr/bin/upsc: " + error.strip())

lines = output.split("\n")
clients = []

# every line contains an IP address
for ip_address in lines:
if ip_address:
bash_command = "timeout 2 /usr/bin/host %s" % ip_address
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if error:
raise Exception("Error invoking /usr/bin/host: " + error.strip())

if 'not found' in output:
clients.append(ip_address)
else:
hostname = output.split()[-1]
# remove trailing dot from hostname
if hostname[-1] == '.':
hostname = hostname[:-1]

clients.append(hostname)

return clients

def read_status():
nut_server_config = read_config('nut-server')
ups_name = nut_server_config['props']['Ups']

if not ups_name:
ups_name = 'UPS'

nut_server_status = nut_server_config['props']['status']

if nut_server_status == 'enabled':
ups_server = 'localhost'
else:
nut_monitor_config = read_config('nut-monitor')
ups_server = nut_monitor_config['props']['Master']

bash_command = "timeout 2 /usr/bin/upsc %s@%s" % (ups_name, ups_server)
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if error:
raise Exception("Error invoking /usr/bin/upsc: " + error.strip())

lines = output.split("\n")
status = {}

for line in lines:
tokens = line.split(": ")

if (len(tokens) > 1):
prop_name = tokens[0].replace(".", "_").replace("-", "_")
prop_value = tokens[1]
status[prop_name] = prop_value

return status

try:
status = read_status()
clients = read_clients()
nut_server_config = read_config('nut-server')
nut_monitor_config = read_config('nut-monitor')
output = simplejson.dumps({ 'status': status, 'clients': clients, 'configuration': { 'nut_server': nut_server_config, 'nut_monitor': nut_monitor_config }})
print (output)
except Exception, e:
print simplejson.dumps({ 'error': "%s" % e })
sys.exit(1)
112 changes: 112 additions & 0 deletions api/settings/read
@@ -0,0 +1,112 @@
#!/usr/bin/python

#
# Copyright (C) 2019 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see COPYING.
#

import simplejson
import sys
import subprocess
import fileinput

class UpsModel:
def __init__(self, description, model_name, manufacturer, support_level, drivers):
self.description = description
self.model_name = model_name
self.manufacturer = manufacturer
self.support_level = support_level
self.drivers = set([])
self.add_drivers(drivers)

def add_drivers(self, drivers):
for driver in drivers:
self.drivers.add(driver)

def read_models():
lines = [line.rstrip('\n') for line in open('/usr/share/nut/driver.list')]
models_drivers = {}
models = []

for line in lines:
driver_params = line.split('\t')

if len(driver_params) < 6:
continue

# skip comments
if line[0] == '#':
continue

driver_string = driver_params[5].replace('"', '').replace("'", "")
drivers = driver_string.split(" or ")

manufacturer = driver_params[0].replace('"', '')
model_name = driver_params[3].replace('"', '')
support_level_num = int(driver_params[2].replace('"', ''))
support_level_stars = ''

for i in range(support_level_num):
support_level_stars += '*'

description = manufacturer + ' - ' + model_name + ' (' + support_level_stars + ')'

# some models in /usr/share/nut/driver.list are duplicated or have multiple drivers
if not description in models_drivers:
ups_model = UpsModel(description, model_name, manufacturer, support_level_num, drivers)
models_drivers[description] = ups_model
else:
ups_model = models_drivers[description]

ups_model.add_drivers(drivers)

for description, ups_model in models_drivers.iteritems():
model_json_string = '{ "model_name": "%s", "manufacturer": "%s", "drivers": %s, "description": "%s", "support_level": "%d" }' % \
(ups_model.model_name, ups_model.manufacturer, list(ups_model.drivers), ups_model.description, ups_model.support_level)
model_json_string = model_json_string.replace("'", '"')
model_json = simplejson.loads(model_json_string)
models.append(model_json)

return models

def read_config(config_name):
bash_command = "/sbin/e-smith/config getjson %s" % config_name
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
config_json = simplejson.loads(output)
return config_json

try:
line = fileinput.input()[0].rstrip()
input_json = simplejson.loads(line)
app_info = input_json["app_info"]

if app_info == 'configuration':
nut_server_config = read_config('nut-server')
nut_monitor_config = read_config('nut-monitor')
output = simplejson.dumps({'configuration': { 'nut_server': nut_server_config, 'nut_monitor': nut_monitor_config } })
print (output)
elif app_info == 'models':
models = read_models()
output = simplejson.dumps({ 'models': models })
print (output)
else:
raise ValueError('app_info must be one of \'configuration\' or \'models\'')
except Exception, e:
print simplejson.dumps({ 'error': "%s" % e })
sys.exit(1)
59 changes: 59 additions & 0 deletions api/settings/update
@@ -0,0 +1,59 @@
#!/bin/bash

#
# Copyright (C) 2019 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see COPYING.
#

. /usr/libexec/nethserver/api/lib/helper_functions

# Read JSON from stdin and set the record
data=$(cat /dev/stdin)

nutserver_status=$(echo $data | jq -r '.configuration.nut_server.props.status')
access=$(echo $data | jq -r '.configuration.nut_server.props.access')
user=$(echo $data | jq -r '.configuration.nut_server.props.User')
tcpport=$(echo $data | jq -r '.configuration.nut_server.props.TCPPort')
device=$(echo $data | jq -r '.configuration.nut_server.props.Device')
model=$(echo $data | jq -r '.configuration.nut_server.props.Model')
password=$(echo $data | jq -r '.configuration.nut_server.props.Password')
ups=$(echo $data | jq -r '.configuration.nut_server.props.Ups')

nutmonitor_status=$(echo $data | jq -r '.configuration.nut_monitor.props.status')
master=$(echo $data | jq -r '.configuration.nut_monitor.props.Master')
notify=$(echo $data | jq -r '.configuration.nut_monitor.props.Notify')

/sbin/e-smith/config setprop nut-server status "$nutserver_status" access "$access" \
User "$user" TCPPort "$tcpport" Device "$device" Model "$model" Password "$password" Ups "$ups"

if [ $? -gt 0 ]; then
invalid_error
fi

/sbin/e-smith/config setprop nut-monitor status "$nutmonitor_status" Master "$master" Notify "$notify"

if [ $? -gt 0 ]; then
invalid_error
fi

/sbin/e-smith/signal-event -j nethserver-nut-save
if [ $? -gt 0 ]; then
error "EventFailed" "See /var/log/messages"
else
success
fi