55import subprocess
66import sys
77from argparse import ArgumentParser
8+ from collections import namedtuple
89from configparser import ConfigParser , NoOptionError
910from contextlib import suppress
10- from collections import namedtuple
1111from tempfile import NamedTemporaryFile
1212from textwrap import dedent
1313
1414
1515class CaddyfileGenerator :
16-
17- Site = namedtuple ('Site' , [
18- 'alias_domains' ,
19- 'backend_authority' ,
20- 'domain' ,
21- ])
16+ Site = namedtuple (
17+ "Site" ,
18+ [
19+ "alias_domains" ,
20+ "backend_authority" ,
21+ "domain" ,
22+ ],
23+ )
2224
2325 def __init__ (self ):
2426 self ._redir_target_of = {}
@@ -30,31 +32,41 @@ class CaddyfileGenerator:
3032 self ._redir_target_of [domain ] = site .domain
3133
3234 def write_to (self , fp ):
33- print (dedent ("""\
35+ print (
36+ dedent ("""\
3437 # NOTE: This file has been generated, do not edit
3538 (common) {
3639 encode zstd gzip
3740 log {
3841 output stdout
3942 }
40- }""" ), file = fp )
43+ }""" ),
44+ file = fp ,
45+ )
4146
4247 for domain , backend_authority in sorted (self ._backend_of .items ()):
43- print (dedent ("""
48+ print (
49+ dedent ("""
4450 %s {
4551 import common
4652 reverse_proxy %s {
4753 header_down +Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
4854 }
49- }""" ) % (domain , backend_authority ), file = fp )
50-
51- for source_domain , target_domain in sorted (
52- self ._redir_target_of .items ()):
53- print (dedent ("""
55+ }""" ) # noqa: E501
56+ % (domain , backend_authority ),
57+ file = fp ,
58+ )
59+
60+ for source_domain , target_domain in sorted (self ._redir_target_of .items ()):
61+ print (
62+ dedent ("""
5463 %s {
5564 import common
5665 redir https://%s{uri}
57- }""" ) % (source_domain , target_domain ), file = fp )
66+ }""" )
67+ % (source_domain , target_domain ),
68+ file = fp ,
69+ )
5870
5971
6072def run (options ):
@@ -65,51 +77,59 @@ def run(options):
6577 caddyfile = CaddyfileGenerator ()
6678 for domain in config .sections ():
6779 try :
68- alias_domains = config .get (domain , ' aliases' ).split ()
80+ alias_domains = config .get (domain , " aliases" ).split ()
6981 except NoOptionError :
7082 alias_domains = []
71- backend_authority = config .get (domain , ' backend' )
83+ backend_authority = config .get (domain , " backend" )
7284
73- site = CaddyfileGenerator .Site (alias_domains , backend_authority ,
74- domain )
85+ site = CaddyfileGenerator .Site (alias_domains , backend_authority , domain )
7586 caddyfile .add (site )
7687
7788 with NamedTemporaryFile () as temp_file :
7889 # The idea is to diff against previous content or against
7990 # empty file when there is no previos content
80- with suppress (OSError ):
81- with open (options .output_filename , ' r+b' ) as fin :
91+ with suppress (OSError ): # noqa: SIM117
92+ with open (options .output_filename , " r+b" ) as fin :
8293 temp_file .file .write (fin .read ())
8394 temp_file .file .flush ()
8495
85- with open (options .output_filename , 'w' ) as fout :
96+ with open (options .output_filename , "w" ) as fout :
8697 caddyfile .write_to (fout )
8798
88- exit_code = subprocess .call (['diff' , '-u' , temp_file .name ,
89- options .output_filename ])
99+ exit_code = subprocess .call ( # noqa: S603
100+ ["diff" , "-u" , temp_file .name , options .output_filename ],
101+ )
90102
91103 # Write stateful output in the format expected by SaltStack
92104 if exit_code and options .saltstack :
93105 print ()
94106 print ("changed=yes comment='Caddyfile changed'" )
95107
96108
97- if __name__ == ' __main__' :
109+ if __name__ == " __main__" :
98110 parser = ArgumentParser ()
99- parser .add_argument ('--config' , dest = 'config_filename' , metavar = 'FILENAME' ,
100- default = 'sites.cfg' ,
101- help = 'Path to config file to read'
102- ' (default: %(default)s)' )
103- parser .add_argument ('--output' , dest = 'output_filename' , metavar = 'FILENAME' ,
104- default = 'Caddyfile' ,
105- help = 'Path to write Caddyfile to'
106- ' (default: %(default)s)' )
107- parser .add_argument ('--saltstack' , action = 'store_true' ,
108- help = 'Add lines signaling changes to SaltStack'
109- ' (default: omitted)' )
111+ parser .add_argument (
112+ "--config" ,
113+ dest = "config_filename" ,
114+ metavar = "FILENAME" ,
115+ default = "sites.cfg" ,
116+ help = "Path to config file to read (default: %(default)s)" ,
117+ )
118+ parser .add_argument (
119+ "--output" ,
120+ dest = "output_filename" ,
121+ metavar = "FILENAME" ,
122+ default = "Caddyfile" ,
123+ help = "Path to write Caddyfile to (default: %(default)s)" ,
124+ )
125+ parser .add_argument (
126+ "--saltstack" ,
127+ action = "store_true" ,
128+ help = "Add lines signaling changes to SaltStack (default: omitted)" ,
129+ )
110130 options = parser .parse_args ()
111131
112132 try :
113133 run (options )
114- except IOError as e :
134+ except OSError as e :
115135 sys .exit (e )
0 commit comments