@@ -11,20 +11,37 @@ def get_port_from_config(config_file_path, key_type):
11
11
return port
12
12
except Exception as e :
13
13
logger .error (f"Error reading port from config file: { e } " )
14
- return '9999'
14
+ return '9999'
15
15
16
16
def obscure_password (password ):
17
17
"""Obscure the password using rclone."""
18
18
try :
19
19
result = subprocess .run (["rclone" , "obscure" , password ], check = True , stdout = subprocess .PIPE )
20
20
return result .stdout .decode ().strip ()
21
21
except subprocess .CalledProcessError as e :
22
- print ( "Error obscuring password:" , e )
22
+ logger . error ( f "Error obscuring password: { e } " )
23
23
return None
24
24
25
+ def wait_for_url (url , endpoint = "/dav/" , timeout = 600 ):
26
+ start_time = time .time ()
27
+ logger .info (f"Waiting to start the rclone process until the Zurg WebDAV { url } { endpoint } is accessible." )
28
+ while time .time () - start_time < timeout :
29
+ try :
30
+ response = requests .get (f"{ url } { endpoint } " )
31
+ if response .status_code == 207 :
32
+ logger .debug (f"Zurg WebDAV { url } { endpoint } is accessible." )
33
+ return True
34
+ else :
35
+ logger .debug (f"Received status code { response .status_code } while waiting for { url } { endpoint } to be accessible." )
36
+ except requests .ConnectionError as e :
37
+ logger .debug (f"Connection error while waiting for the Zurg WebDAV { url } { endpoint } to be accessible: { e } " )
38
+ time .sleep (5 )
39
+ logger .error (f"Timeout: Zurg WebDAV { url } { endpoint } is not accessible after { timeout } seconds." )
40
+ return False
41
+
25
42
def setup ():
26
43
logger .info ("Checking rclone flags" )
27
-
44
+
28
45
try :
29
46
if not RCLONEMN :
30
47
raise Exception ("Please set a name for the rclone mount" )
@@ -84,28 +101,36 @@ def setup():
84
101
logger .info (f"Configuring rclone for { mn } " )
85
102
subprocess .run (["umount" , f"/data/{ mn } " ], check = False , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
86
103
os .makedirs (f"/data/{ mn } " , exist_ok = True )
104
+
87
105
if NFSMOUNT is not None and NFSMOUNT .lower () == "true" :
88
106
if NFSPORT :
89
- port = NFSPORT
107
+ port = NFSPORT
90
108
logger .info (f"Setting up rclone NFS mount server for { mn } at 0.0.0.0:{ port } " )
91
109
rclone_command = ["rclone" , "serve" , "nfs" , f"{ mn } :" , "--config" , "/config/rclone.config" , "--addr" , f"0.0.0.0:{ port } " , "--vfs-cache-mode=full" , "--dir-cache-time=10" ]
92
- else :
93
- port = random .randint (8001 , 8999 )
110
+ else :
111
+ port = random .randint (8001 , 8999 )
94
112
logger .info (f"Setting up rclone NFS mount server for { mn } at 0.0.0.0:{ port } " )
95
- rclone_command = ["rclone" , "serve" , "nfs" , f"{ mn } :" , "--config" , "/config/rclone.config" , "--addr" , f"0.0.0.0:{ port } " , "--vfs-cache-mode=full" , "--dir-cache-time=10" ]
113
+ rclone_command = ["rclone" , "serve" , "nfs" , f"{ mn } :" , "--config" , "/config/rclone.config" , "--addr" , f"0.0.0.0:{ port } " , "--vfs-cache-mode=full" , "--dir-cache-time=10" ]
96
114
else :
97
115
rclone_command = ["rclone" , "mount" , f"{ mn } :" , f"/data/{ mn } " , "--config" , "/config/rclone.config" , "--allow-other" , "--poll-interval=0" , "--dir-cache-time=10" ]
98
116
if not RIVEN or idx != len (mount_names ) - 1 :
99
117
rclone_command .append ("--daemon" )
100
-
101
- logger .info (f"Starting rclone{ ' daemon' if '--daemon' in rclone_command else '' } for { mn } " )
102
- process_name = "rclone"
103
- subprocess_logger = SubprocessLogger (logger , process_name )
104
- process = subprocess .Popen (rclone_command , stdout = subprocess .DEVNULL , stderr = subprocess .PIPE )
105
- subprocess_logger .start_monitoring_stderr (process , mn , process_name )
118
+
119
+ url = f"http://localhost:{ rd_port if mn == RCLONEMN_RD else ad_port } "
120
+ if os .path .exists (f"/healthcheck/{ mn } " ):
121
+ os .rmdir (f"/healthcheck/{ mn } " )
122
+ if wait_for_url (url ):
123
+ os .makedirs (f"/healthcheck/{ mn } " ) # makdir for healthcheck. Don't like it, but it works for now...
124
+ logger .info (f"The Zurg WebDAV URL { url } /dav is accessible. Starting rclone{ ' daemon' if '--daemon' in rclone_command else '' } for { mn } " )
125
+ process_name = "rclone"
126
+ subprocess_logger = SubprocessLogger (logger , process_name )
127
+ process = subprocess .Popen (rclone_command , stdout = subprocess .DEVNULL , stderr = subprocess .PIPE )
128
+ subprocess_logger .start_monitoring_stderr (process , mn , process_name )
129
+ else :
130
+ logger .error (f"The Zurg WebDav URL { url } /dav is not accessible within the timeout period. Skipping rclone setup for { mn } " )
106
131
107
132
logger .info ("rclone startup complete" )
108
133
109
134
except Exception as e :
110
135
logger .error (e )
111
- exit (1 )
136
+ exit (1 )
0 commit comments