@@ -418,10 +418,11 @@ def box_install(args):
418418 generate_yml (dir )
419419
420420 # start box with updated docker-compose.yml
421- box_start ( args )
421+ subprocess . check_output ([ "docker-compose" , "up" , "-d" ] )
422422
423- #TODO: AFTER SOLVING A TODO ONLY REMOVE THE "TODO:" NOT THE WHOLE LINE!!!
423+ # now, update the adapter to get the internal IP of the newly added service
424424
425+ """
425426 proxied_servers = []
426427 proxied_ips = {}
427428
@@ -446,6 +447,62 @@ def box_install(args):
446447 # copy nginx.adapted.conf into container
447448 #TODO: run the script directly from python, including sending HUP signal to adapter
448449 subprocess.check_output(["./add_nginx_conf.sh", service_name])
450+ """
451+
452+ update_adapter (config , servicedir , service_name );
453+
454+ return 0
455+
456+ def update_adapter (config , service_directory , service_name ):
457+ """Informs the adapter of a new service or updates new service configuration in the adapter.
458+
459+ :config: parsed box.yml
460+ :service_directory: the directory where the services are saved
461+ :service_name: the name of the newly added service, or None for updating all services
462+ :returns: shell return code
463+ """
464+
465+ # a list of all service names to update
466+ servicesToUpdate = []
467+
468+ if service_name is None :
469+ # update IPs of all services
470+ services = config ['services' ]
471+ for service_name , service in services .iteritems ():
472+ servicesToUpdate .append (service_name )
473+ else :
474+ # parse nginx.conf of the service to get a list of all its proxied containers
475+ servicesToUpdate .append (service_name )
476+
477+ proxied_servers = []
478+ proxied_ips = {}
479+
480+ for service_name in servicesToUpdate :
481+ # parse nginx.conf of all services to get a list of all proxied containers
482+ #TODO: make sure to only read URLs after a "proxy_pass"
483+ with open (join (service_directory , service_name , "nginx.conf" )) as f :
484+ proxied_servers = [w .replace ('http://' , '' ) for w in re .findall ('(https?:\/\/[A-Za-z0-9]+)' ,f .read ())]
485+
486+ # now get the IPs of the proxied containers
487+ for proxy in proxied_servers :
488+ container_ip = subprocess .check_output (["docker" , "inspect" ,
489+ "-f" , "'{{ .NetworkSettings.IPAddress }}'" , proxy ]).replace ("'" , "" ).strip ()
490+ proxied_ips [proxy ] = container_ip
491+
492+ # finally, replace the values
493+ for service_name in servicesToUpdate :
494+ with open (join (service_directory , service_name , "nginx.conf" )) as infile , open (join (service_directory , service_name , "nginx.adapted.conf" ), 'w' ) as outfile :
495+ for line in infile :
496+ for src , target in proxied_ips .iteritems ():
497+ line = line .replace ("http://" + src , "http://" + target )
498+ outfile .write (line )
499+
500+ # copy nginx.adapted.conf into container
501+ #TODO: run the whole script directly from python
502+ subprocess .check_output (["./add_nginx_conf.sh" , service_name ])
503+
504+ # send the adapter the HUP kill signal
505+ subprocess .check_output (["docker" , "kill" , '--signal="HUP"' , "adapter" ])
449506
450507 return 0
451508
@@ -462,7 +519,10 @@ def box_start(args):
462519 return 1
463520 subprocess .check_output (["docker-compose" , "up" , "-d" ])
464521
465- #TODO: regenerate nginx.adapted.conf files for all proxied services
522+ # regenerate nginx.adapted.conf files for all proxied services
523+ config = read_config (dir )
524+ servicedir = join (dir , "services" )
525+ update_adapter (config , servicedir , None );
466526
467527 print ("You can now reach your Layers Box at https://{}/" .format (url ))
468528 return 0
0 commit comments