2020-04-28 20:15:59 +00:00
|
|
|
import os
|
|
|
|
import frappe
|
2020-02-10 08:00:54 +00:00
|
|
|
|
|
|
|
from frappe.commands.site import _new_site
|
2020-03-20 15:26:23 +00:00
|
|
|
from check_connection import get_config, get_site_config
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-04-16 06:19:40 +00:00
|
|
|
def get_password(env_var, default=None):
|
|
|
|
return os.environ.get(env_var) or _get_password_from_secret(f"{env_var}_FILE") or default
|
|
|
|
|
|
|
|
|
|
|
|
def _get_password_from_secret(env_var):
|
2020-04-28 20:15:59 +00:00
|
|
|
"""Fetches the secret value from the docker secret file
|
2020-04-16 06:19:40 +00:00
|
|
|
usually located inside /run/secrets/
|
|
|
|
Arguments:
|
2020-04-28 20:15:59 +00:00
|
|
|
env_var {str} -- Name of the environment variable
|
2020-04-16 06:19:40 +00:00
|
|
|
containing the path to the secret file.
|
|
|
|
Returns:
|
|
|
|
[str] -- Secret value
|
|
|
|
"""
|
|
|
|
passwd = None
|
|
|
|
secret_file_path = os.environ.get(env_var)
|
|
|
|
if secret_file_path:
|
|
|
|
with open(secret_file_path) as secret_file:
|
|
|
|
passwd = secret_file.read().strip()
|
|
|
|
|
|
|
|
return passwd
|
|
|
|
|
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
def main():
|
|
|
|
site_name = os.environ.get("SITE_NAME", 'site1.localhost')
|
|
|
|
mariadb_root_username = os.environ.get("DB_ROOT_USER", 'root')
|
2020-04-16 06:19:40 +00:00
|
|
|
mariadb_root_password = get_password("MYSQL_ROOT_PASSWORD", 'admin')
|
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)
|
|
|
|
install_apps = install_apps.split(',') if install_apps else []
|
|
|
|
frappe.init(site_name, new_site=True)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
_new_site(
|
|
|
|
None,
|
|
|
|
site_name,
|
|
|
|
mariadb_root_username=mariadb_root_username,
|
|
|
|
mariadb_root_password=mariadb_root_password,
|
2020-04-16 06:19:40 +00:00
|
|
|
admin_password=get_password("ADMIN_PASSWORD", 'admin'),
|
2020-03-20 15:26:23 +00:00
|
|
|
verbose=True,
|
|
|
|
install_apps=install_apps,
|
|
|
|
source_sql=None,
|
|
|
|
force=force,
|
|
|
|
reinstall=False,
|
|
|
|
)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
config = get_config()
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
site_config = get_site_config(site_name)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-27 10:37:12 +00:00
|
|
|
mysql_command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
|
2020-03-20 15:26:23 +00:00
|
|
|
db_host=config.get('db_host'),
|
|
|
|
mariadb_root_username=mariadb_root_username,
|
|
|
|
mariadb_root_password=mariadb_root_password
|
|
|
|
)
|
2020-03-27 10:37:12 +00:00
|
|
|
|
|
|
|
# update User's host to '%' required to connect from any container
|
|
|
|
command = mysql_command + "\"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;\"".format(
|
2020-03-20 15:26:23 +00:00
|
|
|
db_name=site_config.get('db_name')
|
|
|
|
)
|
|
|
|
os.system(command)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
# Set db password
|
2020-04-29 08:33:11 +00:00
|
|
|
command = mysql_command + "\"ALTER USER '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;\"".format(
|
2020-03-20 15:26:23 +00:00
|
|
|
db_name=site_config.get('db_name'),
|
|
|
|
db_password=site_config.get('db_password')
|
|
|
|
)
|
|
|
|
os.system(command)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-03-20 15:26:23 +00:00
|
|
|
# Grant permission to database
|
2020-04-29 06:14:58 +00:00
|
|
|
command = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format(
|
2020-03-20 15:26:23 +00:00
|
|
|
db_name=site_config.get('db_name')
|
|
|
|
)
|
|
|
|
os.system(command)
|
|
|
|
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()
|