Skip to content

Commit 7d51222

Browse files
committed
Fixed algorithm for getting all proxied services
1 parent 05778ec commit 7d51222

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

add_nginx_conf.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if [ $# -le 1 ]
88
elif [ $# -eq 0 ]
99
then
1010
echo "No arguments passed to setup script. Aborting..."
11-
exit 1
11+
exit 1
1212
else
1313
echo "Too many service names specified! Aborting..."
1414
exit 1
@@ -22,29 +22,29 @@ out=$(docker ps -a | grep -c adapter-data)
2222
if [ $out -gt 0 ]
2323
then
2424
echo "Adapter data container found. Checking whether the service has been added already..."
25-
nsv=$td/documentation
26-
osv=$(docker exec adapter ls $nsv)
27-
25+
nsv=$td/$sn
26+
osv=$(docker exec adapter ls $nsv)
27+
2828
if [ -z $osv ]
29-
then
29+
then
3030
echo "No previous configuration for this service found. Proceeding with setup..."
31-
echo "Creating path for new dir..."
31+
echo "Creating path for new dir..."
3232
else
3333
echo "Previous configuration detected. Deleting old configuration..."
3434
docker exec adapter rm -rf $osv
35-
echo "Recreating path for new dir..."
35+
echo "Recreating path for new dir..."
3636
fi
3737

3838
docker exec adapter mkdir -p $nsv/
3939

40-
sf="nginx.${sn}.conf"
41-
if [ -f ./services/$sn/$sf ]
42-
then
43-
echo "Service's nginx.conf found."
44-
echo "Copying service's nginx.conf to newly created path $nsv..."
45-
docker cp ./services/documentation/${sf} adapter-data:$nsv
40+
sf="nginx.adapted.conf"
41+
if [ -f ./services/$sn/$sf ]
42+
then
43+
echo "Service's nginx.adapted.conf found."
44+
echo "Copying service's nginx.adapted.conf to newly created path $nsv..."
45+
docker cp ./services/$sn/${sf} adapter-data:$nsv
4646
else
47-
echo "No configuration file found! The file must be named 'nginx.conf'. Aborting..."
47+
echo "No configuration file found! The file must be named 'nginx.adapted.conf'. Aborting..."
4848
exit 1
4949
fi
5050

layersbox

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -420,38 +420,32 @@ def box_install(args):
420420
# start box with updated docker-compose.yml
421421
box_start(args)
422422

423-
# get internal IP of new container
424-
# (linking only those container names that appear there)
425-
#TODO: AFTER SOLVING A TODO ONLY REMOVE THE "TODO:" FROM THE LINE!!!
426-
#TODO: find out the containers to link, e.g. by reading out nginx.conf
427-
#TODO: currently, this only gets the IP if there is a container that by coincidence has the same name as the installer
428-
container_ip = subprocess.check_output(["docker", "inspect",
429-
"-f", "'{{ .NetworkSettings.IPAddress }}'", service_name]).replace("'", "").strip()
430-
431-
entries_list=[]
432-
#separator_location="location"
433-
separator_http="http"
434-
# write IP into nginx.conf
435-
# taken from http://stackoverflow.com/questions/13089234/replacing-text-in-a-file-with-python
436-
with open(join(servicedir, service_name, "nginx.conf")) as infile, open(join(servicedir, service_name, "nginx." + service_name + ".conf"), 'w') as outfile:
423+
#TODO: AFTER SOLVING A TODO ONLY REMOVE THE "TODO:" NOT THE WHOLE LINE!!!
424+
425+
proxied_servers = []
426+
proxied_ips = {}
427+
428+
# parse nginx.conf to get a list of all proxied containers
429+
#TODO: make sure to only read URLs after a "proxy_pass"
430+
with open(join(servicedir, service_name, "nginx.conf")) as f:
431+
proxied_servers = [w.replace('http://', '') for w in re.findall('(https?:\/\/[A-Za-z0-9]+)',f.read())]
432+
433+
# now get the IPs for the proxied containers
434+
for proxy in proxied_servers:
435+
container_ip = subprocess.check_output(["docker", "inspect",
436+
"-f", "'{{ .NetworkSettings.IPAddress }}'", proxy]).replace("'", "").strip()
437+
proxied_ips[proxy] = container_ip
438+
439+
# finally, replace the values
440+
with open(join(servicedir, service_name, "nginx.conf")) as infile, open(join(servicedir, service_name, "nginx.adapted.conf"), 'w') as outfile:
437441
for line in infile:
438-
if separator_http in line:
439-
# for line in infile:
440-
# print("Read the following line:{}".format(line))
441-
entry_name=re.search(r'\//(.*)\:', line.split()[1]).group(0)[2:-1]
442-
entries_list.extend(entry_name)
443-
print("The following location will be added to the Adapter: {}".format(entry_name))
444-
for entry_name in entries_list:
445-
replacements = {'http://' + entry_name :'http://' + container_ip}
446-
for src, target in replacements.iteritems():
447-
line = line.replace(src, target)
448-
outfile.write(line)
449-
450-
# copy nginx.conf into container
451-
#TODO: run the script directly from python
452-
subprocess.check_output(["./add_nginx_conf.sh", service_name])
442+
for src, target in proxied_ips.iteritems():
443+
line = line.replace("http://" + src, "http://" + target)
444+
outfile.write(line)
453445

454-
#TODO: send signal HUP to adapter container (currently done from shell)
446+
# copy nginx.adapted.conf into container
447+
#TODO: run the script directly from python, including sending HUP signal to adapter
448+
subprocess.check_output(["./add_nginx_conf.sh", service_name])
455449

456450
return 0
457451

@@ -464,10 +458,13 @@ def box_start(args):
464458
if not check_tls(dir, url):
465459
return 1
466460
except:
467-
print("This directory does not seem to contain a LayersBox")
461+
print("This directory does not seem to contain a Layers Box configuration.")
468462
return 1
469463
subprocess.check_output(["docker-compose", "up", "-d"])
470-
print("You can reach LayersBox now at https://{}/".format(url))
464+
465+
#TODO: regenerate nginx.adapted.conf files for all proxied services
466+
467+
print("You can now reach your Layers Box at https://{}/".format(url))
471468
return 0
472469

473470

@@ -476,7 +473,7 @@ def box_stop(args):
476473
try:
477474
read_url(args.directory)
478475
except:
479-
print("This directory does not seem to contain a LayersBox")
476+
print("This directory does not seem to contain a Layers Box configuration.")
480477
return 1
481478
subprocess.check_output(["docker-compose", "stop"])
482479
return 0

0 commit comments

Comments
 (0)