Exemplo n.º 1
0
 def put(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         workflow_id = self.get_json_argument("workflow_id", "")
         name = self.get_json_argument("name", "")
         configuration = self.get_json_argument("configuration", {})
         description = self.get_json_argument("description", "")
         enable = self.get_json_argument("enable", None)
         LOG.debug("UpdateWorkflowHandler workflow_id: %s, name: %s, description: %s", workflow_id, name, description)
         if workflow_id and Workflows.instance().get(workflow_id):
             result["workflow_id"] = workflow_id
             data = {}
             if name:
                 data["name"] = name
             if configuration:
                 data["configuration"] = configuration
             if description:
                 data["description"] = description
             if enable is not None:
                 data["enable"] = enable
             if data:
                 success = Workflows.instance().update(workflow_id, data)
                 if not success:
                     Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 2
0
 def post(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         schedule_name = self.get_json_argument("schedule_name", "")
         source = self.get_json_argument("source", "")
         source_id = self.get_json_argument("source_id", "")
         input_data = self.get_json_argument("input_data", {})
         minute = int(self.get_json_argument("minute", -1))  # [0, 59]
         hour = int(self.get_json_argument("hour", -1))  # [0, 23]
         day_of_month = int(self.get_json_argument("day_of_month",
                                                   -1))  # [1, 31]
         day_of_week = int(self.get_json_argument(
             "day_of_week", -1))  # [1, 7] (Sunday = 7)
         enable = True if self.get_json_argument("enable", False) else False
         if (((source == Schedules.application
               and AppManager.instance().info(source_id)) or
              (source == Schedules.workflow
               and Workflows.instance().get(source_id))) and schedule_name
                 and (minute == -1 or (minute >= 0 and minute <= 59))
                 and (hour == -1 or (hour >= 0 and hour <= 23))
                 and (day_of_month == -1 or
                      (day_of_month >= 1 and day_of_month <= 31))
                 and (day_of_week == -1 or
                      (day_of_week >= 1 and day_of_week <= 7))):
             if not isinstance(input_data, dict):
                 raise JSONLoadError("input_data must be dict type")
             schedule_id = Schedules.instance().add(
                 schedule_name,
                 source,
                 source_id,
                 minute=minute,
                 hour=hour,
                 day_of_month=day_of_month,
                 day_of_week=day_of_week,
                 enable=enable,
                 input_data=input_data)
             if schedule_id is not False:
                 result["schedule_id"] = schedule_id
             else:
                 Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug(
             "CreateScheduleHandler, schedule_name: %s, source: %s, source_id: %s",
             schedule_name, source, source_id)
     except JSONLoadError as e:
         LOG.error(e)
         Errors.set_result_error("InvalidParameters", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 3
0
 def delete(self):
     result = {"result": Errors.OK}
     try:
         workflow_id = self.get_argument("workflow_id", "")
         if workflow_id:
             success = Workflows.instance().delete(workflow_id)
             if not success:
                 Errors.set_result_error("OperationFailed", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 4
0
 def get(self):
     result = {"result": Errors.OK}
     try:
         workflow_id = self.get_argument("workflow_id", "")
         if workflow_id:
             workflow_info = Workflows.instance().get(workflow_id)
             if workflow_info:
                 result["info"] = workflow_info
             elif workflow_info is None:
                 Errors.set_result_error("AppNotExists", result)
             else:
                 Errors.set_result_error("OperationFailed", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 5
0
 def put(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         work_id = self.get_json_argument("work_id", "")
         if work_id:
             work = Works.instance().get(work_id)
             if work:
                 workflow = Workflows.instance().get(work["workflow_id"])
                 if workflow:
                     if workflow["enable"]:
                         if work["stage"] == Stage.finished:
                             if work["status"] != Status.success:
                                 success = Works.instance().update(
                                     work_id, {
                                         "stage": Stage.recovering,
                                         "status": None
                                     })
                                 if not success:
                                     Errors.set_result_error(
                                         "OperationFailed", result)
                             else:
                                 Errors.set_result_error(
                                     "WorkAlreadySuccess", result)
                         else:
                             Errors.set_result_error(
                                 "WorkStillRunning", result)
                     else:
                         Errors.set_result_error("WorkflowDisabled", result)
                 elif workflow is None:
                     Errors.set_result_error("WorkflowNotExists", result)
                 else:
                     Errors.set_result_error("OperationFailed", result)
             elif work is None:
                 Errors.set_result_error("WorkNotExists", result)
             else:
                 Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug("RecoverWorkHandler, work_id: %s", work_id)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 6
0
 def post(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         name = self.get_json_argument("name", "")
         configuration = self.get_json_argument("configuration", {})
         description = self.get_json_argument("description", "")
         enable = self.get_json_argument("enable", False)
         if name:
             workflow_id = Workflows.instance().add(name, configuration, description = description, enable = enable)
             result["workflow_id"] = workflow_id
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 7
0
 def get(self):
     result = {"result": Errors.OK}
     try:
         offset = int(self.get_argument("offset", "0"))
         limit = int(self.get_argument("limit", "0"))
         filters = {}
         workflow_id = self.get_argument("id", "")
         if workflow_id:
             filters["id"] = workflow_id
         name = self.get_argument("name", "")
         if name:
             filters["name"] = name
         LOG.debug("ListWorkflowHandler offset: %s, limit: %s, filters: %s", offset, limit, filters)
         r = Workflows.instance().list(offset = offset, limit = limit, filters = filters)
         result["workflows"] = r["workflows"]
         result["total"] = r["total"]
         result["offset"] = offset
         result["limit"] = limit
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 8
0
 def post(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         name = self.get_json_argument("name", "")
         workflow_id = self.get_json_argument("workflow_id", "")
         input_data = self.get_json_argument("input_data", {})
         workflow = Workflows.instance().get(workflow_id)
         if workflow and name:
             if not isinstance(input_data, dict):
                 raise JSONLoadError("input_data must be dict type")
             if workflow["enable"]:
                 work_id = Works.instance().add(
                     name,
                     workflow_id,
                     input_data=input_data,
                     configuration=workflow["configuration"])
                 if work_id is not False:
                     result["work_id"] = work_id
                 else:
                     Errors.set_result_error("OperationFailed", result)
             else:
                 Errors.set_result_error("WorkflowDisabled", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug("CreateWorkHandler, name: %s, workflow_id: %s", name,
                   workflow_id)
     except JSONLoadError as e:
         LOG.error(e)
         Errors.set_result_error("InvalidParameters", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 9
0
 def put(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         schedule_id = self.get_json_argument("schedule_id", "")
         result["schedule_id"] = schedule_id
         data = self.get_json_exists_arguments([
             "schedule_name",
             "source",
             "source_id",
             "input_data",
             "minute",  # [0, 59]
             "hour",  # [0, 23]
             "day_of_month",  # [1, 31]
             "day_of_week",  # [1, 7] (Sunday = 7)
             "enable",
         ])
         if (data
                 and (schedule_id and Schedules.instance().get(schedule_id))
                 and
             (("schedule_name" in data and data["schedule_name"] != "")
              or "schedule_name" not in data) and
             ("source_id" not in data or
              ("source_id" in data
               and data["source"] == Schedules.application
               and AppManager.instance().info(data["source_id"])) or
              ("source_id" in data and data["source"] == Schedules.workflow
               and Workflows.instance().get(data["source_id"])))
                 and (("minute" in data and
                       (data["minute"] == -1 or
                        (data["minute"] >= 0 and data["minute"] <= 59)))
                      or "minute" not in data)
                 and (("hour" in data and
                       (data["hour"] == -1 or
                        (data["hour"] >= 0 and data["hour"] <= 23)))
                      or "hour" not in data) and
             (("day_of_month" in data and
               (data["day_of_month"] == -1 or
                (data["day_of_month"] >= 1 and data["day_of_month"] <= 31)))
              or "day_of_month" not in data) and
             (("day_of_week" in data and
               (data["day_of_week"] == -1 or
                (data["day_of_week"] >= 1 and data["day_of_week"] <= 7)))
              or "day_of_week" not in data)
                 and (("enable" in data and data["enable"] in [True, False])
                      or "enable" not in data)):
             if "input_data" in data and not isinstance(
                     data["input_data"], dict):
                 raise JSONLoadError("input_data must be dict type")
             if "app_id" in data:
                 data["application_id"] = data["app_id"]
                 del data["app_id"]
             success = Schedules.instance().update(schedule_id, data)
             if not success:
                 Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug("UpdateScheduleHandler, schedule_id: %s, data: %s",
                   schedule_id, data)
     except JSONLoadError as e:
         LOG.error(e)
         Errors.set_result_error("InvalidParameters", result)
     except Exception as e:
         LOG.exception(e)
         Errors.set_result_error("ServerException", result)
     self.write(result)
     self.finish()
Exemplo n.º 10
0
def main():
    parser = argparse.ArgumentParser(prog='litemanager')
    parser.add_argument("-c",
                        "--config",
                        required=True,
                        help="configuration file path")
    parser.add_argument("-v",
                        "--version",
                        action='version',
                        version='%(prog)s ' + __version__)
    args = parser.parse_args()

    if args.config:
        success = load_config(args.config)
        if success:
            common.init_storage()
            logger.config_logging(file_name="manager.log",
                                  log_level=CONFIG["log_level"],
                                  dir_name=CONFIG["log_path"],
                                  day_rotate=False,
                                  when="D",
                                  interval=1,
                                  max_size=20,
                                  backup_count=5,
                                  console=True)

            LOG.info("service start")

            try:
                if CONFIG["ldfs_http_host"] and CONFIG["ldfs_http_port"]:
                    LDFS = LiteDFS(CONFIG["ldfs_http_host"],
                                   CONFIG["ldfs_http_port"])
                venvs_db = Venvs()
                venv_history_db = VenvHistory()
                tasks_db = Tasks()
                applications_db = Applications()
                application_history_db = ApplicationHistory()
                workflows_db = Workflows()
                works_db = Works()
                schedules_db = Schedules()
                services_db = Services()
                venv_manager = VenvManager()
                app_manager = AppManager()
                task_scheduler = Scheduler(CONFIG["scheduler_interval"])
                http_server = tornado.httpserver.HTTPServer(
                    Application(),
                    max_buffer_size=CONFIG["max_buffer_size"],
                    chunk_size=10 * 1024 * 1024)
                http_server.listen(CONFIG["http_port"],
                                   address=CONFIG["http_host"])
                # http_server.bind(CONFIG["http_port"], address = CONFIG["http_host"])
                listener = DiscoveryListener(Connection, task_scheduler)
                listener.listen(CONFIG["tcp_port"], CONFIG["tcp_host"])
                stop_service.Servers.HTTP_SERVER = http_server
                stop_service.Servers.SERVERS.append(task_scheduler)
                stop_service.Servers.SERVERS.append(venvs_db)
                stop_service.Servers.SERVERS.append(venv_history_db)
                stop_service.Servers.SERVERS.append(applications_db)
                stop_service.Servers.SERVERS.append(application_history_db)
                stop_service.Servers.SERVERS.append(tasks_db)
                stop_service.Servers.SERVERS.append(workflows_db)
                stop_service.Servers.SERVERS.append(works_db)
                stop_service.Servers.SERVERS.append(schedules_db)
                stop_service.Servers.SERVERS.append(services_db)
                stop_service.Servers.SERVERS.append(venv_manager)
                stop_service.Servers.SERVERS.append(app_manager)
                signal.signal(signal.SIGTERM, stop_service.sig_handler)
                signal.signal(signal.SIGINT, stop_service.sig_handler)
                tornado.ioloop.IOLoop.instance().start()
            except Exception as e:
                LOG.exception(e)

            LOG.info("service end")
        else:
            print("failed to load configuration: %s" % args.config)
    else:
        parser.print_help()