2015-06-30 05:51:55 +00:00
|
|
|
#########################################################################
|
2015-01-22 15:56:23 +00:00
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2021-01-04 10:04:45 +00:00
|
|
|
# Copyright (C) 2013 - 2021, The pgAdmin Development Team
|
2015-01-22 15:56:23 +00:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Perform the initial setup of the application, by creating the auth
|
|
|
|
and settings database."""
|
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
import argparse
|
|
|
|
import json
|
2016-06-21 13:12:14 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2020-04-30 11:52:48 +00:00
|
|
|
import builtins
|
2015-10-20 07:03:18 +00:00
|
|
|
|
2021-03-02 09:23:05 +00:00
|
|
|
USER_NOT_FOUND = "The specified user ID (%s) could not be found."
|
|
|
|
|
2017-08-25 10:56:44 +00:00
|
|
|
# Grab the SERVER_MODE if it's been set by the runtime
|
|
|
|
if 'SERVER_MODE' in globals():
|
|
|
|
builtins.SERVER_MODE = globals()['SERVER_MODE']
|
|
|
|
else:
|
|
|
|
builtins.SERVER_MODE = None
|
2016-06-21 13:12:14 +00:00
|
|
|
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 10:00:57 +00:00
|
|
|
# We need to include the root directory in sys.path to ensure that we can
|
|
|
|
# find everything we need when running in the standalone runtime.
|
|
|
|
root = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
if sys.path[0] != root:
|
|
|
|
sys.path.insert(0, root)
|
|
|
|
|
2021-04-27 07:24:57 +00:00
|
|
|
from pgadmin.model import db, User, Version, ServerGroup, Server, \
|
|
|
|
SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
|
|
|
|
from pgadmin import create_app
|
|
|
|
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 10:00:57 +00:00
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
def add_value(attr_dict, key, value):
|
|
|
|
"""Add a value to the attribute dict if non-empty.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
attr_dict (dict): The dictionary to add the values to
|
|
|
|
key (str): The key for the new value
|
|
|
|
value (str): The value to add
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The updated attribute dictionary
|
|
|
|
"""
|
|
|
|
if value != "" and value is not None:
|
|
|
|
attr_dict[key] = value
|
|
|
|
|
|
|
|
return attr_dict
|
|
|
|
|
|
|
|
|
|
|
|
def dump_servers(args):
|
|
|
|
"""Dump the server groups and servers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args (ArgParser): The parsed command line options
|
|
|
|
"""
|
|
|
|
|
2020-06-01 05:52:38 +00:00
|
|
|
# What user?
|
|
|
|
if args.user is not None:
|
|
|
|
dump_user = args.user
|
|
|
|
else:
|
|
|
|
dump_user = config.DESKTOP_USER
|
|
|
|
|
|
|
|
# And the sqlite path
|
|
|
|
if args.sqlite_path is not None:
|
|
|
|
config.SQLITE_PATH = args.sqlite_path
|
|
|
|
|
|
|
|
print('----------')
|
|
|
|
print('Dumping servers with:')
|
|
|
|
print('User:', dump_user)
|
|
|
|
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
|
|
|
print('----------')
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-06-01 05:52:38 +00:00
|
|
|
app = create_app(config.APP_NAME + '-cli')
|
|
|
|
with app.app_context():
|
2018-11-21 16:09:05 +00:00
|
|
|
user = User.query.filter_by(email=dump_user).first()
|
|
|
|
|
|
|
|
if user is None:
|
2021-03-02 09:23:05 +00:00
|
|
|
print(USER_NOT_FOUND % dump_user)
|
2018-11-21 16:09:05 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
user_id = user.id
|
|
|
|
|
|
|
|
# Dict to collect the output
|
|
|
|
object_dict = {}
|
|
|
|
|
|
|
|
# Counters
|
|
|
|
servers_dumped = 0
|
|
|
|
|
|
|
|
# Dump servers
|
|
|
|
servers = Server.query.filter_by(user_id=user_id).all()
|
|
|
|
server_dict = {}
|
|
|
|
for server in servers:
|
|
|
|
if args.servers is None or str(server.id) in args.servers:
|
|
|
|
# Get the group name
|
|
|
|
group_name = ServerGroup.query.filter_by(
|
|
|
|
user_id=user_id, id=server.servergroup_id).first().name
|
|
|
|
|
|
|
|
attr_dict = {}
|
|
|
|
add_value(attr_dict, "Name", server.name)
|
|
|
|
add_value(attr_dict, "Group", group_name)
|
|
|
|
add_value(attr_dict, "Host", server.host)
|
|
|
|
add_value(attr_dict, "HostAddr", server.hostaddr)
|
|
|
|
add_value(attr_dict, "Port", server.port)
|
|
|
|
add_value(attr_dict, "MaintenanceDB", server.maintenance_db)
|
|
|
|
add_value(attr_dict, "Username", server.username)
|
|
|
|
add_value(attr_dict, "Role", server.role)
|
|
|
|
add_value(attr_dict, "SSLMode", server.ssl_mode)
|
|
|
|
add_value(attr_dict, "Comment", server.comment)
|
2020-09-03 07:29:28 +00:00
|
|
|
add_value(attr_dict, "Shared", server.shared)
|
2018-11-21 16:09:05 +00:00
|
|
|
add_value(attr_dict, "DBRestriction", server.db_res)
|
|
|
|
add_value(attr_dict, "PassFile", server.passfile)
|
|
|
|
add_value(attr_dict, "SSLCert", server.sslcert)
|
|
|
|
add_value(attr_dict, "SSLKey", server.sslkey)
|
|
|
|
add_value(attr_dict, "SSLRootCert", server.sslrootcert)
|
|
|
|
add_value(attr_dict, "SSLCrl", server.sslcrl)
|
|
|
|
add_value(attr_dict, "SSLCompression", server.sslcompression)
|
|
|
|
add_value(attr_dict, "BGColor", server.bgcolor)
|
|
|
|
add_value(attr_dict, "FGColor", server.fgcolor)
|
|
|
|
add_value(attr_dict, "Service", server.service)
|
|
|
|
add_value(attr_dict, "Timeout", server.connect_timeout)
|
|
|
|
add_value(attr_dict, "UseSSHTunnel", server.use_ssh_tunnel)
|
|
|
|
add_value(attr_dict, "TunnelHost", server.tunnel_host)
|
|
|
|
add_value(attr_dict, "TunnelPort", server.tunnel_port)
|
|
|
|
add_value(attr_dict, "TunnelUsername", server.tunnel_username)
|
|
|
|
add_value(attr_dict, "TunnelAuthentication",
|
|
|
|
server.tunnel_authentication)
|
|
|
|
|
|
|
|
servers_dumped = servers_dumped + 1
|
|
|
|
|
|
|
|
server_dict[servers_dumped] = attr_dict
|
|
|
|
|
|
|
|
object_dict["Servers"] = server_dict
|
|
|
|
|
|
|
|
try:
|
|
|
|
f = open(args.dump_servers, "w")
|
|
|
|
except Exception as e:
|
|
|
|
print("Error opening output file %s: [%d] %s" %
|
|
|
|
(args.dump_servers, e.errno, e.strerror))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.write(json.dumps(object_dict, indent=4))
|
|
|
|
except Exception as e:
|
|
|
|
print("Error writing output file %s: [%d] %s" %
|
|
|
|
(args.dump_servers, e.errno, e.strerror))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
print("Configuration for %s servers dumped to %s." %
|
|
|
|
(servers_dumped, args.dump_servers))
|
|
|
|
|
|
|
|
|
2020-09-09 05:55:43 +00:00
|
|
|
def _validate_servers_data(data, is_admin):
|
2020-09-02 09:09:54 +00:00
|
|
|
"""
|
|
|
|
Used internally by load_servers to validate servers data.
|
|
|
|
:param data: servers data
|
|
|
|
:return: error message if any
|
|
|
|
"""
|
2020-09-09 05:55:43 +00:00
|
|
|
skip_servers = []
|
2020-09-02 09:09:54 +00:00
|
|
|
# Loop through the servers...
|
|
|
|
if "Servers" not in data:
|
|
|
|
return ("'Servers' attribute not found in file '%s'" %
|
|
|
|
args.load_servers)
|
|
|
|
|
|
|
|
for server in data["Servers"]:
|
|
|
|
obj = data["Servers"][server]
|
|
|
|
|
2020-09-09 05:55:43 +00:00
|
|
|
# Check if server is shared.Won't import if user is non-admin
|
|
|
|
if obj.get('Shared', None) and not is_admin:
|
|
|
|
print("Won't import the server '%s' as it is shared " %
|
|
|
|
obj["Name"])
|
|
|
|
skip_servers.append(server)
|
|
|
|
continue
|
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
def check_attrib(attrib):
|
|
|
|
if attrib not in obj:
|
|
|
|
return ("'%s' attribute not found for server '%s'" %
|
|
|
|
(attrib, server))
|
2021-04-02 03:28:43 +00:00
|
|
|
return None
|
2020-09-02 09:09:54 +00:00
|
|
|
|
2021-04-02 03:28:43 +00:00
|
|
|
for attrib in ("Group", "Name"):
|
|
|
|
errmsg = check_attrib(attrib)
|
|
|
|
if errmsg:
|
|
|
|
return errmsg
|
2020-09-02 09:09:54 +00:00
|
|
|
|
|
|
|
is_service_attrib_available = obj.get("Service", None) is not None
|
|
|
|
|
|
|
|
if not is_service_attrib_available:
|
2021-04-02 03:28:43 +00:00
|
|
|
for attrib in ("Port", "Username"):
|
|
|
|
errmsg = check_attrib(attrib)
|
|
|
|
if errmsg:
|
|
|
|
return errmsg
|
|
|
|
|
|
|
|
for attrib in ("SSLMode", "MaintenanceDB"):
|
|
|
|
errmsg = check_attrib(attrib)
|
|
|
|
if errmsg:
|
|
|
|
return errmsg
|
2020-09-02 09:09:54 +00:00
|
|
|
|
|
|
|
if "Host" not in obj and "HostAddr" not in obj and not \
|
|
|
|
is_service_attrib_available:
|
|
|
|
return ("'Host', 'HostAddr' or 'Service' attribute "
|
|
|
|
"not found for server '%s'" % server)
|
|
|
|
|
2020-09-09 05:55:43 +00:00
|
|
|
for server in skip_servers:
|
|
|
|
del data["Servers"][server]
|
2020-09-02 09:09:54 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
def load_servers(args):
|
|
|
|
"""Load server groups and servers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args (ArgParser): The parsed command line options
|
|
|
|
"""
|
2020-06-01 05:52:38 +00:00
|
|
|
|
|
|
|
# What user?
|
2020-09-02 09:09:54 +00:00
|
|
|
load_user = args.user if args.user is not None else config.DESKTOP_USER
|
2020-06-01 05:52:38 +00:00
|
|
|
|
|
|
|
# And the sqlite path
|
|
|
|
if args.sqlite_path is not None:
|
|
|
|
config.SQLITE_PATH = args.sqlite_path
|
|
|
|
|
|
|
|
print('----------')
|
|
|
|
print('Loading servers with:')
|
|
|
|
print('User:', load_user)
|
|
|
|
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
|
|
|
print('----------')
|
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
try:
|
|
|
|
with open(args.load_servers) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
except json.decoder.JSONDecodeError as e:
|
|
|
|
print("Error parsing input file %s: %s" %
|
|
|
|
(args.load_servers, e))
|
|
|
|
sys.exit(1)
|
|
|
|
except Exception as e:
|
|
|
|
print("Error reading input file %s: [%d] %s" %
|
|
|
|
(args.load_servers, e.errno, e.strerror))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
|
2020-06-01 05:52:38 +00:00
|
|
|
app = create_app(config.APP_NAME + '-cli')
|
2018-11-21 16:09:05 +00:00
|
|
|
with app.app_context():
|
2020-06-01 05:52:38 +00:00
|
|
|
user = User.query.filter_by(email=load_user).first()
|
2018-11-21 16:09:05 +00:00
|
|
|
|
|
|
|
if user is None:
|
2021-03-02 09:23:05 +00:00
|
|
|
print(USER_NOT_FOUND % load_user)
|
2018-11-21 16:09:05 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
user_id = user.id
|
|
|
|
|
|
|
|
# Counters
|
|
|
|
groups_added = 0
|
|
|
|
servers_added = 0
|
|
|
|
|
2019-02-21 15:40:37 +00:00
|
|
|
# Get the server groups
|
|
|
|
groups = ServerGroup.query.filter_by(user_id=user_id)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
|
|
|
def print_summary():
|
|
|
|
print("Added %d Server Group(s) and %d Server(s)." %
|
|
|
|
(groups_added, servers_added))
|
2020-09-02 09:09:54 +00:00
|
|
|
|
2020-09-09 05:55:43 +00:00
|
|
|
err_msg = _validate_servers_data(data, user.has_role("Administrator"))
|
2020-09-02 09:09:54 +00:00
|
|
|
if err_msg is not None:
|
|
|
|
print(err_msg)
|
2018-11-21 16:09:05 +00:00
|
|
|
print_summary()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
for server in data["Servers"]:
|
|
|
|
obj = data["Servers"][server]
|
|
|
|
|
|
|
|
# Get the group. Create if necessary
|
2020-09-02 09:09:54 +00:00
|
|
|
group_id = next(
|
|
|
|
(g.id for g in groups if g.name == obj["Group"]), -1)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
|
|
|
if group_id == -1:
|
|
|
|
new_group = ServerGroup()
|
|
|
|
new_group.name = obj["Group"]
|
|
|
|
new_group.user_id = user_id
|
|
|
|
db.session.add(new_group)
|
|
|
|
|
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
print("Error creating server group '%s': %s" %
|
|
|
|
(new_group.name, e))
|
|
|
|
print_summary()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
group_id = new_group.id
|
|
|
|
groups_added = groups_added + 1
|
2019-02-21 15:40:37 +00:00
|
|
|
groups = ServerGroup.query.filter_by(user_id=user_id)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
|
|
|
# Create the server
|
|
|
|
new_server = Server()
|
|
|
|
new_server.name = obj["Name"]
|
|
|
|
new_server.servergroup_id = group_id
|
|
|
|
new_server.user_id = user_id
|
2020-08-31 12:19:46 +00:00
|
|
|
new_server.ssl_mode = obj["SSLMode"]
|
|
|
|
new_server.maintenance_db = obj["MaintenanceDB"]
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.host = obj.get("Host", None)
|
|
|
|
|
|
|
|
new_server.hostaddr = obj.get("HostAddr", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.port = obj.get("Port", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.username = obj.get("Username", None)
|
2020-08-31 12:19:46 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.role = obj.get("Role", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.ssl_mode = obj["SSLMode"]
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.comment = obj.get("Comment", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.db_res = obj.get("DBRestriction", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.passfile = obj.get("PassFile", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.sslcert = obj.get("SSLCert", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.sslkey = obj.get("SSLKey", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.sslrootcert = obj.get("SSLRootCert", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.sslcrl = obj.get("SSLCrl", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.sslcompression = obj.get("SSLCompression", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.bgcolor = obj.get("BGColor", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.fgcolor = obj.get("FGColor", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.service = obj.get("Service", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.connect_timeout = obj.get("Timeout", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.use_ssh_tunnel = obj.get("UseSSHTunnel", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.tunnel_host = obj.get("TunnelHost", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.tunnel_port = obj.get("TunnelPort", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.tunnel_username = obj.get("TunnelUsername", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-09-02 09:09:54 +00:00
|
|
|
new_server.tunnel_authentication = \
|
|
|
|
obj.get("TunnelAuthentication", None)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2020-12-03 05:10:52 +00:00
|
|
|
new_server.shared = \
|
|
|
|
obj.get("Shared", None)
|
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
db.session.add(new_server)
|
|
|
|
|
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
print("Error creating server '%s': %s" %
|
|
|
|
(new_server.name, e))
|
|
|
|
print_summary()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
servers_added = servers_added + 1
|
|
|
|
|
|
|
|
print_summary()
|
|
|
|
|
|
|
|
|
|
|
|
def setup_db():
|
|
|
|
"""Setup the configuration database."""
|
2018-01-26 16:54:21 +00:00
|
|
|
|
2017-04-24 03:06:55 +00:00
|
|
|
create_app_data_directory(config)
|
|
|
|
|
2017-07-10 15:08:35 +00:00
|
|
|
app = create_app()
|
2015-07-22 16:42:39 +00:00
|
|
|
|
2020-08-31 11:15:31 +00:00
|
|
|
print("pgAdmin 4 - Application Initialisation")
|
|
|
|
print("======================================\n")
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 10:00:57 +00:00
|
|
|
|
2017-07-10 15:08:35 +00:00
|
|
|
with app.app_context():
|
2017-08-29 14:03:02 +00:00
|
|
|
# Run migration for the first time i.e. create database
|
|
|
|
from config import SQLITE_PATH
|
|
|
|
if not os.path.exists(SQLITE_PATH):
|
|
|
|
db_upgrade(app)
|
|
|
|
else:
|
|
|
|
version = Version.query.filter_by(name='ConfigDB').first()
|
|
|
|
schema_version = version.value
|
|
|
|
|
|
|
|
# Run migration if current schema version is greater than the
|
|
|
|
# schema version stored in version table
|
|
|
|
if CURRENT_SCHEMA_VERSION >= schema_version:
|
|
|
|
db_upgrade(app)
|
|
|
|
|
|
|
|
# Update schema version to the latest
|
|
|
|
if CURRENT_SCHEMA_VERSION > schema_version:
|
|
|
|
version = Version.query.filter_by(name='ConfigDB').first()
|
|
|
|
version.value = CURRENT_SCHEMA_VERSION
|
|
|
|
db.session.commit()
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2019-05-20 12:53:08 +00:00
|
|
|
if os.name != 'nt':
|
|
|
|
os.chmod(config.SQLITE_PATH, 0o600)
|
2019-04-17 15:57:34 +00:00
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2021-03-01 12:29:47 +00:00
|
|
|
def clear_servers():
|
|
|
|
"""Clear groups and servers configurations.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args (ArgParser): The parsed command line options
|
|
|
|
"""
|
|
|
|
|
|
|
|
# What user?
|
|
|
|
load_user = args.user if args.user is not None else config.DESKTOP_USER
|
|
|
|
|
|
|
|
# And the sqlite path
|
|
|
|
if args.sqlite_path is not None:
|
|
|
|
config.SQLITE_PATH = args.sqlite_path
|
|
|
|
|
|
|
|
app = create_app(config.APP_NAME + '-cli')
|
|
|
|
with app.app_context():
|
|
|
|
user = User.query.filter_by(email=load_user).first()
|
|
|
|
|
|
|
|
if user is None:
|
2021-03-02 09:23:05 +00:00
|
|
|
print(USER_NOT_FOUND % load_user)
|
2021-03-01 12:29:47 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
user_id = user.id
|
|
|
|
|
|
|
|
# Remove all servers
|
|
|
|
servers = Server.query.filter_by(user_id=user_id)
|
|
|
|
for server in servers:
|
|
|
|
db.session.delete(server)
|
|
|
|
|
|
|
|
# Remove all groups
|
|
|
|
groups = ServerGroup.query.filter_by(user_id=user_id)
|
|
|
|
for group in groups:
|
|
|
|
db.session.delete(group)
|
|
|
|
servers = Server.query.filter_by(user_id=user_id)
|
|
|
|
|
|
|
|
for server in servers:
|
|
|
|
db.session.delete(server)
|
|
|
|
|
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
2021-03-02 09:23:05 +00:00
|
|
|
print("Error clearing server configuration with error (%s)" %
|
|
|
|
str(e))
|
2021-03-01 12:29:47 +00:00
|
|
|
|
|
|
|
|
2018-11-21 16:09:05 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
# Configuration settings
|
|
|
|
import config
|
|
|
|
from pgadmin.model import SCHEMA_VERSION
|
|
|
|
from pgadmin.setup import db_upgrade, create_app_data_directory
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Setup the pgAdmin config DB')
|
|
|
|
|
2020-06-01 05:52:38 +00:00
|
|
|
exp_group = parser.add_argument_group('Dump server config')
|
2018-11-21 16:09:05 +00:00
|
|
|
exp_group.add_argument('--dump-servers', metavar="OUTPUT_FILE",
|
|
|
|
help='Dump the servers in the DB', required=False)
|
|
|
|
exp_group.add_argument('--servers', metavar="SERVERS", nargs='*',
|
|
|
|
help='One or more servers to dump', required=False)
|
|
|
|
|
2020-06-01 05:52:38 +00:00
|
|
|
imp_group = parser.add_argument_group('Load server config')
|
2018-11-21 16:09:05 +00:00
|
|
|
imp_group.add_argument('--load-servers', metavar="INPUT_FILE",
|
|
|
|
help='Load servers into the DB', required=False)
|
2021-03-01 12:29:47 +00:00
|
|
|
imp_group.add_argument('--replace', dest='replace', action='store_true',
|
|
|
|
help='replace server configurations',
|
|
|
|
required=False)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
2021-03-01 12:29:47 +00:00
|
|
|
imp_group.set_defaults(replace=False)
|
2020-06-01 05:52:38 +00:00
|
|
|
# Common args
|
|
|
|
parser.add_argument('--sqlite-path', metavar="PATH",
|
|
|
|
help='Dump/load with the specified pgAdmin config DB'
|
|
|
|
' file. This is particularly helpful when there'
|
|
|
|
' are multiple pgAdmin configurations. It is also'
|
|
|
|
' recommended to use this option when running'
|
|
|
|
' pgAdmin in desktop mode.', required=False)
|
|
|
|
parser.add_argument('--user', metavar="USER_NAME",
|
|
|
|
help='Dump/load servers for the specified username',
|
|
|
|
required=False)
|
2018-11-21 16:09:05 +00:00
|
|
|
|
|
|
|
args, extra = parser.parse_known_args()
|
|
|
|
|
|
|
|
config.SETTINGS_SCHEMA_VERSION = SCHEMA_VERSION
|
|
|
|
if "PGADMIN_TESTING_MODE" in os.environ and \
|
|
|
|
os.environ["PGADMIN_TESTING_MODE"] == "1":
|
|
|
|
config.SQLITE_PATH = config.TEST_SQLITE_PATH
|
|
|
|
|
|
|
|
# What to do?
|
|
|
|
if args.dump_servers is not None:
|
2020-06-01 05:52:38 +00:00
|
|
|
try:
|
|
|
|
dump_servers(args)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2018-11-21 16:09:05 +00:00
|
|
|
elif args.load_servers is not None:
|
2020-06-01 05:52:38 +00:00
|
|
|
try:
|
2021-03-01 12:29:47 +00:00
|
|
|
if args.replace:
|
|
|
|
clear_servers()
|
2020-06-01 05:52:38 +00:00
|
|
|
load_servers(args)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2018-11-21 16:09:05 +00:00
|
|
|
else:
|
|
|
|
setup_db()
|