21
21
22
22
import argparse
23
23
import hashlib
24
- import http .server
25
24
import logging
26
25
import json
27
26
import os
32
31
import time
33
32
import traceback
34
33
import sys
34
+ import tempfile
35
35
from pathlib import Path
36
36
37
+ from http .server import HTTPServer , ThreadingHTTPServer , BaseHTTPRequestHandler
38
+
37
39
from watchdog .observers import Observer
38
40
from watchdog .events import FileSystemEventHandler
39
41
@@ -58,7 +60,7 @@ def __init__(self):
58
60
)
59
61
parser .add_argument (
60
62
"--tls" ,
61
- help = ("This flag enables TLS for incoming " " connections" ),
63
+ help = ("This flag enables TLS for incoming connections" ),
62
64
action = "store_true" ,
63
65
)
64
66
parser .add_argument (
@@ -103,7 +105,7 @@ def __init__(self):
103
105
)
104
106
parser .add_argument (
105
107
"--input-dir" ,
106
- help = ("The directory containing the input files " " the config map." ),
108
+ help = ("The directory containing the input files the config map." ),
107
109
default = "/var/input-files" ,
108
110
)
109
111
parser .add_argument (
@@ -125,28 +127,26 @@ def __init__(self):
125
127
)
126
128
parser .add_argument (
127
129
"--copy-file" ,
128
- help = ("A file to copy from the config map to the " " output directory." ),
130
+ help = ("A file to copy from the config map to the output directory." ),
129
131
action = "append" ,
130
132
)
131
133
parser .add_argument (
132
134
"--copy-binary" ,
133
- help = ("A binary to copy from the to the output" " directory." ),
135
+ help = ("A binary to copy from the to the output directory." ),
134
136
action = "append" ,
135
137
)
136
138
parser .add_argument (
137
139
"--copy-library" ,
138
- help = (
139
- "A version of the client library to copy " "to the output directory."
140
- ),
140
+ help = ("A version of the client library to copy to the output directory." ),
141
141
action = "append" ,
142
142
)
143
143
parser .add_argument (
144
144
"--input-monitor-conf" ,
145
- help = ("The name of a monitor conf template in the " " input files" ),
145
+ help = ("The name of a monitor conf template in the input files" ),
146
146
)
147
147
parser .add_argument (
148
148
"--main-container-version" ,
149
- help = ("The version of the main foundationdb " " container in the pod" ),
149
+ help = ("The version of the main foundationdb container in the pod" ),
150
150
)
151
151
parser .add_argument (
152
152
"--main-container-conf-dir" ,
@@ -306,7 +306,7 @@ def is_at_least(self, target_version):
306
306
)
307
307
308
308
309
- class Server (http . server . BaseHTTPRequestHandler ):
309
+ class Server (BaseHTTPRequestHandler ):
310
310
ssl_context = None
311
311
312
312
@classmethod
@@ -316,12 +316,12 @@ def start(cls):
316
316
"""
317
317
config = Config .shared ()
318
318
(address , port ) = config .bind_address .split (":" )
319
- log .info ("Listening on %s:%s" % ( address , port ) )
320
- httpd = http . server . HTTPServer ((address , int (port )), cls )
319
+ log .info (f "Listening on { address } : { port } " )
320
+ server = ThreadingHTTPServer ((address , int (port )), cls )
321
321
322
322
if config .enable_tls :
323
323
context = Server .load_ssl_context ()
324
- httpd .socket = context .wrap_socket (httpd .socket , server_side = True )
324
+ server .socket = context .wrap_socket (server .socket , server_side = True )
325
325
observer = Observer ()
326
326
event_handler = CertificateEventHandler ()
327
327
for path in set (
@@ -333,7 +333,7 @@ def start(cls):
333
333
observer .schedule (event_handler , path )
334
334
observer .start ()
335
335
336
- httpd .serve_forever ()
336
+ server .serve_forever ()
337
337
338
338
@classmethod
339
339
def load_ssl_context (cls ):
@@ -516,7 +516,19 @@ def log_message(self, format, *args):
516
516
517
517
class CertificateEventHandler (FileSystemEventHandler ):
518
518
def on_any_event (self , event ):
519
- log .info ("Detected change to certificates" )
519
+ if event .is_directory :
520
+ return None
521
+
522
+ if event .event_type not in ["created" , "modified" ]:
523
+ return None
524
+
525
+ # We ignore all old files
526
+ if event .src_path .endswith (".old" ):
527
+ return None
528
+
529
+ log .info (
530
+ f"Detected change to certificates path: { event .src_path } , type: { event .event_type } "
531
+ )
520
532
time .sleep (10 )
521
533
log .info ("Reloading certificates" )
522
534
Server .load_ssl_context ()
@@ -536,10 +548,13 @@ def copy_files():
536
548
path = os .path .join (config .input_dir , filename )
537
549
if not os .path .isfile (path ) or os .path .getsize (path ) == 0 :
538
550
raise Exception ("No contents for file %s" % path )
551
+
539
552
for filename in config .copy_files :
540
- tmp_file = os .path .join (config .output_dir , f"{ filename } .tmp" )
541
- shutil .copy (os .path .join (config .input_dir , filename ), tmp_file )
542
- os .replace (tmp_file , os .path .join (config .output_dir , filename ))
553
+ tmp_file = tempfile .NamedTemporaryFile (
554
+ mode = "w+b" , dir = config .output_dir , delete = False
555
+ )
556
+ shutil .copy (os .path .join (config .input_dir , filename ), tmp_file .name )
557
+ os .replace (tmp_file .name , os .path .join (config .output_dir , filename ))
543
558
544
559
return "OK"
545
560
@@ -554,9 +569,13 @@ def copy_binaries():
554
569
)
555
570
if not target_path .exists ():
556
571
target_path .parent .mkdir (parents = True , exist_ok = True )
557
- tmp_file = f"{ target_path } .tmp"
558
- shutil .copy (path , tmp_file )
559
- os .replace (tmp_file , target_path )
572
+ tmp_file = tempfile .NamedTemporaryFile (
573
+ mode = "w+b" ,
574
+ dir = target_path .parent ,
575
+ delete = False ,
576
+ )
577
+ shutil .copy (path , tmp_file .name )
578
+ os .replace (tmp_file .name , target_path )
560
579
target_path .chmod (0o744 )
561
580
return "OK"
562
581
@@ -573,9 +592,11 @@ def copy_libraries():
573
592
)
574
593
if not target_path .exists ():
575
594
target_path .parent .mkdir (parents = True , exist_ok = True )
576
- tmp_file = f"{ target_path } .tmp"
577
- shutil .copy (path , tmp_file )
578
- os .replace (tmp_file , target_path )
595
+ tmp_file = tempfile .NamedTemporaryFile (
596
+ mode = "w+b" , dir = target_path .parent , delete = False
597
+ )
598
+ shutil .copy (path , tmp_file .name )
599
+ os .replace (tmp_file .name , target_path )
579
600
return "OK"
580
601
581
602
@@ -591,13 +612,16 @@ def copy_monitor_conf():
591
612
"$" + variable , config .substitutions [variable ]
592
613
)
593
614
594
- tmp_file = os .path .join (config .output_dir , "fdbmonitor.conf.tmp" )
615
+ tmp_file = tempfile .NamedTemporaryFile (
616
+ mode = "w+b" , dir = config .output_dir , delete = False
617
+ )
595
618
target_file = os .path .join (config .output_dir , "fdbmonitor.conf" )
596
619
597
- with open (tmp_file , "w" ) as output_conf_file :
620
+ with open (tmp_file . name , "w" ) as output_conf_file :
598
621
output_conf_file .write (monitor_conf )
599
622
600
- os .replace (tmp_file , target_file )
623
+ os .replace (tmp_file .name , target_file )
624
+
601
625
return "OK"
602
626
603
627
@@ -629,5 +653,7 @@ def __init__(self, message):
629
653
copy_libraries ()
630
654
copy_monitor_conf ()
631
655
632
- if not Config .shared ().init_mode :
633
- Server .start ()
656
+ if Config .shared ().init_mode :
657
+ sys .exit (0 )
658
+
659
+ Server .start ()
0 commit comments