Esempio n. 1
0
 def make_deploy_yaml(self, namespace, service_name, image_tag, ports, env,
                      version, replicas, env_args):
     """
     :param namespace:
     :param service_name:
     :param image_tag:
     :param ports:
     :param env:
     :param version:
     :param replicas:
     :param env_args:
     :return:
     """
     deploy_yaml = os.path.join(
         DEPLOY_YAML_DIR,
         "deploy_{0}_{1}.yaml".format(service_name, version))
     if os.path.exists(deploy_yaml):
         RecodeLog.warn(
             msg="{0}:文件已经存在,以前可能已经部署过,请检查后再执行!".format(deploy_yaml))
         return False
     # 容器内部端口
     container_port = list()
     # container port获取
     for x in ports:
         container_port.append({'containerPort': int(x)})
     # 初始化服务的dict
     deploy_dict = self.format_config_deploy(namespace=namespace,
                                             service_name=service_name,
                                             image_tag="{0}:{1}".format(
                                                 image_tag, version),
                                             container_port=container_port,
                                             env_args=env_args,
                                             replicas=replicas)
     # 生成yaml
     if not self.write_yaml(achieve=deploy_yaml, data=deploy_dict):
         RecodeLog.error(msg="生成yaml文件失败:{0}".format(deploy_yaml))
         return False
     if not self.apply_deployment(
             data=deploy_dict, namespace=namespace, env=env,
             name=service_name):
         return False
     return True
Esempio n. 2
0
    def get_port(path, rex='EXPOSE'):
        """
        :param rex:
        :param path:
        :return:
        """
        docker_file = os.path.join(path, 'Dockerfile')
        if not os.path.exists(docker_file):
            RecodeLog.warn(msg="不存在{0},可能为前端服务,请确认!".format(docker_file))
            return []

        with open(docker_file, 'r') as fff:
            data = fff.readlines()
        port = list()
        for line in data:
            if rex in line:
                port.append(
                    line.split(" ")[1].replace('\n', '').replace(' ', ''))
            else:
                continue
        return port
Esempio n. 3
0
 def front_build_pre(self, path, dockerfile_path, deploy_name):
     """
     :param path:
     :param dockerfile_path:
     :param deploy_name:
     :return:
     """
     bootstrap_dict = {
         "spring": {
             "application": {
                 "name": deploy_name
             },
             "cloud": {
                 "consul": {
                     "host": "${consul_host:127.0.0.1}",
                     "port": "${consul_port:8500}",
                     "enabled": True,
                     "discovery": {
                         "enabled": True,
                         "instance-id":
                         "${spring.application.name}:${server.port}",
                         "prefer-ip-address": True,
                         "health-check-interval": "10s",
                         "hostname": "${spring.application.name}",
                         "service-name": "${spring.application.name}"
                     }
                 }
             },
             "mvc": {
                 "favicon": {
                     "enabled": False
                 }
             },
             "boot": {
                 "admin": {
                     "client": {
                         "url":
                         "${nccc_admin_monitor:http://localhost:9020}",
                         "instance": {
                             "prefer-ip": True
                         }
                     }
                 }
             }
         },
         "server": {
             "port": "${server-port:8888}"
         }
     }
     if not os.path.exists(dockerfile_path):
         RecodeLog.error(msg="前端编译依赖的目录不存在,请检查")
         raise Exception("前端编译依赖的目录不存在,请检查")
     if os.path.exists(path):
         RecodeLog.warn(msg="存在之前的目录:{0},删除!".format(path))
         shutil.rmtree(path=path)
     shutil.copytree(src=dockerfile_path, dst=path)
     # 写入文件bootstrap
     bootstrap_yaml = os.path.join(path, 'target', 'classes',
                                   'bootstrap.yml')
     if not self.write_yaml(achieve=bootstrap_yaml, data=bootstrap_dict):
         raise Exception("写入文件失败:{0},内容:{1}".format(bootstrap_yaml,
                                                    bootstrap_dict))
     self.format_pom_xml(src=SRC_POM_DIR,
                         dsc=os.path.join(path, 'pom.xml'),
                         deploy_name=deploy_name)
Esempio n. 4
0
    def make_service_yaml(self, namespace, service_name, port_type,
                          exist_node_port, ports, env):
        """
        :param namespace:
        :param service_name:
        :param port_type:
        :param exist_node_port:
        :param ports:
        :param env:
        :return:
        """
        if len(ports) == 0:
            RecodeLog.warn(msg="没有端口")
            return True
        node_ports = list()
        service_achieve = os.path.join(SERVICE_YAML_DIR,
                                       "service_{0}.yaml".format(service_name))
        if os.path.exists(service_achieve):
            RecodeLog.info(msg="Service:{1},服务对应的配置文件:{0},已存在,跳过创建!".format(
                service_achieve, service_name))
            # 读取已经存在的yaml文件
            service_yaml = self.read_yaml(achieve=service_achieve)
            # 执行已经存在的yaml文件内容
            if not self.apply_service(data=service_yaml,
                                      namespace=namespace,
                                      env=env,
                                      name=service_name):
                return False
            return True
        for port in ports:
            if port_type == "NodePort":
                node_ports.append({
                    'name':
                    "{0}-{1}".format(service_name, port),
                    'nodePort':
                    self.make_node_port(data=exist_node_port,
                                        service_name=service_name,
                                        namespace=namespace),
                    'port':
                    int(port),
                    'protocol':
                    'TCP',
                    'targetPort':
                    int(port)
                })
            else:
                node_ports.append({
                    'name': "{0}-{1}".format(service_name, port),
                    'port': int(port),
                    'protocol': 'TCP',
                    'targetPort': int(port)
                })
        service_yaml = self.format_service(namespace=namespace,
                                           node_port_set=node_ports,
                                           port_type=port_type,
                                           service_name=service_name)

        if not self.write_yaml(achieve=service_achieve, data=service_yaml):
            RecodeLog.error(msg="生成yaml文件失败:{0}".format(service_yaml))
            # raise Exception("生成yaml文件失败:{0}".format(service_yaml))
            return False
        if not self.apply_service(
                data=service_yaml, namespace=namespace, env=env,
                name=service_name):
            return False
        return True