Example #1
0
    def publish(self, oper: Oper, site: Site, server_type=0):
        try:
            # 判断本地代码文件是否存在(不存在则拉取文件)
            path = '{}/{}/{}.tar.gz'.format(CODE_DIR, site.project, oper.oper_version.version)
            p = Path(path)
            if not p.is_file():
                if not p.parent.is_dir():
                    p.parent.mkdir(parents=True, exist_ok=True)

                with open(path, 'wb') as f:
                    f.write(gl.pull_archive(gl.get_project(site.project), oper.oper_version.version))

            # 远程服务器代码保存路径
            dest = "{0}/{1}/{1}-{2}-{3}".format(site.dev_path, site.project, oper.oper_version.version[:8], oper.oper_version.version)

            fbs = set()

            # 过滤指定环境的服务器
            webservers = site.webserver_set.filter(type=server_type).all()
            for webserver in webservers:
                server = webserver.server
                fb = FabricApi(server.ip, server.deploy_user, server.deploy_pwd)
                # 判断代码文件是否存在(不存在则发送文件)
                fb.send_file(path, dest)
                fbs.add(fb)

            #
            for fb in fbs:
                fb.update_symbolic_link(site.site_path, dest)

            oper.oper_status += 1

            if oper.oper_status == 1:
                site.pre_version = oper.oper_version
            else:
                site.pre_version = None
                site.online_version = oper.oper_version
                site.status = 2
                site.deploy_status = False

            oper.deploy_status = 0
            oper.save()
            site.save()
        except:
            self.rollback(oper, site, False if server_type else True)
Example #2
0
    def rollback(self, oper: Oper, site: Site, all=False):
        try:
            dest = "{0}/{1}/{1}-{2}-{3}".format(site.dev_path, site.project, oper.online_version.version[:8], oper.online_version.version)

            webservers = site.webserver_set
            if not all:
                webservers = webservers.filter(type=0)

            for webserver in webservers.all():
                server = webserver.server
                FabricApi(server.ip, server.deploy_user, server.deploy_pwd).update_symbolic_link(site.site_path, dest)

            oper.oper_status = 5 if all else 4
            site.online_version = oper.online_version
        except:
            oper.oper_status = 6
            site.status = 3
        finally:
            oper.deploy_status = 0
            oper.save()

            site.deploy_status = False
            site.pre_version = None
            site.save()