Skip to content

Commit

Permalink
framework: WIP status json
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed May 9, 2024
1 parent 2255457 commit 9d6294d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
6 changes: 3 additions & 3 deletions framework/internal/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def is_running(status: dict):

return 'State' in status and status['State'] == "running"

def exec(service: str, command: list[str]):
def exec(service: str, command: list[str]) -> tuple[int, bytes]:
"""
Execute a command in a docker compose service
"""

print(service, command)
cmd = ['docker', 'compose', 'exec', service] + command
subprocess.run(cmd)
result = subprocess.run(cmd, stdout=subprocess.PIPE)
return result.returncode, result.stdout

def up(service: str):
"""
Expand Down
10 changes: 10 additions & 0 deletions framework/internal/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import hashlib
import sys

def get_files(path: str, recursive: bool = False):
"""
Expand Down Expand Up @@ -43,3 +44,12 @@ def read_dotenv(file='.env') -> dict[str, str]:
key, value = line.strip().split('=', 1)
dotenv[key] = value
return dotenv

def run_safe(func, *args, **kwargs):
"""Run a function and catch any exceptions that occur"""

try:
return func()
except Exception as e:
print(f"Error running status function: {e}", file=sys.stderr)
return None
62 changes: 62 additions & 0 deletions framework/status-json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3

import yaml
from internal.utils import get_files, run_safe
import internal.compose as compose
import internal.conf as conf

def get_services():
services = {}
for service in compose.config().get('services', {}):
compose_status = compose.status(service)
services[service] = {
'image': compose_status.get('Image', 'N/A'),
'status': compose_status.get('Status', 'N/A'),
}
return services

def get_nfd():
nfd = {}
_, stdout = compose.exec('nfd', ['nfdc', 'status'])
for line in stdout.decode('utf-8').splitlines():
line = line.strip()
if '=' in line:
name, value = line.split('=', 1)
nfd[name] = value
return nfd

def get_nlsr():
nlsr = {}
_, stdout = compose.exec('nlsr', ['nlsr', '-V'])
nlsr['version'] = stdout.decode('utf-8').strip()
return nlsr

def get_ndnping():
config = conf.get()
result = {}

for host_path in get_files(config.host_vars_path):
host_name = host_path.split('/')[-1]
ping_prefix: str | None = None

with open(host_path) as stream:
host = yaml.safe_load(stream)
ping_prefix = host['default_prefix']

if ping_prefix:
code, stdout = compose.exec('ndnpingserver', ['ndnping', '-c', '3', '-i', '10', ping_prefix])
print(stdout.decode('utf-8'))
result[host_name] = code == 0

return result


if __name__ == '__main__':
status = {
'services': run_safe(get_services),
'nfd': run_safe(get_nfd),
'nlsr': run_safe(get_nlsr),
'ndnping': run_safe(get_ndnping),
}

print(status)

0 comments on commit 9d6294d

Please sign in to comment.