def run(self, args): if args.name not in CONFIG_KEYS: output_error("Invalid Config item") with fasteners.InterProcessLock(CUSTOM_CONFIG_FILE): data = json.load(open(DEFAULT_CONFIG_FILE)) if os.path.exists(CUSTOM_CONFIG_FILE): config_json = read_file_content_json(CUSTOM_CONFIG_FILE) data.update(config_json) # Do Nothing if same as previous value if data[args.name] == args.value: return # TODO: Validate Value create_custom_config_file_if_not_exists() new_data = read_file_content_json(CUSTOM_CONFIG_FILE) v = args.value if args.name in BOOL_CONFIGS: v = boolify(args.value) if args.name in INT_CONFIGS: v = int(args.value) new_data[args.name] = v file_content_overwrite(CUSTOM_CONFIG_FILE, new_data) # If any value changed which requires restart of REST server restart = False if args.name in RESTART_CONFIGS: restart = True sync_to_peers(restart=restart)
def sync_to_peers(): if os.path.exists(WEBHOOKS_FILE): try: sync_file_to_peers(WEBHOOKS_FILE_TO_SYNC) except GlusterCmdException as e: output_error("Failed to sync Webhooks file: [Error: {0}]" "{1}".format(e[0], e[2])) if os.path.exists(CUSTOM_CONFIG_FILE): try: sync_file_to_peers(CUSTOM_CONFIG_FILE_TO_SYNC) except GlusterCmdException as e: output_error("Failed to sync Config file: [Error: {0}]" "{1}".format(e[0], e[2])) out = execute_in_peers("node-reload") table = PrettyTable(["NODE", "NODE STATUS", "SYNC STATUS"]) table.align["NODE STATUS"] = "r" table.align["SYNC STATUS"] = "r" for p in out: table.add_row([p.hostname, "UP" if p.node_up else "DOWN", "OK" if p.ok else "NOT OK: {0}".format( p.error)]) print (table)
def sync_to_peers(restart=False): if os.path.exists(WEBHOOKS_FILE): try: sync_file_to_peers(WEBHOOKS_FILE_TO_SYNC) except GlusterCmdException as e: output_error("Failed to sync Webhooks file: [Error: {0}]" "{1}".format(e[0], e[2])) if os.path.exists(CUSTOM_CONFIG_FILE): try: sync_file_to_peers(CUSTOM_CONFIG_FILE_TO_SYNC) except GlusterCmdException as e: output_error("Failed to sync Config file: [Error: {0}]" "{1}".format(e[0], e[2])) action = "node-reload" if restart: action = "node-restart" out = execute_in_peers(action) table = PrettyTable(["NODE", "NODE STATUS", "SYNC STATUS"]) table.align["NODE STATUS"] = "r" table.align["SYNC STATUS"] = "r" for p in out: table.add_row([ p.hostname, "UP" if p.node_up else "DOWN", "OK" if p.ok else "NOT OK: {0}".format(p.error) ]) print(table)
def run(self, args): if args.name not in CONFIG_KEYS: output_error("Invalid Config item") with fasteners.InterProcessLock(CUSTOM_CONFIG_FILE): data = json.load(open(DEFAULT_CONFIG_FILE)) if os.path.exists(CUSTOM_CONFIG_FILE): config_json = read_file_content_json(CUSTOM_CONFIG_FILE) data.update(config_json) # Do Nothing if same as previous value if data[args.name] == args.value: return # TODO: Validate Value create_custom_config_file_if_not_exists() new_data = read_file_content_json(CUSTOM_CONFIG_FILE) v = args.value if args.name in BOOL_CONFIGS: v = boolify(args.value) new_data[args.name] = v file_content_overwrite(CUSTOM_CONFIG_FILE, new_data) # If any value changed which requires restart of REST server restart = False if args.name in RESTART_CONFIGS: restart = True sync_to_peers(restart=restart)
def handle_output_error(err, errcode=1, json_output=False): if json_output: print (json.dumps({ "output": "", "error": err })) sys.exit(errcode) else: output_error(err, errcode)
def run(self, args): create_webhooks_file_if_not_exists() with fasteners.InterProcessLock(WEBHOOKS_FILE): data = json.load(open(WEBHOOKS_FILE)) if data.get(args.url, None) is None: output_error("Webhook does not exists") del data[args.url] file_content_overwrite(WEBHOOKS_FILE, data) sync_to_peers()
def run(self, args): create_webhooks_file_if_not_exists() with LockedOpen(WEBHOOKS_FILE, 'r+'): data = json.load(open(WEBHOOKS_FILE)) if data.get(args.url, None) is None: output_error("Webhook does not exists") del data[args.url] file_content_overwrite(WEBHOOKS_FILE, data) sync_to_peers()
def run(self, args): create_webhooks_file_if_not_exists() with LockedOpen(WEBHOOKS_FILE, 'r+'): data = json.load(open(WEBHOOKS_FILE)) if data.get(args.url, None) is None: output_error("Webhook does not exists") data[args.url] = args.bearer_token file_content_overwrite(WEBHOOKS_FILE, data) sync_to_peers()
def run(self, args): create_webhooks_file_if_not_exists() with fasteners.InterProcessLock(WEBHOOKS_FILE): data = json.load(open(WEBHOOKS_FILE)) if data.get(args.url, None) is None: output_error("Webhook does not exists") data[args.url] = args.bearer_token file_content_overwrite(WEBHOOKS_FILE, data) sync_to_peers()
def mkdirp(path, exit_on_err=False, logger=None): """ Try creating required directory structure ignore EEXIST and raise exception for rest of the errors. Print error in stderr and exit """ try: os.makedirs(path) except (OSError, IOError) as e: if e.errno == EEXIST and os.path.isdir(path): pass else: output_error("Fail to create dir %s: %s" % (path, e))
def run(self, args): create_apps_file_if_not_exists() with LockedOpen(APPS_FILE, 'r+'): data = json.load(open(APPS_FILE)) if data.get(args.appid, None) is None: output_error("Application does not exists") del data[args.appid] file_content_overwrite(APPS_FILE, data) sync_to_peers()
def run(self, args): data = json.load(open(DEFAULT_CONFIG_FILE)) if os.path.exists(CUSTOM_CONFIG_FILE): data.update(json.load(open(CUSTOM_CONFIG_FILE))) if args.name is not None and args.name not in CONFIG_KEYS: output_error("Invalid Config item") table = PrettyTable(["NAME", "VALUE"]) if args.name is None: for k, v in data.items(): table.add_row([k, v]) else: table.add_row([args.name, data[args.name]]) print(table)
def run(self, args): data = json.load(open(DEFAULT_CONFIG_FILE)) if os.path.exists(CUSTOM_CONFIG_FILE): data.update(json.load(open(CUSTOM_CONFIG_FILE))) if args.name is not None and args.name not in CONFIG_KEYS: output_error("Invalid Config item") table = PrettyTable(["NAME", "VALUE"]) if args.name is None: for k, v in data.items(): table.add_row([k, v]) else: table.add_row([args.name, data[args.name]]) print (table)