예제 #1
0
파일: cli.py 프로젝트: meisanggou/jingyun
def op_table():
    commands_man = args_man.add_subparsers(title="Commands",
                                           description=None,
                                           metavar="COMMAND OPTIONS",
                                           dest="sub_cmd")
    create_man = commands_man.add_parser("create", help=g_help("create"))
    create_man.add_argument("-c",
                            metavar="path",
                            dest="conf_path",
                            help=g_help("conf_path"))
    create_man.add_argument("-d",
                            metavar="path",
                            dest="directory",
                            help=g_help("dir"))
    create_man.add_argument("--file-prefix",
                            metavar="path",
                            dest="file_prefix",
                            help=g_help("file_prefix"))
    create_man.add_argument("-f",
                            metavar="path",
                            dest="files",
                            help=g_help("file"),
                            nargs="*",
                            default=[])

    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    args = args_man.parse_args()
    if args.sub_cmd == "create":
        create_table(args)
예제 #2
0
파일: user.py 프로젝트: meisanggou/jingyun
def cli_main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-a",
                        "--action",
                        dest="action",
                        help=g_help("action"),
                        choices=["reset", "block", "unlock", "genetic", "new"])
    parser.add_argument("-u", "--user", dest="user", help=g_help("user"))
    parser.add_argument("-p",
                        "--password",
                        dest="password",
                        help=g_help("new_password"),
                        default="123456")
    if len(sys.argv) <= 1:
        sys.argv.append("-h")

    args = parser.parse_args()

    if args.action is None:
        error_and_exit("Please Set [action] use -a or --action", 1)
    if args.user is None:
        error_and_exit("Please Set [user] use -u or --user", 2)
    if args.action == "reset":
        reset_password(args.user, args.password)
    elif args.action == "block":
        block_user(args.user)
    elif args.action == "unlock":
        unlock_user(args.user)
    elif args.action == "genetic":
        grant_genetic(args.user)
    elif args.action == "new":
        new_user(args.user, args.password)
예제 #3
0
파일: cli.py 프로젝트: meisanggou/jingyun
def get_key(endpoint, app, filters):
    params = dict(app=app)
    for item in filters:
        s = item.split("=", 1)
        if len(s) != 2:
            error_and_exit(g_help("error_filter", item))
        params[s[0]] = s[1]
    try:
        resp = requests.get(endpoint, params=params, headers=headers)
        if resp.status_code != 200:
            error_and_exit(g_help("error_get", resp.status_code))
        r_data = resp.json()
        if r_data["status"] is False:
            error_and_exit(r_data["data"])
        data = r_data["data"]
        for key in data.keys():
            if key[0] == "_":
                data[key[1:]] = data[key]
                del data[key]
    except requests.RequestException as re:
        error_and_exit(g_help("error_get", str(re)))
        return
    except ValueError as ve:
        error_and_exit(g_help("error_get", str(ve)))
        return
    except KeyError as ke:
        error_and_exit(g_help("error_get", str(ke)))
        return
    return data
예제 #4
0
def find_one_port():
    args_man.add_argument("-s",
                          "--server",
                          dest="server",
                          help=g_help("server"),
                          choices=define_server.keys())
    args_man.add_argument("-p",
                          "--ports",
                          dest="ports",
                          help=g_help("port"),
                          nargs="*")

    args = args_man.parse_args()
    all_ports = []
    if args.server is not None:
        all_ports.extend(define_server[args.server])
    if args.ports is not None:
        for port in args.ports:
            ports = re.split(r"\D", port)
            ports = map(lambda x: int(x), filter(lambda x: len(x) > 0, ports))
            all_ports.extend(ports)
    for item in all_ports:
        logger.debug("check is listen %s" % item)
        if is_listen(item) is False:
            logger.info(item)
            sys.exit(0)
    sys.exit(1)
예제 #5
0
파일: cli.py 프로젝트: meisanggou/jingyun
def read_conf():
    arg_man.add_argument("-c",
                         "--conf-path",
                         dest="conf_path",
                         help=g_help("conf_file"),
                         metavar="conf_path",
                         required=True)
    arg_man.add_argument("-i",
                         "--ignore-error",
                         dest="ignore_error",
                         help=g_help("ignore_error"),
                         action='store_true',
                         default=False)
    arg_man.add_argument("-s",
                         "--section",
                         dest="section",
                         help=g_help("section"),
                         metavar="section",
                         required=True)
    arg_man.add_argument("option",
                         metavar="option",
                         nargs="*",
                         help=g_help("option"))

    if len(sys.argv) <= 1:
        sys.argv.append("-h")

    args = arg_man.parse_args()

    values = read(args.conf_path,
                  args.section,
                  args.option,
                  ignore=args.ignore_error)
    for v in values:
        print(v)
예제 #6
0
파일: cli.py 프로젝트: meisanggou/jingyun
def read(conf_path, section, options, ignore=False):
    if os.path.exists(conf_path) is False:
        msg = g_help("file_not_exist", conf_path)
        if ignore is True:
            logger.warning(msg)
            return map(lambda x: "", options)
        error_and_exit(msg)
    config = ConfigParser.ConfigParser()
    config.read(conf_path)
    if config.has_section(section) is False:
        msg = g_help("section_not_found", section)
        if ignore is True:
            logger.warning(msg)
            return map(lambda x: "", options)
        error_and_exit(msg)
    values = []
    for option in options:
        if config.has_option(section, option) is False:
            msg = g_help("option_not_found", option)
            if ignore is True:
                logger.warning(msg)
                values.append("")
                continue
            error_and_exit(msg)
        values.append(config.get(section, option))
    return values
예제 #7
0
파일: cli.py 프로젝트: meisanggou/jingyun
def create_table(args):
    if args.conf_path is None:
        args.conf_path = get_environ("DB_CONF_PATH")
    t = TableDB(conf_path=args.conf_path)
    suffix_comp = re.compile(r"\w+\.(json|function|procedure|trigger|view)$",
                             re.I)
    all_files = args.files
    if args.file_prefix is not None:
        all_files = map(lambda x: args.file_prefix + x, all_files)
    if args.directory is not None:
        desc_files = os.listdir(args.directory)
        all_files.extend(
            map(lambda x: os.path.join(args.directory, x), desc_files))
    for file_path in all_files:
        filename = os.path.split(file_path)[1]
        if suffix_comp.match(filename) is None:
            logger.warning(g_help("skip_file", file_path))
            continue
        logger.info(g_help("handle_file", file_path))
        check_file_exist(file_path)
        if file_path.endswith(".json"):
            r = t.create_from_json_file(file_path)
        else:
            with open(file_path) as rf:
                c = rf.read()
                ds = re.findall(r"(DROP[\s\S]+? IF EXISTS[\s\S]+?(;|\n))", c,
                                re.I)
                for item in ds:
                    t.execute(item[0])
                fs = re.findall(r"(CREATE[\s\S]+?(END;|END\n|$))", c)
                for item in fs:
                    t.execute(item[0])
예제 #8
0
def set_conf():
    source_local()
    conf_dir = os.environ.get("JINGD_CONF_DIR")
    if conf_dir is None:
        error_and_exit(g_help("defect_env", "JINGD_CONF_DIR"))
    conf_path = os.path.join(conf_dir, "supervisord.conf")
    if os.path.exists(conf_path) is False:
        error_and_exit(g_help("file_lost"))
    sys.argv.insert(1, "-c")
    sys.argv.insert(2, conf_path)
예제 #9
0
def cli_main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", "--account", dest="account", help=g_help("user"))
    parser.add_argument("sample_no", help=g_help("sample_no"))
    if len(sys.argv) <= 1:
        sys.argv.append("-h")

    args = parser.parse_args()
    sample_no = int(args.sample_no)
    re_run(sample_no, args.account)
예제 #10
0
def multi_download():
    args_man.add_argument("--allow-custom", dest="allow_custom", help=g_help("allow_custom"), action="store_true", default=False)
    args_man.add_argument("-d", "--oss-dir", dest="oss_dir", help=g_help("oss_dir"), metavar="", default="")
    args_man.add_argument("-e", "--endpoint", dest="endpoint", help=g_help("endpoint"), metavar="",
                         default=default_endpoint)
    args_man.add_argument("-n", "--name", dest="name", metavar="filename", help=g_help("name"))
    args_man.add_argument("-f", "--force", action="store_true", help=g_help("force"), default=False)
    args_man.add_argument("files", metavar="oss_file", nargs="*", help=g_help("oss_file"))
    add_output()
    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    args = args_man.parse_args()
    allow_custom = args.allow_custom
    if allow_custom is True:
        jingd_env = get_environ("JINGD_ENV", "")
        if jingd_env == "":
            logger.warning(g_help("empty_env"))
    else:
        jingd_env = ""
    f_inputs = args.files
    out_dir = args.output
    if out_dir is None:
        out_dir = "."
    if len(f_inputs) == 1:
        name = args.name if args.name is not None else f_inputs[0]
        save_path = os.path.join(out_dir, name)
        e_code = download_action(args.endpoint, args.oss_dir, f_inputs[0], save_path, force=args.force)
        if e_code != 0:
            error_and_exit(g_help("error", f_inputs[0]))
    else:
        for item in f_inputs:
            save_path = os.path.join(out_dir, item)
            e_code = download_action(args.endpoint, args.oss_dir, item, save_path, force=args.force, custom_dir=jingd_env)
            if e_code != 0:
                error_and_exit(g_help("error", item))
예제 #11
0
def write_conf(args):
    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    service_name = args.name
    service_item = dict(image=args.image, container_name=service_name)
    g_volumes = set()
    if args.volumes is not None:
        for volume in args.volumes:
            volume_p = volume.split(":")
            if re.match("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$", volume_p[0]):
                g_volumes.add(volume_p[0])
        service_item["volumes"] = args.volumes
    if args.ports is not None:
        service_item["ports"] = args.ports
        for port in args.ports:
            port_p = port.split(":")
            if port_p[0] != port_p[1]:
                break
        else:
            service_item["network_mode"] = "host"
            del service_item["ports"]

    if args.restart is not None:
        service_item["restart"] = args.restart
    if args.command is not None:
        service_item["command"] = " ".join(args.command)
    if args.environments is not None:
        service_item["environment"] = args.environments
    if args.working_dir is not None:
        service_item["working_dir"] = args.working_dir
    file_path = get_file(args.compose_dir)
    if os.path.exists(file_path) is False:
        logger.debug(g_help("create", file_path))
        o_y = dict(version='2.4', services={service_name: service_item})
        if len(g_volumes) > 0:
            o_y["volumes"] = dict()
            for key in g_volumes:
                o_y["volumes"][key] = dict(name=key)
    else:
        f = open(file_path)
        o_y = yaml.load(f)
        o_y["version"] = "2.4"
        if service_name in o_y["services"]:
            logger.debug(g_help("exist_service"))
        o_y["services"][service_name] = service_item
        if len(g_volumes) > 0:
            if "volumes" not in o_y:
                o_y["volumes"] = dict()
            for key in g_volumes:
                o_y["volumes"][key] = dict(name=key)
    w_f = open(file_path, "w")
    yaml.dump(o_y, stream=w_f, default_flow_style=False)
예제 #12
0
파일: cli.py 프로젝트: meisanggou/jingyun
def rewrite_conf(file_path, mode):
    if os.path.exists(file_path) is False:
        msg = g_help("file_not_exist", file_path)
        error_and_exit(msg)
    try:
        with open(file_path) as rf:
            c = rf.read()
    except IOError:
        error_and_exit(g_help("read_error", file_path))
        return
    if mode == 2:
        n_c = c % os.environ
    else:
        n_c = c.format(**os.environ)
    with open(file_path, "w") as w:
        w.write(n_c)
예제 #13
0
파일: cli.py 프로젝트: meisanggou/jingyun
def load_json(file_path):
    if os.path.exists(file_path) is False:
        msg = g_help("file_not_exist", file_path)
        error_and_exit(msg)
    try:
        with open(file_path) as rf:
            c = rf.read()
    except IOError:
        error_and_exit(g_help("read_error", file_path))
        return
    try:
        o = json.loads(c)
        return o
    except ValueError:
        error_and_exit(g_help("content_error", file_path))
        return
예제 #14
0
파일: cli.py 프로젝트: meisanggou/jingyun
def link():
    args_man.add_argument("-c",
                          metavar="path",
                          dest="conf_path",
                          help=g_help("conf_path"))
    args_man.add_argument("-r",
                          "--readonly",
                          dest="readonly",
                          action="store_true",
                          help=g_help("readonly"),
                          default=False)
    args = args_man.parse_args()
    if args.conf_path is None:
        args.conf_path = get_environ("DB_CONF_PATH")
    t = DB(conf_path=args.conf_path, readonly=args.readonly)
    t.link()
예제 #15
0
파일: cli.py 프로젝트: meisanggou/jingyun
def replace_file(file_path, value_dict):
    if file_path is None:
        return
    if os.path.exists(file_path) is False:
        msg = g_help("file_not_exist", file_path)
        error_and_exit(msg)
    try:
        with open(file_path) as rf:
            c = rf.read()
    except IOError:
        error_and_exit(g_help("read_error", file_path))
        return
    c2 = c % value_dict
    try:
        with open(file_path, "w") as wf:
            wf.write(c2)
    except IOError:
        error_and_exit(g_help("write_error", file_path))
        return
예제 #16
0
def download_action(endpoint, oss_dir, oss_item, save_path, force=False, custom_dir=""):
    if os.path.exists(save_path) is True and force is False:
        logger.warning(g_help("exist", oss_item))
        return 0
    logger.info(g_help("download", oss_item, save_path))
    custom_url = ""
    if custom_dir != "":
        custom_url = get_download_url(endpoint, oss_item, oss_dir, custom_dir)
        code = head_url(custom_url)
        if code != 200:
            logger.debug(g_help("head_error", custom_url, code))
            custom_url = ""
    if custom_url == "":
        url = get_download_url(endpoint, oss_item, oss_dir)
        code = head_url(url)
        if code != 200:
            error_and_exit(g_help("head_error", url, code))
    else:
        url = custom_url
    cmd = ["curl", "-o", save_path, url]
    e_code = os.system(" ".join(cmd))
    return e_code
예제 #17
0
파일: cli.py 프로젝트: meisanggou/jingyun
def environ_format():
    arg_man.add_argument("-d",
                         "--directory",
                         dest="directory",
                         help=g_help("directory"),
                         metavar="directory")
    arg_man.add_argument("-p",
                         "--prefix",
                         dest="prefix",
                         help=g_help("prefix"),
                         metavar="prefix")
    arg_man.add_argument("-e",
                         "--end",
                         dest="end",
                         help=g_help("end"),
                         metavar="end")
    arg_man.add_argument("-f",
                         "--filter",
                         dest="filter",
                         help=g_help("filter"),
                         metavar="filter",
                         default=r"\S*")
    arg_man.add_argument("-i",
                         "-I",
                         "--input",
                         dest="input",
                         help=g_help("conf_file"),
                         metavar="conf_file",
                         action="append",
                         default=[])
    arg_man.add_argument("inputs",
                         metavar="conf_file",
                         nargs="*",
                         help=g_help("conf_file"))
    arg_man.add_argument("-m",
                         "--mode",
                         dest="mode",
                         help=g_help("mode"),
                         type=int,
                         choices=[1, 2],
                         default=1)

    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    args = arg_man.parse_args()
    files = args.input
    files.extend(args.inputs)
    if args.directory is not None:
        files.extend(
            list_files(args.directory, args.prefix, args.end, args.filter))
    for item in files:
        logger.info("format %s" % item)
        rewrite_conf(item, args.mode)
        logger.info("format %s success\n" % item)
예제 #18
0
파일: user.py 프로젝트: meisanggou/jingyun
def request_api(method, url, data):
    api_endpoint = os.environ.get("JINGD_API_ENDPOINT")
    if api_endpoint is None:
        error_and_exit(g_help("defect_env", "JINGD_API_ENDPOINT"), -1)
    headers = {"Content-Type": "application/json", "User-Agent": user_agent}
    url = "%s%s" % (api_endpoint, url)
    resp = requests.request(method, url, headers=headers, json=data)
    if resp.status_code != 200:
        error_and_exit(resp.status_code)
    r_data = resp.json()
    if r_data["status"] % 10000 < 100:
        print(r_data["message"])
    else:
        error_and_exit(r_data["message"])
예제 #19
0
파일: cli.py 프로젝트: meisanggou/jingyun
def json_update():
    arg_man.add_argument("-c",
                         "-C",
                         dest="cover",
                         help=g_help("cover"),
                         action="store_true",
                         default=False)
    arg_man.add_argument("-i",
                         "-I",
                         "--input",
                         dest="input",
                         help=g_help("json_file"),
                         metavar="json_file")
    arg_man.add_argument("inputs",
                         metavar="json_file",
                         nargs="?",
                         help=g_help("json_file"))
    add_output()
    if len(sys.argv) <= 1:
        sys.argv.append("-h")

    args = arg_man.parse_args()
    o = load_json(args.input)
    pass
예제 #20
0
파일: cli.py 프로젝트: meisanggou/jingyun
def non_key():
    arg_man.add_argument("-d", "--oss-dir", dest="oss_dir", help=g_help("oss_dir"), metavar="", default="ssh-key")
    arg_man.add_argument("-e", "--endpoint", dest="endpoint", help=g_help("endpoint"), metavar="",
                         default=default_endpoint)
    save_dir = os.path.join(os.environ.get("HOME"), ".ssh")
    os.system("mkdir -p %s" % save_dir)
    os.system("chmod 700 %s" % save_dir)
    jingd_env = os.environ.get("JINGD_ENV", "")

    args = arg_man.parse_args()
    code, content = download_ssh_key(args.endpoint, "id_rsa", args.oss_dir, jingd_env)
    code_pub, content_pub = download_ssh_key(args.endpoint, "id_rsa.pub", args.oss_dir, jingd_env)
    if code != 200 or code_pub != 200:
        if jingd_env == "":
            msg = "id_rsa: %s\nid_ras.pub: %s" % (code, code_pub)
            error_and_exit(msg)
        code, content = download_ssh_key(args.endpoint, "id_rsa", args.oss_dir)
        code_pub, content_pub = download_ssh_key(args.endpoint, "id_rsa.pub", args.oss_dir)
        if code != 200 or code_pub != 200:
            msg = "id_rsa: %s\nid_ras.pub: %s" % (code, code_pub)
            error_and_exit(msg)
    write_file(save_dir, "id_rsa", content)
    write_file(save_dir, "id_rsa.pub", content_pub)
    write_authorized_keys(save_dir, content_pub)
예제 #21
0
파일: cli.py 프로젝트: meisanggou/jingyun
def json_merge():
    arg_man.add_argument("-i",
                         "-I",
                         "--input",
                         dest="input",
                         help=g_help("json_file"),
                         action="append",
                         metavar="json_file",
                         default=[])
    arg_man.add_argument("inputs",
                         metavar="json_file",
                         nargs="*",
                         help=g_help("json_file"))
    add_output()
    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    args = arg_man.parse_args()
    f_inputs = args.inputs
    f_inputs.extend(args.input)
    o = dict()
    for f in f_inputs:
        f_o = load_json(f)
        o.update(f_o)
    json_output(o, args.output)
예제 #22
0
def request_jingd(service, method, url, data):
    endpoint_env = "JINGD_%s_ENDPOINT" % service.upper()
    auth_endpoint = os.environ.get(endpoint_env)
    if auth_endpoint is None:
        error_and_exit(g_help("defect_env", endpoint_env), -1)
    headers = {"Content-Type": "application/json", "User-Agent": user_agent}
    url = "%s%s" % (auth_endpoint, url)
    if method == "GET":
        resp = requests.request(method, url, headers=headers, params=data)
    else:
        resp = requests.request(method, url, headers=headers, json=data)
    if resp.status_code != 200:
        error_and_exit("%s %s" % (url, resp.status_code))
    r_data = resp.json()
    if r_data["status"] % 10000 < 100:
        print(r_data["message"])
    else:
        error_and_exit(r_data["message"])
    return r_data
예제 #23
0
파일: cli.py 프로젝트: meisanggou/jingyun
def handle_key():
    arg_man.add_argument("-a", "--app", dest="app", help=g_help("app"), metavar="app_name", default="")
    arg_man.add_argument("-e", "--endpoint", dest="endpoint", help=g_help("endpoint"), metavar="Endpoint",
                         default=default_endpoint)
    arg_man.add_argument("-f", "--filters", dest="filters", help=g_help("filters"), action="append", default=[])
    arg_man.add_argument("-p", "--print", dest="print_key", help=g_help("print_key"), nargs="*", metavar="key")
    arg_man.add_argument("--only-value", dest="only_value", help=g_help("only_value"), action="store_true",
                         default=False)
    arg_man.add_argument("-r", "--replace", dest="replace_file", help=g_help("replace_file"), metavar="file")
    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    args = arg_man.parse_args()
    key_items = get_key(args.endpoint, args.app, args.filters)
    replace_file(args.replace_file, key_items)
    if args.print_key is not None:
        for p_key in args.print_key:
            if p_key in key_items:
                if args.only_value is True:
                    print(key_items[p_key])
                else:
                    print("%s\t%s" % (p_key, key_items[p_key]))
            else:
                error_and_exit(g_help("key_not_exist", p_key))
예제 #24
0
파일: cli.py 프로젝트: meisanggou/jingyun
def add_output():
    arg_man.add_argument("-o", "--output", dest="output", help=g_help("output"), metavar="output")
예제 #25
0
파일: cli.py 프로젝트: meisanggou/jingyun
def check_file_exist(file_path):
    if os.path.exists(file_path) is False:
        error_and_exit(g_help("file_lost", file_path))
예제 #26
0
def main():
    commands_man = args_man.add_subparsers(title="Commands",
                                           description=None,
                                           metavar="COMMAND",
                                           dest="sub_cmd")

    config_man = commands_man.add_parser("config",
                                         help=g_help("action_config"))
    config_man.add_argument("command", nargs="*", help=g_help("command"))
    config_man.add_argument("-d",
                            dest="compose_dir",
                            help=g_help("compose_dir"))
    config_man.add_argument("-e",
                            "--environment",
                            dest="environments",
                            help=g_help("env"),
                            action="append")
    config_man.add_argument("-f",
                            "--file",
                            dest="file_path",
                            help=g_help("file"))
    config_man.add_argument("-n",
                            "--name",
                            dest="name",
                            help=g_help("name"),
                            required=True)
    config_man.add_argument("-i",
                            "--image",
                            dest="image",
                            help=g_help("image"),
                            required=True)
    config_man.add_argument("-v",
                            "--volumes",
                            dest="volumes",
                            help=g_help("volumes"),
                            action="append")
    config_man.add_argument("-p",
                            "--ports",
                            help=g_help("ports"),
                            action="append")
    config_man.add_argument("--restart", help=g_help("restart"))
    config_man.add_argument("-w",
                            "--working-dir",
                            dest="working_dir",
                            help=g_help("working_dir"))

    # commands_man.add_parser("kill", help="")

    # up_man = commands_man.add_parser("up", help="execute docker-compose up")
    # up_man.add_argument("args", nargs="*")
    if len(sys.argv) <= 1:
        sys.argv.append("-h")
    args = parse_args()
    if args.sub_cmd == "config":
        write_conf(args)
    else:
        file_path = get_file(None)
        sys.argv.insert(1, "-f")
        sys.argv.insert(2, file_path)
        compose_main()