6 #chef_update_string = '{"default_attributes": {"asdc-connections": {"asdc-controller1": {"environmentName": "test"}}}}'
7 #json_file_to_update="./volumes/mso/chef-config/mso-docker.json"
11 print(" echo <json> | %s <file> " % ( sys.argv[0], ))
13 print("file\tpath of the valid json file to update")
14 print("json\ta json string containing the values to update in <file>")
18 echo '{"default_attributes": {"asdc-connections": {"asdc-controller1": {"environmentName": "test"}}}}' | %s ./volumes/mso/chef-config/mso-docker.json
26 if sys.argv[1] in ('--help','-h'):
31 updates_json = sys.stdin.read()
32 print(updates_json[81:])
33 updates = json.loads(updates_json)
36 json_file_to_update = sys.argv[1]
37 with open(json_file_to_update) as f:
38 to_update = json.load(f)
40 # update config with updates
41 def update(config, updates):
42 #assert isinstance(config, dict)
43 #assert isinstance(updates, dict)
45 # if key is a simple type, update it. Otherwise, update sub values
48 for update_key,update_value in updates.items():
49 if update_key not in config:
50 raise Exception('Incorrect parameter : %s' % (update_key,))
51 if isinstance(update_value, (int, str)):
52 config[update_key] = update_value
53 elif isinstance (update_value,list) and isinstance (config[update_key],list):
54 config[update_key] = update_value
56 update(config[update_key], update_value)
58 update(to_update, updates)
61 tmp_file = '%s.tmp' % (json_file_to_update,)
62 with open(tmp_file, 'w') as f:
63 json.dump(to_update, f, sort_keys=True, indent=2)
64 os.rename(tmp_file, json_file_to_update)