示例#1
0
 def get(self):
     result = {"result": Errors.OK}
     try:
         app_id = self.get_argument("app_id", "")
         history_id = int(self.get_argument("history_id", "-1"))
         config = self.get_argument("config", "false")
         config = True if config.lower() == "true" else False
         if history_id != -1:
             app_history = AppManager.instance().info_history(
                 history_id, app_id)
             if app_history:
                 if config:
                     app_config = AppManager.instance().get_app_config(
                         app_id, app_history["sha1"])
                     if app_config:
                         result["history_info"] = app_history
                         result["history_config"] = app_config
                     else:
                         Errors.set_result_error("OperationFailed", result)
                 else:
                     result["history_info"] = app_history
             elif app_history is None:
                 Errors.set_result_error("AppHistoryNotExists", result)
             else:
                 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()
示例#2
0
 def get(self):
     result = {"result": Errors.OK, "app_histories": [], "total": 0}
     try:
         app_id = self.get_argument("app_id", "")
         offset = int(self.get_argument("offset", "0"))
         limit = int(self.get_argument("limit", "0"))
         if app_id:
             app_info = AppManager.instance().info(app_id)
             if app_info:
                 app_histories = AppManager.instance().list_history(
                     offset, limit, {"app_id": app_id})
                 if app_histories:
                     result["app_histories"] = app_histories["histories"]
                     result["total"] = app_histories["total"]
                 result["offset"] = offset
                 result["limit"] = limit
             elif app_info is None:
                 Errors.set_result_error("AppNotExists", result)
             else:
                 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()
示例#3
0
 def get(self):
     result = {"result": Errors.OK}
     try:
         app_id = self.get_argument("app_id", "")
         sha1 = self.get_argument("sha1", "")
         if app_id:
             app_info = AppManager.instance().info(app_id)
             if app_info:
                 if sha1 == "":
                     sha1 = app_info["sha1"]
                 f = AppManager.instance().open(app_id, sha1)
                 if f:
                     self.set_header('Content-Type',
                                     'application/octet-stream')
                     if "app_store" in CONFIG:
                         if "tar.gz" in CONFIG["app_store"]:
                             self.set_header(
                                 'Content-Disposition',
                                 'attachment; filename=%s.tar.gz' % app_id)
                         elif "zip" in CONFIG["app_store"]:
                             self.set_header(
                                 'Content-Disposition',
                                 'attachment; filename=%s.zip' % app_id)
                         else:
                             self.set_header(
                                 'Content-Disposition',
                                 'attachment; filename=%s.tar.gz' % app_id)
                     else:
                         self.set_header(
                             'Content-Disposition',
                             'attachment; filename=%s.tar.gz' % app_id)
                     buf_size = 1024 * 1024
                     while True:
                         data = f.read(buf_size)
                         if not data:
                             break
                         self.write(data)
                         self.flush()
                         yield gen.moment
                     f.close()
                     self.finish()
                     return
                 else:
                     Errors.set_result_error("OperationFailed", result)
             elif app_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.set_status(400)
     self.write(result)
     self.finish()
示例#4
0
 def post(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         task_name = self.get_json_argument("task_name", "")
         app_id = self.get_json_argument("app_id", "")
         input_data = self.get_json_argument("input_data", {})
         if app_id and AppManager.instance().info(app_id) and task_name:
             if not isinstance(input_data, dict):
                 raise JSONLoadError("input_data must be dict type")
             task_id = Tasks.instance().add(task_name,
                                            app_id,
                                            input_data=input_data)
             if task_id is not False:
                 result["task_id"] = task_id
             else:
                 Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug("CreateTaskHandler, task_name: %s, app_id: %s",
                   task_name, app_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()
示例#5
0
 def post(self):
     result = {"result": Errors.OK}
     try:
         name = self.get_form_argument("name", "")
         description = self.get_form_argument("description", "")
         file_path = self.file_path.decode("utf-8")
         if name and os.path.exists(file_path) and os.path.isfile(
                 file_path):
             file_name = os.path.split(file_path)[-1].lower()
             if ((CONFIG["app_store"].endswith("tar.gz")
                  and file_name.endswith("tar.gz"))
                     or (CONFIG["app_store"].endswith("zip")
                         and file_name.endswith("zip"))):
                 result["app_id"] = AppManager.instance().create(
                     name, description, file_path)
             else:
                 LOG.warning("application wrong format")
                 Errors.set_result_error("AppWrongFormat", 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)
     LOG.debug("deploy application package use: %ss",
               time.time() - self.start)
     self.write(result)
     self.finish()
示例#6
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()
示例#7
0
 def delete(self):
     result = {"result": Errors.OK}
     try:
         app_id = self.get_argument("app_id", "")
         if app_id:
             success = AppManager.instance().delete(app_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()
示例#8
0
 def post(self):
     result = {"result": Errors.OK}
     try:
         app_id = self.get_form_argument("app_id", "")
         name = self.get_form_argument("name", "")
         description = self.get_form_argument("description", "")
         LOG.debug(
             "UpdateApplicationHandler app_id: %s, name: %s, description: %s",
             app_id, name, description)
         if app_id and AppManager.instance().info(app_id):
             success = AppManager.instance().update(
                 app_id, name, description, self.file_path.decode("utf-8"))
             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)
     LOG.debug("update application package use: %ss",
               time.time() - self.start)
     self.write(result)
     self.finish()
示例#9
0
 def put(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         service_id = self.get_json_argument("service_id", "")
         result["service_id"] = service_id
         data = self.get_json_exists_arguments([
             "name",
             "app_id",
             "description",
             "input_data",
             "signal",
             "enable",
         ])
         if (data and (service_id and Services.instance().get(service_id))
                 and
             (("name" in data and data["name"] != "") or "name" not in data)
                 and ("app_id" not in data or
                      ("app_id" in data
                       and AppManager.instance().info(data["app_id"])))
                 and ("signal" in data and data["signal"] in (-9, -15))
                 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 = Services.instance().update(service_id, data)
             if not success:
                 Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug("UpdateServiceHandler, service_id: %s, data: %s",
                   service_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()
示例#10
0
 def delete(self):
     result = {"result": Errors.OK}
     try:
         app_id = self.get_argument("app_id", "")
         history_id = int(self.get_argument("history_id", "-1"))
         if history_id != -1 and app_id:
             success = AppManager.instance().delete_history(
                 history_id, app_id)
             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()
示例#11
0
 def put(self):
     result = {"result": Errors.OK}
     try:
         self.json_data = json.loads(self.request.body.decode("utf-8"))
         history_id = self.get_json_argument("history_id", "")
         app_id = self.get_json_argument("app_id", "")
         if history_id and app_id:
             success = AppManager.instance().activate_history(history_id,
                                                              app_id=app_id)
             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()
示例#12
0
 def get(self):
     result = {"result": Errors.OK}
     try:
         offset = int(self.get_argument("offset", "0"))
         limit = int(self.get_argument("limit", "0"))
         filters = {}
         name = self.get_argument("name", "")
         if name:
             filters["name"] = name
         app_id = self.get_argument("id", "")
         if app_id:
             filters["id"] = app_id
         LOG.debug("ListApplicationHandler offset: %s, limit: %s", offset,
                   limit)
         r = AppManager.instance().list(offset, limit, filters=filters)
         result["apps"] = r["apps"]
         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()
示例#13
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", "")
         app_id = self.get_json_argument("app_id", "")
         description = self.get_json_argument("description", "")
         input_data = self.get_json_argument("input_data", {})
         signal = self.get_json_argument("signal", -9)
         enable = True if self.get_json_argument("enable", False) else False
         if AppManager.instance().info(app_id) and name:
             if not isinstance(input_data, dict):
                 raise JSONLoadError("input_data must be dict type")
             service_id = Services.instance().add(name,
                                                  app_id,
                                                  description=description,
                                                  enable=enable,
                                                  input_data=input_data,
                                                  signal=signal)
             if service_id is not False:
                 result["service_id"] = service_id
             else:
                 Errors.set_result_error("OperationFailed", result)
         else:
             LOG.warning("invalid arguments")
             Errors.set_result_error("InvalidParameters", result)
         LOG.debug("CreateServiceHandler, name: %s, app_id: %s, enable: %s",
                   name, app_id, enable)
     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()
示例#14
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()
示例#15
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()