Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def postgres_server():
if shutil.which("psql") is None:
pytest.skip("Postgres is not installed on this server and no active postgres could be found.")
storage = None
psql = PostgresHarness({"database": {"port": 5432}})
# psql = PostgresHarness({"database": {"port": 5432, "username": "qcarchive", "password": "mypass"}})
if not psql.is_alive():
print()
print(
f"Could not connect to a Postgres server at {psql.config.database_uri()}, this will increase time per test session by ~3 seconds."
)
print()
storage = TemporaryPostgres()
psql = storage.psql
print("Using Database: ", psql.config.database_uri())
yield psql
if storage:
storage.stop()
def server_upgrade(args, config):
# alembic upgrade head
print("Upgrading QCFractal server.\n")
print(f"QCFractal server base folder: {config.base_folder}")
print("\n>>> Checking the PostgreSQL connection...")
psql = PostgresHarness(config, quiet=False, logger=print)
ensure_postgres_alive(psql)
print("\n>>> Upgrading the Database...")
try:
psql.upgrade()
except ValueError as e:
print(str(e))
sys.exit(1)
def server_init(args, config):
# alembic stamp head
print("Initializing QCFractal configuration.")
# Configuration settings
config.base_path.mkdir(parents=True, exist_ok=True)
overwrite_config = args.get("overwrite_config", False)
clear_database = args.get("clear_database", False)
psql = PostgresHarness(config, quiet=False, logger=print)
# Make sure we do not delete anything.
if config.config_file_path.exists():
print()
if not overwrite_config:
print(
"QCFractal configuration file already exists, to overwrite use '--overwrite-config' "
"or use the `qcfractal-server config` command line to alter settings."
)
sys.exit(2)
else:
user_required_input = f"REMOVEALLDATA {str(config.database_path)}"
print("!WARNING! A QCFractal configuration is currently initialized")
print(
f"!WARNING! Overwriting will delete all current Fractal data, this includes all data in {str(config.database_path)}."
)
def server_user(args, config):
print("QCFractal server user function.\n")
print(f"QCFractal server base folder: {config.base_folder}")
print("\n>>> Checking the PostgreSQL connection...")
psql = PostgresHarness(config, quiet=False, logger=print)
ensure_postgres_alive(psql)
storage = storage_socket_factory(config.database_uri(safe=False))
try:
if args["user_command"] == "add":
print("\n>>> Adding new user...")
success, pw = storage.add_user(args["username"], password=args["password"], permissions=args["permissions"])
if success:
print(f"\n>>> New user successfully added, password:\n{pw}")
if config.fractal.security is None:
print(
"Warning: security is disabled. To enable security, change the configuration YAML field "
"fractal:security to local."
)
else:
def server_info(args, config):
psql = PostgresHarness(config, quiet=False, logger=print)
if args["category"] == "config":
print(f"Displaying QCFractal configuration:\n")
print(yaml.dump(config.dict(), default_flow_style=False))
elif args["category"] == "alembic":
print(f"Displaying QCFractal Alembic CLI configuration:\n")
print(" ".join(psql.alembic_commands()))
def server_dashboard(args, config):
print("QCFractal server dashboard.\n")
print(f"QCFractal server base folder: {config.base_folder}")
print("\n>>> Checking the PostgreSQL connection...")
psql = PostgresHarness(config, quiet=False, logger=print)
ensure_postgres_alive(psql)
from ..dashboard import app
print("\n>>> Starting dashboard...")
app.server.config["FRACTAL_CONFIG"] = config
app.run_server(debug=True)
ssl_options = True
print("Autogenerated SSL certificates, clients must use 'verify=False' when connecting.")
elif ssl_certs == 2:
ssl_options = {"crt": args["tls_cert"], "key": args["tls_key"]}
print("Reading SSL certificates.")
else:
raise KeyError("Both tls-cert and tls-key must be passed in.")
# Build the server itself
if config.fractal.logfile is None:
logfile = None
else:
logfile = str(config.base_path / config.fractal.logfile)
print("\n>>> Checking the PostgreSQL connection...")
psql = PostgresHarness(config, quiet=False, logger=print)
ensure_postgres_alive(psql)
# make sure DB is created
psql.create_database(config.database.database_name)
print("\n>>> Initializing the QCFractal server...")
try:
server = qcfractal.FractalServer(
name=args.get("server_name", None) or config.fractal.name,
port=config.fractal.port,
compress_response=config.fractal.compress_response,
# Security
security=config.fractal.security,
allow_read=config.fractal.allow_read,
ssl_options=ssl_options,
self._active = True
if not tmpdir:
self._db_tmpdir = tempfile.TemporaryDirectory()
else:
self._db_tmpdir = tmpdir
self.quiet = quiet
self.logger = logger
config_data = {"port": find_port(), "directory": self._db_tmpdir.name}
if database_name:
config_data["database_name"] = database_name
self.config = FractalConfig(database=config_data)
self.psql = PostgresHarness(self.config)
self.psql.initialize_postgres()
self.psql.init_database()
atexit.register(self.stop)