Skip to content

Commit

Permalink
refactor render function
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed May 11, 2024
1 parent 050b496 commit 402e67f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
63 changes: 39 additions & 24 deletions framework/internal/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ def config() -> dict:
"""
Get the global configuration for the docker compose
"""
cmd = ['docker', 'compose', 'config', '--format', 'json']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
return json.loads(result.stdout)
try:
cmd = ['docker', 'compose', 'config', '--format', 'json']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
return json.loads(result.stdout)
except Exception as e:
print(f"Failed to get compose config: {e}", file=sys.stderr)
return {}

def status(service: str) -> dict:
"""
Get the status JSON for a docker compose service
"""
cmd = ['docker', 'compose', 'ps', '--format', 'json', service]
result = subprocess.run(cmd, stdout=subprocess.PIPE, timeout=5)

try:
cmd = ['docker', 'compose', 'ps', '--format', 'json', service]
result = subprocess.run(cmd, stdout=subprocess.PIPE, timeout=5)
return json.loads(result.stdout)
except json.decoder.JSONDecodeError:
except Exception:
return {}

def is_running(status: dict):
def is_running(status: dict) -> bool:
"""
Arguments:
- status: output of container_status
Expand All @@ -46,35 +49,47 @@ def exec(service: str, command: list[str], timeout: int = 120) -> tuple[int, byt
return 1, b''

cmd = ['docker', 'exec', container] + command
result = subprocess.run(cmd, stdout=subprocess.PIPE, timeout=timeout)
return result.returncode, result.stdout
try:
result = subprocess.run(cmd, stdout=subprocess.PIPE, timeout=timeout)
return result.returncode, result.stdout
except Exception as e:
print(f"Failed to run ${cmd}: {e}", file=sys.stderr)
return -1, bytes()

def up(service: str):
def up(service: str) -> bool:
"""
Start a docker compose service
"""
try:
cmd = ['docker', 'compose', 'up', '-d', service]
subprocess.call(cmd, stdout=subprocess.PIPE, timeout=300)
print(f"Started service {service}", file=sys.stderr)
return True
except Exception as e:
print(f"Failed to restart service {service}: {e}", file=sys.stderr)
return False

cmd = ['docker', 'compose', 'up', '-d', service]
subprocess.call(cmd, stdout=subprocess.PIPE, timeout=300)
print(f"Started service {service}")

def restart(service: str):
def restart(service: str) -> bool:
"""
Restart a docker compose service
"""

cmd = ['docker', 'compose', 'restart', service]
subprocess.call(cmd, stdout=subprocess.PIPE, timeout=300)
print(f"Restarted service {service}")
try:
cmd = ['docker', 'compose', 'restart', service]
subprocess.call(cmd, stdout=subprocess.PIPE, timeout=300)
print(f"Restarted service {service}", file=sys.stderr)
return True
except Exception as e:
print(f"Failed to restart service {service}: {e}", file=sys.stderr)
return False

def info() -> dict:
"""
Get information about host and docker
"""
cmd = ['docker', 'info', '-f', 'json']
result = subprocess.run(cmd, stdout=subprocess.PIPE, timeout=5)

try:
cmd = ['docker', 'info', '-f', 'json']
result = subprocess.run(cmd, stdout=subprocess.PIPE, timeout=5)
return json.loads(result.stdout)
except json.decoder.JSONDecodeError:
except Exception as e:
print(f"Failed to get host info from docker: {e}", file=sys.stderr)
return {}
9 changes: 5 additions & 4 deletions framework/internal/render.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pathlib
import sys
import yaml
import jinja2

Expand Down Expand Up @@ -53,7 +54,7 @@ def render(node_name: str, dry: bool = False) -> None:
all_host_vars[host_name]['inventory_hostname'] = host_name
all_host_vars[host_name].update(config.globals)
except yaml.YAMLError as exc:
print("Error reading host vars file: ", host_path, exc)
print("Error reading host vars file: ", host_path, exc, file=sys.stderr)

# Rendering variables for this node
render_vars = all_host_vars[node_name].copy()
Expand Down Expand Up @@ -114,7 +115,7 @@ def render(node_name: str, dry: bool = False) -> None:

# Check if the content has changed
if existing_content != output_content:
print(f"File {output_path} has changed.")
print(f"File {output_path} has changed.", file=sys.stderr)
service_changed = True
with open(output_path, 'w') as f:
f.write(output_content)
Expand All @@ -130,9 +131,9 @@ def render(node_name: str, dry: bool = False) -> None:
if not service_running:
# We don't start the service, instead a subsequent
# docker compose up will start everything in the right order
print(f"Service {service} is not running.")
print(f"Service {service} is not running.", file=sys.stderr)
elif service_changed:
print(f"Service {service} has changed. Restarting.")
print(f"Service {service} has changed. Restarting.", file=sys.stderr)
compose.restart(service)

# Run init script if status change
Expand Down

0 comments on commit 402e67f

Please sign in to comment.