Beispiel #1
0
def build_thread(deploy):
    # before checkout
    git = Git(deploy.project.checkout_dir, deploy.project.repo_url)
    before_checkout = deploy.project.before_checkout.replace("\r", "").replace("\n", " && ")
    logger.debug("before_checkout"+before_checkout)
    if before_checkout:
        LocalShell.check_call(
            "WORKSPACE='{0}' && mkdir -p $WORKSPACE && cd $WORKSPACE && {1}".format(
                deploy.project.checkout_dir, before_checkout),
            shell=True)
    # checkout
    git.clone()
    if deploy.mode == 0:
        git.checkout_branch(deploy.branch, deploy.version)
    else:
        git.checkout_tag(deploy.version)
    # after checkout
    after_checkout = deploy.project.after_checkout.replace("\r", "").replace("\n", " && ")
    if after_checkout:
        LocalShell.check_call(
            "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                deploy.project.checkout_dir, after_checkout),
            shell=True)
Beispiel #2
0
def deploy_thread(service, deploy):
    ssh = RemoteShell(host=deploy.host.ssh_host,
                      port=deploy.host.ssh_port,
                      user=deploy.host.ssh_user,
                      passwd=deploy.host.ssh_pass)
    try:
        service.update(deploy, progress=0, status=2)
        # before checkout
        git = Git(deploy.project.checkout_dir, deploy.project.repo_url)
        before_checkout = deploy.project.before_checkout.replace("\r", "").replace("\n", " && ")
        logger.debug("before_checkout"+before_checkout)
        if before_checkout:
            LocalShell.check_call(
                "WORKSPACE='{0}' && mkdir -p $WORKSPACE && cd $WORKSPACE && {1}".format(
                    deploy.project.checkout_dir, before_checkout),
                shell=True)
        service.update(deploy, progress=17)
        # checkout
        git.clone()
        if deploy.mode == 0:
            git.checkout(deploy.branch, deploy.version)
        else:
            git.checkout(tag=deploy.version)
        service.update(deploy, progress=33)
        # after checkout
        after_checkout = deploy.project.after_checkout.replace("\r", "").replace("\n", " && ")
        if after_checkout:
            LocalShell.check_call(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.checkout_dir, after_checkout),
                shell=True)
        service.update(deploy, progress=50)
        # before deploy
        rc, stdout, stderr = ssh.exec_command(
            "mkdir -p {0}".format(
                os.path.join(deploy.project.deploy_history_dir, deploy.softln_filename)))
        if rc:
            raise Error(11003)

        logger.debug("before deploy:")
        rc, stdout, stderr = ssh.exec_command(
            "WORKSPACE='{0}' && cd $WORKSPACE && ls -1t | tail -n +20 | xargs rm -rf".format(
                deploy.project.deploy_history_dir))
        if rc:
            raise Error(11000)
        before_deploy = deploy.project.before_deploy.replace("\r", "").replace("\n", " && ")
        if before_deploy:
            rc, stdout, stderr = ssh.exec_command(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.deploy_dir, before_deploy))
            if rc:
                raise Error(11000)
        service.update(deploy, progress=67)
        # deploy
        logger.debug("deploy:")
        logger.debug("rsync:")
        shell = ("rsync -avzq --rsh=\"sshpass -p {ssh_pass} ssh -p {ssh_port}\" --exclude='.git' {local_dest}/ {ssh_user}@{ssh_host}:{remote_dest}/").format(
            local_dest=deploy.project.checkout_dir,
            remote_dest=os.path.join(deploy.project.deploy_history_dir, deploy.softln_filename),
            ssh_user=deploy.host.ssh_user,
            ssh_host=deploy.host.ssh_host,
            ssh_port=deploy.host.ssh_port,
            ssh_pass=deploy.host.ssh_pass)
        LocalShell.check_call(shell, shell=True)
        rc,stdout, stderr = ssh.exec_command("ln -snf {0} {1}".format(
            os.path.join(deploy.project.deploy_history_dir, deploy.softln_filename), deploy.project.deploy_dir))
        if rc:
            raise Error(11001)
        service.update(deploy, progress=83)

        # after deploy
        logger.debug("after deploy:")
        after_deploy = deploy.project.after_deploy.replace("\r", "").replace("\n", " && ")
        if after_deploy:
            rc, stdout, stderr = ssh.exec_command(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.deploy_dir, after_deploy))
            if rc:
                raise Error(11002)
    except Exception:
        service.update(deploy, status=0)
    else:
        service.update(deploy, progress=100, status=1)
    finally:
        ssh.close()
Beispiel #3
0
def deploy_thread(service, deploy):
    ssh = RemoteShell(host=deploy.host.ssh_host,
                      port=deploy.host.ssh_port,
                      user=deploy.host.ssh_user,
                      passwd=deploy.host.ssh_pass)
    try:
        service.update(deploy, progress=0, status=2)
        # before checkout
        git = Git(deploy.project.checkout_dir, deploy.project.repo_url)
        before_checkout = deploy.project.before_checkout.replace("\r", "").replace("\n", " && ")
        logger.debug("before_checkout"+before_checkout)
        service.append_comment(deploy, "before checkout:\n")
        if before_checkout:
            cmd = "WORKSPACE='{0}' && mkdir -p $WORKSPACE && cd $WORKSPACE && {1}".format(
                    deploy.project.checkout_dir, before_checkout)
            LocalShell.check_call(cmd, shell=True)
        service.append_comment(deploy, "OK!\n")
        service.update(deploy, progress=17)
        # checkout
        service.append_comment(deploy, "checkout:\n")
        git.clone()
        if deploy.mode == 0:
            git.checkout_branch(deploy.branch, deploy.version)
        else:
            git.checkout_tag(deploy.version)
        service.append_comment(deploy, "OK!\n")
        service.update(deploy, progress=33)
        # after checkout
        after_checkout = deploy.project.after_checkout.replace("\r", "").replace("\n", " && ")
        service.append_comment(deploy, "after checkout:\n")
        if after_checkout:
            cmd = "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.checkout_dir, after_checkout)
            LocalShell.check_call(cmd, shell=True)
        service.append_comment(deploy, "OK!\n")
        service.update(deploy, progress=50)
        # before deploy
        service.append_comment(deploy, "before deploy:\n")
        ssh.check_call(
            "mkdir -p {0}".format(
                os.path.join(deploy.project.deploy_history_dir, deploy.softln_filename)))

        logger.debug("before deploy:")
        ssh.check_call(
            "WORKSPACE='{0}' && cd $WORKSPACE && ls -1t | tail -n +{1} | xargs rm -rf".format(
                deploy.project.deploy_history_dir, config.MAX_DEPLOY_HISTORY))
        before_deploy = deploy.project.before_deploy.replace("\r", "").replace("\n", " && ")
        if before_deploy:
            ssh.check_call(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.deploy_dir, before_deploy))
        service.append_comment(deploy, "OK!\n")
        service.update(deploy, progress=67)
        # deploy
        service.append_comment(deploy, "deploy:\n")
        logger.debug("deploy:")
        logger.debug("rsync:")
        cmd = ("rsync -avzq --rsh=\"sshpass -p {ssh_pass} ssh -p {ssh_port}\" --exclude='.git' {local_dest}/ {ssh_user}@{ssh_host}:{remote_dest}/").format(
            local_dest=deploy.project.checkout_dir,
            remote_dest=os.path.join(deploy.project.deploy_history_dir, deploy.softln_filename),
            ssh_user=deploy.host.ssh_user,
            ssh_host=deploy.host.ssh_host,
            ssh_port=deploy.host.ssh_port,
            ssh_pass=deploy.host.ssh_pass)
        LocalShell.check_call(cmd, shell=True)
        ssh.check_call("ln -snf {0} {1}".format(
            os.path.join(deploy.project.deploy_history_dir, deploy.softln_filename), deploy.project.deploy_dir))
        service.append_comment(deploy, "OK!\n")
        service.update(deploy, progress=83)

        # after deploy
        service.append_comment(deploy, "after deploy:\n")
        logger.debug("after deploy:")
        after_deploy = deploy.project.after_deploy.replace("\r", "").replace("\n", " && ")
        if after_deploy:
            ssh.check_call(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.deploy_dir, after_deploy))
        service.append_comment(deploy, "OK!\n")
    except Exception as err:
        logger.error(err)
        service.append_comment(deploy, "Command: "+err.cmd+"\nReturn code: "+str(err.returncode)+"\nOutput: "+err.output)
        service.update(deploy, status=0)
    else:
        service.update(deploy, progress=100, status=1)
    finally:
        ssh.close()
Beispiel #4
0
 def git_log(self, project_id):
     project = self.get(project_id)
     git = Git(project.checkout_dir, project.repo_url)
     return git.log()
Beispiel #5
0
 def git_clone(self, project_id):
     project = self.get(project_id)
     git = Git(project.checkout_dir, project.repo_url)
     git.clone()
Beispiel #6
0
 def git_branch(self, project_id):
     project = self.get(project_id)
     git = Git(project.checkout_dir, project.repo_url)
     return git.remote_branch()
Beispiel #7
0
 def git_branch_commit_log(self, project, branch):
     git = Git(project.checkout_dir, project.repo_url)
     git.checkout_branch(branch)
     return git.log()
Beispiel #8
0
 def git_tag(self, project):
     git = Git(project.checkout_dir, project.repo_url)
     return git.tag()
Beispiel #9
0
 def git_branch(self, project):
     git = Git(project.checkout_dir, project.repo_url)
     return git.remote_branch()
Beispiel #10
0
 def git_clone(self, project):
     git = Git(project.checkout_dir, project.repo_url)
     git.clone()
Beispiel #11
0
def deploy_thread(project_id):
    deploys = DeploysService()
    deploy = deploys.first(project_id=project_id, status=3)
    if not deploy:
        logger.info("no deploy wait in quene.")
        return
    logger.info("deploy thread start: {}".format(deploy.id))
    ssh = RemoteShell(host=deploy.host.ssh_host,
                      port=deploy.host.ssh_port,
                      user=deploy.host.ssh_user,
                      passwd=deploy.host.ssh_pass)
    try:
        deploys.update(deploy, progress=0, status=2)
        # before checkout
        git = Git(deploy.project.checkout_dir, deploy.project.repo_url)
        before_checkout = deploy.project.before_checkout.replace(
            "\r", "").replace("\n", " && ")
        logger.debug("before_checkout"+before_checkout)
        deploys.append_comment(deploy, "before checkout:\n")
        cmd = "mkdir -p {0} && rm -rf {1}/*".format(
                deploy.project.target_dir, deploy.project.target_dir)
        LocalShell.check_call(cmd, shell=True)
        if before_checkout:
            cmd = "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.checkout_dir, before_checkout)
            LocalShell.check_call(cmd, shell=True)
        deploys.append_comment(deploy, "OK!\n")
        deploys.update(deploy, progress=17)
        # checkout
        deploys.append_comment(deploy, "checkout:\n")
        git.clone()
        if deploy.mode == 0:
            git.checkout_branch(deploy.branch, deploy.version)
        else:
            git.checkout_tag(deploy.version)
        deploys.append_comment(deploy, "OK!\n")
        deploys.update(deploy, progress=33)
        # after checkout
        after_checkout = deploy.project.after_checkout.replace(
            "\r", "").replace("\n", " && ")
        deploys.append_comment(deploy, "after checkout:\n")
        if after_checkout:
            cmd = "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.checkout_dir, after_checkout)
            LocalShell.check_call(cmd, shell=True)
        deploys.append_comment(deploy, "OK!\n")
        deploys.update(deploy, progress=50)
        # before deploy
        deploys.append_comment(deploy, "before deploy:\n")
        ssh.check_call(
            "mkdir -p {0}".format(
                os.path.join(deploy.project.deploy_history_dir,
                             deploy.softln_filename)))

        logger.debug("before deploy:")
        ssh.check_call(
            ("WORKSPACE='{0}' && cd $WORKSPACE && ls -1t | tail -n +{1} | "
             "xargs rm -rf").format(deploy.project.deploy_history_dir,
                                    config.MAX_DEPLOY_HISTORY))
        before_deploy = deploy.project.before_deploy.replace("\r", "").replace(
            "\n", " && ")
        if before_deploy:
            ssh.check_call(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.deploy_dir, before_deploy))
        deploys.append_comment(deploy, "OK!\n")
        deploys.update(deploy, progress=67)
        # deploy
        deploys.append_comment(deploy, "deploy:\n")
        logger.debug("deploy:")
        logger.debug("rsync:")
        if deploy.host.ssh_method == 0:
            cmd = ("rsync -avzq "
                   "--rsh=\"sshpass -p {ssh_pass} ssh -p {ssh_port}\" "
                   "--exclude='.git' {local_dest}/ {ssh_user}@{ssh_host}:"
                   "{remote_dest}/"
                   ).format(local_dest=deploy.project.target_dir,
                            remote_dest=os.path.join(
                                deploy.project.deploy_history_dir,
                                deploy.softln_filename),
                            ssh_user=deploy.host.ssh_user,
                            ssh_host=deploy.host.ssh_host,
                            ssh_port=deploy.host.ssh_port,
                            ssh_pass=deploy.host.ssh_pass)
        else:
            cmd = ("rsync -avzq --exclude='.git' {local_dest}/ "
                   "{ssh_user}@{ssh_host}:{remote_dest}/"
                   ).format(local_dest=deploy.project.target_dir,
                            remote_dest=os.path.join(
                                deploy.project.deploy_history_dir,
                                deploy.softln_filename),
                            ssh_user=deploy.host.ssh_user,
                            ssh_host=deploy.host.ssh_host,
                            ssh_port=deploy.host.ssh_port,
                            ssh_pass=deploy.host.ssh_pass)
        LocalShell.check_call(cmd, shell=True)
        ssh.check_call("ln -snf {0} {1}".format(
            os.path.join(deploy.project.deploy_history_dir,
                         deploy.softln_filename),
            deploy.project.deploy_dir))
        deploys.append_comment(deploy, "OK!\n")
        deploys.update(deploy, progress=83)

        # after deploy
        deploys.append_comment(deploy, "after deploy:\n")
        logger.debug("after deploy:")
        after_deploy = deploy.project.after_deploy.replace("\r", "").replace(
            "\n", " && ")
        if after_deploy:
            ssh.check_call(
                "WORKSPACE='{0}' && cd $WORKSPACE && {1}".format(
                    deploy.project.deploy_dir, after_deploy))
        deploys.append_comment(deploy, "OK!\n")
    except Exception as err:
        traceback.print_exc()
        logger.error(err)
        deploys.append_comment(deploy, repr(err))
        deploys.update(deploy, status=0)
    else:
        deploys.update(deploy, progress=100, status=1)
    finally:
        logger.info("deploy thread end: %d" % deploy.id)
        ssh.close()
        deploy = deploys.first(project_id=project_id, status=3)
        if deploy:
            logger.info("deploy thread fetch from wait: {}".format(deploy))
            deploys.deploy(deploy)