Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion root/app/swag-ondemand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os
import threading
import time
import subprocess
import wakeonlan

ACCESS_LOG_FILE = "/config/log/nginx/access.log"
LOG_FILE = "/config/log/ondemand/ondemand.log"
Expand Down Expand Up @@ -34,6 +36,22 @@ def init_docker(self):
logging.exception(e)
os._exit(1)

def handle_wol(self, container):
mac_address = container.labels.get("swag_ondemand_mac")
broadcast_address = container.labels.get("swag_ondemand_broadcast", "255.255.255.255")
ip_to_ping = container.labels.get("swag_ondemand_ip")

ping_success = False
if ip_to_ping:
result = subprocess.run(["ping", "-c", "1", "-W", "2", ip_to_ping], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if result.returncode == 0:
logging.info(f"IP {ip_to_ping} responded to ping, skipping WoL for {container.name}")
ping_success = True

if mac_address and (not ip_to_ping or not ping_success):
wakeonlan.send_magic_packet(mac_address, ip_address=broadcast_address, port=9)
logging.info(f"Sent WoL packet to {mac_address} on {broadcast_address}")

def process_containers(self):
containers = self.docker_client.containers.list(all=True, filters={ "label": ["swag_ondemand=enable"] })
container_names = {container.name for container in containers}
Expand Down Expand Up @@ -78,9 +96,13 @@ def start_containers(self):
continue
self.ondemand_containers[container_name]["last_accessed"] = datetime.now()
accessed = True

if not accessed or self.ondemand_containers[container_name]["status"] == "running":
continue
self.docker_client.containers.get(container_name).start()

container = self.docker_client.containers.get(container_name)
self.handle_wol(container)
container.start()
logging.info(f"Started {container_name}")
self.ondemand_containers[container_name]["status"] = "running"

Expand Down
2 changes: 2 additions & 0 deletions root/etc/s6-overlay/s6-rc.d/init-mod-swag-ondemand-setup/run
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ if [ ! -f /config/nginx/ondemand.conf ]; then
lsiown -R abc:abc /config/nginx/ondemand.conf
fi

echo "\
wakeonlan" >> /mod-pip-packages-to-install.list
echo "Applied the swag-ondemand mod"
Loading