codekingpro/portable-devtools
114k
1##########################################################################2#3# pgAdmin 4 - PostgreSQL Tools4#5# Copyright (C) 2013 - 2024, The pgAdmin Development Team6# This software is released under the PostgreSQL Licence7#8##########################################################################9 10"""11A blueprint module providing utility functions for the notify the user about12the long running background-processes.13"""14from flask import url_for15from pgadmin.user_login_check import pga_login_required16from pgadmin.utils import PgAdminModule17from pgadmin.utils.ajax import make_response, gone, success_return,\18 make_json_response19 20from .processes import BatchProcess21 22MODULE_NAME = 'bgprocess'23 24 25class BGProcessModule(PgAdminModule):26 27 def get_exposed_url_endpoints(self):28 """29 Returns:30 list: URL endpoints for bgprocess31 """32 return [33 'bgprocess.status', 'bgprocess.detailed_status',34 'bgprocess.acknowledge', 'bgprocess.list',35 'bgprocess.stop_process', 'bgprocess.update_cloud_details',36 ]37 38 39# Initialise the module40blueprint = BGProcessModule(41 MODULE_NAME, __name__, url_prefix='/misc/bgprocess'42)43 44 45@blueprint.route('/', methods=['GET'], endpoint='list')46@pga_login_required47def index():48 return make_response(response=BatchProcess.list())49 50 51@blueprint.route('/<pid>', methods=['GET'], endpoint='status')52@blueprint.route(53 '/<pid>/<int:out>/<int:err>/', methods=['GET'], endpoint='detailed_status'54)55@pga_login_required56def status(pid, out=-1, err=-1):57 """58 Check the status of the process running in background.59 Sends back the output of stdout/stderr60 Fetches & sends STDOUT/STDERR logs for the process requested by client61 62 Args:63 pid: Process ID64 out: position of the last stdout fetched65 err: position of the last stderr fetched66 67 Returns:68 Status of the process and logs (if out, and err not equal to -1)69 """70 try:71 process = BatchProcess(id=pid)72 73 return make_response(response=process.status(out, err))74 except LookupError as lerr:75 return gone(errormsg=str(lerr))76 77 78@blueprint.route('/<pid>', methods=['PUT'], endpoint='acknowledge')79@pga_login_required80def acknowledge(pid):81 """82 User has acknowledge the process83 84 Args:85 pid: Process ID86 87 Returns:88 Positive status89 """90 try:91 BatchProcess.acknowledge(pid)92 return success_return()93 except LookupError as lerr:94 return gone(errormsg=str(lerr))95 96 97@blueprint.route('/update_cloud_details/<pid>', methods=['PUT'],98 endpoint='update_cloud_details')99@pga_login_required100def update_cloud_details(pid):101 """102 Update the cloud details and get instance details103 104 Args:105 pid: Process ID106 107 Returns:108 Positive status109 """110 try:111 process = BatchProcess(id=pid)112 status, server = process.update_cloud_details()113 if status and len(server) > 0:114 return make_json_response(115 success=1,116 data={'node': {117 'sid': server['id'],118 'gid': server['servergroup_id'],119 '_type': 'server',120 'icon': 'icon-server-not-connected',121 'id': 'server_{}'.format(server['id']),122 'label': server['name'],123 'status': server['status'],124 'cloud_status': server['cloud_status']125 }}126 )127 elif status and len(server) == 0:128 return success_return()129 else:130 return gone(errormsg=str(server))131 except LookupError as lerr:132 return gone(errormsg=str(lerr))133 134 135@blueprint.route('/stop/<pid>', methods=['PUT'], endpoint='stop_process')136@pga_login_required137def stop_process(pid):138 """139 User has stopped the process140 141 :param pid: Process ID142 """143 try:144 BatchProcess.stop_process(pid)145 return success_return()146 except LookupError as lerr:147 return gone(errormsg=str(lerr))148 149 150def escape_dquotes_process_arg(arg):151 # Double quotes has special meaning for shell command line and they are152 # run without the double quotes. Add extra quotes to save our double153 # quotes from stripping.154 155 # This cannot be at common place as this file executes156 # separately from pgadmin157 dq_id = "#DQ#"158 159 if arg.startswith('"') and arg.endswith('"'):160 return r'{0}{1}{0}'.format(dq_id, arg)161 else:162 return arg163 