From 674c7664804525f8754d3557936b9c8fe30ea762 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Sat, 21 Mar 2020 15:47:35 +0530 Subject: [PATCH] feat: auto migrate on container start if AUTO_MIGRATE set --- build/common/commands/auto_migrate.py | 110 +++++++++++++++++++++++ build/common/worker/docker-entrypoint.sh | 4 + installation/docker-compose-erpnext.yml | 1 + installation/docker-compose-frappe.yml | 1 + 4 files changed, 116 insertions(+) create mode 100644 build/common/commands/auto_migrate.py diff --git a/build/common/commands/auto_migrate.py b/build/common/commands/auto_migrate.py new file mode 100644 index 00000000..55124f0b --- /dev/null +++ b/build/common/commands/auto_migrate.py @@ -0,0 +1,110 @@ +import os +import json +import semantic_version +import git + +from migrate import migrate_sites +from check_connection import get_config + +APP_VERSIONS_JSON_FILE = 'app_versions.json' +APPS_TXT_FILE = 'apps.txt' + +def save_version_file(versions): + with open(APP_VERSIONS_JSON_FILE, 'w') as f: + return json.dump(versions, f, indent=1, sort_keys=True) + +def get_apps(): + apps = [] + try: + with open(APPS_TXT_FILE) as apps_file: + for app in apps_file.readlines(): + if app.strip(): + apps.append(app.strip()) + + except FileNotFoundError as exception: + print(exception) + exit(1) + except: + print(APPS_TXT_FILE+" is not valid") + exit(1) + + return apps + +def get_container_versions(apps): + versions = {} + for app in apps: + try: + version = __import__(app).__version__ + versions.update({app:version}) + except: + pass + + try: + path = os.path.join('..','apps', app) + repo = git.Repo(path) + commit_hash = repo.head.object.hexsha + versions.update({app+'_git_hash':commit_hash}) + except: + pass + + return versions + +def get_version_file(): + versions = None + try: + with open(APP_VERSIONS_JSON_FILE) as versions_file: + versions = json.load(versions_file) + except: + pass + return versions + +def main(): + is_ready = False + apps = get_apps() + + container_versions = get_container_versions(apps) + + version_file = get_version_file() + + if not version_file: + version_file = container_versions + save_version_file(version_file) + is_ready = True + + for app in apps: + container_version = None + file_version = None + version_file_hash = None + container_hash = None + + repo = git.Repo(os.path.join('..','apps',app)) + branch = repo.active_branch.name + + if branch == 'develop': + version_file_hash = version_file.get(app+'_git_hash') + container_hash = container_versions.get(app+'_git_hash') + if container_hash and version_file_hash: + if container_hash != version_file_hash: + is_ready = True + break + + if version_file.get(app): + file_version = semantic_version.Version(version_file.get(app)) + + if container_versions.get(app): + container_version = semantic_version.Version(container_versions.get(app)) + + if file_version and container_version: + if container_version > file_version: + is_ready = True + break + + config = get_config() + + if is_ready and config.get('maintenance_mode') != 1: + migrate_sites(maintenance_mode=True) + version_file = container_versions + save_version_file(version_file) + +if __name__ == "__main__": + main() diff --git a/build/common/worker/docker-entrypoint.sh b/build/common/worker/docker-entrypoint.sh index bf8a90c2..26cb9ef1 100755 --- a/build/common/worker/docker-entrypoint.sh +++ b/build/common/worker/docker-entrypoint.sh @@ -76,6 +76,10 @@ if [ "$1" = 'start' ]; then export FRAPPE_PORT=8000 fi + if [[ ! -z "$AUTO_MIGRATE" ]]; then + su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/auto_migrate.py" + fi if [[ -z "$RUN_AS_ROOT" ]]; then su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ diff --git a/installation/docker-compose-erpnext.yml b/installation/docker-compose-erpnext.yml index f932a512..8e2cbd9e 100644 --- a/installation/docker-compose-erpnext.yml +++ b/installation/docker-compose-erpnext.yml @@ -37,6 +37,7 @@ services: - REDIS_QUEUE=redis-queue:6379 - REDIS_SOCKETIO=redis-socketio:6379 - SOCKETIO_PORT=9000 + - AUTO_MIGRATE=1 volumes: - ./sites:/home/frappe/frappe-bench/sites:rw - assets-vol:/home/frappe/frappe-bench/sites/assets:rw diff --git a/installation/docker-compose-frappe.yml b/installation/docker-compose-frappe.yml index df1e7c43..f8365225 100644 --- a/installation/docker-compose-frappe.yml +++ b/installation/docker-compose-frappe.yml @@ -37,6 +37,7 @@ services: - REDIS_QUEUE=redis-queue:6379 - REDIS_SOCKETIO=redis-socketio:6379 - SOCKETIO_PORT=9000 + - AUTO_MIGRATE=1 volumes: - ./sites:/home/frappe/frappe-bench/sites:rw - assets-vol:/home/frappe/frappe-bench/sites/assets:rw