YongchaoWu | 4e489b0 | 2020-02-24 09:18:16 +0100 | [diff] [blame] | 1 | import json |
| 2 | import subprocess |
| 3 | import os |
| 4 | |
| 5 | print("Update fresh ric configuration in Consul configuration file") |
| 6 | |
| 7 | p = os.path.abspath('..') |
| 8 | consul_config = p + '/consul_cbs' + '/config.json' |
| 9 | |
| 10 | |
| 11 | def write_json(data, filename=consul_config): |
| 12 | with open(filename, 'w') as f: |
| 13 | json.dump(data, f, indent=4) |
| 14 | |
| 15 | |
| 16 | def bash_command(cmd): |
| 17 | result = [] |
| 18 | sp = subprocess.Popen(['/bin/bash', '-c', cmd], stdout=subprocess.PIPE) |
| 19 | for line in sp.stdout.readlines(): |
| 20 | result.append(line.decode().strip()) |
| 21 | return result |
| 22 | |
| 23 | |
| 24 | command = "docker ps | grep simulator | awk '{print $NF}'" |
| 25 | |
| 26 | ric_list = bash_command(command) |
| 27 | |
| 28 | with open(consul_config) as json_file: |
| 29 | data = json.load(json_file) |
| 30 | temp = data['ric'] |
| 31 | for ric in ric_list: |
| 32 | y = {"name": ric, |
| 33 | "baseUrl": "http://" + ric + ":8085/", |
| 34 | "managedElementIds": [ |
| 35 | "kista_" + ric, |
| 36 | "stockholm_" + ric |
| 37 | ] |
| 38 | } |
| 39 | temp.append(y) |
| 40 | |
| 41 | |
| 42 | write_json(data) |
| 43 | print("Update Consul config file with fresh ric configuration, done") |