2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2024-11-09 23:00:56 +00:00

fix: make python commands modular and reusable

This commit is contained in:
Revant Nandgaonkar 2020-03-20 20:56:23 +05:30
parent 8ce9d4214d
commit 7c356ccb27
8 changed files with 112 additions and 95 deletions

View File

@ -1,7 +1,10 @@
import frappe
from frappe.utils.scheduler import start_scheduler
print("Starting background scheduler . . .")
start_scheduler()
def main():
print("Starting background scheduler . . .")
start_scheduler()
exit(0)
exit(0)
if __name__ == "__main__":
main()

View File

@ -20,10 +20,13 @@ def backup(sites, with_files=False):
print("private files backup taken -", odb.backup_path_private_files, "- on", now())
frappe.destroy()
installed_sites = ":".join(get_sites())
sites = os.environ.get("SITES", installed_sites).split(":")
with_files=True if os.environ.get("WITH_FILES") else False
def main():
installed_sites = ":".join(get_sites())
sites = os.environ.get("SITES", installed_sites).split(":")
with_files=True if os.environ.get("WITH_FILES") else False
backup(sites, with_files)
backup(sites, with_files)
exit(0)
exit(0)
if __name__ == "__main__":
main()

View File

@ -108,6 +108,13 @@ def check_redis_socketio(retry=10, delay=3, print_attempt=True):
print("Connection to redis socketio timed out")
exit(1)
# Get site_config.json
def get_site_config(site_name):
site_config = None
with open('{site_name}/site_config.json'.format(site_name=site_name)) as site_config_file:
site_config = json.load(site_config_file)
return site_config
def main():
check_mariadb()
check_redis_queue()
@ -116,4 +123,4 @@ def main():
print('Connections OK')
if __name__ == "__main__":
main()
main()

View File

@ -20,6 +20,6 @@ def console(site):
print("Apps in this namespace:\n{}".format(", ".join(all_apps)))
IPython.embed(display_banner="", header="")
site = sys.argv[-1]
console(site)
def main():
site = sys.argv[-1]
console(site)

View File

@ -23,4 +23,4 @@ def main():
exit(0)
if __name__ == "__main__":
main()
main()

View File

@ -2,12 +2,7 @@ import os, frappe, compileall, re, json
from frappe.migrate import migrate
from frappe.utils import get_sites
def get_config():
config = None
with open('common_site_config.json') as config_file:
config = json.load(config_file)
return config
from check_connection import get_config
def save_config(config):
with open('common_site_config.json', 'w') as f:
@ -24,24 +19,30 @@ def set_maintenance_mode(enable=True):
conf.update({ "maintenance_mode": 0, "pause_scheduler": 0 })
save_config(conf)
def migrate_sites(maintenance_mode=False):
installed_sites = ":".join(get_sites())
sites = os.environ.get("SITES", installed_sites).split(":")
if not maintenance_mode:
maintenance_mode = True if os.environ.get("MAINTENANCE_MODE") else False
installed_sites = ":".join(get_sites())
sites = os.environ.get("SITES", installed_sites).split(":")
maintenance_mode = True if os.environ.get("MAINTENANCE_MODE") else False
if maintenance_mode:
set_maintenance_mode(True)
if maintenance_mode:
set_maintenance_mode(True)
for site in sites:
print('Migrating', site)
frappe.init(site=site)
frappe.connect()
try:
migrate()
finally:
frappe.destroy()
for site in sites:
print('Migrating', site)
frappe.init(site=site)
frappe.connect()
try:
migrate()
finally:
frappe.destroy()
if maintenance_mode:
set_maintenance_mode(False)
if maintenance_mode:
set_maintenance_mode(False)
def main():
migrate_sites()
exit(0)
exit(0)
if __name__ == "__main__":
main()

View File

@ -1,68 +1,68 @@
import os, frappe, json
from frappe.commands.site import _new_site
from check_connection import get_config, get_site_config
site_name = os.environ.get("SITE_NAME", 'site1.localhost')
mariadb_root_username = os.environ.get("DB_ROOT_USER", 'root')
mariadb_root_password = os.environ.get("MYSQL_ROOT_PASSWORD", 'admin')
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)
def main():
site_name = os.environ.get("SITE_NAME", 'site1.localhost')
mariadb_root_username = os.environ.get("DB_ROOT_USER", 'root')
mariadb_root_password = os.environ.get("MYSQL_ROOT_PASSWORD", 'admin')
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)
_new_site(
None,
site_name,
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password,
admin_password=os.environ.get("ADMIN_PASSWORD", 'admin'),
verbose=True,
install_apps=install_apps,
source_sql=None,
force=force,
reinstall=False,
)
_new_site(
None,
site_name,
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password,
admin_password=os.environ.get("ADMIN_PASSWORD", 'admin'),
verbose=True,
install_apps=install_apps,
source_sql=None,
force=force,
reinstall=False,
)
config = None
with open('common_site_config.json') as config_file:
config = json.load(config_file)
config = get_config()
site_config = None
with open('{site_name}/site_config.json'.format(site_name=site_name)) as site_config_file:
site_config = json.load(site_config_file)
site_config = get_site_config(site_name)
# update User's host to '%' required to connect from any container
command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
db_host=config.get('db_host'),
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password
)
command += "\"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;\"".format(
db_name=site_config.get('db_name')
)
os.system(command)
# update User's host to '%' required to connect from any container
command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
db_host=config.get('db_host'),
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password
)
command += "\"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;\"".format(
db_name=site_config.get('db_name')
)
os.system(command)
# Set db password
command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
db_host=config.get('db_host'),
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password
)
command += "\"SET PASSWORD FOR '{db_name}'@'%' = PASSWORD('{db_password}'); FLUSH PRIVILEGES;\"".format(
db_name=site_config.get('db_name'),
db_password=site_config.get('db_password')
)
os.system(command)
# Set db password
command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
db_host=config.get('db_host'),
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password
)
command += "\"SET PASSWORD FOR '{db_name}'@'%' = PASSWORD('{db_password}'); FLUSH PRIVILEGES;\"".format(
db_name=site_config.get('db_name'),
db_password=site_config.get('db_password')
)
os.system(command)
# Grant permission to database
command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
db_host=config.get('db_host'),
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password
)
command += "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format(
db_name=site_config.get('db_name')
)
os.system(command)
# Grant permission to database
command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format(
db_host=config.get('db_host'),
mariadb_root_username=mariadb_root_username,
mariadb_root_password=mariadb_root_password
)
command += "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format(
db_name=site_config.get('db_name')
)
os.system(command)
exit(0)
exit(0)
if __name__ == "__main__":
main()

View File

@ -1,7 +1,10 @@
import os, frappe
from frappe.utils.background_jobs import start_worker
queue = os.environ.get("WORKER_TYPE", "default")
start_worker(queue, False)
def main():
queue = os.environ.get("WORKER_TYPE", "default")
start_worker(queue, False)
exit(0)
exit(0)
if __name__ == "__main__":
main()