Exemplo n.º 1
0
def backup_t0002(src_path, en_compress=True):
    dst_folder_name = 't0002_backup'
    dst_path = os.path.join(os.getcwd(), dst_folder_name)
    log.info('Backup TDX customer data...')
    if not os.path.exists(dst_path):
        os.mkdir(dst_path)
    else:
        shutil.rmtree(dst_path)
        os.mkdir(dst_path)
    if not os.path.exists(sets.T0002_LIST):
        log.error(r'Can not find {}'.format(sets.T0002_LIST))
        exit(0)

    src_list = glob(os.path.join(src_path, '*'))
    with open(sets.T0002_LIST, 'r') as f:
        t0002_list = filter(None, f.read().split('\n'))
    for i in t0002_list:
        if '*' in i:
            key = i.split('.')[-1]
            key_list = list(filter(lambda x: key in x, src_list))
            for k in key_list:
                shutil.copy(os.path.join(src_path, k), dst_path)
        else:
            copy_func = shutil.copy if '.' in i else shutil.copytree
            dst_tmp = dst_path if '.' in i else os.path.join(dst_path, i)
            copy_func(os.path.join(src_path, i), dst_tmp)

    if en_compress:
        cmd = '{} a -t7z {} {}\*'.format(sets.EXE_7Z, dst_folder_name, os.path.join(dst_path, ))
        run_cmd(cmd)
        shutil.rmtree(dst_path)
        zip_full_path = os.path.realpath(dst_folder_name+'.7z')
        subj = 'stool backup' + time_str()
        py_send(subj=subj, atta_files=[zip_full_path])
        os.remove(zip_full_path)
Exemplo n.º 2
0
    def pack_list_func(paras):
        func, var, fix, lock, counter, finish_queue = paras
        is_varg_list = False
        if var is None or len(var) <= 0:
            log.error('Illegal <var> para in <packed_func>')
        if fix is not None and not isinstance(fix, dict):
            assert 0
        if isinstance(var[0], list) or isinstance(
                var[0], np.ndarray) or isinstance(var[0], tuple):
            is_varg_list = True

        ret_set = []
        for item in var:
            if fix is None:
                if is_varg_list:
                    ret = func(*item)
                else:
                    ret = func(item)
            else:
                if is_varg_list:
                    ret = func(*item, **fix)
                else:
                    ret = func(item, **fix)
            with lock:
                counter.value += 1
            ret_set.append(ret)
        with lock:
            finish_queue.get()
        return ret_set
Exemplo n.º 3
0
def terminal_socket(ws, namespace, pod, container):
    log.info('Try create socket connection')
    ssl_ca_cert, key_file, cert_file = K8SClient.gen_ca()
    kub = K8SClient(api_host='kubernetes_api_url',
                    ssl_ca_cert=ssl_ca_cert,
                    key_file=key_file,
                    cert_file=cert_file)

    try:
        container_stream = kub.terminal_start(namespace, pod, container)
    except Exception as err:
        log.error('Connect container error: {}'.format(err))
        ws.close()
        return

    kub_stream = K8SStreamThread(ws, container_stream)
    kub_stream.start()

    log.info('Start terminal')
    try:
        while not ws.closed:
            message = ws.receive()
            if message is not None:
                if message != '__ping__':
                    container_stream.write_stdin(message)
        container_stream.write_stdin('exit\r')
    except Exception as err:
        log.error('Connect container error: {}'.format(err))
    finally:
        container_stream.close()
        ws.close()
Exemplo n.º 4
0
def terminal_socket(ws, namespace, pod, container, token):
    log.info('Try create socket connection')
    kub = K8SClient(
        api_server=os.environ["API_SERVER"].rstrip("/"), id_token=token)
    try:
        container_stream = kub.terminal_start(namespace, pod, container)
    except Exception as err:
        log.error('Connect container error: {}'.format(err))
        ws.close()
        return

    kub_stream = K8SStreamThread(ws, container_stream)
    kub_stream.start()

    log.info('Start terminal')
    try:
        while not ws.closed:
            message = ws.receive()
            if message is not None:
                if message != '__ping__':
                    container_stream.write_stdin(message)
        container_stream.write_stdin('exit\r')
    except Exception as err:
        log.error('Connect container error: {}'.format(err))
    finally:
        container_stream.close()
        ws.close()
Exemplo n.º 5
0
 def task_bar(c, q, d=None, bar_max=None):
     if bar_max is None:
         log.error('Wrong <bar max> para when doing <task_bar>')
     with tqdm(total=bar_max, desc=d, ascii=True) as tbar:
         while c.value <= bar_max:
             tbar.update(c.value - tbar.n)
             if q.qsize() == 0 or c.value == bar_max:
                 tbar.update(bar_max - tbar.n)
                 break
Exemplo n.º 6
0
 def get_info(self, code, item):
     if not os.path.exists(sets.INFO_FILE):
         log.error('No local info file: {}!'.format(sets.INFO_FILE))
     infos = pd.read_csv(sets.INFO_FILE,
                         usecols=[item],
                         columns={
                             'code': str
                         }).set_index('code')
     return infos.loc[code, item]
def add_user_activity(request):
    data = json.loads(request.body.decode("utf-8"))
    response_dict = {}
    validate_request, error_msg = validate_add_user_activity(request)
    if not validate_request:
        log.error(error_msg)
        return JsonResponse(error_msg)
    user_data, is_created = models.UserActivity.add_activity(data)
    response_dict["user_id"] = user_data.user_id.user_id
    response_dict["status_code"] = 200
    return Response(response_dict, status=201)
Exemplo n.º 8
0
    def run(self):
        while not self.ws.closed:

            if not self.stream.is_open():
                log.info('container stream closed')
                self.ws.close()

            try:
                if self.stream.peek_stdout():
                    stdout = self.stream.read_stdout()
                    self.ws.send(stdout)

                if self.stream.peek_stderr():
                    stderr = self.stream.read_stderr()
                    self.ws.send(stderr)
            except Exception as err:
                log.error('container stream err: {}'.format(err))
                self.ws.close()
                break
Exemplo n.º 9
0
    def run(self):
        while not self.ws.closed:

            if not self.stream.is_open():
                log.info('container stream closed')
                self.ws.close()

            try:
                if self.stream.peek_stdout():
                    stdout = self.stream.read_stdout()
                    self.ws.send(stdout)

                if self.stream.peek_stderr():
                    stderr = self.stream.read_stderr()
                    self.ws.send(stderr)
            except Exception as err:
                log.error('container stream err: {}'.format(err))
                self.ws.close()
                break
Exemplo n.º 10
0
 def gen_k_120(self, code):
     try:
         m60 = self.get_k(code, ktype='60')
         if m60 is not None:
             m60.set_index(['date'], inplace=True)
             if m60.shape[0] % 2 == 1:
                 pass
             m120 = m60.resample('120T').agg(self.ohlc_dict).dropna()
             new_index = m60.index[1::2]
             if new_index.shape[0] == m120.shape[0]:
                 file_name = os.path.join(
                     sets.CSV_DIR, '{}_{}.csv'.format('min120', code))
                 m120.reset_index().to_csv(file_name, index=False)
             else:
                 pass
         else:
             pass
     except Exception as err:
         log.error('{}, {}'.format(code, err))
Exemplo n.º 11
0
def raw_ip_parse():
    if not os.path.exists(sets.RAW_IP_FILE):
        log.error(
            'Must manual make and prepare {} for proxy ip parsing job.'.format(
                sets.RAW_IP_FILE))
        exit(0)

    f = open(sets.RAW_IP_FILE, 'r')
    content = f.read()
    f.close()
    ip_list = []
    pat0 = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,5}).*?',
                      re.S)
    pat1 = re.compile(
        r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*:\s*(\d{1,5}).*?', re.S)
    ip_list.extend(pat0.findall(content))
    ip_list.extend(pat1.findall(content))
    ip_list = rm_dupl(ip_list)
    log.info('Raw ips: {}'.format(len(ip_list)))
    return ip_list
Exemplo n.º 12
0
    def rCommand(self, internalIn=None):
        try:
            if internalIn is None:
                request = self.checkRequest(self.rInput.text())
                if request is not None:
                    result = "\n".join(self.R(request).split(self.rNewline)[1:])  # PyQt shenanigans
                    self.updateStack(request)
                else:
                    result = None
            else:
                result = "\n".join(self.R(str(self.internalIn)).split(self.R.newline)[1:])
            if result != self.R.newline and result is not None:
                self.rConsole.append(result)
        except Exception:
            self.parentWidget().messageInfo.showInfo("Sudden PypeR combustion!", True)
            log.error("R interpreter crush")
            self.R = self.parentWidget().reInitR()
        if self.enterButton.isChecked():
            self.rInput.clear()

        self.indicateInput()
        self.updateNamespace()
def add_user(request):
    data = json.loads(request.body.decode("utf-8"))
    response_dict = {}
    validate_request, error_msg = validate_add_user(request)
    if not validate_request:
        log.error(error_msg)
        return JsonResponse(error_msg)
    user_exist = models.User.check_user(
        GU.encryptData(data.get("realname", "")))
    log.error("UserCreation Status{}".format(user_exist))
    if not user_exist:
        user_data, is_created = models.User.add_user(data)
        response_dict["user_id"] = user_data.user_id
        response_dict["status_code"] = 200
        return Response(response_dict, status=201)
    else:
        return Response(
            {
                "status_code": 200,
                "message": "User Already Exists.",
                "user_id": user_exist.user_id
            },
            status=201,
        )