Exemple #1
0
 def restart(self, ip_address, services):
     ip, port = parse_address(ip_address)
     ssh = SSHConnect(host=ip, port=port)
     sftp_client = ssh.get_sftp()
     self.base_init(ssh, sftp_client)
     self.deploy(ssh, ip_address, services)
     ssh.close()
Exemple #2
0
    def init_deploy_dir(self):
        print('init_deploy_dir......', flush=True)
        ip, port = parse_address(CONFIG.K8S_MASTER.get(self.env))
        self.ssh = SSHConnect(host=ip, port=port)
        self.ssh.run('mkdir -p {}'.format(CONFIG.DEPLOY_YAMLS_DIR))
        self.sftp_client = self.ssh.get_sftp()

        # 传python文件
        put_dir(self.sftp_client, CONFIG.PROJECT_DIR, CONFIG.DEPLOY_DIR)
        put_dir(
            self.sftp_client, os.path.join(get_project_root_path(), 'utils'), os.path.join(CONFIG.DEPLOY_DIR, 'utils')
        )
Exemple #3
0
    def install(self, ip_address, services):
        ip, port = parse_address(ip_address)
        ssh = SSHConnect(host=ip, port=port)
        sftp_client = ssh.get_sftp()

        # 基础初始化
        self.base_init(ssh, sftp_client)

        # 传hosts文件
        if len(self.env_config) > 1:
            sftp_client.put('/tmp/hosts', '/etc/hosts')

        # 传JAR包
        self.send_jar_file(ssh, sftp_client, services)

        # 部署
        self.deploy(ssh, ip_address, services)
        ssh.close()
Exemple #4
0
 def rollback(self, ip_address, services):
     ip, port = parse_address(ip_address)
     ssh = SSHConnect(host=ip, port=port)
     sftp_client = ssh.get_sftp()
     self.base_init(ssh, sftp_client)
     for i in services:
         history_jar_path = '{}/{}-{}.jar'.format(CONFIG.HISTORY_DIR, i,
                                                  CONFIG.JAR_VERSION)
         remote_jar_path = '{}/{}-{}.jar'.format(CONFIG.DEPLOY_DIR, i,
                                                 CONFIG.JAR_VERSION)
         ssh.run('cp -rf {} {}'.format(history_jar_path, remote_jar_path))
     self.deploy(ssh, ip_address, services)
     ssh.close()
Exemple #5
0
 def rollback(self, ip_address, projects):
     ip, port = parse_address(ip_address)
     ssh = SSHConnect(host=ip, port=port)
     for project in projects:
         try:
             ssh.run(
                 'cd {0}/{1}/{2} && rm -rf ./* && tar -zxf {0}/{1}/history_version/{2}.tar.gz'
                 .format(CONFIG.DEPLOY_DIR, self.env, project))
         except Exception:
             print('ERROR: {} 项目不存在,无法回滚'.format(project), flush=True)
     ssh.close()
Exemple #6
0
 def edit_hosts_file(self, ip_address, hosts_file):
     ip, port = parse_address(ip_address)
     ssh = SSHConnect(host=ip, port=port)
     hostname = ssh.run('hostname', response=True)
     ssh.close()
     hosts_file.add('{} {}'.format(ip, hostname.strip()))
Exemple #7
0
 def install(self, ip_address, projects):
     ip, port = parse_address(ip_address)
     ssh = SSHConnect(host=ip, port=port)
     sftp_client = ssh.get_sftp()
     ssh.run('mkdir -p {}/{}/history_version && mkdir -p {}'.format(
         CONFIG.DEPLOY_DIR, self.env, CONFIG.TMP_DIR))
     for project in projects:
         targz_path = '{}/{}.tar.gz'.format(CONFIG.LIB_DIR, project)
         remote_targz_path = '{}/{}.tar.gz'.format(CONFIG.TMP_DIR, project)
         sftp_client.put(targz_path, remote_targz_path)
         ssh.run(
             'mkdir -p {0}/{1}/{2} && cd {0}/{1}/{2} && tar -zcf {0}/{1}/history_version/{2}.tar.gz ./'
             .format(CONFIG.DEPLOY_DIR, self.env, project))
         ssh.run('cd {0}/{1}/{2} && rm -rf ./* && tar -zxf {3}/{2}.tar.gz'.
                 format(CONFIG.DEPLOY_DIR, self.env, project,
                        CONFIG.TMP_DIR))
     ssh.close()
Exemple #8
0
class NewServerDeploy(object):

    def __init__(self, services, action, env, replicas, port, namespace):
        self.services = services
        self.action = action
        self.env = env
        self.replicas = (replicas if replicas else CONFIG.REPLICAS)
        self.port = (port if len(services) == 1 and port else None)
        self.namespace = namespace
        self.treafik_domain = CONFIG.TREAFIK_DOMAINS.get(env)
        self.registry_namespace = None
        self.ssh = None
        self.sftp_client = None

    def start_deploy(self):
        if self.action == 'reset':
            self.registry_namespace = CONFIG.REGISTRY_NAMESPACE.get('prod')
        else:
            self.registry_namespace = CONFIG.REGISTRY_NAMESPACE.get(self.env)

        if self.action in ('start', 'rollback', 'stop', 'reload', 'reset'):
            self.init_deploy_dir()
            if self.action in ('start', 'reload', 'reset'):
                self.init_yaml_file()

            cmd = 'source /etc/profile && python36 {}/deploy.py {} {} -e {} -n {}'.format(
                CONFIG.DEPLOY_DIR, self.action, ','.join(self.services), self.env, self.namespace
            )
            print(cmd, flush=True)
            self.ssh.run(cmd)
            self.ssh.close()

    def init_deploy_dir(self):
        print('init_deploy_dir......', flush=True)
        ip, port = parse_address(CONFIG.K8S_MASTER.get(self.env))
        self.ssh = SSHConnect(host=ip, port=port)
        self.ssh.run('mkdir -p {}'.format(CONFIG.DEPLOY_YAMLS_DIR))
        self.sftp_client = self.ssh.get_sftp()

        # 传python文件
        put_dir(self.sftp_client, CONFIG.PROJECT_DIR, CONFIG.DEPLOY_DIR)
        put_dir(
            self.sftp_client, os.path.join(get_project_root_path(), 'utils'), os.path.join(CONFIG.DEPLOY_DIR, 'utils')
        )

    def get_svc_yaml_path(self, service):
        template = '{}/{}.yaml'.format(CONFIG.TEMPLATE_DIR, service)
        if not os.path.exists(template):
            template = CONFIG.YAML_TEMPLATE
        svc_yaml_path = '{}/{}.yaml'.format(CONFIG.TMP_DIR, service)
        exec_shell('\cp {} {}'.format(template, svc_yaml_path))
        return svc_yaml_path

    def init_yaml_file(self):
        for service in self.services:
            svc_yaml_path = self.get_svc_yaml_path(service)
            yaml = FileModify(svc_yaml_path)

            # Domain Ingress
            if service in self.treafik_domain:
                exec_shell('cat {} >> {}'.format(CONFIG.INGRESS_TEMPLATE, svc_yaml_path))
                yaml.replace('DOMAIN', self.treafik_domain.get(service))

            # base settings
            yaml.replace('MINREADYSECONDS', str(CONFIG.MINREADYSECONDS))
            yaml.replace('REVISIONHISTORYLIMIT', str(CONFIG.REVISIONHISTORYLIMIT))
            yaml.replace('APPNAME', service)
            # namespace
            yaml.replace('NAMESPACE', self.namespace)
            # replicas
            yaml.replace('REPLICAS', str(self.replicas))
            # nfs
            yaml.replace('NFS_SERVER', CONFIG.NFS_SERVER.get(self.env))
            # port
            yaml.replace('PORT', str(self.port if self.port else CONFIG.SERVICE_PORTS.get(service)))
            # images path 镜像地址
            yaml.replace('IMAGE_PATH', self.get_image_path(service))

            # 传yaml文件
            self.sftp_client.put(svc_yaml_path, '{}/{}.yaml'.format(CONFIG.DEPLOY_YAMLS_DIR, service))

    def get_image_path(self, service):
        domain = CONFIG.REGISTRY_DOMAIN
        cmd = 'cat {}/{}.yaml |grep registry'.format(CONFIG.DEPLOY_YAMLS_DIR, service)
        registry = self.ssh.run(cmd, response=True, ignore_error=True).strip()
        if registry:
            result = re.findall('registry.+?.aliyuncs.com', registry)
            domain = result[0]
            domain = (
                CONFIG.REGISTRY_DOMAIN_EXTRANET if domain == CONFIG.REGISTRY_DOMAIN else CONFIG.REGISTRY_DOMAIN
            )
        return '{}/{}/{}'.format(domain, self.registry_namespace, service)