def testCompleted(projectId, user): project = Project.objects.get(id=projectId) subprojects = Project.objects.filter(parent=project) unmergedSubprojects = [ subproject for subproject in subprojects if subproject.status < 5 ] hasOpenBugsSubprojects = [ subproject for subproject in subprojects if ProjectModule.hasOpenBugs(subproject.id) ] if ProjectModule.hasOpenBugs(projectId): return False, '仍有 Bug 未关闭' elif len(unmergedSubprojects) > 0: return False, '仍有子项目未合并' elif len(hasOpenBugsSubprojects) > 0: return False, '子项目仍有 Bug 未关闭' else: project.setStatus(4) branch_info = BranchInfo.objects.filter(plist_id=project.id, status__lte=4) for bi in branch_info: bi.status = 4 bi.save() print(user) Git.test_completed(projectId, user) return True, ''
def delete_version_cookie(requestInfo, publisher): ''' 调用delete-version.sh脚本 实现线上版本正式改变 ''' version = requestInfo['version'] if 'version' in requestInfo else '' type = requestInfo['type'] if 'type' in requestInfo else '' which = requestInfo['which'] if 'which' in requestInfo else '' branch_name = requestInfo['branch_name'] if 'branch_name' in requestInfo else '' if not version: return if not type: return # 记录发布人 versions = CookieVersion.objects.filter(name=version,branch_type=type) for v in versions: v.publish_cookie_deleted_no = publisher v.publish_cookie_deleted = time.strftime("%Y-%m-%d %X", time.localtime()) v.save() shellPath = settings.SHELL_PATH os.chdir(shellPath) opResult = os.popen(shellPath+"delete-version.sh %s %s %s" % (version,type,which)) outputs = "<br />".join(opResult.readlines()) #insert log Git.insertShellLog(10,1,outputs) return outputs
def update_remote(requestInfo): id = requestInfo['projectId'] remote = requestInfo['remote']+"," branch = requestInfo['branch'] p = Project.objects.get(id=id) p.remote = remote p.save() Git.createBranch(branch,'master',0,remote) InsertInfo.insert(branch,remote,id,p.status)
def update_git(requestInfo, publisher): which = requestInfo['which'] if 'which' in requestInfo else '' type = requestInfo['type'] if 'type' in requestInfo else '' shellPath = settings.SHELL_PATH os.chdir(shellPath) opResult = os.popen(shellPath+"update-git.sh %s %s" % (which,type)) outputs = "<br />".join(opResult.readlines()) #insert log Git.insertShellLog(10,1,outputs) return outputs
def publish_clean(build_only, build_publish_ecr_only, publish_helm_only, repo, git_ref, init_submodules): g = Git() git_account = load_git_configs()["account"] repo = repo or ProjectConf().name git_url = f"[email protected]:{git_account}/{repo}.git" ref = git_ref or g.get_full_hash() with tempfile.TemporaryDirectory() as tmpdirname: try: g.clone(git_url, tmpdirname) except subprocess.CalledProcessError: logging.warn(f"Could not clone repo {git_url}. Does it exist?") return try: g.checkout(tmpdirname, ref) except subprocess.CalledProcessError: logging.warn( f"Could not checkout to ref {ref} in repo {git_url}. Have you pushed it to remote?" ) return if init_submodules: try: g.init_submodules(tmpdirname) except subprocess.CalledProcessError: logging.warn( "Could not initialize submodules. Make sure you use ssh urls in" " the .gitmodules folder and double check your credentials." ) return with working_directory(tmpdirname): publish(build_only, build_publish_ecr_only, publish_helm_only)
def publish(build_only, build_publish_ecr_only, publish_helm_only): pc = ProjectConf() pr = PublishRules() d = DockerCommands() h = Helm() # tags is a list in case we want to add other tags in the future tags = [Git.get_hash()] if not publish_helm_only: for tag in tags: env = {"HASH": tag} pc.build_docker(env) if not build_only: images = pc.get_docker_images() image_tags = [ Tag(image, tag) for image, tag in product(images, tags) ] asso = {} d.login(pr) for image_tag in image_tags: res = d.publish(pr, image_tag, login=False) asso[image_tag] = res if not build_only and not build_publish_ecr_only: hash = tags[0] for path in pc.get_helm_chart_paths(): h.publish(pc.name, pr, path, hash) logging.info(f"Ran publish on {pc.name} with git hash: {tags[0]}")
def createBranch(projectId): project = Project.objects.get(pk=projectId) if projectId else None if not project: return False, '' # 临时操作仓库目录 todo gitOpPath = settings.TEMP_OP_PATH #~ 判断分支是否已经创建 createdFlag = True for appType in ("anjuke", "haozu", "jinpu"): try: branches = Git.getRemoteBranchList(gitOpPath + appType, settings.BRANCH_ALIAS) except ValidationError: return False, "get Remote Branch List Failure", project.branch realBranch = project.branch + "-" + appType if realBranch not in branches: createdFlag = False break if createdFlag: # 将项目状态更改为正在开发 project.setStatus(2) return False, "Have Created", project.branch else: result, messages = Git.createBranch( project.branch, project.parent.branch if project.parent else 'master', projectId, project.remote) if not result: return False, messages, '' Branch(name=project.branch, pcreateDateTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), status=1, project=project).save() InsertInfo.insert(project.branch, project.remote, project.id, project.status) # 将项目状态更改为正在开发 project.setStatus(2) return True, "Created Successfully!", project.branch
def rsyncToPg(requestInfo,user): import os from django.conf import settings if requestInfo.has_key('branchName'): branchName = requestInfo['branchName'] fpxx = requestInfo['fpxx'] remote = requestInfo['remote'] shellPath = settings.SHELL_PATH os.chdir(shellPath) opResult = os.popen(shellPath+"remote-refresh-pg.sh %s %s %s" % (branchName,remote,fpxx)) outputs = "<br/>".join(opResult.readlines()) # 输出的log日志 errorSyntax = outputs.find("Syntax Error") #insert log 日志 if errorSyntax>=0: return False,str(outputs),[] else: # 将项目状态由正在开发改成正在测试 projectId = requestInfo['projectId'] project = Project.objects.get(id=projectId) project.setStatus(3) #insert log 日志 Git.insertShellLog(1,requestInfo['projectId'],outputs,user.first_name) # 将项目信息更新到Branch的pdevInfo字段 from project.models import Branch branches = Branch.objects.filter(name=branchName) if len(branches) > 0: branch = Branch.objects.filter(name=branchName)[0] branch.pdevInfo = '' branch.save() else: import time selectedFpPath = settings.PG_DEV_PATH+branchName # /home/www/release/v2/pmt-15111 branch = Branch(project=project, name=branchName, pdevInfo=selectedFpPath, pcreateDateTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), status=1).save() return True,"代码已同步至 "+str(branchName),str(branchName)
def pullRebase(requestInfo): ''' 更新 fp 环境代码至最新 ''' fpId = int(requestInfo['fpId'] or 0) if 'fpId' in requestInfo else 0 if fpId > 0: fp = Fp.objects.get(pk=fpId) result, message, gitOutputs = Git.pullRebase(fp.path, fp.branch) return result, message, gitOutputs else: return False, '参数错误', []
def createBranch(projectId): project = Project.objects.get(pk=projectId) if projectId else None if not project: return False, '' # 临时操作仓库目录 todo gitOpPath = settings.TEMP_OP_PATH #~ 判断分支是否已经创建 createdFlag = True for appType in ("anjuke","haozu","jinpu"): try: branches = Git.getRemoteBranchList(gitOpPath+appType, settings.BRANCH_ALIAS) except ValidationError: return False, "get Remote Branch List Failure", project.branch realBranch = project.branch+"-"+appType if realBranch not in branches: createdFlag = False break if createdFlag : # 将项目状态更改为正在开发 project.setStatus(2) return False, "Have Created", project.branch else: result, messages = Git.createBranch(project.branch, project.parent.branch if project.parent else 'master',projectId,project.remote) if not result: return False, messages, '' Branch(name=project.branch, pcreateDateTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), status=1, project=project ).save() InsertInfo.insert(project.branch,project.remote,project.id,project.status); # 将项目状态更改为正在开发 project.setStatus(2) return True, "Created Successfully!", project.branch
def refresh_pg(requestInfo,user): ''' 测试环境更新到最新代码 ''' projectId = int(requestInfo['projectId'] or 0) if 'projectId' in requestInfo else 0 remote = requestInfo['remote'] if 'remote' in requestInfo else '' fpxx = requestInfo['fpxx'] if 'fpxx' in requestInfo else '' if projectId > 0: branch_name = ProjectModule.getBranchByPid(projectId) result,message,gitOutputs = Git.refresh_pg(branch_name,projectId,fpxx,user,remote) return result, message,gitOutputs else: return False, '参数错误', []
def testCompleted(projectId,user): project = Project.objects.get(id=projectId) subprojects = Project.objects.filter(parent=project) unmergedSubprojects = [subproject for subproject in subprojects if subproject.status < 5] hasOpenBugsSubprojects = [subproject for subproject in subprojects if ProjectModule.hasOpenBugs(subproject.id)] if ProjectModule.hasOpenBugs(projectId): return False, '仍有 Bug 未关闭' elif len(unmergedSubprojects) > 0: return False, '仍有子项目未合并' elif len(hasOpenBugsSubprojects) > 0: return False, '子项目仍有 Bug 未关闭' else: project.setStatus(4) branch_info = BranchInfo.objects.filter(plist_id=project.id,status__lte=4) for bi in branch_info: bi.status=4 bi.save() print (user) Git.test_completed(projectId,user) return True, ''
def checkBranchExist(branchName): ''' 检查分支信息是否存在 ''' #组建bash查询语句 gitOpPath = settings.TEMP_OP_PATH try: branchList = Git.getLocalBranchList(gitOpPath) except ValidationError: return "CheckBranchExist Failure" if branchName in branchList: return 1 else: return -1
def mergeBack(requestInfo): ''' 分支合并入回来源分支 ''' projectId = int(requestInfo['projectId']) if 'projectId' in requestInfo else 0 project = Project.objects.get(id=projectId) if projectId > 0 else None if project: result, message, gitOutputs = Git.mergeToBranch(project.branch, project.parent.branch if project.parent else project.branch) if result: project.setStatus(5) return result, message, gitOutputs else: return False, '参数错误', gitOutputs
def rebaseMaster(requestInfo,user): ''' rebase master ''' projectId = int(requestInfo['projectId'] or 0) if 'projectId' in requestInfo else 0 branch_name = requestInfo['branch_name'] if 'branch_name' in requestInfo else '' branch_type = requestInfo['branch_type'] if 'branch_type' in requestInfo else '' new_remote="" for app in branch_type.split(','): if app == "member": app = "anjuke" if "_usersite" in branch_type and "api_usersite" != branch_type: app = "anjuke_usersite" if app +"," not in new_remote: new_remote = new_remote + app + "," result,message,gitOutputs = Git.rebaseMaster(branch_name,new_remote,projectId,user) return result, message,gitOutputs
def __init__(self, config=None): self.headers = {} self.set_config(config) self.git = Git("/Users/xtao/gentoo/usr/bin/git") self.RE_SERVICES = []
def getBranchDiff(requestInfo,ifnull): if ifnull == "": branch = requestInfo['branch'] if 'branch' in requestInfo else '' remote = requestInfo['remote'] if 'remote' in requestInfo else '' else : branch = requestInfo remote = ifnull start = branch.find("-") end = start+6 branch_id = branch[start+1:end] if "pmt" in branch: bt=1 else: bt=2 try: pb = Project.objects.filter(relatedId = branch_id,type = bt)[0] branch_desc = pb.name if '(' in branch_desc : branch_desc = branch_desc.replace('(','') if ')' in branch_desc : branch_desc = branch_desc.replace(')','') if '(' in branch_desc : branch_desc = branch_desc.replace('(','') if ')' in branch_desc : branch_desc = branch_desc.replace(')','') except: branch_desc ="" shellPath = settings.SHELL_PATH remote = remote.replace(",",",") if branch != '': m= '' new_remote="" for branch_type in remote.split(','): if branch_type == "member": branch_type = "anjuke" if "_usersite" in branch_type and "api_usersite" != branch_type: branch_type = "anjuke_usersite" if branch_type +"," not in new_remote: new_remote = new_remote + branch_type + "," branch_name=branch + "-"+ branch_type branch_name = branch_name.strip() url='big' message,url = Git.branchDiff(branch,branch_type) message1=message m = m + message1 +'' if remote == "": m='第一个表单填写前缀(pmt-12345-site),第2个表单填写分支后缀(anjuke,haozu)' if (branch_type != '' and 'nochange' not in url and 'Syntax' not in url and 'wait' not in url and 'exist' not in url): try: b = BranchDiff.objects.get(branch_name=branch_name,status=0) b.delete() info = BranchInfo.objects.get(branch_name = diff.branch_name) info.if_ckeck=0 except: pass url_start = url.find("diff/") url_id = url[url_start+5:] if branch_type !='': diff = BranchDiff( branch_name = branch_name, type = branch_type, title = branch_desc, generator = 0, url_id = url_id, bt = bt, did = branch_id, status = 0) try: diff.save() except: pass return m else: return ''
def __init__(self, config=None): self.headers = {} self.set_config(config) self.git = Git("/usr/bin/git") self.RE_SERVICES = []
class GHTTPServer(object): VALID_SERVICE_TYPES = ['upload-pack', 'receive-pack'] SERVICES = [ [ "POST", 'service_rpc', re.compile("(.*?)/git-upload-pack$"), 'upload-pack' ], [ "POST", 'service_rpc', re.compile("(.*?)/git-receive-pack$"), 'receive-pack' ], ["GET", 'get_info_refs', re.compile("(.*?)/info/refs$")], ["GET", 'get_text_file', re.compile("(.*?)/HEAD$")], ["GET", 'get_text_file', re.compile("(.*?)/objects/info/alternates$")], [ "GET", 'get_text_file', re.compile("(.*?)/objects/info/http-alternates$") ], ["GET", 'get_info_packs', re.compile("(.*?)/objects/info/packs$")], ["GET", 'get_text_file', re.compile("(.*?)/objects/info/[^/]*$")], [ "GET", 'get_loose_object', re.compile("(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$") ], [ "GET", 'get_pack_file', re.compile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.pack$") ], [ "GET", 'get_idx_file', re.compile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$") ], ] def __init__(self, config=None): self.headers = {} self.set_config(config) self.git = Git("/usr/bin/git") self.RE_SERVICES = [] def set_config(self, config): self.config = config or {} def set_config_setting(self, key, value): self.config[key] = value def __call__(self, environ, start_response): self.env = environ body = self.call() start_response(self.status, self.headers.items()) return body def call(self): match = self.match_routing(self.env["PATH_INFO"].lstrip('/'), self.env["REQUEST_METHOD"]) if not match: return self.render_not_found() cmd, path, reqfile, rpc = match self.rpc = rpc self.reqfile = reqfile if cmd == "not_allowed": return self.render_method_not_allowed() self.dir = self.get_git_dir(path) if not self.dir: return self.render_not_found() func = getattr(self, cmd) return func() def service_rpc(self): if not self.has_access(self.rpc, True): return self.render_no_access() input = self.read_body git_cmd = "upload_pack" if self.rpc == "upload-pack" else "receive_pack" self.status = "200" self.headers["Content-Type"] = "application/x-git-%s-result" % self.rpc return getattr(self.git, git_cmd)(self.dir, {"msg": input}, callback) def get_info_refs(self): service_name = self.get_service_type() if self.has_access(service_name): git_cmd = "upload_pack" if service_name == "upload-pack" else "receive_pack" refs = getattr(self.git, git_cmd)(self.dir, { "advertise_refs": True }) self.status = "200" self.headers[ "Content-Type"] = "application/x-git-%s-advertisement" % service_name self.hdr_nocache() def read_file(): yield self.pkt_write("# service=git-%s\n" % service_name) yield self.pkt_flush yield refs return read_file() else: return self.dumb_info_refs() def get_text_file(self): return self.send_file(self.reqfile, "text/plain") def dumb_info_refs(self): self.update_server_info() return self.send_file(self.reqfile, "text/plain; charset=utf-8") def get_info_packs(self): # objects/info/packs return self.send_file(self.reqfile, "text/plain; charset=utf-8") def get_loose_object(self): return self.send_file(self.reqfile, "application/x-git-loose-object", cached=True) def get_pack_file(self): return self.send_file(self.reqfile, "application/x-git-packed-objects", cached=True) def get_idx_file(self): return self.send_file(self.reqfile, "application/x-git-packed-objects-toc", cached=True) def get_service_type(self): def get_param(): for query in self.env["QUERY_STRING"].split('&'): param = tuple(query.split('=')) if param and param[0] == "service": return param[1] service_type = get_param() if not service_type: return False if service_type[0:4] != 'git-': return False return service_type.replace('git-', '') @classmethod def match_routing(cls, path_info, request_method): for service in cls.SERVICES: rpc = None if len(service) == 4: method, handler, re_match, rpc = service elif len(service) == 3: method, handler, re_match = service m = re_match.match(path_info) if m: if method != request_method: return ["not_allowed", None, None, None] cmd = handler path = m.group(1) file = path_info.replace(path + '/', '') return [cmd, path, file, rpc] return None def send_file(self, reqfile, content_type, cached=False): reqfile = join(self.dir, reqfile) if not self.is_subpath(reqfile, self.dir): return self.render_no_access() if not exists(reqfile) or not access(reqfile, os.R_OK): return self.render_not_found() self.status = "200" self.headers["Content-Type"] = content_type self.headers["Last-Modified"] = format_date_time(getmtime(reqfile)) if cached: self.hdr_cache_forenver() else: self.hdr_nocache() size = getsize(reqfile) if size: self.headers["Content-Length"] = size def read_file(): with open(reqfile, "rb") as f: while True: part = f.read(8192) if not part: break yield part return read_file() else: with open(reqfile, "rb") as f: part = f.read() self.headers["Content-Length"] = str(len(part)) return [part] def update_server_info(self): self.git.update_server_info(self.dir) @property def read_body(self): input = self.env["wsgi.input"] return input.read() # ------------------------------ # packet-line handling functions # ------------------------------ @property def pkt_flush(self): return '0000' def pkt_write(self, str): # TODO: use zfill PKT_FORMAT = "{0:{fill}{align}{width}{base}}{1}" return PKT_FORMAT.format(len(str) + 4, str, base='x', width=4, fill='0', align='>') # ------------------------ # header writing functions # ------------------------ def hdr_nocache(self): self.headers["Expires"] = "Fri, 01 Jan 1980 00:00:00 GMT" self.headers["Pragma"] = "no-cache" self.headers["Cache-Control"] = "no-cache, max-age=0, must-revalidate" def hdr_cache_forenver(self): now = int(time.time()) self.headers["Date"] = str(now) self.headers["Expires"] = str(now + 31536000) self.headers["Cache-Control"] = "public, max-age=31536000" # -------------------------------------- # HTTP error response handling functions # -------------------------------------- def render_method_not_allowed(self): env = [] if env["SERVER_PROTOCOL"] == "HTTP/1.1": self.status = "405" self.headers["Content-Type"] = "text/plain" return ["Method Not Allowed"] else: self.status = "400" self.headers["Content-Type"] = "text/plain" return ["Bad Request"] def render_not_found(self): self.status = "404" self.headers["Content-Type"] = "text/plain" return ["Not Found"] def render_no_access(self): self.status = "403" self.headers["Content-Type"] = "text/plain" return ["Forbidden"] def has_access(self, rpc, check_content_type=False): if check_content_type: if self.env["CONTENT_TYPE"] != "application/x-git-%s-request" % rpc: return False if rpc not in self.VALID_SERVICE_TYPES: return False if rpc == 'receive-pack': if "receive_pack" in self.config: return self.config.get("receive_pack") if rpc == 'upload-pack': if "upload_pack" in self.config: return self.config.get("upload_pack") return self.get_config_setting(rpc) def get_config_setting(self, service_name): service_name = service_name.replace('-', '') setting = self.git.get_config_setting(self.dir, "http.%s" % service_name) if service_name == 'uploadpack': return setting != 'false' else: return setting == 'true' def get_git_dir(self, path): root = self.get_project_root() path = join(root, path) if not self.is_subpath(path, root): return False if exists(path): # TODO: check is a valid git directory return path return False def get_project_root(self): root = self.config.get("project_root") or os.getcwd() return root def is_subpath(self, path, checkpath): path = unquote(path) checkpath = unquote(checkpath) # Remove trailing slashes from filepath checkpath = checkpath.replace("\/+$", '') if re.match("^%s(\/|$)" % checkpath, path): return True
class GHTTPServer(object): VALID_SERVICE_TYPES = ['upload-pack', 'receive-pack'] SERVICES = [ ["POST", 'service_rpc', re.compile("(.*?/?.*)/git-upload-pack$"), 'upload-pack'], ["POST", 'service_rpc', re.compile("(.*?/?.*)/git-receive-pack$"), 'receive-pack'], ["GET", 'get_info_refs', re.compile("(.*?/?.*)/info/refs$")], ["GET", 'get_text_file', re.compile("(.*?/?.*)/HEAD$")], ["GET", 'get_text_file', re.compile("(.*?/?.*)/objects/info/alternates$")], ["GET", 'get_text_file', re.compile("(.*?/?.*)/objects/info/http-alternates$")], ["GET", 'get_info_packs', re.compile("(.*?/?.*)/objects/info/packs$")], ["GET", 'get_text_file', re.compile("(.*?/?.*)/objects/info/[^/]*$")], ["GET", 'get_loose_object', re.compile("(.*?/?.*)/objects/[0-9a-f]{2}/[0-9a-f]{38}$")], ["GET", 'get_pack_file', re.compile("(.*?/?.*)/objects/pack/pack-[0-9a-f]{40}\\.pack$")], ["GET", 'get_idx_file', re.compile("(.*?/?.*)/objects/pack/pack-[0-9a-f]{40}\\.idx$")], ] def __init__(self, config=None): self.headers = {} self.set_config(config) self.git = Git(config.get('git_path', '/usr/bin/git')) self.RE_SERVICES = [] def set_config(self, config): self.config = config or {} def set_config_setting(self, key, value): self.config[key] = value def __call__(self, environ, start_response): self.env = environ body = self.call() start_response(self.status, self.headers.items()) return body def git_method(self, *args, **kwargs): if self.rpc == "upload-pack": git_cmd = "upload-pack" else: git_cmd = "receive-pack" return self.git.cmd_pack(git_cmd, *args, **kwargs) def call(self): match = self.match_routing(self.env["PATH_INFO"].lstrip('/'), self.env["REQUEST_METHOD"]) if not match: return self.render_not_found() cmd, path, reqfile, rpc = match self.rpc = rpc self.reqfile = reqfile if cmd == "not_allowed": return self.render_method_not_allowed() self.dir = self.get_git_dir(path) if not self.dir: return self.render_not_found() func = getattr(self, cmd) return func() def service_rpc(self): if not self.has_access(self.rpc, True): return self.render_no_access() input = self.read_body self.status = "200" self.headers["Content-Type"] = "application/x-git-%s-result" % self.rpc return self.git_method(self.dir, {"msg": input}, callback) def get_info_refs(self): service_name = self.get_service_type() if self.has_access(service_name): refs = self.git_method(self.dir, {"advertise_refs": True}) content_type = "application/x-git-%s-advertisement" % service_name self.status = "200" self.headers["Content-Type"] = content_type self.hdr_nocache() def read_file(): yield self.pkt_write("# service=git-%s\n" % service_name) yield self.pkt_flush yield refs return read_file() else: return self.dumb_info_refs() def get_text_file(self): return self.send_file(self.reqfile, "text/plain") def dumb_info_refs(self): self.update_server_info() return self.send_file(self.reqfile, "text/plain; charset=utf-8") def get_info_packs(self): # objects/info/packs return self.send_file(self.reqfile, "text/plain; charset=utf-8") def get_loose_object(self): return self.send_file(self.reqfile, "application/x-git-loose-object", cached=True) def get_pack_file(self): return self.send_file(self.reqfile, "application/x-git-packed-objects", cached=True) def get_idx_file(self): return self.send_file(self.reqfile, "application/x-git-packed-objects-toc", cached=True) def get_service_type(self): def get_param(): for query in self.env["QUERY_STRING"].split('&'): param = tuple(query.split('=')) if param and param[0] == "service": return param[1] service_type = get_param() if not service_type: return False if service_type[0:4] != 'git-': return False return service_type.replace('git-', '') @classmethod def match_routing(cls, path_info, request_method): for service in cls.SERVICES: rpc = None if len(service) == 4: method, handler, re_match, rpc = service elif len(service) == 3: method, handler, re_match = service m = re_match.match(path_info) if m: if method != request_method: return ["not_allowed", None, None, None] cmd = handler path = m.group(1) file = path_info.replace(path + '/', '') return [cmd, path, file, rpc] return None def send_file(self, reqfile, content_type, cached=False): reqfile = join(self.dir, reqfile) if not self.is_subpath(reqfile, self.dir): return self.render_no_access() if not exists(reqfile) or not access(reqfile, os.R_OK): return self.render_not_found() self.status = "200" self.headers["Content-Type"] = content_type self.headers["Last-Modified"] = format_date_time(getmtime(reqfile)) if cached: self.hdr_cache_forenver() else: self.hdr_nocache() size = getsize(reqfile) if size: self.headers["Content-Length"] = size def read_file(): with open(reqfile, "rb") as f: while True: part = f.read(8192) if not part: break yield part return read_file() else: with open(reqfile, "rb") as f: part = f.read() self.headers["Content-Length"] = str(len(part)) return [part] def update_server_info(self): self.git.update_server_info(self.dir) @property def read_body(self): input = self.env["wsgi.input"] if self.env.get('HTTP_CONTENT_ENCODING') == 'gzip': compressedstream = StringIO.StringIO(input.read()) gzipper = gzip.GzipFile(fileobj=compressedstream) return gzipper.read() return input.read() # ------------------------------ # packet-line handling functions # ------------------------------ @property def pkt_flush(self): return '0000' def pkt_write(self, str): # TODO: use zfill PKT_FORMAT = "{0:{fill}{align}{width}{base}}{1}" return PKT_FORMAT.format(len(str) + 4, str, base='x', width=4, fill='0', align='>') # ------------------------ # header writing functions # ------------------------ def hdr_nocache(self): self.headers["Expires"] = "Fri, 01 Jan 1980 00:00:00 GMT" self.headers["Pragma"] = "no-cache" self.headers["Cache-Control"] = "no-cache, max-age=0, must-revalidate" def hdr_cache_forenver(self): now = int(time.time()) self.headers["Date"] = str(now) self.headers["Expires"] = str(now + 31536000) self.headers["Cache-Control"] = "public, max-age=31536000" # -------------------------------------- # HTTP error response handling functions # -------------------------------------- def render_method_not_allowed(self): env = [] if env["SERVER_PROTOCOL"] == "HTTP/1.1": self.status = "405" self.headers["Content-Type"] = "text/plain" return ["Method Not Allowed"] else: self.status = "400" self.headers["Content-Type"] = "text/plain" return ["Bad Request"] def render_not_found(self): self.status = "404" self.headers["Content-Type"] = "text/plain" return ["Not Found"] def render_no_access(self): self.status = "403" self.headers["Content-Type"] = "text/plain" return ["Forbidden"] def has_access(self, rpc, check_content_type=False): if (check_content_type and self.env["CONTENT_TYPE"] != "application/x-git-%s-request" % rpc): return False if rpc not in self.VALID_SERVICE_TYPES: return False if rpc == 'receive-pack': if "receive_pack" in self.config: return self.config.get("receive_pack") if rpc == 'upload-pack': if "upload_pack" in self.config: return self.config.get("upload_pack") return self.get_config_setting(rpc) def get_config_setting(self, service_name): service_name = service_name.replace('-', '') setting = self.git.get_config_setting(self.dir, "http.%s" % service_name) if service_name == 'uploadpack': return setting != 'false' else: return setting == 'true' def get_git_dir(self, path): root = self.config.get('project_root', os.getcwd()) path = join(root, path) if not self.is_subpath(path, root): print '"%s" is not a subpath of "%s"' % (path, root) return False if exists(path): # TODO: check is a valid git directory return path else: print "%s does not exist" % path return False def is_subpath(self, path, directory): path = os.path.realpath(path) directory = os.path.realpath(directory) relative = os.path.relpath(path, directory) return not relative.startswith(os.pardir + os.sep)
def __init__(self, config=None): self.headers = {} self.set_config(config) self.git = Git(config.get('git_path', '/usr/bin/git')) self.RE_SERVICES = []
def deploy( project, git_ref, namespace, dry_run=False, force=False, force_helm=False, repo=None, set_override_values=None, ): if set_override_values is None: set_override_values = [] with tempfile.TemporaryDirectory() as tmpdirname: pr = PublishRules() helm = Helm() cr = cluster_rules(namespace=namespace) helm_path = "{}/{}".format(tmpdirname, project) hr = HelmRules(cr, project) git_account = load_git_configs()["account"] repo = repo or project git_url = f"[email protected]:{git_account}/{repo}.git" git_ref = Git.extract_hash(git_ref, git_url) if not force and cr.check_branch and Git.extract_hash( cr.check_branch, git_url) != git_ref: logging.error( f"You are deploying hash {git_ref} which does not match branch" f" {cr.check_branch} on cluster {cr.cluster_name} for project" f" {project}... exiting") sys.exit(1) helm.pull_package(project, pr, git_ref, tmpdirname) # We need to use --set-string in case the git ref is all digits helm_args = ["--set-string", f"deploy.imageTag={git_ref}"] # Values precedence is command < cluster rules < --set-override-values # Deploy command values values = { "service.certificateArn": cr.get_certificate_arn(), "deploy.ecr": pr.docker_registry, } # Update with cluster rule values values.update(cr.values) # Update with --set-override-values value_overrides = { k: v for k, v in (value.split("=") for value in set_override_values) } values.update(value_overrides) if dry_run: helm.dry_run(hr, helm_path, cr.cluster_name, namespace, helm_args=helm_args, **values) else: helm.start(hr, helm_path, cr.cluster_name, namespace, force_helm, helm_args=helm_args, **values) sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)
def merge_to_master(requestInfo): """ """ branch_name = requestInfo["branchName"] if "branchName" in requestInfo else "" branch_name = branch_name.strip() branch_type = requestInfo["which"] if "which" in requestInfo else "" type1 = requestInfo["type"] if "type" in requestInfo else "" print (branch_name) start = branch_name.find("-") end = start + 6 branch_id = branch_name[start + 1 : end] if "pmt" in branch_name: bt = 1 else: bt = 2 try: branch = Project.objects.filter(relatedId=branch_id, type=bt)[0] branch_desc = branch.name if str(bt) == "1": plist_id = branch.id else: plist_id = 0 if "(" in branch_desc: branch_desc = branch_desc.replace("(", "") if ")" in branch_desc: branch_desc = branch_desc.replace(")", "") if "(" in branch_desc: branch_desc = branch_desc.replace("(", "") if ")" in branch_desc: branch_desc = branch_desc.replace(")", "") except: branch_desc = "" if not branch_name: return "请输入分支名" # if branch_type not in branch_name: # message = '分支名:'+branch_name+"当前仓库:"+branch_type+'不匹配。请去对应的页面做操作。' # return message if ( "api" not in branch_type and "java" not in branch_type and "_chat" not in branch_type and "cms" not in branch_type ): try: if_check = BranchDiff.objects.filter(branch_name=branch_name).order_by("-id")[0] if_check_status = if_check.status except: if_check_status = 0 else: if_check_status = 1 if str(if_check_status) == "1": message = Git.mergeToBranch(branch_name, branch_type, branch_desc) else: message = branch_name + "分支还没被check,请找开发check" def sha1(type): try: shellPath = settings.SHELL_PATH opResult = os.popen(shellPath + "get-sha1.sh %s" % (type)).read() sha = str(opResult) sha1 = sha[0:6] except: sha1 = "error" return sha1 if message == "合并成功": versionList = VersionList( version="", branch_type=type1, branch_name=branch_name, title=branch_desc, sha1=sha1(branch_type), if_on_beta=0, if_on_ga=0, first_create_ga_generator=0, last_create_ga_generator=0, status=0, back_num=0, branch_id=branch_id, plist_id=plist_id, ) versionList.save() branch_info = BranchInfo.objects.get(branch_name=branch_name) branch_info.status = 88 branch_info.save() if str(bt) == "1": ProjectModule.updateInfo(plist_id, branch_id) print (message) return message
def createNewBranch(requestInfo,user): ''' 创建新分支 ''' projectId = int(requestInfo['projectId'] or 0) if 'projectId' in requestInfo else 0 branch = requestInfo['branch_name'] if 'branch_name' in requestInfo else '' branch_type = requestInfo['branch_type'] if 'branch_type' in requestInfo else '' nowNum=branch[-1] try: num=int(nowNum) num = num + 1 index=branch.find('-',8) branch_start=branch[0:index] branch_end=branch[index:40] branch_end = branch_end.replace(str(nowNum), str(num)) branch_name =branch_start+branch_end except: num=1 branch_name = branch+str(num) pb = Branch.objects.get(project=projectId) pb.name = branch_name pb.save() pl = Project.objects.get(id=projectId) pl.status = 3 pl.save(); remote = "" app_info = "" arr_branch_old = [] arr_branch = [] arr_remote = [] for app in branch_type.split(','): if app == "member": app = "anjuke" if "_usersite" in app and "api_usersite" != app: app = "anjuke_usersite" if app+"," not in remote: remote=remote+app+"," branch_name1=branch+"-"+app branch_remote = BranchInfo.objects.filter(branch_name = branch_name1)[0] arr_branch.append(branch_name+"-"+app) arr_branch_old.append(branch_name1) arr_remote.append(branch_remote.create_version) # branch_remote = BranchInfo.objects.filter(plist_id = projectId) # for b in branch_remote: # b.gc = 0 # b.create_version = '' # b.save() InsertInfo.insert(branch_name,remote,projectId,pl.status) for i in range(len(arr_branch)): print (arr_branch[i]) print (arr_remote[i]) print (arr_branch_old[i]) branch_remote = BranchInfo.objects.filter(branch_name = arr_branch[i])[0] branch_remote.create_version = arr_remote[i] branch_remote.gc = 1 branch_remote.save() branch_remote = BranchInfo.objects.filter(branch_name = arr_branch_old[i])[0] branch_remote.create_version = '' branch_remote.gc = 0 branch_remote.save() Git.createNewBranch(branch_name,remote,projectId,user) return 'OK'