Exemplo n.º 1
0
def deployServiceWork(username, passwd, localServicePath, remoteTarPath, version, host, projectName, git_url):
    failureTimes = 0
    try:
        logCollector("#" * 30 + "部署任务开始" + "#" * 30)
        # 删除遗留的项目文件夹,并判断删除结果
        logCollector("#" * 20 + "step 1:删除上次遗留的" + projectName + " 项目" + "#" * 20)
        rmDirCmd = "rm -rf %s/%s" % (localServicePath, projectName)
        (recode, result) = shellCmd(rmDirCmd)
        logCollector(result)
        if recode == 0:
            logCollector("." * 20 + "SUCCESS!!:DELETE THE OLD " + "." * 20)
            logCollector("#" * 20 + "step 2:git 克隆" + projectName + "项目包到本地" + "#" * 20)
            # 从gitlab把项目的工程打包下来,并判断打包是否成功
            gitCloneCmd = "git clone -b %s http://readonly:readonly@%s %s/%s" % (
                version,
                git_url,
                localServicePath,
                projectName,
            )
            (recode, result) = shellCmd(gitCloneCmd)
            logCollector(result)
            if recode == 0:
                logCollector("." * 20 + "SUCCESS!!: GIT CLONE THE MODELS" + "." * 20)
                # 判断pom.xml文件 发布版本是否存在snatshot release 版本字样
                versionTag = resoveXml("%s/%s/%s-business/pom.xml" % (localServicePath, projectName, projectName))
                if not ("SNAPSHOT" in versionTag.upper() or "RELEASE" in versionTag.upper()):
                    if version == versionTag:
                        logCollector("#" * 20 + "setp 3:maven " + projectName + " contract 组件" + "#" * 20)
                        # 进入项目的contract目录下先清理后deploy,并判断是否成功
                        inFounfationContractCmd = "%s/%s/%s-contract" % (localServicePath, projectName, projectName)
                        os.chdir(inFounfationContractCmd)
                        mvnCleanCmd = "mvn clean deploy"
                        (recode, result) = shellCmd(mvnCleanCmd)
                        logCollector(result)
                        if recode == 0:
                            logCollector("." * 20 + "SUCCESS!!:MVN CLEAN DEPLOY" + "." * 20)
                        else:
                            logCollector("." * 20 + "FAILURE!!:MVN CLEAN DEPLOY" + "." * 20)
                            failureTimes = failureTimes + 1
                        # 进入项目的business目录下,打包项目,并判断是否成功
                        logCollector("#" * 20 + "setp 4:maven " + projectName + " business 组件" + "#" * 20)
                        inFounfationBusinessCmd = "../../../%s/%s/%s-business" % (
                            localServicePath,
                            projectName,
                            projectName,
                        )
                        os.chdir(inFounfationBusinessCmd)
                        mvnInstallCmd = "mvn clean install -Dmaven.test.skip=true -U"
                        (recode, result) = shellCmd(mvnInstallCmd)
                        logCollector(result)
                        if recode == 0:
                            logCollector("." * 20 + "SUCCESS!!:MVN CLEAN INSTALL" + "." * 20)
                            logCollector("#" * 20 + "step 5:上传" + projectName + "部署包到远端服务器" + "#" * 20)
                            localPath = "target/"
                            tarFilename = "%s-business-%s-assembly.tar.gz" % (projectName, version)
                            shellStatus = putFileWork(
                                accout=username,
                                passwd=passwd,
                                host=host,
                                localPath=localPath,
                                remotePath=remoteTarPath,
                                filename=tarFilename,
                            )
                            if shellStatus:
                                logCollector("." * 20 + "SUCCESS!!:UPLOAD THE TAR PACKAGE" + "." * 20)
                                return (0, failureTimes)
                            else:
                                logCollector("." * 20 + "FAILURE!!:UPLOAD THE TAR PACKAGE" + "." * 20)
                                failureTimes = failureTimes + 1
                                return (1, failureTimes)
                        else:
                            logCollector("." * 20 + "FAILURE!!:MVN CLEAN INSTALL" + "." * 20)
                            failureTimes = failureTimes + 1
                            return (1, failureTimes)
                    else:
                        logCollector(
                            "." * 20
                            + "FAILURE!!:THE DEPLOY VERSION:%s != THE POM.xml VERSION:%s " % (version, versionTag)
                            + "." * 20
                        )
                        failureTimes = failureTimes + 1
                        return (1, failureTimes)
                else:
                    logCollector(
                        "." * 20 + "FAILURE!!:PLEASE CHECK THE POM.xml FILE(have the %s) " % versionTag + "." * 20
                    )
                    failureTimes = failureTimes + 1
                    return (1, failureTimes)
            else:
                logCollector("." * 20 + "FAILURE!!: GIT CLONE THE MODELS" + "." * 20)
                failureTimes = failureTimes + 1
                return (1, failureTimes)
        else:
            logCollector("." * 20 + "FAILURE!!:DELETE THE OLD " + "." * 20)
            failureTimes = failureTimes + 1
            return (1, failureTimes)
    except subprocess.CalledProcessError, msg:
        logCollector(str(msg))
        logCollector("#" * 20 + "部署任务出现异常" + "#" * 20)
        failureTimes = failureTimes + 1
        return (1, failureTimes)
Exemplo n.º 2
0
def deployWebWork(username, passwd, localServicePath, remoteTarPath, version, host, projectName, git_url):
    failureTimes = 0
    try:
        logCollector("#" * 30 + "部署任务开始" + "#" * 30)
        # 删除遗留的项目文件夹,并判断删除结果
        logCollector("#" * 20 + "step 1:删除上次遗留的" + projectName + " 项目" + "#" * 20)
        rmDirCmd = "rm -rf %s/%s" % (localServicePath, projectName)
        (recode, result) = shellCmd(rmDirCmd)
        logCollector(result)
        # 如果删除ok,开始git 工程包到本地
        if recode == 0:
            logCollector("." * 20 + "SUCCESS!!:DELETE THE OLD " + "." * 20)
            logCollector("#" * 20 + "step 2:git 克隆" + projectName + "项目包到本地" + "#" * 20)
            gitCloneCmd = "git clone -b %s http://readonly:readonly@%s %s/%s" % (
                version,
                git_url,
                localServicePath,
                projectName,
            )  # print gitCloneCmd
            (recode, result) = shellCmd(gitCloneCmd)
            logCollector(result)
            # 如果git ok,进入到 项目目录,判断pom文件时候符合要求,
            if recode == 0:
                logCollector("." * 20 + "SUCCESS!!: GIT CLONE THE MODELS" + "." * 20)
                logCollector("#" * 20 + "setp 3:maven " + projectName + "组件" + "#" * 20)
                inFounfationBusinessCmd = "%s/%s/" % (localServicePath, projectName)
                os.chdir(inFounfationBusinessCmd)
                versionTag = resoveXml("pom.xml")
                # 如果pom文件符合要求,开始maven
                if not ("SNAPSHOT" in versionTag.upper() or "RELEASE" in versionTag.upper()):
                    if versionTag == version:
                        mvnInstallCmd = "mvn clean install -Dmaven.test.skip=true -U"
                        (recode, result) = shellCmd(mvnInstallCmd)
                        logCollector(result)
                        # 如果maven成功,开始上传到远程服务器
                        if recode == 0:
                            logCollector("." * 20 + "SUCCESS!!:MVN CLEAN INSTALL" + "." * 20)
                            logCollector("#" * 20 + "step 5:上传" + projectName + "部署包到远端服务器" + "#" * 20)
                            localPath = "target/"
                            tarFilename = "%s-%s.war" % (projectName, version)
                            shellStatus = putFileWork(
                                accout=username,
                                passwd=passwd,
                                host=host,
                                localPath=localPath,
                                remotePath=remoteTarPath,
                                filename=tarFilename,
                            )
                            if shellStatus:
                                logCollector("." * 20 + "SUCCESS!!:UPLOAD THE TAR PACKAGE" + "." * 20)
                                return (0, failureTimes)
                            else:
                                logCollector("." * 20 + "FAILURE!!:UPLOAD THE TAR PACKAGE" + "." * 20)
                                failureTimes = failureTimes + 1
                                return (1, failureTimes)
                        else:
                            logCollector("." * 20 + "FAILURE!!:MVN CLEAN INSTALL" + "." * 20)
                            failureTimes = failureTimes + 1
                            return (1, failureTimes)
                    else:
                        logCollector(
                            "." * 20
                            + "FAILURE!!:THE DEPLOY VERSION:%s != THE POM.xml VERSION:%s " % (version, versionTag)
                            + "." * 20
                        )
                        failureTimes = failureTimes + 1
                        return (1, failureTimes)
                else:
                    logCollector(
                        "." * 20 + "FAILURE!!:PLEASE CHECK THE POM.xml FILE(have the %s) " % versionTag + "." * 20
                    )
                    failureTimes = failureTimes + 1
                    return (1, failureTimes)
            else:
                logCollector("." * 20 + "FAILURE!!: GIT CLONE THE MODELS" + "." * 20)
                failureTimes = failureTimes + 1
                return (1, failureTimes)
        else:
            logCollector("." * 20 + "FAILURE!!:DELETE THE OLD " + "." * 20)
            failureTimes = failureTimes + 1
            return (1, failureTimes)
    except subprocess.CalledProcessError, msg:
        logCollector(str(msg))
        logCollector("#" * 20 + "部署任务出现异常" + "#" * 20)
        failureTimes = failureTimes + 1
        return (1, failureTimes)