|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import yaml |
| 3 | +import sys |
| 4 | +import netifaces as ni |
| 5 | + |
| 6 | +ni.ifaddresses('en0') |
| 7 | +ip = ni.ifaddresses('en0')[ni.AF_INET][0]['addr'] |
| 8 | + |
| 9 | +# List of clients |
| 10 | +CLIENTS = ['flask', 'vue', 'rubyonrails'] |
| 11 | + |
| 12 | +# docker-compose.yml skeleton to fill out using "service" entries above. |
| 13 | +COMPOSITION = {'version': '3', 'services': {}} |
| 14 | + |
| 15 | +if __name__ == '__main__': |
| 16 | + |
| 17 | + error_clients = [] |
| 18 | + error_services = [] |
| 19 | + installation_path = sys.argv[1] |
| 20 | + args = sys.argv[2:] |
| 21 | + args = list(dict.fromkeys(args)) |
| 22 | + |
| 23 | + services = [x for x in args if x not in CLIENTS] |
| 24 | + clients = [x for x in args if x in CLIENTS] |
| 25 | + |
| 26 | + # Loads the master yaml containing docker service definitions |
| 27 | + with open(installation_path+'/master.yaml') as master_service_file: |
| 28 | + master_services = yaml.load(master_service_file, Loader=yaml.FullLoader) |
| 29 | + |
| 30 | + # Populates clients in docker-compose |
| 31 | + for client in clients: |
| 32 | + if client in master_services: |
| 33 | + COMPOSITION['services'][client] = master_services[client] |
| 34 | + COMPOSITION['services'][client]['depends_on'] = [] |
| 35 | + else: |
| 36 | + error_clients.append(client) |
| 37 | + CLIENTS.remove(client) |
| 38 | + |
| 39 | + # Populates services requested by user |
| 40 | + for service in services: |
| 41 | + if service in master_services: |
| 42 | + for client in clients: |
| 43 | + COMPOSITION['services'][client]['depends_on'].append(service) |
| 44 | + COMPOSITION['services'][service] = master_services[service] |
| 45 | + else: |
| 46 | + error_services.append(service) |
| 47 | + if service == 'kafka': |
| 48 | + COMPOSITION['services'][service]['environment']['KAFKA_ADVERTISED_HOST_NAME'] = ip |
| 49 | + |
| 50 | + with open(installation_path+'docker-compose.yml', 'w') as outfile: |
| 51 | + yaml.dump(COMPOSITION, outfile, default_flow_style=False, indent=2) |
| 52 | + |
| 53 | + if len(error_services) > 0: |
| 54 | + print("Couldn't add the following services: ") |
| 55 | + for service in error_services: |
| 56 | + print("- " + service + "\n") |
0 commit comments