@@ -228,29 +228,23 @@ def merge_ymls(base, addon, databases=[]):
228228 # top level containers, that is names like "mysql" and "mysql-data".
229229 # "name" is the container name, "obj" contains the settings
230230 for name , obj in addon .copy ().iteritems ():
231- # if there is a key called "needs " then return its value, otherwise return empty list
232- needs = 'needs ' in obj and obj .pop ('needs ' ) or []
231+ # if there is a key called "actions " then return its value, otherwise return empty list
232+ actions = 'actions ' in obj and obj .pop ('actions ' ) or []
233233
234234 # merges each container of the new service's docker-compose.yml into the base.yml
235235 merged = data_merge (base , {name : obj })
236236
237- # the following code makes basically sure, that databases are created and
238- # that the adapter is linking to all containers.
239- for need in needs :
240- # if it is a container name, do nothing
241- if type (need ) == str :
242- # pass is required in python when a command is required syntactically
243- # but nothing needs to be done
244- pass
245- elif type (need ) == dict :
246- for k , v in need .iteritems ():
237+ # the following code makes sure that databases are created
238+ for action in actions :
239+ if type (action ) == dict :
240+ for k , v in action .iteritems ():
247241 if k == "create_databases" :
248242 print ("Added database {}" .format (v ))
249243 databases .append (v )
250244 else :
251245 merged , databases = merge_ymls (merged , {k : v }, databases )
252246 else :
253- print ("Need is {}" .format (need ))
247+ print ("Action is {}" .format (action ))
254248 raise
255249 # print(merged)
256250
@@ -417,38 +411,71 @@ def box_install(args):
417411
418412 generate_yml (dir )
419413
420- # start box with updated docker-compose.yml
421- subprocess .check_output (["docker-compose" , "up" , "-d" ])
422-
423- # now, update the adapter to get the internal IP of the newly added service
424-
425- """
426- proxied_servers = []
427- proxied_ips = {}
414+ database_env_files = []
415+ databases = {}
416+ # read out service's docker-compose.yml file, find create_databases there
417+ # since databases only need to be created at install time.
418+ with open (addon_yml , 'r' ) as f :
419+ addon = yaml .load (f .read ())
428420
429- # parse nginx.conf to get a list of all proxied containers
430- #TODO: make sure to only read URLs after a "proxy_pass"
431- with open(join(servicedir, service_name, "nginx.conf")) as f:
432- proxied_servers = [w.replace('http://', '') for w in re.findall('(https?:\/\/[A-Za-z0-9]+)',f.read())]
421+ for name , obj in addon .copy ().iteritems ():
422+ # if there is a key called "actions" then return its value, otherwise return empty list
423+ actions = 'actions' in obj and obj .pop ('actions' ) or []
433424
434- # now get the IPs for the proxied containers
435- for proxy in proxied_servers:
436- container_ip = subprocess.check_output(["docker", "inspect",
437- "-f", "'{{ .NetworkSettings.IPAddress }}'", proxy]).replace("'", "").strip()
438- proxied_ips[proxy] = container_ip
425+ # the following code makes sure that databases to be created are identified
426+ for action in actions :
427+ if type (action ) == dict :
428+ for k , v in action .iteritems ():
429+ if k == "create_databases" :
430+ database_env_files .append (v )
431+
432+ # now we have a list of environment files to check for database names
433+ for env_file in database_env_files :
434+ # we support one database per env file currently
435+ db_key = ""
436+ db_name = ""
437+ db_username = ""
438+ db_file = ""
439+ db_exists = False
440+ with open (join (servicedir , service_name , env_file )) as infile :
441+ for line in infile :
442+ if "_MYSQL_NAME" in line :
443+ db_key = line .split ("_MYSQL_NAME=" )[0 ]
444+ db_name = line .split ("=" )[1 ]
445+ elif "_MYSQL_USERNAME" in line :
446+ db_username = line .split ("=" )[1 ]
447+ elif "_MYSQL_FILE" in line :
448+ db_file = line .split ("=" )[1 ].replace ('\n ' , '' )
449+ db_exists = True
450+ shutil .copyfile (join (servicedir , service_name , db_file ), join (dir , "tmp" , "sqlfile" , "db.sql" ))
451+
452+ # save name and username to secret.env that mysql-create is using
453+ with open (join (dir , "tmp" , "secret.env" ), 'w' ) as f :
454+ #f.writelines(["SERVICE_DB_NAME=%s" % db_name, "SERVICE_DB_USER=%s" % db_username])
455+ f .write ('SERVICE_DB_NAME={}SERVICE_DB_USER={}' .format (db_name ,db_username ))
456+ if not db_exists :
457+ f .write ("SERVICE_DB_EXISTS=True" )
458+
459+ # generate database and parse output
460+ mysqlcreate_output = subprocess .check_output (["docker-compose" , "run" , "mysqlcreate" ])
461+ db_password = mysqlcreate_output .split (" -p" )[1 ].split (" -hmysql" )[0 ]
462+ # write password to env file
463+ with open (join (servicedir , service_name , env_file ), "a" ) as outfile :
464+ outfile .write ("{}_MYSQL_PASSWORD={}" .format (db_key , db_password ))
465+
466+ # clean up and delete secret.env content, otherwise db is created again
467+ with open (join (dir , "tmp" , "secret.env" ), "w" ):
468+ pass
469+ # clean up and delete sqlfile
470+ try :
471+ os .remove (join (dir , "tmp" , "sqlfile" , "db.sql" ))
472+ except OSError :
473+ pass
439474
440- # finally, replace the values
441- with open(join(servicedir, service_name, "nginx.conf")) as infile, open(join(servicedir, service_name, "nginx.adapted.conf"), 'w') as outfile:
442- for line in infile:
443- for src, target in proxied_ips.iteritems():
444- line = line.replace("http://" + src, "http://" + target)
445- outfile.write(line)
446-
447- # copy nginx.adapted.conf into container
448- #TODO: run the script directly from python, including sending HUP signal to adapter
449- subprocess.check_output(["./add_nginx_conf.sh", service_name])
450- """
475+ # start box with updated docker-compose.yml
476+ subprocess .check_output (["docker-compose" , "up" , "-d" ])
451477
478+ # now, update the adapter to get the internal IP of the newly added service
452479 update_adapter (config , servicedir , service_name );
453480
454481 return 0
@@ -616,6 +643,10 @@ def box_init(args):
616643 return 0
617644
618645
646+ def create_databases ():
647+ return 0
648+
649+
619650def parse_args ():
620651 """Parses the command line arguments to choose which action should be taken
621652 """
0 commit comments