This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime: implement database service discovery and refactor (#1798)
- Loading branch information
1 parent
c09e19a
commit 79eba02
Showing
28 changed files
with
793 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import os | ||
|
||
from cloudtik.core._private.core_utils import get_env_string_value | ||
|
||
DATABASE_CONFIG_ENGINE = "engine" | ||
DATABASE_CONFIG_ADDRESS = "address" | ||
DATABASE_CONFIG_PORT = "port" | ||
DATABASE_CONFIG_USERNAME = "username" | ||
DATABASE_CONFIG_PASSWORD = "password" | ||
|
||
DATABASE_ENGINE_MYSQL = "mysql" | ||
DATABASE_ENGINE_POSTGRES = "postgres" | ||
|
||
DATABASE_ENV_ENABLED = "CLOUD_DATABASE" | ||
DATABASE_ENV_ENGINE = "CLOUD_DATABASE_ENGINE" | ||
DATABASE_ENV_ADDRESS = "CLOUD_DATABASE_HOSTNAME" | ||
DATABASE_ENV_PORT = "CLOUD_DATABASE_PORT" | ||
DATABASE_ENV_USERNAME = "CLOUD_DATABASE_USERNAME" | ||
DATABASE_ENV_PASSWORD = "CLOUD_DATABASE_PASSWORD" | ||
|
||
DATABASE_USERNAME_MYSQL_DEFAULT = "root" | ||
DATABASE_USERNAME_POSTGRES_DEFAULT = "cloudtik" | ||
DATABASE_PASSWORD_DEFAULT = "cloudtik" | ||
|
||
|
||
def get_database_engine(database_config): | ||
engine = database_config.get(DATABASE_CONFIG_ENGINE) | ||
return get_validated_engine(engine) | ||
|
||
|
||
def get_validated_engine(engine): | ||
if engine and engine != DATABASE_ENGINE_MYSQL and engine != DATABASE_ENGINE_POSTGRES: | ||
raise ValueError( | ||
"The database engine type {} is not supported.".format(engine)) | ||
return engine or DATABASE_ENGINE_MYSQL | ||
|
||
|
||
def get_database_port(database_config): | ||
port = database_config.get(DATABASE_CONFIG_PORT) | ||
if not port: | ||
engine = get_database_engine(database_config) | ||
port = 3306 if engine == DATABASE_ENGINE_MYSQL else 5432 | ||
return port | ||
|
||
|
||
def get_database_username(database_config): | ||
username = database_config.get( | ||
DATABASE_CONFIG_USERNAME) | ||
if not username: | ||
engine = get_database_engine(database_config) | ||
username = (DATABASE_USERNAME_MYSQL_DEFAULT | ||
if engine == DATABASE_ENGINE_MYSQL | ||
else DATABASE_USERNAME_POSTGRES_DEFAULT) | ||
return username | ||
|
||
|
||
def get_database_password(database_config): | ||
return database_config.get( | ||
DATABASE_CONFIG_PASSWORD, DATABASE_PASSWORD_DEFAULT) | ||
|
||
|
||
def is_database_configured(database_config): | ||
if not database_config: | ||
return False | ||
return True if database_config.get( | ||
DATABASE_CONFIG_ADDRESS) else False | ||
|
||
|
||
def set_database_config(database_config, database_service): | ||
engine, service_addresses = database_service | ||
# take one address | ||
service_address = service_addresses[0] | ||
database_config[DATABASE_CONFIG_ENGINE] = get_validated_engine(engine) | ||
database_config[DATABASE_CONFIG_ADDRESS] = service_address[0] | ||
database_config[DATABASE_CONFIG_PORT] = service_address[1] | ||
|
||
|
||
def export_database_environment_variables(database_config): | ||
if not is_database_configured(database_config): | ||
return | ||
|
||
os.environ[DATABASE_ENV_ENABLED] = get_env_string_value(True) | ||
os.environ[DATABASE_ENV_ENGINE] = get_database_engine(database_config) | ||
os.environ[DATABASE_ENV_ADDRESS] = database_config[DATABASE_CONFIG_ADDRESS] | ||
os.environ[DATABASE_ENV_PORT] = str(get_database_port(database_config)) | ||
|
||
# The defaults apply to built-in Database runtime. | ||
os.environ[DATABASE_ENV_USERNAME] = get_database_username(database_config) | ||
os.environ[DATABASE_ENV_PASSWORD] = get_database_password(database_config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.