Example #1
0
def add(product, _type, idc, name, server_names, log_name, log_format, upstream_node):
    """ 增加域名的配置.

    """
    upstreams = upstream.get()
    upstream_nodes = [i["name"] for i in upstreams]
    if upstream_node not in upstream_nodes:
        raise Exception("%s not exist" % upstream_node)
    else:
        for i in upstreams:
            if i["name"] == upstream_node:
                if i["online"] != 1:
                    raise Exception("%s is offline" % i["name"])
                else:
                    break

    domain_conf_dir = "%s/sites-available/%s/%s/%s/" % (NGINX_CONF_DIR, product, _type, idc)
    domain_conf_path = "%s/%s" % (domain_conf_dir, name)

    if not os.path.exists(domain_conf_dir):
        os.makedirs(domain_conf_dir)

    if os.path.exists(domain_conf_path):
        raise ("%s exists" % domain_conf_path)

    template.gen_server(
        NGINX_TEMPLATE_DIR,
        NGINX_SERVER_TEMPLATE_FILE,
        domain_conf_path,
        server_names,
        log_name,
        log_format,
        upstream_node,
    )

    cmds = """cd %s &&
        git checkout master &&
        git pull &&
        git add sites-available/%s/%s/%s/%s &&
        git commit -m 'add %s' &&
        git push origin master """ % (
        NGINX_CONF_DIR,
        product,
        _type,
        idc,
        name,
        name,
    )

    _shell(cmds)
    return True
Example #2
0
def add(product, _type, idc, name, server_names, log_name, log_format,
        upstream_node):
    """ 增加域名的配置.

    """
    upstreams = upstream.get()
    upstream_nodes = [i["name"] for i in upstreams]
    if upstream_node not in upstream_nodes:
        raise Exception("%s not exist" % upstream_node)
    else:
        for i in upstreams:
            if i["name"] == upstream_node:
                if i["online"] != 1:
                    raise Exception("%s is offline" % i["name"])
                else:
                    break

    domain_conf_dir = "%s/sites-available/%s/%s/%s/" % (NGINX_CONF_DIR,
                                                        product, _type, idc)
    domain_conf_path = "%s/%s" % (domain_conf_dir, name)

    if not os.path.exists(domain_conf_dir):
        os.makedirs(domain_conf_dir)

    if os.path.exists(domain_conf_path):
        raise ("%s exists" % domain_conf_path)

    template.gen_server(NGINX_TEMPLATE_DIR, NGINX_SERVER_TEMPLATE_FILE,
                        domain_conf_path, server_names, log_name, log_format,
                        upstream_node)

    cmds = """cd %s &&
        git checkout master &&
        git pull &&
        git add sites-available/%s/%s/%s/%s &&
        git commit -m 'add %s' &&
        git push origin master """ % (NGINX_CONF_DIR, product, _type, idc,
                                      name, name)

    _shell(cmds)
    return True
Example #3
0
def get(nginx, branch):
    """ 根据 Nginx 机器名打包配置文件, 并上传到远端和获取下载地址.

    """
    # 拿到 Nginx 机器的 产品线,类型,机房.
    loki_path = loki.get_path_from_hostname(nginx)
    if loki_path == []:
        raise Exception("%s has no loki path." % nginx)
    elif len(loki_path) > 1:
        raise Exception("%s has %s loki path - %s." % (
                            nginx, 
                            len(loki_path), 
                            loki_path))

    tmp = loki_path[0].split("/")
    product = tmp[2]   # 产品线
    _type = tmp[-2]   # 类型, 表示内网还是内网.
    idc = tmp[-1]    # 机房.
    del tmp
    logger.info("%s %s %s" % (product, _type, idc))

    # 创建临时 Nginx 配置文件目录.
    if not os.path.exists(NGINX_TMP_STORAGE_DIR):
        os.mkdir(NGINX_TMP_STORAGE_DIR)
    _global_id = global_id.get()
    conf_tmp_dir = "%s/%s/%s" % (os.getcwd(), 
                                 NGINX_TMP_STORAGE_DIR, 
                                 _global_id)

    # 拷贝最新 Nginx 配置.
    _conf(branch, NGINX_CONF_DIR, conf_tmp_dir)

    # 拿到 upstream data.
    upstream_data = list()
    for i in upstream.get():
            if i["online"] == 1:
                servers = loki.get_hostnames_from_id(i["loki_id"])
                if servers == []:
                    raise("%s has no servers." % i["loki_id"])
                _dict = {
                    "name": i["name"],
                    "servers": servers,
                    "port": i["port"],
                    "ip_hash": i["ip_hash"]
                }
                upstream_data.append(_dict)

    # 生成 upstream.conf 文件.
    upstream_path = "%s/upstream.conf" % conf_tmp_dir
    template.gen_upstream(NGINX_TEMPLATE_DIR, 
                 NGINX_UPSTREAM_TEMPLATE_FILE, 
                 upstream_path, 
                 upstream_data)

    # 做软链.
    cmd = "sh nginx/libs/link.sh %s %s %s %s %s %s" % (conf_tmp_dir, product, 
                                                       _type, idc, 
                                                       NGINX_SSL_ORIGIN_DIR, 
                                                       NGINX_SSL_DEST_DIR)
    _shell(cmd)

    # 打包.
    package_name = "%s.tgz" % _global_id
    cmd = "cd %s &&tar czf ../%s *" % (conf_tmp_dir, 
                                       package_name)
    _shell(cmd)

    # 把 Nginx 包上传到存储服务, 并返回下载连接.
    package_path = "%s/%s" % (NGINX_TMP_STORAGE_DIR, package_name)
    logger.info("package_path:%s" % package_path)
    return storage.post(package_path)
Example #4
0
def get(nginx, branch):
    """ 根据 Nginx 机器名打包配置文件, 并上传到远端和获取下载地址.

    """
    # 拿到 Nginx 机器的 产品线,类型,机房.
    loki_path = loki.get_path_from_hostname(nginx)
    if loki_path == []:
        raise Exception("%s has no loki path." % nginx)
    elif len(loki_path) > 1:
        raise Exception("%s has %s loki path - %s." %
                        (nginx, len(loki_path), loki_path))

    tmp = loki_path[0].split("/")
    product = tmp[2]  # 产品线
    _type = tmp[-2]  # 类型, 表示内网还是内网.
    idc = tmp[-1]  # 机房.
    del tmp
    logger.info("%s %s %s" % (product, _type, idc))

    # 创建临时 Nginx 配置文件目录.
    if not os.path.exists(NGINX_TMP_STORAGE_DIR):
        os.mkdir(NGINX_TMP_STORAGE_DIR)
    _global_id = global_id.get()
    conf_tmp_dir = "%s/%s/%s" % (os.getcwd(), NGINX_TMP_STORAGE_DIR,
                                 _global_id)

    # 拷贝最新 Nginx 配置.
    _conf(branch, NGINX_CONF_DIR, conf_tmp_dir)

    # 拿到 upstream data.
    upstream_data = list()
    for i in upstream.get():
        if i["online"] == 1:
            servers = loki.get_hostnames_from_id(i["loki_id"])
            if servers == []:
                raise ("%s has no servers." % i["loki_id"])
            _dict = {
                "name": i["name"],
                "servers": servers,
                "port": i["port"],
                "ip_hash": i["ip_hash"]
            }
            upstream_data.append(_dict)

    # 生成 upstream.conf 文件.
    upstream_path = "%s/upstream.conf" % conf_tmp_dir
    template.gen_upstream(NGINX_TEMPLATE_DIR, NGINX_UPSTREAM_TEMPLATE_FILE,
                          upstream_path, upstream_data)

    # 做软链.
    cmd = "sh nginx/libs/link.sh %s %s %s %s %s %s" % (
        conf_tmp_dir, product, _type, idc, NGINX_SSL_ORIGIN_DIR,
        NGINX_SSL_DEST_DIR)
    _shell(cmd)

    # 打包.
    package_name = "%s.tgz" % _global_id
    cmd = "cd %s &&tar czf ../%s *" % (conf_tmp_dir, package_name)
    _shell(cmd)

    # 把 Nginx 包上传到存储服务, 并返回下载连接.
    package_path = "%s/%s" % (NGINX_TMP_STORAGE_DIR, package_name)
    logger.info("package_path:%s" % package_path)
    return storage.post(package_path)
Example #5
0
    def get(self):
        """ 返回 upstream 列表.

        """
        self.write(json.dumps(upstream.get()))
Example #6
0
    def get(self):
        """ 返回 upstream 列表.

        """
        self.write(json.dumps(upstream.get()))