2015-06-29 12:28:41 +05:30
|
|
|
##########################################################################
|
|
|
|
|
#
|
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
|
#
|
2024-01-01 14:13:48 +05:30
|
|
|
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2015-06-29 12:28:41 +05:30
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
|
#
|
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
|
"""A blueprint module providing utility functions for the application."""
|
|
|
|
|
|
2022-11-19 17:43:41 +13:00
|
|
|
from pgadmin.utils import driver
|
2023-11-03 18:19:01 +05:30
|
|
|
from flask import render_template, Response, request, current_app
|
|
|
|
|
from flask.helpers import url_for
|
2021-11-24 17:22:57 +05:30
|
|
|
from flask_babel import gettext
|
2024-04-29 13:41:02 +05:30
|
|
|
from pgadmin.user_login_check import pga_login_required
|
2024-04-01 11:34:01 +05:30
|
|
|
from pathlib import Path
|
2023-09-20 11:17:07 +05:30
|
|
|
from pgadmin.utils import PgAdminModule, replace_binary_path, \
|
|
|
|
|
get_binary_path_versions
|
2019-05-28 10:59:51 +05:30
|
|
|
from pgadmin.utils.csrf import pgCSRFProtect
|
2018-10-09 11:34:13 +01:00
|
|
|
from pgadmin.utils.session import cleanup_session_files
|
2019-11-11 18:47:43 +05:30
|
|
|
from pgadmin.misc.themes import get_all_themes
|
2023-10-23 17:43:17 +05:30
|
|
|
from pgadmin.utils.ajax import precondition_required, make_json_response, \
|
|
|
|
|
internal_server_error
|
|
|
|
|
from pgadmin.utils.heartbeat import log_server_heartbeat, \
|
2023-01-30 15:39:34 +05:30
|
|
|
get_server_heartbeat, stop_server_heartbeat
|
2016-05-16 01:07:52 +05:30
|
|
|
import config
|
2023-10-23 17:43:17 +05:30
|
|
|
import time
|
2021-06-04 17:55:35 +05:30
|
|
|
import json
|
2023-10-23 17:43:17 +05:30
|
|
|
import os
|
|
|
|
|
from urllib.request import urlopen
|
|
|
|
|
from pgadmin.settings import get_setting, store_setting
|
2015-10-20 12:33:18 +05:30
|
|
|
|
|
|
|
|
MODULE_NAME = 'misc'
|
2015-06-29 12:28:41 +05:30
|
|
|
|
2016-05-16 01:07:52 +05:30
|
|
|
|
|
|
|
|
class MiscModule(PgAdminModule):
|
2019-11-07 18:51:03 +05:30
|
|
|
LABEL = gettext('Miscellaneous')
|
|
|
|
|
|
2017-03-24 14:20:10 +00:00
|
|
|
def register_preferences(self):
|
|
|
|
|
"""
|
|
|
|
|
Register preferences for this module.
|
|
|
|
|
"""
|
|
|
|
|
lang_options = []
|
|
|
|
|
for lang in config.LANGUAGES:
|
2018-02-09 12:57:37 +00:00
|
|
|
lang_options.append(
|
|
|
|
|
{
|
|
|
|
|
'label': config.LANGUAGES[lang],
|
|
|
|
|
'value': lang
|
|
|
|
|
}
|
|
|
|
|
)
|
2017-03-24 14:20:10 +00:00
|
|
|
|
|
|
|
|
# Register options for the User language settings
|
2019-11-07 18:51:03 +05:30
|
|
|
self.preference.register(
|
|
|
|
|
'user_language', 'user_language',
|
2018-02-09 12:57:37 +00:00
|
|
|
gettext("User language"), 'options', 'en',
|
|
|
|
|
category_label=gettext('User language'),
|
2022-03-21 13:29:26 +05:30
|
|
|
options=lang_options,
|
|
|
|
|
control_props={
|
|
|
|
|
'allowClear': False,
|
|
|
|
|
}
|
2017-03-24 14:20:10 +00:00
|
|
|
)
|
|
|
|
|
|
2019-11-07 18:51:03 +05:30
|
|
|
theme_options = []
|
|
|
|
|
|
2019-11-11 18:47:43 +05:30
|
|
|
for theme, theme_data in (get_all_themes()).items():
|
2019-11-07 18:51:03 +05:30
|
|
|
theme_options.append({
|
2019-11-11 18:47:43 +05:30
|
|
|
'label': theme_data['disp_name']
|
2019-11-07 18:51:03 +05:30
|
|
|
.replace('_', ' ')
|
|
|
|
|
.replace('-', ' ')
|
|
|
|
|
.title(),
|
|
|
|
|
'value': theme,
|
2024-07-16 10:25:48 +05:30
|
|
|
'preview_src': 'js/generated/img/' + theme_data['preview_img']
|
|
|
|
|
if 'preview_img' in theme_data else None
|
2019-11-07 18:51:03 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
|
|
self.preference.register(
|
|
|
|
|
'themes', 'theme',
|
2024-06-12 16:18:37 +05:30
|
|
|
gettext("Theme"), 'options', 'light',
|
2019-11-07 18:51:03 +05:30
|
|
|
category_label=gettext('Themes'),
|
|
|
|
|
options=theme_options,
|
2022-03-21 13:29:26 +05:30
|
|
|
control_props={
|
|
|
|
|
'allowClear': False,
|
2024-06-12 16:18:37 +05:30
|
|
|
'creatable': False,
|
2022-03-21 13:29:26 +05:30
|
|
|
},
|
2019-11-07 18:51:03 +05:30
|
|
|
help_str=gettext(
|
2024-06-12 16:18:37 +05:30
|
|
|
'Click the save button to apply the theme. Below is the '
|
|
|
|
|
'preview of the theme.'
|
2019-11-07 18:51:03 +05:30
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2017-06-12 12:01:22 +05:30
|
|
|
def get_exposed_url_endpoints(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
list: a list of url endpoints exposed to the client.
|
|
|
|
|
"""
|
2021-06-04 17:55:35 +05:30
|
|
|
return ['misc.ping', 'misc.index', 'misc.cleanup',
|
2023-01-30 15:39:34 +05:30
|
|
|
'misc.validate_binary_path', 'misc.log_heartbeat',
|
2023-10-23 17:43:17 +05:30
|
|
|
'misc.stop_heartbeat', 'misc.get_heartbeat',
|
|
|
|
|
'misc.upgrade_check']
|
2017-06-12 12:01:22 +05:30
|
|
|
|
2022-06-30 11:06:50 +05:30
|
|
|
def register(self, app, options):
|
|
|
|
|
"""
|
|
|
|
|
Override the default register function to automagically register
|
|
|
|
|
sub-modules at once.
|
|
|
|
|
"""
|
|
|
|
|
from .bgprocess import blueprint as module
|
|
|
|
|
self.submodules.append(module)
|
|
|
|
|
|
|
|
|
|
from .cloud import blueprint as module
|
|
|
|
|
self.submodules.append(module)
|
|
|
|
|
|
|
|
|
|
from .dependencies import blueprint as module
|
|
|
|
|
self.submodules.append(module)
|
|
|
|
|
|
|
|
|
|
from .dependents import blueprint as module
|
|
|
|
|
self.submodules.append(module)
|
|
|
|
|
|
|
|
|
|
from .file_manager import blueprint as module
|
|
|
|
|
self.submodules.append(module)
|
|
|
|
|
|
|
|
|
|
from .statistics import blueprint as module
|
|
|
|
|
self.submodules.append(module)
|
|
|
|
|
|
2022-11-19 17:43:41 +13:00
|
|
|
super().register(app, options)
|
2022-06-30 11:06:50 +05:30
|
|
|
|
2016-05-16 01:07:52 +05:30
|
|
|
|
2015-06-29 12:28:41 +05:30
|
|
|
# Initialise the module
|
2016-05-16 01:07:52 +05:30
|
|
|
blueprint = MiscModule(MODULE_NAME, __name__)
|
|
|
|
|
|
2015-06-29 12:28:41 +05:30
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
|
# A special URL used to "ping" the server
|
|
|
|
|
##########################################################################
|
2017-07-18 15:13:16 +01:00
|
|
|
@blueprint.route("/", endpoint='index')
|
2016-05-16 01:07:52 +05:30
|
|
|
def index():
|
|
|
|
|
return ''
|
2015-10-20 12:33:18 +05:30
|
|
|
|
|
|
|
|
|
2016-05-16 01:07:52 +05:30
|
|
|
##########################################################################
|
|
|
|
|
# A special URL used to "ping" the server
|
|
|
|
|
##########################################################################
|
2018-07-05 11:12:03 +01:00
|
|
|
@blueprint.route("/ping")
|
2019-05-28 17:05:54 +05:30
|
|
|
@pgCSRFProtect.exempt
|
2015-06-29 12:28:41 +05:30
|
|
|
def ping():
|
|
|
|
|
"""Generate a "PING" response to indicate that the server is alive."""
|
|
|
|
|
return "PING"
|
2016-05-16 01:07:52 +05:30
|
|
|
|
|
|
|
|
|
2018-07-05 11:12:03 +01:00
|
|
|
# For Garbage Collecting closed connections
|
|
|
|
|
@blueprint.route("/cleanup", methods=['POST'])
|
2019-05-28 10:59:51 +05:30
|
|
|
@pgCSRFProtect.exempt
|
2018-07-05 11:12:03 +01:00
|
|
|
def cleanup():
|
|
|
|
|
driver.ping()
|
2018-10-09 11:34:13 +01:00
|
|
|
# Cleanup session files.
|
|
|
|
|
cleanup_session_files()
|
2018-07-05 11:12:03 +01:00
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
2023-01-30 15:39:34 +05:30
|
|
|
@blueprint.route("/heartbeat/log", methods=['POST'])
|
2023-01-19 15:57:02 +05:30
|
|
|
@pgCSRFProtect.exempt
|
2023-01-30 15:39:34 +05:30
|
|
|
def log_heartbeat():
|
2023-01-19 15:57:02 +05:30
|
|
|
data = None
|
|
|
|
|
if hasattr(request.data, 'decode'):
|
|
|
|
|
data = request.data.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
if data != '':
|
|
|
|
|
data = json.loads(data)
|
|
|
|
|
|
2023-01-30 15:39:34 +05:30
|
|
|
status, msg = log_server_heartbeat(data)
|
|
|
|
|
if status:
|
|
|
|
|
return make_json_response(data=msg, status=200)
|
|
|
|
|
else:
|
|
|
|
|
return make_json_response(data=msg, status=404)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/heartbeat/stop", methods=['POST'])
|
|
|
|
|
@pgCSRFProtect.exempt
|
|
|
|
|
def stop_heartbeat():
|
|
|
|
|
data = None
|
|
|
|
|
if hasattr(request.data, 'decode'):
|
|
|
|
|
data = request.data.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
if data != '':
|
|
|
|
|
data = json.loads(data)
|
|
|
|
|
|
2024-01-24 18:33:43 +05:30
|
|
|
_, msg = stop_server_heartbeat(data)
|
2023-01-30 15:39:34 +05:30
|
|
|
return make_json_response(data=msg,
|
2023-01-19 15:57:02 +05:30
|
|
|
status=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/get_heartbeat/<int:sid>", methods=['GET'])
|
|
|
|
|
@pgCSRFProtect.exempt
|
|
|
|
|
def get_heartbeat(sid):
|
|
|
|
|
heartbeat_data = get_server_heartbeat(sid)
|
|
|
|
|
return make_json_response(data=heartbeat_data,
|
|
|
|
|
status=200)
|
|
|
|
|
|
|
|
|
|
|
2018-03-15 13:26:24 +05:30
|
|
|
##########################################################################
|
2018-06-08 08:45:02 +01:00
|
|
|
# A special URL used to shut down the server
|
2018-03-15 13:26:24 +05:30
|
|
|
##########################################################################
|
|
|
|
|
@blueprint.route("/shutdown", methods=('get', 'post'))
|
2019-05-28 17:05:54 +05:30
|
|
|
@pgCSRFProtect.exempt
|
2018-03-15 13:26:24 +05:30
|
|
|
def shutdown():
|
|
|
|
|
if config.SERVER_MODE is not True:
|
|
|
|
|
func = request.environ.get('werkzeug.server.shutdown')
|
|
|
|
|
if func is None:
|
|
|
|
|
raise RuntimeError('Not running with the Werkzeug Server')
|
|
|
|
|
func()
|
|
|
|
|
return 'SHUTDOWN'
|
|
|
|
|
else:
|
|
|
|
|
return ''
|
2021-06-04 17:55:35 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
|
# A special URL used to validate the binary path
|
|
|
|
|
##########################################################################
|
|
|
|
|
@blueprint.route("/validate_binary_path",
|
|
|
|
|
endpoint="validate_binary_path",
|
|
|
|
|
methods=["POST"])
|
2024-04-29 13:41:02 +05:30
|
|
|
@pga_login_required
|
2021-06-04 17:55:35 +05:30
|
|
|
def validate_binary_path():
|
|
|
|
|
"""
|
|
|
|
|
This function is used to validate the specified utilities path by
|
2022-11-30 12:02:45 +05:30
|
|
|
running the utilities with their versions.
|
2021-06-04 17:55:35 +05:30
|
|
|
"""
|
|
|
|
|
data = None
|
|
|
|
|
if hasattr(request.data, 'decode'):
|
|
|
|
|
data = request.data.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
if data != '':
|
|
|
|
|
data = json.loads(data)
|
|
|
|
|
|
|
|
|
|
version_str = ''
|
2024-04-01 11:34:01 +05:30
|
|
|
|
|
|
|
|
# Do not allow storage dir as utility path
|
|
|
|
|
if 'utility_path' in data and data['utility_path'] is not None and \
|
|
|
|
|
Path(config.STORAGE_DIR) != Path(data['utility_path']) and \
|
|
|
|
|
Path(config.STORAGE_DIR) not in Path(data['utility_path']).parents:
|
2023-09-20 11:17:07 +05:30
|
|
|
binary_versions = get_binary_path_versions(data['utility_path'])
|
|
|
|
|
for utility, version in binary_versions.items():
|
|
|
|
|
if version is None:
|
2021-06-04 17:55:35 +05:30
|
|
|
version_str += "<b>" + utility + ":</b> " + \
|
|
|
|
|
"not found on the specified binary path.<br/>"
|
2023-09-20 11:17:07 +05:30
|
|
|
else:
|
|
|
|
|
version_str += "<b>" + utility + ":</b> " + version + "<br/>"
|
2021-06-04 17:55:35 +05:30
|
|
|
else:
|
|
|
|
|
return precondition_required(gettext('Invalid binary path.'))
|
|
|
|
|
|
|
|
|
|
return make_json_response(data=gettext(version_str), status=200)
|
2023-10-23 17:43:17 +05:30
|
|
|
|
|
|
|
|
|
2024-04-01 11:34:01 +05:30
|
|
|
@blueprint.route("/upgrade_check", endpoint="upgrade_check",
|
|
|
|
|
methods=['GET'])
|
2024-04-29 13:41:02 +05:30
|
|
|
@pga_login_required
|
2023-10-23 17:43:17 +05:30
|
|
|
def upgrade_check():
|
|
|
|
|
# Get the current version info from the website, and flash a message if
|
|
|
|
|
# the user is out of date, and the check is enabled.
|
|
|
|
|
ret = {
|
|
|
|
|
"outdated": False,
|
|
|
|
|
}
|
|
|
|
|
if config.UPGRADE_CHECK_ENABLED:
|
|
|
|
|
last_check = get_setting('LastUpdateCheck', default='0')
|
|
|
|
|
today = time.strftime('%Y%m%d')
|
|
|
|
|
if int(last_check) < int(today):
|
|
|
|
|
data = None
|
|
|
|
|
url = '%s?version=%s' % (
|
|
|
|
|
config.UPGRADE_CHECK_URL, config.APP_VERSION)
|
|
|
|
|
current_app.logger.debug('Checking version data at: %s' % url)
|
|
|
|
|
try:
|
|
|
|
|
# Do not wait for more than 5 seconds.
|
|
|
|
|
# It stuck on rendering the browser.html, while working in the
|
|
|
|
|
# broken network.
|
|
|
|
|
if os.path.exists(config.CA_FILE):
|
|
|
|
|
response = urlopen(url, data, 5, cafile=config.CA_FILE)
|
|
|
|
|
else:
|
|
|
|
|
response = urlopen(url, data, 5)
|
|
|
|
|
current_app.logger.debug(
|
|
|
|
|
'Version check HTTP response code: %d' % response.getcode()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response.getcode() == 200:
|
|
|
|
|
data = json.loads(response.read().decode('utf-8'))
|
|
|
|
|
current_app.logger.debug('Response data: %s' % data)
|
|
|
|
|
except Exception:
|
|
|
|
|
current_app.logger.exception(
|
|
|
|
|
'Exception when checking for update')
|
|
|
|
|
return internal_server_error('Failed to check for update')
|
|
|
|
|
|
|
|
|
|
if data is not None and \
|
|
|
|
|
data[config.UPGRADE_CHECK_KEY]['version_int'] > \
|
|
|
|
|
config.APP_VERSION_INT:
|
|
|
|
|
ret = {
|
|
|
|
|
"outdated": True,
|
|
|
|
|
"current_version": config.APP_VERSION,
|
|
|
|
|
"upgrade_version": data[config.UPGRADE_CHECK_KEY][
|
|
|
|
|
'version'],
|
|
|
|
|
"product_name": config.APP_NAME,
|
|
|
|
|
"download_url": data[config.UPGRADE_CHECK_KEY][
|
|
|
|
|
'download_url']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
store_setting('LastUpdateCheck', today)
|
|
|
|
|
return make_json_response(data=ret)
|