def post(self): result = {} args = request.json log_files = args.get("log_files") db = args.get("db") jobs = args.get("jobs") log_prefix = args.get("log_prefix") title = args.get("title") try: from BadgerReport import BadgerReport ctime = get_current_time(format='%y%m%d%H%M%S%f') badgerRpts = BadgerReport() pid_file_path = os.path.join(config.SESSION_DB_PATH, "process_logs", ctime) report_file = badgerRpts.generateReports(log_files, db, jobs, log_prefix, title, ctime, pid_file_path) process_log_dir = report_file['log_dir'] report_status = get_process_status(process_log_dir) result['pid'] = report_status.get('pid') result['exit_code'] = report_status.get('exit_code') result['process_log_id'] = report_file["process_log_id"] if report_status.get('exit_code') is None: result['in_progress'] = True try: j = Process(pid=int(report_file["process_log_id"]), command=report_file['cmd'], logdir=process_log_dir, desc=dumps("pgBadger Report"), user_id=current_user.id, acknowledge='pgDevOps') db_session.add(j) db_session.commit() except Exception as e: print str(e) pass """bg_process={} bg_process['process_type'] = "badger" bg_process['cmd'] = report_file['cmd'] bg_process['file'] = report_file['file'] bg_process['report_file'] = report_file['report_file'] bg_process['process_log_id'] = report_file["process_log_id"]""" if report_file['error']: result['error'] = 1 result['msg'] = report_file['error'] else: result['error'] = 0 result['report_file'] = report_file['file'] report_file_path = os.path.join(reports_path, report_file['file']) if not os.path.exists(report_file_path): result['error'] = 1 result['msg'] = "Check the parameters provided." except Exception as e: import traceback result = {} result['error'] = 1 result['msg'] = str(e) time.sleep(2) return result
def post(self): result = {} args = request.json if not validate_backup_fields(args): return InvalidParameterResult().http_response() try: from BackupRestore import BackupRestore backuprestore = BackupRestore() ctime = get_current_time(format='%y%m%d%H%M%S%f') import util if args['host'] in ['localhost', '127.0.0.1']: args['host'] = util.get_host_ip() result = backuprestore.backup_restore(ctime, 'restore', args['host'], args['port'], args['username'], args['dbName'], args['sshServer'], args['backupDirectory'], args['fileName'], args['format'], args.get('advOptions', ""), password=args.get( 'password', None)) process_log_dir = result['log_dir'] process_status = get_process_status(process_log_dir) result['pid'] = process_status.get('pid') result['exit_code'] = process_status.get('exit_code') result['process_log_id'] = result["process_log_id"] if process_status.get('exit_code') is None: result['in_progress'] = True try: j = Process(pid=int(result["process_log_id"]), command=result['cmd'], logdir=result["log_dir"], desc=dumps(str(args['action'])), user_id=current_user.id, acknowledge='pgDevOps') db_session.add(j) db_session.commit() except Exception as e: print str(e) pass if result['error']: response = Result(500, "error", result['error'], extra_fields={'data': result}) else: response = Result(200, "Success", "Success", extra_fields={'data': result}) except Exception as e: import traceback response = ServerErrorResult(message=str(e)) time.sleep(1) return response.http_response()
def _create_process(self, _desc, _cmd, _args): ctime = get_current_time(format='%y%m%d%H%M%S%f') log_dir = os.path.join(config.SESSION_DB_PATH, 'process_logs') def random_number(size): import random import string return ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(size)) created = False size = 0 id = ctime while not created: try: id += random_number(size) log_dir = os.path.join(log_dir, id) size += 1 if not os.path.exists(log_dir): os.makedirs(log_dir, int('700', 8)) created = True except OSError as oe: import errno if oe.errno != errno.EEXIST: raise # ID self.id = ctime # Description self.desc = _desc # Status Acknowledged time self.atime = None # Command self.cmd = _cmd # Log Directory self.log_dir = log_dir # Standard ouput log file self.stdout = os.path.join(log_dir, 'out') # Standard error log file self.stderr = os.path.join(log_dir, 'err') # Start time self.stime = None # End time self.etime = None # Exit code self.ecode = None # Arguments self.args = _args args_csv_io = StringIO() csv_writer = csv.writer(args_csv_io, delimiter=str(','), quoting=csv.QUOTE_MINIMAL) if sys.version_info.major == 2: csv_writer.writerow([ a.encode('utf-8') if isinstance(a, unicode) else a for a in _args ]) else: csv_writer.writerow(_args) args_val = args_csv_io.getvalue().strip(str('\r\n')) j = Process( pid=int(id), command=_cmd, arguments=args_val.decode('utf-8', 'replace') if IS_PY2 and hasattr(args_val, 'decode') \ else args_val, logdir=log_dir, desc=dumps(self.desc), user_id=current_user.id ) db.session.add(j) db.session.commit()
def _create_process(self, _desc, _cmd, _args): ctime = get_current_time(format='%y%m%d%H%M%S%f') log_dir = os.path.join(config.SESSION_DB_PATH, 'process_logs') def random_number(size): import secrets import string return ''.join( secrets.choice(string.ascii_uppercase + string.digits) for _ in range(size)) created = False size = 0 uid = ctime while not created: try: uid += random_number(size) log_dir = os.path.join(log_dir, uid) size += 1 if not os.path.exists(log_dir): os.makedirs(log_dir, int('700', 8)) created = True except OSError as oe: import errno if oe.errno != errno.EEXIST: raise # ID self.id = ctime # Description self.desc = _desc # Status Acknowledged time self.atime = None # Command self.cmd = _cmd # Log Directory self.log_dir = log_dir # Standard ouput log file self.stdout = os.path.join(log_dir, 'out') # Standard error log file self.stderr = os.path.join(log_dir, 'err') # Start time self.stime = None # End time self.etime = None # Exit code self.ecode = None # Process State self.process_state = PROCESS_NOT_STARTED # Arguments self.args = _args args_csv_io = StringIO() csv_writer = csv.writer(args_csv_io, delimiter=str(','), quoting=csv.QUOTE_MINIMAL) csv_writer.writerow(_args) args_val = args_csv_io.getvalue().strip(str('\r\n')) tmp_desc = dumps(self.desc) j = Process(pid=int(uid), command=_cmd, arguments=args_val, logdir=log_dir, desc=tmp_desc, user_id=current_user.id) db.session.add(j) db.session.commit()