Beispiel #1
0
 def post(self):
     args = parser.parse_args()
     user = g.user_info["username"]
     project, _ = gitlab_project(args["product_id"], args["project_type"])
     # 支持的action create, delete, move, update
     data = {
         'branch_name':
         args["branch"],
         'commit_message':
         args["action"] + " " + args["path"],
         'actions': [{
             'action': args["action"],
             'file_path': args["path"],
             'content': args["content"]
         }]
     }
     if isinstance(project, dict):
         return project, 500
     else:
         try:
             project.commits.create(data)
             # 假如删除,删除数据库中封装的SLS信息
             if args["action"] == "delete":
                 delete_sls(args["path"])
             audit_log(user, args["path"], args["product_id"], "sls",
                       args["action"])
         except Exception as e:
             logger.error("Commit file: %s" % e)
             return {"status": False, "message": str(e)}, 500
         return {"status": True, "message": ""}, 200
Beispiel #2
0
    def get(self):
        args = parser.parse_args()
        project, product_name = gitlab_project(args["product_id"],
                                               args["project_type"])
        if isinstance(project, dict):
            return project, 500
        else:
            file_list = []
            try:
                items = project.repository_tree(path=args["path"],
                                                ref_name=args["branch"])
            except Exception as e:
                logger.error("Get file list error: %s" % e)
                return {"status": False, "message": str(e)}, 404

        if args["path"] == "/" or args["path"] is "":
            for i in items:
                if i["type"] == "tree":
                    file_list.append({
                        "title": i["name"],
                        "type": i["type"],
                        "path": i["name"],
                        "loading": False,
                        "children": []
                    })
                else:
                    file_list.append({
                        "title": i["name"],
                        "type": i["type"],
                        "path": i["name"],
                    })
            return {
                "data": [{
                    "title": product_name,
                    "expand": True,
                    "children": file_list,
                    "type": "tree",
                }],
                "status":
                True,
                "message":
                ""
            }, 200
        else:
            for i in items:
                if i["type"] == "tree":
                    file_list.append({
                        "title": i["name"],
                        "type": i["type"],
                        "path": args["path"] + "/" + i["name"],
                        "loading": False,
                        "children": []
                    })
                else:
                    file_list.append({
                        "title": i["name"],
                        "type": i["type"],
                        "path": args["path"] + "/" + i["name"],
                    })
            return {"data": file_list, "status": True, "message": ""}, 200
Beispiel #3
0
 def get(self):
     args = parser.parse_args()
     project = gitlab_project(args["product_id"])
     if isinstance(project, dict):
         return project, 500
     else:
         try:
             content = project.files.get(file_path=args["path"], ref=args["branch"])
         except Exception as e:
             return {"status": False, "message": str(e)}, 404
         return {"content": content.decode().decode("utf-8"), "status": True, "message": ""}, 200
Beispiel #4
0
 def get(self):
     args = parser.parse_args()
     project = gitlab_project(args["product_id"])
     if isinstance(project, dict):
         return project, 500
     else:
         branch_list = []
         branch = project.branches.list()
         for b in branch:
             branch_list.append(b.name)
         return {"branchs": {"branch": branch_list}, "status": True, "message": ""}, 200
Beispiel #5
0
 def get(self):
     args = parser.parse_args()
     project, _ = gitlab_project(args["product_id"], args["project_type"])
     if isinstance(project, dict):
         return project, 500
     else:
         try:
             content = project.files.get(file_path=args["path"], ref=args["branch"])
             content_decode = content.decode().decode("utf-8")
         except Exception as e:
             logger.error("Get file content: %s" % e)
             return {"status": False, "message": str(e)}, 404
         return {"data": content_decode, "status": True, "message": ""}, 200
Beispiel #6
0
 def get(self):
     args = parser.parse_args()
     project = gitlab_project(args["product_id"])
     if isinstance(project, dict):
         return project, 500
     else:
         file_list = []
         try:
             items = project.repository_tree(path=args["path"], ref_name=args["branch"])
         except Exception as e:
             return {"status": False, "message": str(e)}, 404
         for i in items:
             file_list.append({"name": i["name"], "type": i["type"]})
         return {"files": {"file": file_list}, "status": True, "message": ""}, 200
Beispiel #7
0
 def get(self):
     args = parser.parse_args()
     project, _ = gitlab_project(args["product_id"], args["project_type"])
     if isinstance(project, dict):
         return project, 500
     else:
         branch_list = []
         try:
             branch = project.branches.list()
             for b in branch:
                 branch_list.append(b.name)
         except Exception as e:
             logger.error("Get branch error: %s" % e)
             return {"status": False, "message": str(e)}, 500
         return {"data": branch_list, "status": True, "message": ""}, 200
Beispiel #8
0
 def post(self):
     args = parser.parse_args()
     user = g.user_info["username"]
     project, _ = gitlab_project(args["product_id"], args["project_type"])
     file = request.files['file']
     if args["path"]:
         file_path = args["path"] + "/" + file.filename
     content = file.read()
     try:
         content_decode = content.decode()
         actions = [
             {
                 'action': 'create',
                 'file_path': file_path,
                 'content': content_decode
             }
         ]
     except Exception as e:
         return {"status": False, "message": str(e)}, 500
     # try:
     #     content_decode = content.decode()
     #     actions = [
     #         {
     #             'action': args["action"],
     #             'file_path': file_path,
     #             'content': base64.b64encode(content_decode),
     #             'encoding': 'base64',
     #         }
     #     ]
     # except Exception as e:
     #     print(e)
     data = {
         'branch': args["branch"],
         'commit_message': args["action"] + " " + args["path"],
         'actions': actions
     }
     if isinstance(project, dict):
         return project, 500
     else:
         try:
             project.commits.create(data)
             audit_log(user, file_path, args["product_id"], "sls", "upload")
         except Exception as e:
             logger.error("Upload file: %s" % e)
             return {"status": False, "message": str(e)}, 500
         return {"status": True, "message": ""}, 200
Beispiel #9
0
 def post(self):
     args = parser.parse_args()
     args["id"] = uuid_prefix("s")
     user = g.user_info["username"]
     db = DB()
     yaml = ""
     status, result = db.select("sls", "where data -> '$.path'='%s'" % args["path"])
     if status is True:
         if len(result) == 0:
             insert_status, insert_result = db.insert("sls", json.dumps(args, ensure_ascii=False))
             db.close_mysql()
             if insert_status is not True:
                 logger.error("Add sls error: %s" % insert_result)
                 return {"status": False, "message": insert_result}, 500
             # 根据步骤循环
             for step in args['steps']:
                 # 文件管理
                 if step.get("state_name") == "file_managed" and args.get("file_managed"):
                     for file_managed in args.get("file_managed"):
                         # 在文件管理找到对应的ID
                         if step["id"] == file_managed.get("name"):
                             # 获取YAML文件格式
                             file = ParseYaml.file_managed(name=file_managed.get("name"),
                                                           destination=file_managed.get("destination"),
                                                           source=file_managed.get("source"),
                                                           user=file_managed.get("user"),
                                                           group=file_managed.get("group"),
                                                           template=file_managed.get("template"),
                                                           mode=file_managed.get("mode"))
                             yaml += file
                 if step.get("state_name") == "cmd_run" and args.get("cmd_run"):
                     for cmd_run in args.get("cmd_run"):
                         if step["id"] == cmd_run.get("name"):
                             # 获取YAML文件格式
                             file = ParseYaml.cmd_run(name=cmd_run.get("name"),
                                                      cmd=cmd_run.get("cmd"),
                                                      env=cmd_run.get("env"),
                                                      unless=cmd_run.get("unless"),
                                                      require=cmd_run.get("require"))
                             yaml += file
                 if step.get("state_name") == "pkg_installed" and args.get("pkg_installed"):
                     for pkg_installed in args.get("pkg_installed"):
                         if step["id"] == pkg_installed.get("name"):
                             # 获取YAML文件格式
                             file = ParseYaml.pkg_installed(name=pkg_installed.get("name"),
                                                            pkgs=pkg_installed.get("pkgs"))
                             yaml += file
                 if step.get("state_name") == "file_directory" and args.get("file_directory"):
                     for file_directory in args.get("file_directory"):
                         if step["id"] == file_directory.get("name"):
                             # 获取YAML文件格式
                             file = ParseYaml.file_directory(name=file_directory.get("name"),
                                                             destination=file_directory.get("destination"),
                                                             user=file_directory.get("user"),
                                                             group=file_directory.get("group"),
                                                             mode=file_directory.get("mode"),
                                                             makedirs=file_directory.get("makedirs"))
                             yaml += file
             project, _ = gitlab_project(args["product_id"], args["project_type"])
             data = {
                 'branch_name': args["branch"],
                 'commit_message': args["action"] + " " + args["path"],
                 'actions': [
                     {
                         'action': args["action"],
                         'file_path': args["path"],
                         'content': yaml
                     }
                 ]
             }
             if isinstance(project, dict):
                 return project, 500
             else:
                 try:
                     project.commits.create(data)
                 except Exception as e:
                     logger.error("Commit file: %s" % e)
                     return {"status": False, "message": str(e)}, 500
             audit_log(user, args["id"], args["product_id"], "sls", "add")
             return {"status": True, "message": ""}, 201
         else:
             db.close_mysql()
             return {"status": False, "message": "The sls name already exists"}, 200
     else:
         db.close_mysql()
         logger.error("Select sls name error: %s" % result)
         return {"status": False, "message": result}, 500