Commit 3766503c authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

slapos/software: run quickjs as a service

parent b8965e75
...@@ -18,7 +18,7 @@ md5sum = ed0356dab0213a99fcd56e8a48e1c4d2 ...@@ -18,7 +18,7 @@ md5sum = ed0356dab0213a99fcd56e8a48e1c4d2
[instance-profile] [instance-profile]
filename = instance.cfg.in filename = instance.cfg.in
md5sum = c7f9de7830c1a6a57ae8683a1d329977 md5sum = 43e19ec3145928d79803528796eee8e0
[instance-default] [instance-default]
filename = instance-default.cfg.jinja filename = instance-default.cfg.jinja
...@@ -26,7 +26,7 @@ md5sum = 0ed9aac3b3a61c41b8a7b61ab8067c77 ...@@ -26,7 +26,7 @@ md5sum = 0ed9aac3b3a61c41b8a7b61ab8067c77
[instance-peer] [instance-peer]
filename = instance-peer.cfg.jinja.in filename = instance-peer.cfg.jinja.in
md5sum = 8615afba84028f1a61ca922cb1c0dcc5 md5sum = 7d063b33dbdd7548e2a23bc4d8a66a43
[main] [main]
_update_hash_filename_ = drone-scripts/main.js.jinja _update_hash_filename_ = drone-scripts/main.js.jinja
......
[buildout] [buildout]
parts = parts =
main qjs-launcher
symlink-quickjs-binary
publish-connection-information publish-connection-information
[directory] [directory]
...@@ -15,6 +14,7 @@ var = $${:home}/var ...@@ -15,6 +14,7 @@ var = $${:home}/var
log = $${:var}/log log = $${:var}/log
public = $${:srv}/public public = $${:srv}/public
run = $${:etc}/run
service = $${:etc}/service service = $${:etc}/service
[js-dynamic-template] [js-dynamic-template]
...@@ -44,14 +44,16 @@ template = ${pubsub:target} ...@@ -44,14 +44,16 @@ template = ${pubsub:target}
template = ${worker:target} template = ${worker:target}
<= js-dynamic-template <= js-dynamic-template
[symlink-quickjs-binary] [user]
recipe = slapos.recipe.build recipe = slapos.recipe.build:download
binary-path = ${quickjs:location}/bin/qjs url = {{ parameter_dict['flightScript'] }}
target = $${directory:bin}/qjs destination = $${directory:etc}/user.js
init = offline = false
import os
if not os.path.exists(options['target']): [qjs-launcher]
os.symlink(options['binary-path'], options['target']) recipe = slapos.cookbook:wrapper
wrapper-path = $${directory:run}/qjs-launcher
command-line = ${quickjs:location}/bin/qjs $${main:rendered} $${user:target} &> $${directory:log}/qjs-launcher.log
[script-js] [script-js]
recipe = slapos.recipe.template:jinja2 recipe = slapos.recipe.template:jinja2
......
...@@ -50,12 +50,6 @@ recipe = slapos.cookbook:mkdirectory ...@@ -50,12 +50,6 @@ recipe = slapos.cookbook:mkdirectory
home = $${buildout:directory} home = $${buildout:directory}
etc = $${:home}/etc etc = $${:home}/etc
[user]
recipe = slapos.recipe.build:download
url = $${slap-configuration:configuration.flightScript}
destination = $${directory:etc}/user.js
offline = false
[gwsocket-port] [gwsocket-port]
recipe = slapos.cookbook:free_port recipe = slapos.cookbook:free_port
minimum = 6789 minimum = 6789
...@@ -79,7 +73,6 @@ inline = ...@@ -79,7 +73,6 @@ inline =
[instance-peer] [instance-peer]
<= dynamic-template-base <= dynamic-template-base
url = ${instance-peer:output} url = ${instance-peer:output}
depends = $${user:recipe}
extra-context = extra-context =
key configuration peer-configuration:output key configuration peer-configuration:output
key ipv6 slap-configuration:ipv6-random key ipv6 slap-configuration:ipv6-random
......
...@@ -30,13 +30,11 @@ import json ...@@ -30,13 +30,11 @@ import json
import os import os
import socket import socket
import struct import struct
import subprocess
import time import time
import websocket import websocket
from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass
MAIN_SCRIPT_NAME = 'main.js'
''' '''
0. positionArray 0. positionArray
0.1 latitude 0.1 latitude
...@@ -53,7 +51,6 @@ MONITORED_ITEM_NB = 3 ...@@ -53,7 +51,6 @@ MONITORED_ITEM_NB = 3
OPC_UA_PORT = 4840 OPC_UA_PORT = 4840
OPC_UA_NET_IF = 'lo' OPC_UA_NET_IF = 'lo'
MCAST_GRP = 'ff15::1111' MCAST_GRP = 'ff15::1111'
USER_SCRIPT_NAME = 'user.js'
# OPC UA Pub/Sub related constants # OPC UA Pub/Sub related constants
VERSION = 1 VERSION = 1
...@@ -114,8 +111,7 @@ setUpModule, SlapOSInstanceTestCase = makeModuleSetUpAndTestCaseClass( ...@@ -114,8 +111,7 @@ setUpModule, SlapOSInstanceTestCase = makeModuleSetUpAndTestCaseClass(
os.path.join(os.path.dirname(__file__), '..', 'software.cfg'))) os.path.join(os.path.dirname(__file__), '..', 'software.cfg')))
class JSDroneTestCase(SlapOSInstanceTestCase): class SubscriberTestCase(SlapOSInstanceTestCase):
@classmethod @classmethod
def getInstanceParameterDict(cls): def getInstanceParameterDict(cls):
return { return {
...@@ -135,37 +131,11 @@ class JSDroneTestCase(SlapOSInstanceTestCase): ...@@ -135,37 +131,11 @@ class JSDroneTestCase(SlapOSInstanceTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
subscriber_partition = self.get_partition('JSDroneTestCase-2') subscriber_partition = self.get_partition('SubscriberTestCase-2')
instance_path = json.loads(
subscriber_partition.getConnectionParameterDict()['_'])['instance-path']
quickjs_bin = os.path.join(instance_path, 'bin', 'qjs')
script_dir = os.path.join(instance_path, 'etc')
self.qjs_process = subprocess.Popen(
[
quickjs_bin,
os.path.join(script_dir, MAIN_SCRIPT_NAME),
os.path.join(script_dir, USER_SCRIPT_NAME),
]
)
self.websocket_server_address = json.loads( self.websocket_server_address = json.loads(
subscriber_partition.getConnectionParameterDict()['_'])['websocket-url'] subscriber_partition.getConnectionParameterDict()['_'])['websocket-url']
time.sleep(0.5) time.sleep(0.5)
def tearDown(self):
ws = websocket.WebSocket()
ws.connect(self.websocket_server_address, timeout=5)
try:
ws.send("quit")
except websocket.WebSocketTimeoutException:
pass
finally:
ws.close()
time.sleep(0.1)
if self.qjs_process.returncode == None:
self.qjs_process.kill()
self.qjs_process.communicate()
super().tearDown()
def ua_networkMessage_encodeHeader(self): def ua_networkMessage_encodeHeader(self):
ua_byte1 = int(VERSION) ua_byte1 = int(VERSION)
ua_byte1 |= int(PUBLISHER_ID_ENABLED) << 4 ua_byte1 |= int(PUBLISHER_ID_ENABLED) << 4
...@@ -255,7 +225,10 @@ class JSDroneTestCase(SlapOSInstanceTestCase): ...@@ -255,7 +225,10 @@ class JSDroneTestCase(SlapOSInstanceTestCase):
s.sendto(ua_message, ('::1', OPC_UA_PORT)) s.sendto(ua_message, ('::1', OPC_UA_PORT))
def test_process(self): def test_process(self):
expected_process_name_list = ['http-server-on-watch'] expected_process_name_list = [
'qjs-launcher',
'http-server-on-watch',
]
with self.slap.instance_supervisor_rpc as supervisor: with self.slap.instance_supervisor_rpc as supervisor:
process_names = [process['name'] process_names = [process['name']
for process in supervisor.getAllProcessInfo()] for process in supervisor.getAllProcessInfo()]
...@@ -272,7 +245,7 @@ class JSDroneTestCase(SlapOSInstanceTestCase): ...@@ -272,7 +245,7 @@ class JSDroneTestCase(SlapOSInstanceTestCase):
def test_subscriber_instance_parameter_dict(self): def test_subscriber_instance_parameter_dict(self):
self.assertEqual( self.assertEqual(
json.loads(self.get_partition('JSDroneTestCase-2').getInstanceParameterDict()['_']), json.loads(self.get_partition('SubscriberTestCase-2').getInstanceParameterDict()['_']),
{ {
'autopilotIp': '192.168.27.1', 'autopilotIp': '192.168.27.1',
'autopilotPort': 7909, 'autopilotPort': 7909,
...@@ -371,3 +344,4 @@ class JSDroneTestCase(SlapOSInstanceTestCase): ...@@ -371,3 +344,4 @@ class JSDroneTestCase(SlapOSInstanceTestCase):
b'\\u001b[32minfo/client\\u001b[0m\\tReceived speed of drone 0: %.2f ? %.2f m/s %.2f m/s\\n"}' % SPEED_ARRAY_VALUES, b'\\u001b[32minfo/client\\u001b[0m\\tReceived speed of drone 0: %.2f ? %.2f m/s %.2f m/s\\n"}' % SPEED_ARRAY_VALUES,
ws.recv_frame().data, ws.recv_frame().data,
) )
ws.close()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment