forked from coanghel/rclone-docker-automount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrclone_initializer.py
70 lines (63 loc) · 2.52 KB
/
rclone_initializer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import os
import time
import json
import logging
from requests.auth import HTTPBasicAuth
# Logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Environment variables
USERNAME = os.getenv('RCLONE_USERNAME')
PASSWORD = os.getenv('RCLONE_PASSWORD')
PORT = os.getenv('RCLONE_PORT') or '5572'
# Constants
RCLONE_URL = f"http://rclone:{PORT}"
AUTH = HTTPBasicAuth(USERNAME, PASSWORD) if USERNAME and PASSWORD else None
HEADERS = {'Content-Type': 'application/json', 'User-Agent': 'RcloneClient/1.0'}
RETRY_INTERVAL = 10 # seconds
def is_rclone_ready():
try:
response = requests.options(f"{RCLONE_URL}/rc/noopauth", auth=AUTH)
return response.ok
except requests.exceptions.RequestException as e:
logging.error(f"Error checking rclone readiness: {e}")
return False
def read_mount_payloads():
try:
with open('config/mounts.json', 'r') as file:
return json.load(file)
except FileNotFoundError:
logging.error("config/mounts.json file not found.")
except json.JSONDecodeError:
logging.error("Error decoding JSON from the config/mounts.json file.")
return None
def mount_payloads(mount_payloads):
all_mount_success = True
for mount_payload in mount_payloads:
try:
logging.info(f"Mounting {mount_payload['fs']} to {mount_payload['mountPoint']}")
response = requests.post(f"{RCLONE_URL}/mount/mount", json=mount_payload, headers=HEADERS, auth=AUTH)
if response.ok:
logging.info(f"Mount successful.")
else:
logging.error(f"Failed to mount {mount_payload['fs']}: Status code: {response.status_code}, Response: {response.text}")
all_mount_success = False
except requests.exceptions.RequestException as e:
logging.error(f"Request failed: {e}")
all_mount_success = False
return all_mount_success
def initialize():
logging.info("Waiting for rclone service to be available...")
while not is_rclone_ready():
logging.info(f"Rclone not ready, waiting {RETRY_INTERVAL} seconds...")
time.sleep(RETRY_INTERVAL)
logging.info("Rclone is ready.")
mount_payloads_data = read_mount_payloads()
if mount_payloads_data is None:
return False
return mount_payloads(mount_payloads_data)
if __name__ == '__main__':
if initialize():
logging.info("Rclone initialization complete.")
else:
logging.error("Rclone initialization failed.")