def convert_to_service_json_list(service_name_list: list) -> RoxResponse: """ Convert list of service names to corresponding list of JSON dictionaries. :param service_name_list: List of service names. :return: RoxResponse instance containing a list of corresponding JSON dictionaries. Service names which could not be converted to JSON structure are included as error_data attribute. """ valid_service_jsons = [] invalid_service_names = [] for name in service_name_list: res = convert_to_service_json(name) if res.success: valid_service_jsons.append(res.data) else: invalid_service_names.append(name) res = RoxResponse(True) res.data = valid_service_jsons res.error_data = invalid_service_names return res
def shutdown_services(service_name_list: list) -> RoxResponse: """ Stop all services defined by given list of service names. :param service_name_list: List of service names. :return: RoxResponse instance documenting which services could not be stopped. """ if len(service_name_list) < 1: # Service list is empty and therefore invalid. return RoxResponse(False, MSG_MISSING_SERVICES_ERROR) # Collect names of all services which could not be stopped. not_stopped_name_list = [] all_services_stopped = True for service_name in service_name_list: res = shutdown_service(service_name) if not res.success: not_stopped_name_list.append(service_name) all_services_stopped = False res = RoxResponse(all_services_stopped) res.error_data = not_stopped_name_list return res
def get_local_services() -> RoxResponse: """ Get locally stored services and provide them as dictionary mapping service name to its JSON instance. Provide list of invalid services as RoxResponse's error data parameter. :return: RoxResponse instance containing a dictionary mapping each service name to its corresponding JSON instance Provide list of invalid services as error data parameter. """ valid_services = {} invalid_services = [] success = True for f in os.scandir(LOCAL_SETTINGS[SERVICE_DIR]): if f.is_file() and f.name.endswith(".json"): fd = None service_name = None try: fd = open(os.path.join(LOCAL_SETTINGS[SERVICE_DIR], f.name), 'r') service_name = f.name[:-5] service_json = json.load(fd) valid_services[service_name] = service_json except (OSError, json.decoder.JSONDecodeError): success = False if service_name: invalid_services.append(service_name) finally: if fd: fd.close() res = RoxResponse(success) res.data = valid_services res.error_data = invalid_services return res
def start_services(service_json_list: list) -> RoxResponse: """ Start all services defined by given list of JSON dictionaries. :param service_json_list: List of JSON dictionaries defining multiple services. :return: RoxResponse instance documenting which services could not be started. """ if len(service_json_list) < 1: # Service list is empty and therefore invalid. return RoxResponse(False, MSG_MISSING_SERVICES_ERROR) # Collect names of all services which could not be started. not_started_json_list = [] all_services_started = True err_message = "" for service_json in service_json_list: res = start_service(service_json) if not res.success: not_started_json_list.append(service_json["params"]["name"]) all_services_started = False err_message = res.message res = RoxResponse(all_services_started, err_message) res.error_data = not_started_json_list return res