11import requests
22import time
3- import argparse
3+ import os
44import 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
7654def 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
9367if __name__ == "__main__" :
9468 main ()
0 commit comments