mirror of
https://github.com/frappe/frappe_docker.git
synced 2025-02-03 12:38:29 +00:00
Merge pull request #305 from Thunderbottom/improve-commands
fix: use subprocess instead of os to run commands
This commit is contained in:
commit
84b111414f
@ -5,6 +5,7 @@ import semantic_version
|
|||||||
from frappe.commands.site import _new_site
|
from frappe.commands.site import _new_site
|
||||||
from frappe.installer import update_site_config
|
from frappe.installer import update_site_config
|
||||||
from check_connection import get_config, get_site_config, COMMON_SITE_CONFIG_FILE
|
from check_connection import get_config, get_site_config, COMMON_SITE_CONFIG_FILE
|
||||||
|
from utils import run_command
|
||||||
|
|
||||||
|
|
||||||
def get_password(env_var, default=None):
|
def get_password(env_var, default=None):
|
||||||
@ -91,31 +92,26 @@ def main():
|
|||||||
|
|
||||||
if db_type == "mariadb":
|
if db_type == "mariadb":
|
||||||
site_config = get_site_config(site_name)
|
site_config = get_site_config(site_name)
|
||||||
|
db_name = site_config.get('db_name')
|
||||||
|
db_password = site_config.get('db_password')
|
||||||
|
|
||||||
mysql_command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
|
mysql_command = ["mysql", f"-h{db_host}", f"-u{mariadb_root_username}", f"-p{mariadb_root_password}", "-e"]
|
||||||
db_host=config.get('db_host'),
|
|
||||||
mariadb_root_username=mariadb_root_username,
|
# Drop User if exists
|
||||||
mariadb_root_password=mariadb_root_password
|
command = mysql_command + [f"DROP USER IF EXISTS '{db_name}'@'%'; FLUSH PRIVILEGES;"]
|
||||||
)
|
run_command(command)
|
||||||
|
|
||||||
# update User's host to '%' required to connect from any container
|
# 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(
|
command = mysql_command + [f"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;"]
|
||||||
db_name=site_config.get('db_name')
|
run_command(command)
|
||||||
)
|
|
||||||
os.system(command)
|
|
||||||
|
|
||||||
# Set db password
|
# Set db password
|
||||||
command = mysql_command + "\"ALTER USER '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;\"".format(
|
command = mysql_command + [f"ALTER USER '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"]
|
||||||
db_name=site_config.get('db_name'),
|
run_command(command)
|
||||||
db_password=site_config.get('db_password')
|
|
||||||
)
|
|
||||||
os.system(command)
|
|
||||||
|
|
||||||
# Grant permission to database
|
# Grant permission to database
|
||||||
command = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format(
|
command = mysql_command + [f"GRANT ALL PRIVILEGES ON `{db_name}`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;"]
|
||||||
db_name=site_config.get('db_name')
|
run_command(command)
|
||||||
)
|
|
||||||
os.system(command)
|
|
||||||
|
|
||||||
if frappe.redis_server:
|
if frappe.redis_server:
|
||||||
frappe.redis_server.connection_pool.disconnect()
|
frappe.redis_server.connection_pool.disconnect()
|
||||||
|
@ -8,6 +8,7 @@ import boto3
|
|||||||
|
|
||||||
from new import get_password
|
from new import get_password
|
||||||
from push_backup import DATE_FORMAT, check_environment_variables
|
from push_backup import DATE_FORMAT, check_environment_variables
|
||||||
|
from utils import run_command
|
||||||
from frappe.utils import get_sites, random_string
|
from frappe.utils import get_sites, random_string
|
||||||
from frappe.installer import make_conf, get_conf_params, make_site_dirs, update_site_config
|
from frappe.installer import make_conf, get_conf_params, make_site_dirs, update_site_config
|
||||||
from check_connection import get_site_config, get_config, COMMON_SITE_CONFIG_FILE
|
from check_connection import get_site_config, get_config, COMMON_SITE_CONFIG_FILE
|
||||||
@ -28,21 +29,17 @@ def get_backup_dir():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def decompress_db(files_base, site):
|
def decompress_db(database_file, site):
|
||||||
database_file = files_base + '-database.sql.gz'
|
command = ["gunzip", "-c", database_file]
|
||||||
command = 'gunzip -c {database_file} > {database_extract}'.format(
|
with open(database_file.replace(".gz", ""), "w") as db_file:
|
||||||
database_file=database_file,
|
print('Extract Database GZip for site {}'.format(site))
|
||||||
database_extract=database_file.replace('.gz', '')
|
run_command(command, stdout=db_file)
|
||||||
)
|
|
||||||
|
|
||||||
print('Extract Database GZip for site {}'.format(site))
|
|
||||||
os.system(command)
|
|
||||||
|
|
||||||
|
|
||||||
def restore_database(files_base, site_config_path, site):
|
def restore_database(files_base, site_config_path, site):
|
||||||
# restore database
|
# restore database
|
||||||
database_file = files_base + '-database.sql.gz'
|
database_file = files_base + '-database.sql.gz'
|
||||||
decompress_db(files_base, site)
|
decompress_db(database_file, site)
|
||||||
config = get_config()
|
config = get_config()
|
||||||
|
|
||||||
# Set db_type if it exists in backup site_config.json
|
# Set db_type if it exists in backup site_config.json
|
||||||
@ -203,28 +200,17 @@ def restore_postgres(config, site_config, database_file):
|
|||||||
db_name = site_config.get('db_name')
|
db_name = site_config.get('db_name')
|
||||||
db_password = site_config.get('db_password')
|
db_password = site_config.get('db_password')
|
||||||
|
|
||||||
psql_command = "psql postgres://{root_login}:{root_password}@{db_host}:{db_port}".format(
|
psql_command = ["psql"]
|
||||||
root_login=db_root_user,
|
psql_uri = f"postgres://{db_root_user}:{db_root_password}@{db_host}:{db_port}"
|
||||||
root_password=db_root_password,
|
|
||||||
db_host=db_host,
|
|
||||||
db_port=db_port
|
|
||||||
)
|
|
||||||
|
|
||||||
print('Restoring PostgreSQL')
|
print('Restoring PostgreSQL')
|
||||||
os.system(psql_command + ' -c "DROP DATABASE IF EXISTS \"{db_name}\""'.format(db_name=db_name))
|
run_command(psql_command + [psql_uri, "-c", f"DROP DATABASE IF EXISTS \"{db_name}\""])
|
||||||
os.system(psql_command + ' -c "DROP USER IF EXISTS {db_name}"'.format(db_name=db_name))
|
run_command(psql_command + [psql_uri, "-c", f"DROP USER IF EXISTS {db_name}"])
|
||||||
os.system(psql_command + ' -c "CREATE DATABASE \"{db_name}\""'.format(db_name=db_name))
|
run_command(psql_command + [psql_uri, "-c", f"CREATE DATABASE \"{db_name}\""])
|
||||||
os.system(psql_command + ' -c "CREATE user {db_name} password \'{db_password}\'"'.format(
|
run_command(psql_command + [psql_uri, "-c", f"CREATE user {db_name} password '{db_password}'"])
|
||||||
db_name=db_name,
|
run_command(psql_command + [psql_uri, "-c", f"GRANT ALL PRIVILEGES ON DATABASE \"{db_name}\" TO {db_name}"])
|
||||||
db_password=db_password))
|
with open(database_file.replace('.gz', ''), 'r') as db_file:
|
||||||
os.system(psql_command + ' -c "GRANT ALL PRIVILEGES ON DATABASE \"{db_name}\" TO {db_name}"'.format(
|
run_command(psql_command + [f"{psql_uri}/{db_name}", "<"], stdin=db_file)
|
||||||
db_name=db_name))
|
|
||||||
|
|
||||||
os.system("{psql_command}/{db_name} < {database_file}".format(
|
|
||||||
psql_command=psql_command,
|
|
||||||
database_file=database_file.replace('.gz', ''),
|
|
||||||
db_name=db_name,
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
def restore_mariadb(config, site_config, database_file):
|
def restore_mariadb(config, site_config, database_file):
|
||||||
@ -236,54 +222,32 @@ def restore_mariadb(config, site_config, database_file):
|
|||||||
db_root_user = os.environ.get("DB_ROOT_USER", 'root')
|
db_root_user = os.environ.get("DB_ROOT_USER", 'root')
|
||||||
|
|
||||||
db_host = site_config.get('db_host', config.get('db_host'))
|
db_host = site_config.get('db_host', config.get('db_host'))
|
||||||
db_port = site_config.get('db_port', config.get('db_port'))
|
db_port = site_config.get('db_port', config.get('db_port', 3306))
|
||||||
|
db_name = site_config.get('db_name')
|
||||||
|
db_password = site_config.get('db_password')
|
||||||
|
|
||||||
# mysql command prefix
|
# mysql command prefix
|
||||||
mysql_command = 'mysql -u{db_root_user} -h{db_host} -p{db_password}'.format(
|
mysql_command = ["mysql", f"-u{db_root_user}", f"-h{db_host}", f"-p{db_root_password}", f"-P{db_port}"]
|
||||||
db_root_user=db_root_user,
|
|
||||||
db_host=db_host,
|
|
||||||
db_port=db_port,
|
|
||||||
db_password=db_root_password
|
|
||||||
)
|
|
||||||
|
|
||||||
# drop db if exists for clean restore
|
# drop db if exists for clean restore
|
||||||
drop_database = "{mysql_command} -e \"DROP DATABASE IF EXISTS \`{db_name}\`;\"".format(
|
drop_database = mysql_command + ["-e", f"DROP DATABASE IF EXISTS `{db_name}`;"]
|
||||||
mysql_command=mysql_command,
|
run_command(drop_database)
|
||||||
db_name=site_config.get('db_name'),
|
|
||||||
)
|
|
||||||
os.system(drop_database)
|
|
||||||
|
|
||||||
# create db
|
# create db
|
||||||
create_database = "{mysql_command} -e \"CREATE DATABASE IF NOT EXISTS \`{db_name}\`;\"".format(
|
create_database = mysql_command + ["-e", f"CREATE DATABASE IF NOT EXISTS `{db_name}`;"]
|
||||||
mysql_command=mysql_command,
|
run_command(create_database)
|
||||||
db_name=site_config.get('db_name'),
|
|
||||||
)
|
|
||||||
os.system(create_database)
|
|
||||||
|
|
||||||
# create user
|
# create user
|
||||||
create_user = "{mysql_command} -e \"CREATE USER IF NOT EXISTS \'{db_name}\'@\'%\' IDENTIFIED BY \'{db_password}\'; FLUSH PRIVILEGES;\"".format(
|
create_user = mysql_command + ["-e", f"CREATE USER IF NOT EXISTS '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"]
|
||||||
mysql_command=mysql_command,
|
run_command(create_user)
|
||||||
db_name=site_config.get('db_name'),
|
|
||||||
db_password=site_config.get('db_password'),
|
|
||||||
)
|
|
||||||
os.system(create_user)
|
|
||||||
|
|
||||||
# grant db privileges to user
|
# grant db privileges to user
|
||||||
grant_privileges = "{mysql_command} -e \"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;\"".format(
|
grant_privileges = mysql_command + ["-e", f"GRANT ALL PRIVILEGES ON `{db_name}`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"]
|
||||||
mysql_command=mysql_command,
|
run_command(grant_privileges)
|
||||||
db_name=site_config.get('db_name'),
|
|
||||||
db_password=site_config.get('db_password'),
|
|
||||||
)
|
|
||||||
os.system(grant_privileges)
|
|
||||||
|
|
||||||
command = "{mysql_command} '{db_name}' < {database_file}".format(
|
|
||||||
mysql_command=mysql_command,
|
|
||||||
db_name=site_config.get('db_name'),
|
|
||||||
database_file=database_file.replace('.gz', ''),
|
|
||||||
)
|
|
||||||
|
|
||||||
print('Restoring MariaDB')
|
print('Restoring MariaDB')
|
||||||
os.system(command)
|
with open(database_file.replace('.gz', ''), 'r') as db_file:
|
||||||
|
run_command(mysql_command + [f"{db_name}"], stdin=db_file)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
15
build/common/commands/utils.py
Normal file
15
build/common/commands/utils.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def run_command(command, stdout=None, stdin=None, stderr=None):
|
||||||
|
stdout = stdout or subprocess.PIPE
|
||||||
|
stderr = stderr or subprocess.PIPE
|
||||||
|
stdin = stdin or subprocess.PIPE
|
||||||
|
process = subprocess.Popen(command, stdout=stdout, stdin=stdin, stderr=stderr)
|
||||||
|
out, error = process.communicate()
|
||||||
|
if process.returncode:
|
||||||
|
print("Something went wrong:")
|
||||||
|
print(f"return code: {process.returncode}")
|
||||||
|
print(f"stdout:\n{out}")
|
||||||
|
print(f"\nstderr:\n{error}")
|
||||||
|
exit(process.returncode)
|
Loading…
x
Reference in New Issue
Block a user