Skip to content

Commit

Permalink
split hunt driver from hunt instances to ease manual search
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed May 10, 2023
1 parent 124e617 commit c44b9a6
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 85 deletions.
77 changes: 8 additions & 69 deletions client/panduza/admin/init_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

2 changes: 2 additions & 0 deletions client/panduza/admin/pzadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
58 changes: 58 additions & 0 deletions client/panduza/admin/writer_docker_compose.py
Original file line number Diff line number Diff line change
@@ -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)


33 changes: 33 additions & 0 deletions client/panduza/admin/writer_tree.py
Original file line number Diff line number Diff line change
@@ -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))



15 changes: 10 additions & 5 deletions platform/panduza_platform/meta_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

###########################################################################
###########################################################################
Expand Down
28 changes: 17 additions & 11 deletions platform/panduza_platform/meta_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

###########################################################################
###########################################################################
Expand Down

0 comments on commit c44b9a6

Please sign in to comment.