Skip to content

Commit 0814fe0

Browse files
authored
Merge pull request #1 from trentnbauer/trentnbauer-patch-1
Trentnbauer patch 1
2 parents eec1b6a + 486bb50 commit 0814fe0

2 files changed

Lines changed: 31 additions & 59 deletions

File tree

dockerfile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
# Use an official Python runtime as a parent image
21
FROM python:3.9-slim
32

4-
# Set the working directory in the container
53
WORKDIR /app
64

7-
# Install any necessary dependencies
8-
# We install requests directly since it's the only one
5+
# Install curl for the healthcheck
6+
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
7+
8+
# Install python dependencies
99
RUN pip install --no-cache-dir requests
1010

11-
# Copy the current directory contents into the container at /app
1211
COPY main.py .
1312

14-
# Set the entrypoint to python and the script
15-
# This allows you to pass arguments directly to the docker run command
16-
ENTRYPOINT ["python", "main.py"]
13+
# Use CMD instead of ENTRYPOINT for better environment variable handling
14+
CMD ["python", "main.py"]

main.py

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
11
import requests
22
import time
3-
import argparse
3+
import os
44
import sys
55

6-
# Function to parse command line arguments
7-
def parse_arguments():
8-
parser = argparse.ArgumentParser(description="UpSnap Wake-on-LAN Automation Script")
9-
10-
parser.add_argument(
11-
"--url",
12-
required=True,
13-
help="The base URL of the UpSnap instance (e.g., http://localhost:8090)"
14-
)
15-
16-
parser.add_argument(
17-
"--username",
18-
required=True,
19-
help="The username for authentication"
20-
)
21-
22-
parser.add_argument(
23-
"--password",
24-
required=True,
25-
help="The password for authentication"
26-
)
27-
28-
parser.add_argument(
29-
"--delay",
30-
type=int,
31-
required=True,
32-
help="Time to wait in seconds before running (e.g., 600 for 10 minutes)"
33-
)
6+
# Load Environment Variables
7+
UPSNAP_URL = os.getenv("UPSNAP_URL")
8+
UPSNAP_USERNAME = os.getenv("UPSNAP_USERNAME")
9+
UPSNAP_PASSWORD = os.getenv("UPSNAP_PASSWORD")
10+
# Default to 600 seconds (10 minutes) if not set
11+
DELAY = int(os.getenv("UPSNAP_DELAY", "600"))
3412

35-
return parser.parse_args()
13+
# specific check to ensure variables exist
14+
if not all([UPSNAP_URL, UPSNAP_USERNAME, UPSNAP_PASSWORD]):
15+
print("Error: Missing required environment variables.")
16+
print("Please set UPSNAP_URL, UPSNAP_USERNAME, and UPSNAP_PASSWORD.")
17+
sys.exit(1)
3618

37-
# Function to authenticate and get token
38-
def authenticate(base_url, username, password):
19+
def authenticate():
3920
print("Authenticating...")
40-
auth_url = f"{base_url}/api/collections/users/auth-with-password"
41-
auth_data = {"identity": username, "password": password}
21+
auth_url = f"{UPSNAP_URL}/api/collections/users/auth-with-password"
22+
auth_data = {"identity": UPSNAP_USERNAME, "password": UPSNAP_PASSWORD}
4223
try:
4324
response = requests.post(auth_url, json=auth_data)
4425
response.raise_for_status()
@@ -47,11 +28,10 @@ def authenticate(base_url, username, password):
4728
print(f"Error during authentication: {e}")
4829
return None
4930

50-
# Function to get all devices
51-
def get_devices(base_url, token):
31+
def get_devices(token):
5232
print("Getting list of devices...")
5333
headers = {'Authorization': f'Bearer {token}'}
54-
devices_url = f"{base_url}/api/collections/devices/records"
34+
devices_url = f"{UPSNAP_URL}/api/collections/devices/records"
5535
try:
5636
response = requests.get(devices_url, headers=headers)
5737
response.raise_for_status()
@@ -60,35 +40,29 @@ def get_devices(base_url, token):
6040
print(f"Error retrieving devices: {e}")
6141
return None
6242

63-
# Function to send WOL packet
64-
def wake_device(base_url, token, device_id):
43+
def wake_device(token, device_id):
6544
print(f"Sending WOL packet to device {device_id}...")
6645
headers = {'Authorization': f'Bearer {token}'}
67-
wake_url = f"{base_url}/api/upsnap/wake/{device_id}"
46+
wake_url = f"{UPSNAP_URL}/api/upsnap/wake/{device_id}"
6847
try:
6948
response = requests.get(wake_url, headers=headers)
7049
response.raise_for_status()
7150
print(f"WOL packet sent successfully to device {device_id}.")
7251
except requests.RequestException as e:
7352
print(f"Error sending WOL packet to device {device_id}: {e}")
7453

75-
# Main script
7654
def main():
77-
# Parse switches
78-
args = parse_arguments()
55+
print(f"Script started. Waiting for {DELAY} seconds...")
56+
# Health check depends on the container running, so we sleep first
57+
time.sleep(DELAY)
7958

80-
print(f"Script started. Waiting for {args.delay} seconds...")
81-
time.sleep(args.delay)
82-
83-
# Authenticate using provided args
84-
token = authenticate(args.url, args.username, args.password)
59+
token = authenticate()
8560

8661
if token:
87-
# Get devices using provided args
88-
devices = get_devices(args.url, token)
62+
devices = get_devices(token)
8963
if devices:
9064
for device in devices.get("items", []):
91-
wake_device(args.url, token, device.get("id"))
65+
wake_device(token, device.get("id"))
9266

9367
if __name__ == "__main__":
9468
main()

0 commit comments

Comments
 (0)