Commit 3479c2dc authored by Yann Garcia's avatar Yann Garcia
Browse files

Add MECSupport plugin in latest ACME release

parent 10b9f1a5
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -63,6 +63,11 @@ externalRoot=/$SVC_PATH/
[webui]
root=/webui

#[http.wsgi]
#enable=true
#threadPoolSize=100
#connectionLimit=100 

[mqtt]
enable=$MQTT_ENABLE
address=$MQTT_HOST
+30 −7
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ from acmecse.etc.Utils import normalizeURL
from acmecse.etc.Types import ResponseStatusCode, ContentSerializationType, AuthorizationResult
from acmecse.helpers.PluginManager import plugin, init, start, restart, stop, finish, configure, validate, requires
from acmecse.helpers.interpreter.Interpreter import SType
from acmecse.helpers.BackgroundWorker import BackgroundWorker, BackgroundWorkerPool
from acmecse.etc.ResponseStatusCodes import ResponseException
from acmecse.runtime.Configuration import Configuration
from acmecse.etc.Types import Result, ResponseStatusCode
@@ -61,7 +62,8 @@ class MECSupport:
		'isInfo',
		'headers',
		'register',
		'thread'
		'thread',
		'mecRegistrationWorker'
	)
	""" Slots for the MECSupport. """

@@ -76,6 +78,7 @@ class MECSupport:
		self.headers['Content-Type'] = 'application/json; charset=UTF-8'
		self.register = None
		self.thread = None
		self.mecRegistrationWorker = None
		self.use_wss = False
		self.wss_path = None
		L.isInfo and L.log('initMECSupport: MEC Client initialized')
@@ -99,11 +102,17 @@ class MECSupport:
			setattr(self.mecConnection, 'server_thread', self.thread)
			setattr(self.mecConnection, 'server_stop_event', threading.Event())
			self.thread.start()
			self.isStopped = False
			L.isInfo and L.log('startMECSupport: MEC Flask app running in background thread')
			self.run()
			self.isStopped = False
			# Register to MEC platform
			self.registerToMECPlatform()
			L.isInfo and L.log('startMECSupport: Starting MEC registration worker')
			self.mecRegistrationWorker = BackgroundWorkerPool.newWorker(20, # Don't care, the worker will be stopped by registerAsIoTPlatform() in any case
																	    self.registerAsIoTPlatform, 
																	    'MECSupport').start()
		except Exception as e:
			L.logErr(f'startMECSupport: failed to start MEC Flask thread: {e}')
		L.isInfo and L.log('<<< startMECSupport')
		return

	@finish
@@ -112,6 +121,10 @@ class MECSupport:
		if not Configuration.mec_enable:
			L.isInfo and L.log('MEC Client disabled')
			return
		if self.mecRegistrationWorker is not None:
			L.isInfo and L.log('finishMECSupport: Stopping MEC registration worker')
			self.mecRegistrationWorker.stop()
			self.mecRegistrationWorker = None
		for id in list(self.mecConnections):
			self.disconnectFromMec(id[0], id[1])	# 0 = host, 1 = port
		if self.thread and self.thread.is_alive():
@@ -199,6 +212,7 @@ class MECSupport:

		L.isInfo and L.log('configureMECSupport: MEC Client: self.http_address=' + self.http_address)
		L.isInfo and L.log('configureMECSupport: MEC Client initialized')
		return

	@validate
	def validateMECSupport(self, config: Configuration) -> None:
@@ -239,12 +253,18 @@ class MECSupport:
	#	Additional methods
	#

	def run(self) -> None:
		L.isDebug and L.logDebug(f'run: MEC enable: {Configuration.mec_enable}')
	def registerToMECPlatform(self):
		L.isDebug and L.logDebug(f'registerToMECPlatform: MEC enable: {Configuration.mec_enable}')
		if Configuration.mec_enable:
			self.confirm_readyToMec()
			self.post_mec_service()
		return

	def registerAsIoTPlatform(self):
		L.isDebug and L.logDebug(f'registerAsIoTPlatform: MEC enable: {Configuration.mec_enable}')
		if Configuration.mec_enable:
			self.registerToMec()
		return

	def connectToMec(self, host:str, port:int) -> Optional[Flask]:
		L.isDebug and L.logDebug(f'connectToMec: host: {host} - port: {port}')
@@ -339,7 +359,7 @@ class MECSupport:
			customServicesTransportInfo['description'] = 'ACME oneM2M CSE ID'
			customServicesTransportInfo['type'] = 'REST_HTTP'
			customServicesTransportInfo['protocol'] = 'REST_HTTP'
			customServicesTransportInfo['version'] = '4'
			customServicesTransportInfo['version'] = '5'
			customServicesTransportInfo['endpoint'] = {}
			if self.fullAddress == '':
				customServicesTransportInfo['endpoint']['uris'] = [ 'https://' + self.cse_external_ip + ':' + str(self.http_port) ]
@@ -367,6 +387,9 @@ class MECSupport:
					L.isInfo and L.log('MEC Client: Status not 201, trying again...')
		except Exception as e:
			L.logErr(f'MECClient.registerToMec: ' + str(e))
		L.isInfo and L.log('MEC Client: Stopping MEC registration worker')
		self.mecRegistrationWorker.stop()
		self.mecRegistrationWorker = None
		return None


@@ -441,7 +464,7 @@ class MECSupport:
					"name": "REST",
					"type": "REST_HTTP",
					"protocol": "HTTPS",
					"version": "4.0",
					"version": "5.0",
					"endpoint": {
						"uris": [
							self.fullAddress
+2124 −0

File added.

Preview size limit exceeded, changes collapsed.

+28 −97
Original line number Diff line number Diff line
FROM ubuntu:22.04
FROM meep-docker-registry:30001/meep-tinyiot-base:latest

ENV SERVER_IP=""
ENV MQTT_ENABLE=""
@@ -11,87 +11,18 @@ ENV SERVER_TYPE=""
ENV ENABLE_COAP=""
ENV CSE_BASE_NAME=""
ENV CSE_BASE_RI=""

# ENV MEEP_MEP_NAME=""
# ENV HTTP_PORT=""
# ENV MQTT_SERVER=""
# ENV MQTT_PORT=""
# ENV MEC_SANDBOX_SERVER=""
# ENV MEEP_MEP_NAME=""
# ENV REGISTRAR_CSE_HOST=""
# ENV REGISTRAR_CSE_PORT=""

RUN DEBIAN_FRONTEND=noninteractive apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y \
      autoconf \
      bison \
      build-essential \
      cmake \
      curl \
      dos2unix \
      doxygen \
      emacs \
      expect \
      flex \
      g++ \
      gcc \
      git \
      gnutls-bin \
      iputils-ping \
      libedit2 \
      libedit-dev \
      libffi-dev \
      libglib2.0-dev \
      libgcrypt-dev \
      libjsoncpp-dev \
      libncurses5-dev \
      libpcap-dev \
      libssl-dev \
      libtool-bin \
      libtool \
      libxml2-dev \
      libxml2-utils \
      libyaml-dev \
      lsof \
      ntp \
      pkg-config \
      sudo \
      sshpass \
      tcpdump \
      tzdata \
      libcurl4-openssl-dev \
      gettext \
      jq
ENV MEC_ENABLE=""
ENV MEC_HOST_URL=""
ENV MEC_PLATFORM=""
ENV MEC_SANDBOX_ID=""

WORKDIR /usr/src/app
# Make docker file from TinyIoT folder
COPY ./data/source /usr/src/app/tinyIoT

# RUN git clone https://github.com/seslabSJU/tinyIoT.git tinyIoT

WORKDIR /usr/src/app/tinyIoT

# The commit is aligned wuth the changes pushed by the COPY
# The issue here is that applying patch with 
#RUN git checkout 43d4c7a

COPY ./data /usr/src/app/tinyIoT

# RUN ls /usr/src/app/tinyIoT

# RUN cd /usr/src/app/tinyIoT/server && ./autogen.sh && make
# RUN cd /usr/src/app/tinyIoT/server && make
# RUN cd /usr/src/app/tinyIoT
RUN cd / && chmod +x /usr/src/app/tinyIoT/entrypoint.sh
# Make sure script has execution permission
# RUN chmod +x /usr/src/app/tinyIoT/entrypoint.sh

# Set correct ENTRYPOINT with full path
# ENTRYPOINT ["/usr/src/app/tinyIoT/entrypoint.sh"]

# RUN cd && ls
# RUN cd /usr && ls
# RUN cd /usr/src/app && ls
# RUN ls -l /usr/src/app/tinyIoT/entrypoint.sh

ENTRYPOINT ["/usr/src/app/tinyIoT/entrypoint.sh"]
+91 −30
Original line number Diff line number Diff line
#!/bin/bash
set -e

# Process MEEP_HOST_URL
SERVER_IP="${MEEP_HOST_URL#http://}"; MEEP_HOST_URL="${MEEP_HOST_URL#https://}"
echo "MEEP_HOST_URL: ${MEEP_HOST_URL}"
# Other environment variables
echo "SERVER_TYPE: ${SERVER_TYPE}"
echo "SERVER_PORT: ${SERVER_PORT}"
echo "MQTT_ENABLE: ${MQTT_ENABLE}"
echo "CSE_BASE_NAME: ${CSE_BASE_NAME}"
echo "CSE_BASE_RI: ${CSE_BASE_RI}"
echo "MQTT_HOST: ${MQTT_HOST}"
echo "MQTT_PORT: ${MQTT_PORT}"
echo "MQTT_USERNAME: ${MQTT_USERNAME}"
echo "MQTT_PASSWORD: ${MQTT_PASSWORD}"
MQTT_ENABLE=${MQTT_ENABLE:-1}
SERVER_IP="${SERVER_IP:-192.168.0.112}"

# MEEP_SANDBOX_NAME
SERVICE_NAME="meep-tinyiot-in-cse"
# Get the namespace from the service account
NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)
# Get the token from the service account
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
# Query the Kubernetes API to get the NodePort for the service
# NODE_PORT=$(curl -sSk \
#   -H "Authorization: Bearer $TOKEN" \
#   https://kubernetes.default.svc/api/v1/namespaces/$NAMESPACE/services/$SERVICE_NAME \
#   | jq -r '.spec.ports[0].nodePort')
# echo "External NodePort exposed for service [$SERVICE_NAME] in namespace [$NAMESPACE] is: $NODE_PORT"
echo "Using Helm-injected and Kubernetes auto-injected environment variables instead of API queries"

if [[ ! -z "${MEEP_MEP_NAME}" ]]; then
    svcPath="${MEEP_SANDBOX_NAME}/${MEEP_MEP_NAME}"
@@ -41,9 +19,45 @@ then
fi

sleep 5 # Wait for ETSI MEC Platform up and stable

MQTT_ENABLE=${MQTT_ENABLE:-1}
MQTT_HOST=${MQTT_HOST:- "meep-mosquitto"}
MQTT_HOST=${MQTT_HOST:-${MEP1_MEEP_MOSQUITTO_SERVICE_HOST:-${MEEP_MOSQUITTO_SERVICE_HOST:-"meep-mosquitto"}}}
MQTT_PORT=${MQTT_PORT:-1883}
SERVER_IP=${SERVER_IP:-"0.0.0.0"}
[ -z "$SERVER_IP" ] && SERVER_IP="0.0.0.0"
SERVER_PORT=${SERVER_PORT:-3002}
[ -z "$SERVER_PORT" ] && SERVER_PORT="3002"
ENABLE_COAP=${ENABLE_COAP:-0}
CSE_BASE_NAME=${CSE_BASE_NAME:-"TinyIoT"}
CSE_BASE_RI=${CSE_BASE_RI:-"tinyiot"}
SERVER_TYPE=${SERVER_TYPE:-IN_CSE}
[ -z "$SERVER_TYPE" ] && SERVER_TYPE="IN_CSE"
MQTT_USERNAME=${MQTT_USERNAME:-"tinyiot-in-cse"}
MQTT_PASSWORD=${MQTT_PASSWORD:-"mqtt"}

# MEC Configuration
MEC_ENABLE=${MEC_ENABLE:-0}
# Strip http:// or https:// prefix from MEEP_HOST_URL for MEC_HOST_URL
MEC_HOST_URL=$(echo "${MEEP_HOST_URL:-meep-platform.etsi.org}" | sed 's|https\?://||')
[ -z "$MEC_HOST_URL" ] && MEC_HOST_URL="meep-platform.etsi.org"
MEC_PLATFORM=${MEC_PLATFORM:-"mep1"}
MEC_SANDBOX_ID=${MEEP_SANDBOX_NAME:-"meep-sandbox"}
MEC_HTTP_HOST=${MEC_HTTP_HOST:-"meep-tinyiot-in-cse"}

echo "ENVIRONMENT VARIABLES SET:"
echo "SERVER_IP: ${SERVER_IP}"
echo "SERVER_TYPE: ${SERVER_TYPE}"
echo "SERVER_PORT: ${SERVER_PORT}"
echo "CSE_BASE_NAME: ${CSE_BASE_NAME}"
echo "CSE_BASE_RI: ${CSE_BASE_RI}"
echo "MQTT_HOST: ${MQTT_HOST}"
echo "MQTT_ENABLE: ${MQTT_ENABLE}"
echo "MQTT_PORT: ${MQTT_PORT}"
echo "MQTT_USERNAME: ${MQTT_USERNAME}"
echo "MQTT_PASSWORD: ${MQTT_PASSWORD}"
echo "MEC_ENABLE: ${MEC_ENABLE}"
echo "MEC_HOST_URL: ${MEC_HOST_URL}"
echo "MEC_PLATFORM: ${MEC_PLATFORM}"
echo "MEC_SANDBOX_ID: ${MEC_SANDBOX_ID}"

export MQTT_ENABLE
export MQTT_HOST
@@ -56,10 +70,57 @@ export SERVER_IP
export ENABLE_COAP
export CSE_BASE_NAME
export CSE_BASE_RI
export MEC_ENABLE
export MEC_HOST_URL
export MEC_PLATFORM
export MEC_SANDBOX_ID
export MEC_HTTP_HOST

workdir="/usr/src/app/tinyIoT/server"
cd "$workdir" || { echo "Directory $workdir not found"; exit 1; }
envsubst < config.h.in > config.h
make clean
make
./server
 No newline at end of file
make clean && make

# Start server in background
./server &
SERVER_PID=$!

# Register to MEC platform after server is ready
if [ "${MEC_ENABLE}" == "1" ]; then
    sleep 5
    echo "Registering to MEC platform..."
    curl -k -s -X POST "https://${MEC_HOST_URL}/${MEC_SANDBOX_ID}/${MEC_PLATFORM}/iots/v1/registered_iot_platforms" \
        -H "Content-Type: application/json" \
        -d "{
            \"iotPlatformId\": \"${CSE_BASE_RI}\",
            \"userTransportInfo\": [{
                \"id\": \"${CSE_BASE_RI}-mqtt-01\",
                \"name\": \"${CSE_BASE_RI}\",
                \"description\": \"TinyIoT CSE MQTT\",
                \"type\": \"MB_TOPIC_BASED\",
                \"protocol\": \"MQTT\",
                \"version\": \"3.1.1\",
                \"endpoint\": {\"addresses\": [{\"host\": \"meep-cloud-mosquitto\", \"port\": 1883}]},
                \"security\": {\"username\": \"${MQTT_USERNAME}\", \"password\": \"${MQTT_PASSWORD}\", \"useTLS\": false},
                \"implSpecificInfo\": {
                    \"eventTopics\": [\"/oneM2M/req/+/${CSE_BASE_RI}/#\"],
                    \"uplinkTopics\": [\"/oneM2M/resp/${CSE_BASE_RI}/+/#\"],
                    \"downlinkTopics\": [\"/oneM2M/reg_req/+/${CSE_BASE_RI}/#\", \"/oneM2M/reg_resp/${CSE_BASE_RI}/+/#\"]
                }
            }],
            \"customServicesTransportInfo\": [{
                \"id\": \"${CSE_BASE_RI}-http-01\",
                \"name\": \"${CSE_BASE_RI}\",
                \"description\": \"TinyIoT CSE HTTP\",
                \"type\": \"REST_HTTP\",
                \"protocol\": \"REST_HTTP\",
                \"version\": \"1.1\",
                \"endpoint\": {\"addresses\": [{\"host\": \"${MEC_HTTP_HOST}\", \"port\": ${SERVER_PORT}}]},
                \"security\": {}
            }],
            \"enabled\": true
        }" && echo "MEC registration successful" || echo "MEC registration failed (non-fatal)"
fi

# Wait for server process
wait $SERVER_PID
Loading