diff --git a/client/panduza/admin/init_directory.py b/client/panduza/admin/init_directory.py index e26982a..3fee183 100644 --- a/client/panduza/admin/init_directory.py +++ b/client/panduza/admin/init_directory.py @@ -3,82 +3,21 @@ from pathlib import Path from dataclasses import dataclass +from .writer_tree import TreeWriter from .writer_env_file import EnvFileWriter from .writer_mosquitto_conf import MosquittoConfWriter - - -@dataclass -class DockerComposeWriter: - filepath: str - - def write(self): - text = '''version: '3' -services: - - # docker compose run --service-ports mosquitto - mosquitto: - image: eclipse-mosquitto - user: "${UID}:${GID}" - ports: - - 1883:1883 - - 9001:9001 - volumes: - - ./data/mosquitto.conf:/mosquitto/config/mosquitto.conf - - panduza-py-platform: - image: ghcr.io/panduza/panduza-py-platform:latest - # To use your local platform build - # image: local/panduza-py-platform - privileged: true - network_mode: host - user: "${UID}:${GID}" - environment: - - HUNT - volumes: - - .:/etc/panduza - - /run/udev:/run/udev:ro - # command: bash - ''' - with open(self.filepath, 'w') as f: - print(f" + Write file '{self.filepath}'") - f.write(text) - - - -@dataclass -class TreeWriter: - filepath: str - - def write(self): - tree = { - "machine": "rename_this_machine", - "brokers": { - "rename_this_broker": { - "addr": "localhost", - "port": 1883, - "interfaces": [ - { - "name": "fake_psu", - "driver": "py.psu.fake" - } - ] - } - } - } - with open(self.filepath, 'w') as f: - print(f" + Write file '{self.filepath}'") - f.write(json.dumps(tree, indent=4)) - - - - - +from .writer_docker_compose import DockerComposeWriter def init_directory(args): os.makedirs(args.directory_path, exist_ok=True) os.makedirs(Path(args.directory_path) / 'data', exist_ok=True) - DockerComposeWriter(Path(args.directory_path) / 'docker-compose.yml').write() + + dcw = DockerComposeWriter(Path(args.directory_path) / 'docker-compose.yml') + dcw.set_dev_mode(args.dev) + dcw.write() + TreeWriter(Path(args.directory_path) / 'tree.json').write() MosquittoConfWriter(Path(args.directory_path) / 'data/mosquitto.conf').write() EnvFileWriter(Path(args.directory_path) / '.env').write() + diff --git a/client/panduza/admin/pzadmin.py b/client/panduza/admin/pzadmin.py index 565bf28..f9bb20b 100644 --- a/client/panduza/admin/pzadmin.py +++ b/client/panduza/admin/pzadmin.py @@ -23,6 +23,8 @@ def pzadmin_main(): help='for the command "start", the platform is started in background') parser.add_argument('--stop-all', dest='stopall', action='store_true', help='for the command "stop", mosquitto is also stopped') + parser.add_argument('--dev', dest='dev', action='store_true', + help='tell the command that the admin is a panduza dev') args = parser.parse_args() diff --git a/client/panduza/admin/writer_docker_compose.py b/client/panduza/admin/writer_docker_compose.py new file mode 100644 index 0000000..f230fce --- /dev/null +++ b/client/panduza/admin/writer_docker_compose.py @@ -0,0 +1,58 @@ +import os +import json +import subprocess +from pathlib import Path +from dataclasses import dataclass + + + +@dataclass +class DockerComposeWriter: + filepath: str + + def set_dev_mode(self, dev): + self.__dev = dev + + def write(self): + text = '''version: '3' +services: + + # docker compose run --service-ports mosquitto + mosquitto: + image: eclipse-mosquitto + user: "${UID}:${GID}" + ports: + - 1883:1883 + - 9001:9001 + volumes: + - ./data/mosquitto.conf:/mosquitto/config/mosquitto.conf + + panduza-py-platform: + ''' + if not self.__dev: + text += ''' image: ghcr.io/panduza/panduza-py-platform:latest + # To use your local platform build + # image: local/panduza-py-platform + ''' + else: + text += '''# image: ghcr.io/panduza/panduza-py-platform:latest + # To use your local platform build + image: local/panduza-py-platform + ''' + text += '''privileged: true + network_mode: host + user: "${UID}:${GID}" + environment: + - HUNT + volumes: + - .:/etc/panduza + - /run/udev:/run/udev:ro + # command: bash +''' + + # Write the file + with open(self.filepath, 'w') as f: + print(f" + Write file '{self.filepath}'") + f.write(text) + + diff --git a/client/panduza/admin/writer_tree.py b/client/panduza/admin/writer_tree.py new file mode 100644 index 0000000..4653a32 --- /dev/null +++ b/client/panduza/admin/writer_tree.py @@ -0,0 +1,33 @@ +import os +import json +import subprocess +from pathlib import Path +from dataclasses import dataclass + + +@dataclass +class TreeWriter: + filepath: str + + def write(self): + tree = { + "machine": "rename_this_machine", + "brokers": { + "rename_this_broker": { + "addr": "localhost", + "port": 1883, + "interfaces": [ + { + "name": "fake_psu", + "driver": "py.psu.fake" + } + ] + } + } + } + with open(self.filepath, 'w') as f: + print(f" + Write file '{self.filepath}'") + f.write(json.dumps(tree, indent=4)) + + + diff --git a/platform/panduza_platform/meta_driver.py b/platform/panduza_platform/meta_driver.py index 25b3933..a02b163 100644 --- a/platform/panduza_platform/meta_driver.py +++ b/platform/panduza_platform/meta_driver.py @@ -31,14 +31,19 @@ def hunt(self): name = "unnamed" if "name" not in config else config["name"] description = "" if "description" not in config else config["description"] template = self._PZADRV_tree_template() - instances = self._PZADRV_hunt_instances() - obj = { + driver = { "name": name, "description": description, - "template": template, - "instances": instances + "template": template } - return obj + meat = self._PZADRV_hunt_instances() + instances = None + if meat: + instances = { + "name": name, + "instances": meat + } + return driver, instances ########################################################################### ########################################################################### diff --git a/platform/panduza_platform/meta_platform.py b/platform/panduza_platform/meta_platform.py index dc569e0..d4da48d 100644 --- a/platform/panduza_platform/meta_platform.py +++ b/platform/panduza_platform/meta_platform.py @@ -277,19 +277,25 @@ def hunt_mode(self): self._log.info("*********************************") os.makedirs(f"{self.run_dir}/panduza/platform", exist_ok=True) - filepath = f"{self.run_dir}/panduza/platform/py.json" + filepath_drivers = f"{self.run_dir}/panduza/platform/py_drivers.json" + filepath_instances = f"{self.run_dir}/panduza/platform/py_instances.json" - f = open(filepath, "w") - - hunting_bag = [] + # Hunt data for each driver + hunting_bag_driver = [] + hunting_bag_instances = [] for drv in self.drivers: - meat = drv().hunt() - if meat: - hunting_bag.append(meat) - - content = { "drivers": hunting_bag } - f.write(json.dumps(content, indent=4)) - f.close() + self._log.info(f"Hunt with: {drv()._PZADRV_config()['name']}") + driver, instances = drv().hunt() + if driver: + hunting_bag_driver.append(driver) + if instances: + hunting_bag_instances.extend(instances) + + # Write data into files + with open(filepath_drivers, "w") as f: + f.write(json.dumps(hunting_bag_driver, indent=4)) + with open(filepath_instances, "w") as f: + f.write(json.dumps(hunting_bag_instances, indent=4)) ########################################################################### ###########################################################################