2020-04-28 20:15:59 +00:00
|
|
|
import os
|
2021-12-10 08:53:00 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
import frappe
|
2020-06-29 17:58:10 +00:00
|
|
|
import semantic_version
|
2020-07-16 00:36:48 +00:00
|
|
|
from constants import COMMON_SITE_CONFIG_FILE, RDS_DB, RDS_PRIVILEGES
|
2021-12-10 08:53:00 +00:00
|
|
|
from frappe.installer import update_site_config
|
|
|
|
from utils import get_config, get_password, get_site_config, run_command
|
2020-04-16 06:19:40 +00:00
|
|
|
|
2021-02-28 19:17:00 +00:00
|
|
|
# try to import _new_site from frappe, which could possibly
|
|
|
|
# exist in either commands.py or installer.py, and so we need
|
|
|
|
# to maintain compatibility across all frappe versions.
|
|
|
|
try:
|
|
|
|
# <= version-{11,12}
|
|
|
|
from frappe.commands.site import _new_site
|
|
|
|
except ImportError:
|
|
|
|
# >= version-13 and develop
|
|
|
|
from frappe.installer import _new_site
|
|
|
|
|
2020-04-16 06:19:40 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
def main():
|
2020-06-29 17:58:10 +00:00
|
|
|
config = get_config()
|
2021-12-10 08:52:40 +00:00
|
|
|
db_type = "mariadb"
|
|
|
|
db_port = config.get("db_port", 3306)
|
|
|
|
db_host = config.get("db_host")
|
|
|
|
site_name = os.environ.get("SITE_NAME", "site1.localhost")
|
|
|
|
db_root_username = os.environ.get("DB_ROOT_USER", "root")
|
|
|
|
mariadb_root_password = get_password("MYSQL_ROOT_PASSWORD", "admin")
|
2020-06-29 17:58:10 +00:00
|
|
|
postgres_root_password = get_password("POSTGRES_PASSWORD")
|
2020-09-29 12:42:41 +00:00
|
|
|
db_root_password = mariadb_root_password
|
2020-06-29 17:58:10 +00:00
|
|
|
|
|
|
|
if postgres_root_password:
|
2021-12-10 08:52:40 +00:00
|
|
|
db_type = "postgres"
|
2020-06-29 17:58:10 +00:00
|
|
|
db_host = os.environ.get("POSTGRES_HOST")
|
2020-06-30 01:02:46 +00:00
|
|
|
db_port = 5432
|
2020-09-29 12:42:41 +00:00
|
|
|
db_root_password = postgres_root_password
|
2020-06-29 17:58:10 +00:00
|
|
|
if not db_host:
|
2021-12-10 08:52:40 +00:00
|
|
|
db_host = config.get("db_host")
|
|
|
|
print("Environment variable POSTGRES_HOST not found.")
|
|
|
|
print("Using db_host from common_site_config.json")
|
2020-06-29 17:58:10 +00:00
|
|
|
|
|
|
|
sites_path = os.getcwd()
|
|
|
|
common_site_config_path = os.path.join(sites_path, COMMON_SITE_CONFIG_FILE)
|
2021-12-10 08:52:40 +00:00
|
|
|
update_site_config(
|
|
|
|
"root_login",
|
|
|
|
db_root_username,
|
|
|
|
validate=False,
|
|
|
|
site_config_path=common_site_config_path,
|
|
|
|
)
|
|
|
|
update_site_config(
|
|
|
|
"root_password",
|
|
|
|
db_root_password,
|
|
|
|
validate=False,
|
|
|
|
site_config_path=common_site_config_path,
|
|
|
|
)
|
2020-06-29 17:58:10 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
force = True if os.environ.get("FORCE", None) else False
|
|
|
|
install_apps = os.environ.get("INSTALL_APPS", None)
|
2021-12-10 08:52:40 +00:00
|
|
|
install_apps = install_apps.split(",") if install_apps else []
|
2020-03-20 15:26:23 +00:00
|
|
|
frappe.init(site_name, new_site=True)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-06-29 17:58:10 +00:00
|
|
|
if semantic_version.Version(frappe.__version__).major > 11:
|
|
|
|
_new_site(
|
|
|
|
None,
|
|
|
|
site_name,
|
2020-09-29 12:42:41 +00:00
|
|
|
mariadb_root_username=db_root_username,
|
|
|
|
mariadb_root_password=db_root_password,
|
2021-12-10 08:52:40 +00:00
|
|
|
admin_password=get_password("ADMIN_PASSWORD", "admin"),
|
2020-06-29 17:58:10 +00:00
|
|
|
verbose=True,
|
|
|
|
install_apps=install_apps,
|
|
|
|
source_sql=None,
|
|
|
|
force=force,
|
|
|
|
db_type=db_type,
|
|
|
|
reinstall=False,
|
|
|
|
db_host=db_host,
|
|
|
|
db_port=db_port,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
_new_site(
|
|
|
|
None,
|
|
|
|
site_name,
|
2020-09-29 12:42:41 +00:00
|
|
|
mariadb_root_username=db_root_username,
|
|
|
|
mariadb_root_password=db_root_password,
|
2021-12-10 08:52:40 +00:00
|
|
|
admin_password=get_password("ADMIN_PASSWORD", "admin"),
|
2020-06-29 17:58:10 +00:00
|
|
|
verbose=True,
|
|
|
|
install_apps=install_apps,
|
|
|
|
source_sql=None,
|
|
|
|
force=force,
|
|
|
|
reinstall=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
if db_type == "mariadb":
|
|
|
|
site_config = get_site_config(site_name)
|
2021-12-10 08:52:40 +00:00
|
|
|
db_name = site_config.get("db_name")
|
|
|
|
db_password = site_config.get("db_password")
|
2020-06-29 17:58:10 +00:00
|
|
|
|
2021-12-10 08:52:40 +00:00
|
|
|
mysql_command = [
|
|
|
|
"mysql",
|
|
|
|
f"-h{db_host}",
|
|
|
|
f"-u{db_root_username}",
|
|
|
|
f"-p{mariadb_root_password}",
|
|
|
|
"-e",
|
|
|
|
]
|
2020-06-29 17:58:10 +00:00
|
|
|
|
2020-07-10 13:51:59 +00:00
|
|
|
# Drop User if exists
|
2021-12-10 08:52:40 +00:00
|
|
|
command = mysql_command + [
|
|
|
|
f"DROP USER IF EXISTS '{db_name}'; FLUSH PRIVILEGES;"
|
|
|
|
]
|
2020-07-10 13:51:59 +00:00
|
|
|
run_command(command)
|
|
|
|
|
2020-10-29 05:08:51 +00:00
|
|
|
# Grant permission to database and set password
|
2020-07-16 00:36:48 +00:00
|
|
|
grant_privileges = "ALL PRIVILEGES"
|
2020-06-29 17:58:10 +00:00
|
|
|
|
2020-07-15 15:58:52 +00:00
|
|
|
# for Amazon RDS
|
2020-07-16 00:38:24 +00:00
|
|
|
if config.get(RDS_DB) or site_config.get(RDS_DB):
|
2020-07-16 00:36:48 +00:00
|
|
|
grant_privileges = RDS_PRIVILEGES
|
|
|
|
|
2021-12-10 08:52:40 +00:00
|
|
|
command = mysql_command + [
|
|
|
|
f"\
|
2020-11-24 07:38:28 +00:00
|
|
|
CREATE USER IF NOT EXISTS '{db_name}'@'%' IDENTIFIED BY '{db_password}'; \
|
|
|
|
GRANT {grant_privileges} ON `{db_name}`.* TO '{db_name}'@'%'; \
|
2021-12-10 08:52:40 +00:00
|
|
|
FLUSH PRIVILEGES;"
|
|
|
|
]
|
2020-07-16 00:36:48 +00:00
|
|
|
run_command(command)
|
2020-07-15 15:58:52 +00:00
|
|
|
|
2020-06-29 17:58:10 +00:00
|
|
|
if frappe.redis_server:
|
|
|
|
frappe.redis_server.connection_pool.disconnect()
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
exit(0)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|