@@ -22,6 +22,10 @@ from ocflib.vhost.web import get_vhosts
2222
2323APACHE_SITE_CONFIG = '/etc/apache2/ocf-vhost.conf'
2424NGINX_SITE_CONFIG = '/etc/nginx/sites-enabled/virtual'
25+ NGINX_WEB_SITE_CONFIG = '/etc/nginx/ocf-vhost.conf'
26+
27+ # Must match $backend_port in ocf_www::init
28+ BACKEND_PORT = 16767
2529
2630LETS_ENCRYPT_SSL = Path ('/services/http/ssl' )
2731SYSTEM_SSL = Path ('/etc/ssl/private' )
@@ -259,15 +263,15 @@ def build_config(src_vhosts, template, dev_config=False):
259263 ))
260264
261265 return '\n \n ' .join (
262- template .render (vhost = vhost )
266+ template .render (vhost = vhost , backend_port = BACKEND_PORT )
263267 for vhost in sorted (
264268 vhosts ,
265269 key = lambda vhost : (vhost .user , vhost .fqdn , bool (vhost .ssl )),
266270 )
267271 )
268272
269273
270- def test_and_overwrite_config (config_path , new_config , target ):
274+ def test_and_overwrite_config (config_path , new_config , test_cmd ):
271275 """Diffs and tests the new config and overwrites the old config if
272276 the test passes.
273277
@@ -306,10 +310,7 @@ def test_and_overwrite_config(config_path, new_config, target):
306310 os .rename (new_path , config_path )
307311
308312 report ('Performing config test.' )
309- if target == 'web' :
310- ret = subprocess .call (('apachectl' , 'configtest' ))
311- else :
312- ret = subprocess .call (('nginx' , '-t' ))
313+ ret = subprocess .call (test_cmd )
313314
314315 if ret != 0 :
315316 report ('Test failed!' )
@@ -404,7 +405,6 @@ def main():
404405 changed |= process_app_vhosts ()
405406
406407 if args .target == 'web' :
407- site_cfg = APACHE_SITE_CONFIG
408408 # Build app vhosts so that they can get proxied to apphost.o.b.e
409409 # Placed before regular vhosts so they take priority in domain matching
410410 # (sometimes hosts have entries in both vhost.conf and vhost-app.conf)
@@ -414,17 +414,58 @@ def main():
414414 for domain , conf in get_app_vhosts ().items ()
415415 if 'dev' not in conf ['flags' ]
416416 }
417- config = build_config (
417+
418+ web_vhosts = get_vhosts ()
419+
420+ # Apache config (existing behavior)
421+ apache_config = build_config (
418422 prod_app_vhosts ,
419423 jinja_env .get_template ('vhost-web.jinja' ),
420424 dev_config = args .dev ,
421425 )
422- config += '\n \n '
423- config += build_config (
424- get_vhosts () ,
426+ apache_config += '\n \n '
427+ apache_config += build_config (
428+ web_vhosts ,
425429 jinja_env .get_template ('vhost-web.jinja' ),
426430 dev_config = args .dev ,
427431 )
432+
433+ # Nginx frontend config
434+ nginx_config = build_config (
435+ prod_app_vhosts ,
436+ jinja_env .get_template ('vhost-web-nginx.jinja' ),
437+ dev_config = args .dev ,
438+ )
439+ nginx_config += '\n \n '
440+ nginx_config += build_config (
441+ web_vhosts ,
442+ jinja_env .get_template ('vhost-web-nginx.jinja' ),
443+ dev_config = args .dev ,
444+ )
445+
446+ if args .dry_run :
447+ report ('=== Apache config ===' )
448+ report (apache_config )
449+ report ('\n === Nginx config ===' )
450+ report (nginx_config )
451+ return 0
452+
453+ changed |= test_and_overwrite_config (
454+ APACHE_SITE_CONFIG , apache_config , ('apachectl' , 'configtest' ),
455+ )
456+ changed |= test_and_overwrite_config (
457+ NGINX_WEB_SITE_CONFIG , nginx_config , ('nginx' , '-t' ),
458+ )
459+
460+ if changed :
461+ if not args .no_reload :
462+ report ('Things changed, reloading.' )
463+ subprocess .check_call (('systemctl' , 'reload' , 'apache2' ))
464+ subprocess .check_call (('systemctl' , 'reload' , 'nginx' ))
465+ else :
466+ report ('Not reloading, as you requested.' )
467+ else :
468+ report ('Nothing changed, not doing anything.' )
428469 else :
429470 site_cfg = NGINX_SITE_CONFIG
430471 config = build_config (
@@ -433,22 +474,22 @@ def main():
433474 dev_config = args .dev ,
434475 )
435476
436- if args .dry_run :
437- report (config )
438- return 0
477+ if args .dry_run :
478+ report (config )
479+ return 0
439480
440- changed |= test_and_overwrite_config (site_cfg , config , args . target )
441- if changed :
442- if not args . no_reload :
443- report ( 'Things changed, reloading.' )
444- if args . target == 'web' :
445- subprocess . check_call (( 'systemctl' , 'reload' , 'apache2' ))
446- else :
481+ changed |= test_and_overwrite_config (
482+ site_cfg , config , ( 'nginx' , '-t' ),
483+ )
484+
485+ if changed :
486+ if not args . no_reload :
487+ report ( 'Things changed, reloading.' )
447488 subprocess .check_call (('systemctl' , 'reload' , 'nginx' ))
489+ else :
490+ report ('Not reloading, as you requested.' )
448491 else :
449- report ('Not reloading, as you requested.' )
450- else :
451- report ('Nothing changed, not doing anything.' )
492+ report ('Nothing changed, not doing anything.' )
452493
453494
454495if __name__ == '__main__' :
0 commit comments