Esempio n. 1
0
def close_selinux():
    resp = get_shell_response('getenforce').strip()
    if resp != 'Disabled':
        path = '/etc/selinux/config'
        f = FileModify(path)
        f.replace('(?<=SELINUX=).*', 'disabled')
        exec_shell('setenforce 0')
Esempio n. 2
0
 def device_alter(self, server_path):
     path = os.path.join(server_path, 'yaobili-business-device', 'src',
                         'main', 'java', 'com', 'yaobili', 'platform',
                         'device', 'constants', 'TopicConstants.java')
     topic = CONFIG.TOPIC.get(self.env)
     f = FileModify(path)
     f.replace('TOPIC_SUF = ".*?";', 'TOPIC_SUF = "{}";'.format(topic))
     print('修改device topic为 {}, 文件地址为:{}'.format(topic, path), flush=True)
Esempio n. 3
0
def init_api_server_config():
    path = os.path.join(TEMPLATE_DIR, 'master', 'kube-apiserver.service')
    f = FileModify(path)
    for i in IPS.get('master'):
        ip, port = parse_address(i)
        f.replace('(?<=--bind-address=).*', '{} \\'.format(ip))
        f.replace('(?<=service-cluster-ip-range=).*',
                  '{} \\'.format(SETTINGS.SERVICE_CIDR))
        f.replace('(?<=service-node-port-range=).*',
                  '{} \\'.format(SETTINGS.NODE_PORT_RANGE))
        f.replace('(?<=etcd-servers=).*', '{} \\'.format(get_etcd_endpoints()))
        send_files(
            os.path.join(TEMPLATE_DIR, 'master', 'kube-apiserver.service'),
            '/usr/lib/systemd/system', i)
 def get_history_data(self,k=None):
     """  返回历史数据 """
     history_path = os.path.join(BASE_DIR, '.history')
     f = FileModify(history_path, autocreate=True)
     data = []
     for line in f.content().split('\n')[:-1]:
         data.append(tuple(line.split('||')))
     if k:
         for i in data:
             if i[0] == k:
                 return i
         return ()
     else:
         return data
Esempio n. 5
0
    def base_init(self, ssh, sftp_client):
        # 基础文件夹
        ssh.run('mkdir -p %s/{lib,pid,history}' %
                os.path.dirname(CONFIG.DEPLOY_DIR))

        # 初始化python36环境
        f = FileModify(
            os.path.join(get_project_root_path(), 'utils',
                         'install_python36.sh'))
        ssh.run(f.content())

        # 传python文件
        sftp_client.put(os.path.join(BASE_DIR, 'config.py'),
                        os.path.join(CONFIG.ROOT_DIR, 'config.py'))
        sftp_client.put(os.path.join(BASE_DIR, 'deploy.py'),
                        os.path.join(CONFIG.ROOT_DIR, 'deploy.py'))
        put_dir(sftp_client, os.path.join(get_project_root_path(), 'utils'),
                os.path.join(CONFIG.ROOT_DIR, 'utils'))
Esempio n. 6
0
    def init_hosts(self):
        if len(self.env_config) > 1:
            hosts_file = FileModify('/tmp/hosts', autocreate=True)
            hosts_file.cover("""
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
""")
            threads = []
            for ip_address in self.env_config:
                t = ExcTread(target=self.edit_hosts_file,
                             args=(ip_address, hosts_file),
                             name=ip_address)
                t.start()
                threads.append(t)

            for i in threads:
                i.join()
                if i.exception:
                    self._exception[i.name] = i.exception
                    self.env_config.pop(i.name)
Esempio n. 7
0
def init_network():
    # 关掉NetworkManager服务
    stop_service(['NetworkManager'])
    # 修改配置文件
    net_info = get_netcard()
    for n, ip in net_info:
        path = '/etc/sysconfig/network-scripts/ifcfg-{0}'.format(n)
        if os.path.isfile(path):
            gateway = get_gateway(n)
            f = FileModify(path)
            f.cover("""
            TYPE="Ethernet"
            BOOTPROTO=static
            IPADDR={ip}
            NETMASK=255.255.255.0
            GATEWAY={gateway}
            NM_CONTROLLED=no
            NAME={name}
            DEVICE={name}
            ONBOOT=yes
            DNS1=223.5.5.5
            """.format(name=n, ip=ip, gateway=gateway))
    # 重启服务
    restart_service(['network'])
Esempio n. 8
0
def init_etcd_ca():
    path = os.path.join(TEMPLATE_DIR,'etcd','etcd-csr.json')
    f = FileModify(path)
    template_content = f.content()
    result = json.loads(template_content,encoding='UTF-8')
    for ip_ in IPS.get('etcd'):
        ip,port = parse_address(ip_)
        result['hosts'].append(ip)
    f.cover(json.dumps(result))
    cfssl_dir = get_cfssl_dir()
    os.chdir(os.path.join(TEMPLATE_DIR,'etcd'))
    exec_shell('{0}/cfssl gencert -ca={1}/ca.pem -ca-key={1}/ca-key.pem -config={1}/ca-config.json '
               '-profile=kubernetes etcd-csr.json | {0}/cfssljson -bare etcd'.format(cfssl_dir,os.path.join(TEMPLATE_DIR,'ca')))
    f.cover(template_content)
Esempio n. 9
0
def init_controller_manager_config():
    path = os.path.join(TEMPLATE_DIR, 'master',
                        'kube-controller-manager.service')
    f = FileModify(path)
    for i in IPS.get('master'):
        f.replace('(?<=service-cluster-ip-range=).*',
                  '{} \\'.format(SETTINGS.SERVICE_CIDR))
        f.replace('(?<=cluster-cidr=).*', '{} \\'.format(SETTINGS.POD_CIDR))
        send_files(
            os.path.join(TEMPLATE_DIR, 'master',
                         'kube-controller-manager.service'),
            '/usr/lib/systemd/system', i)
    def is_alarm(self,k,result):
        """
        判断是否进行告警提示,相同告警进行拦截,相同告警在过了间隔时间后才会再次预警
        :return: 返回True,则可以进行告警,反之则否
        """
        history_path = os.path.join(BASE_DIR, '.history')
        f = FileModify(history_path, autocreate=True)
        data = self.get_history_data(k)
        now = int(time.time())
        if data and isinstance(data,tuple):
            alarm_data_3 = eval(data[2]).get('alarm_data').get('3')
            if alarm_data_3 == result.get('alarm_data').get('3'):
                time_diff = now - int(data[1])
                if time_diff > 3600 * CONFIG.ISOLATE_TIME:
                    f.replace(data[1],now)
                    return True
                else:
                    return False
            else:
                f.replace('^{}.*'.format(k),'{}||{}||{}'.format(k,now,result))
                return True

        f.add('{}||{}||{}'.format(k,now,result))
        return True
Esempio n. 11
0
def init_ca():
    path = os.path.join(TEMPLATE_DIR, 'master', 'kubernetes-csr.json')
    f = FileModify(path)
    template_content = f.content()
    result = json.loads(template_content, encoding='UTF-8')
    for ip_ in IPS.get('master'):
        ip, port = parse_address(ip_)
        result['hosts'].append(ip)
    result['hosts'].append(SETTINGS.CLUSTER_KUBERNETES_SVC_IP)
    f.cover(json.dumps(result))
    cfssl_dir = get_cfssl_dir()
    os.chdir(os.path.join(TEMPLATE_DIR, 'master'))
    ca_dir = os.path.join(TEMPLATE_DIR, 'ca')
    exec_shell(
        '{0}/cfssl gencert -ca={1}/ca.pem -ca-key={1}/ca-key.pem -config={1}/ca-config.json '
        '-profile=kubernetes kubernetes-csr.json | {0}/cfssljson -bare kubernetes'
        .format(cfssl_dir, ca_dir))
    exec_shell(
        '{0}/cfssl gencert -ca={1}/ca.pem -ca-key={1}/ca-key.pem -config={1}/ca-config.json '
        '-profile=kubernetes admin-csr.json | {0}/cfssljson -bare admin'.
        format(cfssl_dir, ca_dir))
    f.cover(template_content)
Esempio n. 12
0
def init():
    ips = get_all_ip(IPS)
    scripts_dir = os.path.join(CONFIG.PROJECT_DIR, 'utils', 'scripts', 'k8s',
                               'init')
    for ipa in ips:
        if not check_rsa_secret():
            create_rsa_secret()
        ssh_copy_id(ipa)
        ip, port = parse_address(ipa)
        print('init {}...'.format(ip))
        if not check_is_localip(ip):
            ssh = SSHConnect(host=ip, port=int(port))
            for filename in os.listdir(scripts_dir):
                f = FileModify(os.path.join(scripts_dir, filename))
                ssh.run(f.content())
            ssh.close()
        else:
            for filename in os.listdir(scripts_dir):
                f = FileModify(os.path.join(scripts_dir, filename))
                exec_shell(f.content())
Esempio n. 13
0
    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))
Esempio n. 14
0
def api_alter(project_path):
    main_js_path = os.path.join(project_path, 'src', 'main.js')
    f = FileModify(main_js_path)
    f.replace("(?<=import api from './assets/js/).*?(?=';)", 'api_old')
Esempio n. 15
0
def set_hostname(hostname):
    exec_shell('hostname {0}'.format(hostname))
    path = '/etc/hostname'
    f = FileModify(path)
    f.cover(hostname)
Esempio n. 16
0
def set_hosts(ip, hostname):
    path = '/etc/hosts'
    f = FileModify(path)
    f.add('{ip} {hostname}'.format(ip=ip, hostname=hostname))
Esempio n. 17
0
def shop_alter(project_path, project):
    index_html_path = os.path.join(project_path, 'index.html')
    f = FileModify(index_html_path)
    f.replace('<title>.*?</title>',
              '<title>{}</title>'.format(CONFIG.TOPICS[project].get('title')))

    appvue_path = os.path.join(project_path, 'src', 'App.vue')
    f = FileModify(appvue_path)
    f.replace('(?<=import bodyHeader from "@/components/).*(?=";)',
              CONFIG.TOPICS[project].get('top'))
    f.replace('(?<=import submenu from "@/components/).*?(?=";)',
              CONFIG.TOPICS[project].get('left'))

    route_path = os.path.join(project_path, 'src', 'router', 'index.js')
    f = FileModify(route_path)
    login = CONFIG.TOPICS[project].get('login')
    f.replace_all(
        '// region.*?path: \'/\'.*?},', """// region
        {
          path: '/',
          name: '%s',
          component: %s,
          meta: {
            keepAlive: false,
          }
        },""" % (login, login))
Esempio n. 18
0
def init_config():
    path = os.path.join(TEMPLATE_DIR, 'etcd', 'etcd.conf')
    f = FileModify(path)
    etcd_cluster = get_etcd_cluster()
    f.replace('(?<=ETCD_INITIAL_CLUSTER=").*(?=")',etcd_cluster)
    etcd_cluster_dict = get_etcd_cluster('dict')
    for i in IPS.get('etcd'):
        ip,port = parse_address(i)
        f.replace('(?<=ETCD_NAME=").*(?=")',etcd_cluster_dict.get(ip))
        f.replace('(?<=ETCD_LISTEN_PEER_URLS=").*(?=")','https://{}:2380'.format(ip))
        f.replace('(?<=ETCD_LISTEN_CLIENT_URLS=").*(?=")','https://{}:2379,https://127.0.0.1:2379'.format(ip))
        f.replace('(?<=ETCD_INITIAL_ADVERTISE_PEER_URLS=").*(?=")','https://{}:2380'.format(ip))
        f.replace('(?<=ETCD_ADVERTISE_CLIENT_URLS=").*(?=")','https://{}:2379'.format(ip))
        send_files(path,'/opt/kubernetes/cfg/',i)