From 6c6b1536a7e3e67d73ece1e55d139b83d7b9d94f Mon Sep 17 00:00:00 2001 From: chabisik Date: Fri, 1 Jan 2021 18:35:09 +0100 Subject: [PATCH] mise a jour --- .../device/bootstrap_client.py | 49 ++++++++++++++++--- .../gateway/bootstrap_client.py | 49 ++++++++++++++++--- Dockerfiles/forContainerNet/server/Dockerfile | 8 +-- 3 files changed, 86 insertions(+), 20 deletions(-) diff --git a/Dockerfiles/forContainerNet/device/bootstrap_client.py b/Dockerfiles/forContainerNet/device/bootstrap_client.py index 6e34c43..26e5c42 100755 --- a/Dockerfiles/forContainerNet/device/bootstrap_client.py +++ b/Dockerfiles/forContainerNet/device/bootstrap_client.py @@ -1,4 +1,5 @@ -#!/usr/bin/python +#!/usr/bin/python3 +#coding: utf-8 import os import subprocess import requests @@ -7,12 +8,44 @@ import requests BOOTSTRAP_SERVER_ADDRESS = '10.10.10.10:5555' def retrieve_config(): - my_config = "None" - my_ip = subprocess.check_output("sleep 5 && echo $MY_IP", shell=True).rstrip() - resp = requests.get("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip)) - my_config = resp.text - if my_config != "None": - subprocess.check_output("node /mydir/*.js %s"%(my_config), shell=True) - #print("my_ip=%s and of type %s"%(my_ip,type(my_ip))) + my_json_config = {"verdict":"oops"} + my_ip_ = str(subprocess.check_output("echo $MY_IP", shell=True)) #subprocess returning format: b'X.X.X.X\n' + my_ip = my_ip_.replace("b'",'').replace("'",'').replace("\\n",'') + print("MY_IP : %s"%(my_ip)) + while my_json_config["verdict"] != "yes": + try: + resp = requests.get("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip), timeout=2) + except: + print("Unable to join the bootstrap server") + try: + my_json_config = resp.json() + print("Extracted configs [succ] : %s"%(my_json_config)) + except: + print("Unable to extract configs from bootstrap server's answer") + print("Extracted configs [fail] : %s"%(my_json_config)) + print("request url : ==>%s<=="%("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip))) + if "verdict" in my_json_config and my_json_config["verdict"] == "yes": + my_config = config_json_to_string(my_json_config) + subprocess.check_output("node /mydir/*.js %s"%(my_config), shell=True) + else: + my_json_config = {"verdict":"oops"} + +def config_json_to_string(json_config): + config = "" + if "local_ip" in json_config: + config += "--local_ip "+json_config["local_ip"] + if "local_port" in json_config: + config += "--local_port "+json_config["local_port"] + if "local_name" in json_config: + config += "--local_name "+json_config["local_name"] + if "remote_ip" in json_config: + config += "--remote_ip "+json_config["remote_ip"] + if "remote_port" in json_config: + config += "--remote_port "+json_config["remote_port"] + if "remote_name" in json_config: + config += "--remote_name "+json_config["remote_name"] + if "send_period" in json_config: + config += "--send_period "+json_config["send_period"] + return config retrieve_config() diff --git a/Dockerfiles/forContainerNet/gateway/bootstrap_client.py b/Dockerfiles/forContainerNet/gateway/bootstrap_client.py index 6e34c43..26e5c42 100755 --- a/Dockerfiles/forContainerNet/gateway/bootstrap_client.py +++ b/Dockerfiles/forContainerNet/gateway/bootstrap_client.py @@ -1,4 +1,5 @@ -#!/usr/bin/python +#!/usr/bin/python3 +#coding: utf-8 import os import subprocess import requests @@ -7,12 +8,44 @@ import requests BOOTSTRAP_SERVER_ADDRESS = '10.10.10.10:5555' def retrieve_config(): - my_config = "None" - my_ip = subprocess.check_output("sleep 5 && echo $MY_IP", shell=True).rstrip() - resp = requests.get("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip)) - my_config = resp.text - if my_config != "None": - subprocess.check_output("node /mydir/*.js %s"%(my_config), shell=True) - #print("my_ip=%s and of type %s"%(my_ip,type(my_ip))) + my_json_config = {"verdict":"oops"} + my_ip_ = str(subprocess.check_output("echo $MY_IP", shell=True)) #subprocess returning format: b'X.X.X.X\n' + my_ip = my_ip_.replace("b'",'').replace("'",'').replace("\\n",'') + print("MY_IP : %s"%(my_ip)) + while my_json_config["verdict"] != "yes": + try: + resp = requests.get("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip), timeout=2) + except: + print("Unable to join the bootstrap server") + try: + my_json_config = resp.json() + print("Extracted configs [succ] : %s"%(my_json_config)) + except: + print("Unable to extract configs from bootstrap server's answer") + print("Extracted configs [fail] : %s"%(my_json_config)) + print("request url : ==>%s<=="%("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip))) + if "verdict" in my_json_config and my_json_config["verdict"] == "yes": + my_config = config_json_to_string(my_json_config) + subprocess.check_output("node /mydir/*.js %s"%(my_config), shell=True) + else: + my_json_config = {"verdict":"oops"} + +def config_json_to_string(json_config): + config = "" + if "local_ip" in json_config: + config += "--local_ip "+json_config["local_ip"] + if "local_port" in json_config: + config += "--local_port "+json_config["local_port"] + if "local_name" in json_config: + config += "--local_name "+json_config["local_name"] + if "remote_ip" in json_config: + config += "--remote_ip "+json_config["remote_ip"] + if "remote_port" in json_config: + config += "--remote_port "+json_config["remote_port"] + if "remote_name" in json_config: + config += "--remote_name "+json_config["remote_name"] + if "send_period" in json_config: + config += "--send_period "+json_config["send_period"] + return config retrieve_config() diff --git a/Dockerfiles/forContainerNet/server/Dockerfile b/Dockerfiles/forContainerNet/server/Dockerfile index cfcc60f..501a0f1 100644 --- a/Dockerfiles/forContainerNet/server/Dockerfile +++ b/Dockerfiles/forContainerNet/server/Dockerfile @@ -3,9 +3,9 @@ FROM node:buster # Installing required libraries RUN apt-get update && \ - apt-get install -y net-tools iputils-ping python3 python3-pip && \ - pip3 install flask && \ - pip3 install requests && \ + apt-get install -y net-tools iputils-ping python-pip && \ + pip install flask && \ + pip install requests && \ mkdir mydir && \ cd mydir && \ npm install express && \ @@ -18,7 +18,7 @@ COPY bootstrap_client.py /mydir SHELL ["/bin/bash", "-c"] -RUN echo "nohup python3 /mydir/bootstrap_client.py &" > start.sh && \ +RUN echo "nohup python /mydir/bootstrap_client.py &" > start.sh && \ echo "/bin/bash" >> start.sh && \ chmod 777 start.sh