Ejemplo n.º 1
0
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)
Ejemplo n.º 2
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))
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def re_run(sample_no, account=None):
    seq_files = req_sample_info(sample_no)
    if seq_files is None:
        error_and_exit("Not Found seq_files")
    files = seq_files.split(",")
    if account is None:
        rights = req_sample_right(sample_no)
        for item in rights:
            if item["role"] == 0:
                account = item["account"]
    if account is None:
        error_and_exit("Auto Find Account Fail, Please Set Account")
    process_no = -1
    all_process = req_process()
    for p in all_process:
        if p["process_name"] == files[0]:
            process_no = p["process_no"]
    if process_no == -1:
        error_and_exit("Not Found Process Name Is %s" % files[0])
    params = req_process_detail(process_no)
    r_seq_files = dict()
    for p in params:
        r_seq_files[p["param_name"]] = ""
    keys = r_seq_files.keys()
    if len(keys) != len(files) - 1:
        print(keys)
        print(files)
        error_and_exit("Please Check input file")
    bucket = None
    for i in range(len(keys)):
        bucket, file_path = files[i + 1].split(":", 1)
        r_seq_files[keys[i]] = file_path
    req_analysis(account, sample_no, r_seq_files, bucket, process_no)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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
Ejemplo n.º 7
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
Ejemplo n.º 8
0
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))
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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"])
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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
Ejemplo n.º 13
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
Ejemplo n.º 14
0
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