Browse Source

Release Lightning-rod v0.4.5:

- Added REST server for local LR management
 - Modules loader updated:
   - Single module loader added
 - Service Manager improved:
   - Add checks status for WSTUN server
   - Cleaning procedures added for WSTUN monitor threads
   - services.json backup management fixed
 - Webservice Manager updated:
   - NGINX listening ports changed
 - Docker procedures updated
 - Installation procedures updated
 - Requirements fixed: Flask

Change-Id: I73fdb55a2ea58d0f7a76eab78734d067133438c5
changes/66/629566/3
Nicola Peditto 4 years ago
parent
commit
c2d6ecf0f5
  1. 3
      MANIFEST.in
  2. 24
      doc/installation/docker/rpi_docker.md
  3. 24
      doc/installation/docker/x86_64_docker.md
  4. 42
      doc/installation/raspberry_pi_3.rst
  5. 41
      doc/installation/ubuntu1604.rst
  6. 23
      iotronic_lightningrod/Board.py
  7. 159
      iotronic_lightningrod/lightningrod.py
  8. 30
      iotronic_lightningrod/modules/device_manager.py
  9. 133
      iotronic_lightningrod/modules/rest_manager.py
  10. 434
      iotronic_lightningrod/modules/service_manager.py
  11. 0
      iotronic_lightningrod/modules/web/__init__.py
  12. 1912
      iotronic_lightningrod/modules/web/static/css/bootstrap-grid.css
  13. 1
      iotronic_lightningrod/modules/web/static/css/bootstrap-grid.css.map
  14. 7
      iotronic_lightningrod/modules/web/static/css/bootstrap-grid.min.css
  15. 1
      iotronic_lightningrod/modules/web/static/css/bootstrap-grid.min.css.map
  16. 330
      iotronic_lightningrod/modules/web/static/css/bootstrap-reboot.css
  17. 1
      iotronic_lightningrod/modules/web/static/css/bootstrap-reboot.css.map
  18. 8
      iotronic_lightningrod/modules/web/static/css/bootstrap-reboot.min.css
  19. 1
      iotronic_lightningrod/modules/web/static/css/bootstrap-reboot.min.css.map
  20. 8950
      iotronic_lightningrod/modules/web/static/css/bootstrap.css
  21. 1
      iotronic_lightningrod/modules/web/static/css/bootstrap.css.map
  22. 7
      iotronic_lightningrod/modules/web/static/css/bootstrap.min.css
  23. 1
      iotronic_lightningrod/modules/web/static/css/bootstrap.min.css.map
  24. 6433
      iotronic_lightningrod/modules/web/static/js/bootstrap.bundle.js
  25. 1
      iotronic_lightningrod/modules/web/static/js/bootstrap.bundle.js.map
  26. 7
      iotronic_lightningrod/modules/web/static/js/bootstrap.bundle.min.js
  27. 1
      iotronic_lightningrod/modules/web/static/js/bootstrap.bundle.min.js.map
  28. 3925
      iotronic_lightningrod/modules/web/static/js/bootstrap.js
  29. 1
      iotronic_lightningrod/modules/web/static/js/bootstrap.js.map
  30. 7
      iotronic_lightningrod/modules/web/static/js/bootstrap.min.js
  31. 1
      iotronic_lightningrod/modules/web/static/js/bootstrap.min.js.map
  32. 24
      iotronic_lightningrod/modules/web/templates/base.html
  33. 19
      iotronic_lightningrod/modules/web/templates/config.html
  34. 9
      iotronic_lightningrod/modules/web/templates/home.html
  35. 18
      iotronic_lightningrod/modules/web/templates/network.html
  36. 31
      iotronic_lightningrod/modules/web/templates/status.html
  37. 9
      iotronic_lightningrod/modules/webservice_manager.py
  38. 1
      requirements.txt
  39. 1
      setup.cfg

3
MANIFEST.in

@ -8,6 +8,9 @@ include scripts/lr_install
include scripts/lr_configure
include scripts/device_bkp_rest
include iotronic_lightningrod/proxies/configs/*
include iotronic_lightningrod/modules/web/static/css/*
include iotronic_lightningrod/modules/web/static/js/*
include iotronic_lightningrod/modules/web/templates/*
include AUTHORS
include ChangeLog

24
doc/installation/docker/rpi_docker.md

@ -3,34 +3,10 @@
GitHub repo:
- https://github.com/openstack/iotronic-lightning-rod
# Configure Lightning-rod environment
* Create the folder in your system to store Lightning-rod settings <LR_CONF_PATH> (e.g. "/etc/iotronic/"):
```
sudo mkdir <LR_CONF_PATH>
```
* Get Lightning-rod configuration template files:
```
cd <LR_CONF_PATH>
sudo wget https://raw.githubusercontent.com/openstack/iotronic-lightning-rod/master/templates/settings.example.json -O settings.json
sudo wget https://raw.githubusercontent.com/openstack/iotronic-lightning-rod/master/etc/iotronic/iotronic.conf
```
* Configure Lightning-rod identity:
```
cd <LR_CONF_PATH>
wget https://raw.githubusercontent.com/openstack/iotronic-lightning-rod/master/scripts/lr_configure
chmod +x lr_configure
./lr_configure -c <REGISTRATION-TOKEN> <WAMP-REG-AGENT-URL> <LR_CONF_PATH>
```
# Create container:
```
docker run -d --privileged \
-v lr_var:/var/lib/iotronic -v lr_le:/etc/letsencrypt/ \
-v <LR_CONF_PATH>/settings.json:/etc/iotronic/settings.json \
-v <LR_CONF_PATH>/iotronic.conf:/etc/iotronic/iotronic.conf \
--net=host --restart unless-stopped \
--name=lightning-rod mdslab/rpi-openstack-iotronic-lightning-rod
```

24
doc/installation/docker/x86_64_docker.md

@ -3,34 +3,10 @@
GitHub repo:
- https://github.com/openstack/iotronic-lightning-rod
# Configure Lightning-rod environment
* Create the folder in your system to store Lightning-rod settings <LR_CONF_PATH> (e.g. "/etc/iotronic/"):
```
sudo mkdir <LR_CONF_PATH>
```
* Get Lightning-rod configuration template files:
```
cd <LR_CONF_PATH>
sudo wget https://raw.githubusercontent.com/openstack/iotronic-lightning-rod/master/templates/settings.example.json -O settings.json
sudo wget https://raw.githubusercontent.com/openstack/iotronic-lightning-rod/master/etc/iotronic/iotronic.conf
```
* Configure Lightning-rod identity:
```
cd <LR_CONF_PATH>
wget https://raw.githubusercontent.com/openstack/iotronic-lightning-rod/master/scripts/lr_configure
chmod +x lr_configure
./lr_configure -c <REGISTRATION-TOKEN> <WAMP-REG-AGENT-URL> <LR_CONF_PATH>
```
# Create container:
```
docker run -d --privileged \
-v lr_var:/var/lib/iotronic -v lr_le:/etc/letsencrypt/ \
-v <LR_CONF_PATH>/settings.json:/etc/iotronic/settings.json \
-v <LR_CONF_PATH>/iotronic.conf:/etc/iotronic/iotronic.conf \
--net=host --restart unless-stopped \
--name=lightning-rod mdslab/openstack-iotronic-lightning-rod
```

42
doc/installation/raspberry_pi_3.rst

@ -1,8 +1,47 @@
IoTronic Lightning-rod installation guide for Raspberry Pi 2/3
============================================================
==============================================================
We tested this procedure on a Raspberry Pi 2/3 board (Raspbian).
Requirements
~~~~~~~~~~~~
* OS requirement
::
apt install python3 python3-setuptools python3-pip gdb lsof
* NodeJS
::
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs
npm install -g npm
echo "NODE_PATH=/usr/lib/node_modules" | tee -a /etc/environment
source /etc/environment > /dev/null
* WSTUN:
::
npm install -g --unsafe @mdslab/wstun
* NGINX:
::
apt install -y nginx
sed -i 's/# server_names_hash_bucket_size 64;/server_names_hash_bucket_size 64;/g' /etc/nginx/nginx.conf
* Certbot
::
apt-get install python-certbot-nginx
Install Lightning-rod
~~~~~~~~~~~~~~~~~~~~~
@ -26,6 +65,7 @@ Iotronic setup
Arguments required:
* <REGISTRATION-TOKEN> , token released by IoTronic registration procedure
* <WAMP-REG-AGENT-URL> , IoTronic Crossbar server WAMP URL:
ws(s)://<IOTRONIC-CROSSBAR-IP>:<IOTRONIC-CROSSBAR-PORT>/
e.g.

41
doc/installation/ubuntu1604.rst

@ -4,6 +4,46 @@ IoTronic Lightning-rod installation guide for Ubuntu 16.04
We tested this procedure on a Ubuntu 16.04 (also within a LXD
container). Everything needs to be run as root.
Requirements
~~~~~~~~~~~~
* OS requirement
::
apt install python3 python3-setuptools python3-pip gdb lsof
* NodeJS
::
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs
npm install -g npm
echo "NODE_PATH=/usr/lib/node_modules" | tee -a /etc/environment
source /etc/environment > /dev/null
* WSTUN:
::
npm install -g --unsafe @mdslab/wstun
* NGINX:
::
apt install -y nginx
sed -i 's/# server_names_hash_bucket_size 64;/server_names_hash_bucket_size 64;/g' /etc/nginx/nginx.conf
* Certbot
::
apt-get install python-certbot-nginx
Install Lightning-rod
~~~~~~~~~~~~~~~~~~~~~
::
@ -26,6 +66,7 @@ Iotronic setup
Arguments required:
* <REGISTRATION-TOKEN> , token released by IoTronic registration procedure
* <WAMP-REG-AGENT-URL> , IoTronic Crossbar server WAMP URL:
ws(s)://<IOTRONIC-CROSSBAR-IP>:<IOTRONIC-CROSSBAR-PORT>/
e.g.

23
iotronic_lightningrod/Board.py

@ -27,6 +27,9 @@ CONF = cfg.CONF
SETTINGS = '/etc/iotronic/settings.json'
# global FIRST_BOOT
FIRST_BOOT = False
class Board(object):
@ -107,14 +110,26 @@ class Board(object):
except Exception as err:
if str(err) != 'uuid':
LOG.warning("settings.json file exception: " + str(err))
if self.status == None:
LOG.warning("settings.json file exception: " + str(err))
# STATUS REGISTERED
try:
self.code = board_config['code']
LOG.info('First registration board settings: ')
LOG.info(' - code: ' + str(self.code))
self.getWampAgent(self.iotronic_config)
if self.code == "<REGISTRATION-TOKEN>":
# LR start to waiting for first configuration
global FIRST_BOOT
if FIRST_BOOT == False:
FIRST_BOOT = True
LOG.info("FIRST BOOT procedure started")
self.status = "first_boot"
else:
LOG.info('First registration board settings: ')
LOG.info(' - code: ' + str(self.code))
self.getWampAgent(self.iotronic_config)
except Exception as err:
LOG.error("Wrong code: " + str(err))
os._exit(1)

159
iotronic_lightningrod/lightningrod.py

@ -32,12 +32,15 @@ import signal
import ssl
from stevedore import extension
import sys
import time
import txaio
from pip._vendor import pkg_resources
# IoTronic imports
from iotronic_lightningrod.Board import Board
from iotronic_lightningrod.Board import FIRST_BOOT
from iotronic_lightningrod.common.exception import timeoutALIVE
from iotronic_lightningrod.common.exception import timeoutRPC
from iotronic_lightningrod.common import utils
@ -71,6 +74,7 @@ lr_opts = [
CONF = cfg.CONF
CONF.register_opts(lr_opts)
global SESSION
SESSION = None
global board
board = None
@ -81,6 +85,7 @@ RPC_proxies = {}
zombie_alert = True
# ASYNCIO
global loop
loop = None
component = None
txaio.start_logging(level="info")
@ -110,6 +115,8 @@ class LightningRod(object):
CONF(project='iotronic')
logging.setup(CONF, DOMAIN)
self.w = None
if (utils.checkIotronicConf(CONF)):
if CONF.debug:
@ -133,8 +140,20 @@ class LightningRod(object):
global board
board = Board()
self.w = WampManager(board.wamp_config)
# Start REST server
singleModuleLoader("rest", session=None)
if(board.status == "first_boot"):
LOG.info("LR FIRST BOOT: waiting for first configuration...")
while (board.status == "first_boot"):
time.sleep(5)
# LR was configured and we have to load its new configuration
board.loadSettings()
# Start Wamp Manager
self.w = WampManager(board.wamp_config)
self.w.start()
else:
@ -146,7 +165,8 @@ class LightningRod(object):
# No zombie alert activation
zombie_alert = False
LOG.info("LR is shutting down...")
self.w.stop()
if self.w != None:
self.w.stop()
Bye()
except Exception as e:
LOG.error("Error closing LR")
@ -177,6 +197,46 @@ class WampManager(object):
LOG.info("WAMP server stopped!")
def iotronic_status(board_status):
if board_status != "first_boot":
# WS ALIVE
try:
alive = asyncio.run_coroutine_threadsafe(
wamp_singleCheck(SESSION),
loop
)
alive = alive.result()
except Exception as e:
LOG.error(" - Iotronic check: " + str(e))
alive = e
else:
alive = "Not connected!"
return alive
async def wamp_singleCheck(session):
try:
# LOG.debug("ALIVE sending...")
with timeoutALIVE(seconds=CONF.rpc_alive_timer, action="ws_alive"):
res = await session.call(
str(board.agent) + u'.stack4things.wamp_alive',
board_uuid=board.uuid,
board_name=board.name
)
LOG.debug("WampCheck attempt " + str(res))
except exception.ApplicationError as e:
LOG.error(" - Iotronic Connection RPC error: " + str(e))
return res
async def wamp_checks(session):
while (True):
@ -594,6 +654,8 @@ def wampConnect(wamp_conf):
"\n- connected = " + str(connected)
)
board.session_id = "N/A"
if board.status == "operative" and reconnection is False:
#################
@ -696,6 +758,58 @@ def moduleWampRegister(session, meth_list):
LOG.info(" --> " + str(meth[0]))
def singleModuleLoader(module_name, session=None):
ep = []
for ep in pkg_resources.iter_entry_points(group='s4t.modules'):
# LOG.info(" - " + str(ep))
pass
if not ep:
LOG.info("No modules available!")
sys.exit()
else:
modules = extension.ExtensionManager(
namespace='s4t.modules',
# invoke_on_load=True,
# invoke_args=(session,),
)
LOG.info('Module "' + module_name + '" loading:')
for ext in modules.extensions:
if (ext.name == 'rest'):
mod = ext.plugin(board, session)
global MODULES
MODULES[mod.name] = mod
# Methods list for each module
meth_list = inspect.getmembers(mod, predicate=inspect.ismethod)
global RPC
RPC[mod.name] = meth_list
if len(meth_list) == 3:
# there are at least two methods for each module:
# "__init__" and "finalize"
LOG.info(" - No RPC to register for "
+ str(ext.name) + " module!")
else:
if(session != None):
LOG.info(" - RPC list of " + str(mod.name) + ":")
moduleWampRegister(SESSION, meth_list)
# Call the finalize procedure for each module
mod.finalize()
def modulesLoader(session):
"""Modules loader method thorugh stevedore libraries.
@ -727,36 +841,41 @@ def modulesLoader(session):
for ext in modules.extensions:
# LOG.debug(ext.name)
LOG.debug(ext.name)
if (ext.name == 'gpio') & (board.type == 'server'):
LOG.info("- GPIO module disabled for 'server' devices")
else:
mod = ext.plugin(board, session)
global MODULES
MODULES[mod.name] = mod
if ext.name != "rest":
# Methods list for each module
meth_list = inspect.getmembers(mod, predicate=inspect.ismethod)
mod = ext.plugin(board, session)
global RPC
RPC[mod.name] = meth_list
global MODULES
MODULES[mod.name] = mod
if len(meth_list) == 3:
# there are at least two methods for each module:
# "__init__" and "finalize"
# Methods list for each module
meth_list = inspect.getmembers(
mod, predicate=inspect.ismethod
)
LOG.info(" - No RPC to register for "
+ str(ext.name) + " module!")
global RPC
RPC[mod.name] = meth_list
else:
LOG.info(" - RPC list of " + str(mod.name) + ":")
moduleWampRegister(SESSION, meth_list)
if len(meth_list) == 3:
# there are at least two methods for each module:
# "__init__" and "finalize"
# Call the finalize procedure for each module
mod.finalize()
LOG.info(" - No RPC to register for "
+ str(ext.name) + " module!")
else:
LOG.info(" - RPC list of " + str(mod.name) + ":")
moduleWampRegister(SESSION, meth_list)
# Call the finalize procedure for each module
mod.finalize()
LOG.info("Lightning-rod modules loaded.")
LOG.info("\n\nListening...")

30
iotronic_lightningrod/modules/device_manager.py

@ -27,7 +27,6 @@ from datetime import datetime
from iotronic_lightningrod.config import package_path
from iotronic_lightningrod.lightningrod import RPC_devices
from iotronic_lightningrod.lightningrod import SESSION
from iotronic_lightningrod.modules import Module
from iotronic_lightningrod.modules import utils
import iotronic_lightningrod.wampmessage as WM
@ -88,13 +87,13 @@ class DeviceManager(Module.Module):
for meth in dev_meth_list:
if (meth[0] != "__init__") & (meth[0] != "finalize"):
# LOG.info(" - " + str(meth[0]))
LOG.info(" - " + str(meth[0]))
# rpc_addr = u'iotronic.' + board.uuid + '.' + meth[0]
rpc_addr = u'iotronic.' + str(board.session_id) + '.' + \
board.uuid + '.' + meth[0]
# LOG.debug(" --> " + str(rpc_addr))
SESSION.register(meth[1], rpc_addr)
self.device_session.register(meth[1], rpc_addr)
LOG.info(" --> " + str(meth[0]) + " registered!")
@ -164,17 +163,22 @@ class DeviceManager(Module.Module):
rpc_name = utils.getFuncName()
LOG.info("RPC " + rpc_name + " CALLED")
command = "ifconfig"
message = getIfconfig()
w_msg = WM.WampSuccess(message)
out = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE
)
return w_msg.serialize()
output = out.communicate()[0].decode('utf-8').strip()
message = str(output)
w_msg = WM.WampSuccess(message)
def getIfconfig():
return w_msg.serialize()
command = "ifconfig"
out = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE
)
output = str(out.communicate()[0].decode('utf-8').strip())
return output

133
iotronic_lightningrod/modules/rest_manager.py

@ -0,0 +1,133 @@
# Copyright 2017 MDSLAB - University of Messina
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
__author__ = "Nicola Peditto <n.peditto@gmail.com>"
from iotronic_lightningrod.lightningrod import board
from iotronic_lightningrod.lightningrod import iotronic_status
from iotronic_lightningrod.modules import device_manager
from iotronic_lightningrod.modules import Module
from iotronic_lightningrod.modules import service_manager
from datetime import datetime
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
import os
import subprocess
import threading
from oslo_config import cfg
from oslo_log import log as logging
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
class RestManager(Module.Module):
def __init__(self, board, session=None):
super(RestManager, self).__init__("RestManager", board)
def finalize(self):
threading.Thread(target=self._runRestServer, args=()).start()
def restore(self):
pass
def _runRestServer(self):
APP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(APP_PATH, 'modules/web/templates/')
STATIC_PATH = os.path.join(APP_PATH, 'modules/web/static/')
app = Flask(
__name__,
template_folder=TEMPLATE_PATH,
static_folder=STATIC_PATH,
static_url_path="/static"
)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/status')
def status():
wstun_status = service_manager.wstun_status()
if wstun_status == 0:
wstun_status = "Online"
else:
wstun_status = "Offline"
info = {
'board_id': board.uuid,
'board_name': board.name,
'wagent': board.agent,
'session_id': board.session_id,
'timestamp': str(
datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')),
'wstun_status': wstun_status,
'board_reg_status': str(board.status),
'iotronic_status': str(iotronic_status(board.status)),
'service_list': str(service_manager.services_list())
}
return render_template('status.html', **info)
@app.route('/network')
def network():
info = {
'ifconfig': device_manager.getIfconfig().replace('\n', '<br>')
}
return render_template('network.html', **info)
def lr_config(ragent, code):
bashCommand = "lr_configure %s %s " % (code, ragent)
process = subprocess.Popen(bashCommand.split(),
stdout=subprocess.PIPE)
output, error = process.communicate()
# print(output)
return
@app.route('/config', methods=['GET', 'POST'])
def config():
if request.method == 'POST':
ragent = request.form['urlwagent']
code = request.form['code']
lr_config(ragent, code)
return redirect("/status", code=302)
else:
if board.status == "first_boot":
urlwagent = request.args.get('urlwagent') or ""
code = request.args.get('code') or ""
info = {
'urlwagent': urlwagent,
'code': code
}
return render_template('config.html', **info)
else:
return redirect("/status", code=302)
app.run(host='0.0.0.0', port=1474, debug=False, use_reloader=False)

434
iotronic_lightningrod/modules/service_manager.py

@ -20,10 +20,14 @@ import json
import os
import psutil
import pyinotify
import queue
import signal
import socket
import subprocess
import time
import threading
from datetime import datetime
from threading import Thread
from urllib.parse import urlparse
@ -35,7 +39,7 @@ from iotronic_lightningrod.modules import utils
import iotronic_lightningrod.wampmessage as WM
from iotronic_lightningrod import lightningrod
from random import randint
from oslo_config import cfg
from oslo_log import log as logging
@ -62,6 +66,17 @@ CONF.register_opts(wstun_opts, group=service_group)
s_conf_FILE = CONF.lightningrod_home + "/services.json"
ws_server_alive = 0
WS_MON_LIST = {}
global wstun_ip
wstun_ip = None
global wstun_port
wstun_port = None
class ServiceManager(Module.Module):
def __init__(self, board, session):
@ -72,6 +87,11 @@ class ServiceManager(Module.Module):
self.wstun_ip = urlparse(board.wamp_config["url"])[1].split(':')[0]
self.wstun_port = "8080"
global wstun_port
wstun_port = self.wstun_port
global wstun_ip
wstun_ip = self.wstun_ip
is_wss = False
wurl_list = board.wamp_config["url"].split(':')
if wurl_list[0] == "wss":
@ -112,101 +132,128 @@ class ServiceManager(Module.Module):
else:
if len(s_conf['services']) != 0:
print("WSTUN server checks:")
wstun_process_list = []
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(4)
global ws_server_alive
ws_server_alive = sock.connect_ex(
(self.wstun_ip, int(self.wstun_port)))
for p in psutil.process_iter():
if len(p.cmdline()) != 0:
if (p.name() == "node" and "wstun" in p.cmdline()[1]):
wstun_process_list.append(p)
if ws_server_alive == 0:
if len(s_conf) != 0:
print("\nWSTUN processes:")
print(" - WSTUN server is online!")
for s_uuid in s_conf['services']:
sock.close() # close check socket
service_name = \
s_conf['services'][s_uuid]['name']
service_pid = \
s_conf['services'][s_uuid]['pid']
LOG.info(" - " + service_name)
if len(s_conf['services']) != 0:
if len(wstun_process_list) != 0:
wstun_process_list = []
for wp in wstun_process_list:
try:
for p in psutil.process_iter():
if len(p.cmdline()) != 0:
if ((p.name() == "node") and (
"wstun" in p.cmdline()[1]
)):
wstun_process_list.append(p)
except Exception as e:
LOG.error(
" --> PSUTIL [finalize]: " +
"error getting wstun processes info: " + str(e)
)
if service_pid == wp.pid:
LOG.info(
" --> the tunnel for '" + service_name
+ "' already exists; killing..."
)
if len(s_conf) != 0:
print("\nWSTUN processes:")
# 1. Kill wstun process (if exists)
for s_uuid in s_conf['services']:
# No zombie alert activation
lightningrod.zombie_alert = False
LOG.debug(
"[WSTUN-RESTORE] - "
"on-finalize zombie_alert: " +
str(lightningrod.zombie_alert)
)
service_name = \
s_conf['services'][s_uuid]['name']
service_pid = \
s_conf['services'][s_uuid]['pid']
LOG.info(" - " + service_name)
try:
os.kill(service_pid, signal.SIGINT)
print("OLD WSTUN KILLED: " + str(wp))
LOG.info(" --> service '" + service_name
+ "' with PID " + str(service_pid)
+ " was killed; "
+ "creating new one...")
if len(wstun_process_list) != 0:
except OSError:
LOG.warning(
" - WSTUN process already killed, "
"creating new one...")
for wp in wstun_process_list:
break
if service_pid == wp.pid:
LOG.info(
" --> the tunnel for '" + service_name
+ "' already exists; killing..."
)
# 2. Create the reverse tunnel
public_port = \
s_conf['services'][s_uuid]['public_port']
local_port = \
s_conf['services'][s_uuid]['local_port']
# 1. Kill wstun process (if exists)
wstun = self._startWstun(
public_port, local_port, event="boot"
)
# No zombie alert activation
lightningrod.zombie_alert = False
LOG.debug(
"[WSTUN-RESTORE] - "
"on-finalize zombie_alert: " +
str(lightningrod.zombie_alert)
)
if wstun != None:
try:
os.kill(service_pid, signal.SIGINT)
print("OLD WSTUN KILLED: " + str(wp))
LOG.info(
" --> service '" + service_name
+ "' with PID " + str(service_pid)
+ " was killed; "
+ "creating new one...")
service_pid = wstun.pid
except OSError:
LOG.warning(
" - WSTUN process already killed, "
"creating new one...")
# 3. Update services.json file
s_conf['services'][s_uuid]['pid'] = \
service_pid
s_conf['services'][s_uuid]['updated_at'] = \
datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
break
self._updateServiceConf(s_conf, s_uuid,
output=True)
# 2. Create the reverse tunnel
public_port = \
s_conf['services'][s_uuid]['public_port']
local_port = \
s_conf['services'][s_uuid]['local_port']
LOG.info(" --> Cloud service '" + service_name
+ "' tunnel established.")
else:
message = "Error spawning " + str(service_name) \
+ " service tunnel!"
LOG.error(" - " + message)
wstun = self._startWstunOnBoot(
public_port, local_port, event="boot")
signal.signal(signal.SIGCHLD, self._zombie_hunter)
if wstun != None:
# Reactivate zombies monitoring
if not lightningrod.zombie_alert:
lightningrod.zombie_alert = True
service_pid = wstun.pid
else:
LOG.info(" --> No service tunnels to establish.")
# 3. Update services.json file
s_conf['services'][s_uuid]['pid'] = \
service_pid
s_conf['services'][s_uuid]['updated_at'] = \
datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
self._updateServiceConf(s_conf, s_uuid,
output=True)
LOG.info(" --> Cloud service '" + service_name
+ "' tunnel established.")
else:
message = "Error spawning " + str(service_name) \
+ " service tunnel!"
LOG.error(" - " + message)
signal.signal(signal.SIGCHLD, self._zombie_hunter)
signal.signal(signal.SIGCHLD, self._zombie_hunter)
# Reactivate zombies monitoring
if not lightningrod.zombie_alert:
lightningrod.zombie_alert = True
else:
LOG.info(" --> No service tunnels to establish.")
signal.signal(signal.SIGCHLD, self._zombie_hunter)
else:
sock.close() # close check socket
print(" - WSTUN server is offline!")
LOG.error("WSTUN server is offline!")
def restore(self):
LOG.info("Cloud service tunnels to restore:")
@ -227,21 +274,30 @@ class ServiceManager(Module.Module):
# No zombie alert activation
lightningrod.zombie_alert = False
LOG.debug("[WSTUN-RESTORE] - Restore zombie_alert: " + str(
lightningrod.zombie_alert))
LOG.debug(
"[WSTUN-RESTORE] - Restore zombie_alert: "
+ str(lightningrod.zombie_alert)
)
# Collect all alive WSTUN proccesses
for p in psutil.process_iter():
if (p.name() == "node"):
if (p.status() == psutil.STATUS_ZOMBIE):
LOG.warning("WSTUN ZOMBIE: " + str(p))
wstun_process_list.append(p)
elif ("wstun" in p.cmdline()[1]):
LOG.warning("WSTUN ALIVE: " + str(p))
wstun_process_list.append(p)
psutil.Process(p.pid).kill()
LOG.warning(" --> PID " + str(p.pid) + " killed!")
try:
for p in psutil.process_iter():
if (p.name() == "node"):
if (p.status() == psutil.STATUS_ZOMBIE):
LOG.warning("WSTUN ZOMBIE: " + str(p))
wstun_process_list.append(p)
elif ("wstun" in p.cmdline()[1]):
LOG.warning("WSTUN ALIVE: " + str(p))
wstun_process_list.append(p)
psutil.Process(p.pid).kill()
LOG.warning(" --> PID " + str(p.pid)
+ " killed!")
except Exception as e:
LOG.error(
" --> PSUTIL [restore]: " +
"error getting wstun processes info: " + str(e)
)
LOG.debug("[WSTUN-RESTORE] - WSTUN processes to restore:\n"
+ str(wstun_process_list))
@ -270,12 +326,21 @@ class ServiceManager(Module.Module):
zombie_list = []
for p in psutil.process_iter():
if len(p.cmdline()) == 0:
if ((p.name() == "node") and
(p.status() == psutil.STATUS_ZOMBIE)):
print(" - process: " + str(p))
zombie_list.append(p.pid)
try:
for p in psutil.process_iter():
if len(p.cmdline()) == 0:
if ((p.name() == "node") and
(p.status() == psutil.STATUS_ZOMBIE)):
print(" - process: " + str(p))
zombie_list.append(p.pid)
except Exception as e:
LOG.error(
" --> PSUTIL [_zombie_hunter]: " +
"error getting wstun processes info. " +
"Please restore manually your services: " + str(e)
)
return
if len(zombie_list) == 0:
# print(" - no action required.")
@ -404,15 +469,13 @@ class ServiceManager(Module.Module):
:return: JSON Services configuration
"""
try:
with open(s_conf_FILE) as settings:
s_conf = json.load(settings)
except Exception as err:
LOG.error(" --> Parsing error in "
+ s_conf_FILE + ": " + str(err))
LOG.error(" --> Parsing error in " + s_conf_FILE + ": " + str(err))
if os.path.isfile(s_conf_FILE):
@ -437,7 +500,7 @@ class ServiceManager(Module.Module):
return s_conf
def _wstunMon(self, wstun):
def _wstunMon(self, wstun, local_port):
wfd_check = True
@ -503,6 +566,8 @@ class ServiceManager(Module.Module):
event_notifier = pyinotify.ThreadedNotifier(
watch_manager, EventProcessor()
)
event_notifier.setName("TN-" + str(local_port))
WS_MON_LIST[str(local_port)] = event_notifier
watch_this = os.path.abspath(
"/proc/" + str(wstun.pid) + "/fd/" + str(wstun_fd)
@ -512,13 +577,27 @@ class ServiceManager(Module.Module):
def _startWstun(self, public_port, local_port, event="no-set"):
import socket
count_ws = 0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(4)
result = sock.connect_ex((self.wstun_ip, int(self.wstun_port)))
if result == 0:
global ws_server_alive
ws_server_alive = sock.connect_ex(
(self.wstun_ip, int(self.wstun_port))
)
while(ws_server_alive != 0 and count_ws < 5):
count_ws = count_ws + 1
LOG.warning(
"WSTUN server is offline! Retry " + str(count_ws) + "/5..."
)
time.sleep(randint(3, 6))
global ws_server_alive
ws_server_alive = sock.connect_ex(
(self.wstun_ip, int(self.wstun_port))
)
if ws_server_alive == 0:
sock.close() # close check socket
@ -542,12 +621,22 @@ class ServiceManager(Module.Module):
# WSTUN MON
# #############################################################
Thread(
try:
if event != "enable":
WS_MON_LIST[str(local_port)].stop()
except Exception as err:
LOG.error("Error stopping WSTUN monitor: " + str(err))
wsmon = Thread(
target=self._wstunMon,
args=(wstun,)
).start()
name="THR-" + str(local_port),
args=(wstun, local_port, )
)
wsmon.start()
# self._wstunMon(wstun)
# print(threading.enumerate())
print(WS_MON_LIST)
# #############################################################
@ -561,25 +650,89 @@ class ServiceManager(Module.Module):
return wstun
def _startWstunOnBoot(self, public_port, local_port, event="no-set"):
opt_reverse = "-r" + str(public_port) + ":127.0.0.1:" + str(
local_port)
try:
wstun = subprocess.Popen(
[CONF.services.wstun_bin, opt_reverse, self.wstun_url],
stdout=subprocess.PIPE
)
if (event != "boot"):
print("WSTUN start event:")
cmd_print = 'WSTUN exec: ' + str(CONF.services.wstun_bin) \
+ opt_reverse + ' ' + self.wstun_url
print(" - " + str(cmd_print))
LOG.debug(cmd_print)
# WSTUN MON
# #############################################################
wsmon = Thread(
target=self._wstunMon,
name="THR-" + str(local_port),
args=(wstun, local_port,)
)
wsmon.start()
# #############################################################
except Exception as err:
LOG.error("Error spawning WSTUN process: " + str(err))
wstun = None
return wstun
async def ServicesStatus(self):
rpc_name = utils.getFuncName()
LOG.info("RPC " + rpc_name + " CALLED")
thr_list = str(threading.enumerate())
# print(WS_MON_LIST)
print(thr_list + "\n" + str(WS_MON_LIST))
w_msg = WM.WampSuccess(thr_list)
return w_msg.serialize()
def _updateServiceConf(self, s_conf, s_uuid, output=True):
# Apply the changes to services.json
with open(s_conf_FILE, 'w') as f:
json.dump(s_conf, f, indent=4)
if s_conf == "":
if output:
LOG.info(" - service updated:\n" + json.dumps(
s_conf['services'][s_uuid],
indent=4,
sort_keys=True
))
else:
LOG.info(" - services.json file updated!")
LOG.error(" - ERROR new services.json content is empty: " +
"Restoring backup.")
# Backup json file before update
os.system(
'cp ' + s_conf_FILE + ' ' + s_conf_FILE + '.bkp'
)
# Restore backup json file on error
os.system(
'cp ' + s_conf_FILE + '.bkp ' + s_conf_FILE
)
else:
# Apply the changes to services.json
with open(s_conf_FILE, 'w') as f:
json.dump(s_conf, f, indent=4)
print(s_conf)
if output:
LOG.info(" - service updated:\n" + json.dumps(
s_conf['services'][s_uuid],
indent=4,
sort_keys=True
))
else:
LOG.info(" - services.json file updated!")
# Backup json file before update
os.system(
'cp ' + s_conf_FILE + ' ' + s_conf_FILE + '.bkp'
)
async def ServiceEnable(self, service, public_port):
@ -647,8 +800,8 @@ class ServiceManager(Module.Module):
w_msg = WM.WampSuccess(message)
else:
message = "Error spawning " + str(service_name) \
+ " service tunnel!"
message = "Error spawning '" + str(service_name) \
+ "' service tunnel!"
LOG.error(" - " + message)
w_msg = WM.WampError(message)
@ -822,8 +975,7 @@ class ServiceManager(Module.Module):
s_conf['services'][s_uuid]['updated_at'] = \
datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
self._updateServiceConf(s_conf, s_uuid,
output=True)
self._updateServiceConf(s_conf, s_uuid, output=True)
message = "service " + str(service_name) \
+ " restored on port " \
@ -898,3 +1050,41 @@ class ServiceManager(Module.Module):
w_msg = WM.WampError(message)
return w_msg.serialize()
def services_list():
try:
s_list = ""
with open(s_conf_FILE) as settings:
s_conf = json.load(settings)
for s_uuid in s_conf['services']:
s_service = str(s_conf['services'][s_uuid]['name']) \
+ " - " + str(s_conf['services'][s_uuid]['public_port']) \
+ " - " + str(s_conf['services'][s_uuid]['local_port'])
s_list = s_list + "<li>" + s_service + "</li>"
except Exception as err:
LOG.error("Error getting services list: " + str(err))
s_list = str(err)
return s_list
def wstun_status():
if(wstun_ip != None) and (wstun_port != None):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(4)
global ws_server_alive
ws_server_alive = sock.connect_ex((wstun_ip, int(wstun_port)))
sock.close() # close check socket
else:
ws_server_alive = "N/A"
return ws_server_alive

0
iotronic_lightningrod/modules/web/__init__.py

1912
iotronic_lightningrod/modules/web/static/css/bootstrap-grid.css vendored

File diff suppressed because it is too large Load Diff

1
iotronic_lightningrod/modules/web/static/css/bootstrap-grid.css.map

File diff suppressed because one or more lines are too long

7
iotronic_lightningrod/modules/web/static/css/bootstrap-grid.min.css vendored

File diff suppressed because one or more lines are too long

1
iotronic_lightningrod/modules/web/static/css/bootstrap-grid.min.css.map

File diff suppressed because one or more lines are too long

330
iotronic_lightningrod/modules/web/static/css/bootstrap-reboot.css vendored

@ -0,0 +1,330 @@
/*!
* Bootstrap Reboot v4.1.0 (https://getbootstrap.com/)
* Copyright 2011-2018 The Bootstrap Authors
* Copyright 2011-2018 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md)
*/
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
font-family: sans-serif;
line-height: 1.15;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
-ms-overflow-style: scrollbar;
-webkit-tap-highlight-color: transparent;
}
@-ms-viewport {
width: device-width;
}
article, aside, dialog, figcaption, figure, footer, header, hgroup, main, nav, section {
display: block;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #212529;
text-align: left;
background-color: #fff;
}
[tabindex="-1"]:focus {
outline: 0 !important;
}
hr {
box-sizing: content-box;
height: 0;
overflow