diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index ff606d940d..e6ca590fcc 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -1,6 +1,8 @@ +# -*- coding: utf-8 -*- + from __future__ import unicode_literals, absolute_import, print_function import click -import json, os, sys +import json, os, sys, subprocess from distutils.spawn import find_executable import frappe from frappe.commands import pass_context, get_site @@ -508,6 +510,48 @@ def rebuild_global_search(context): finally: frappe.destroy() +@click.command('auto-deploy') +@click.argument('app') +@click.option('--migrate', is_flag=True, default=False, help='Migrate after pulling') +@click.option('--restart', is_flag=True, default=False, help='Restart after migration') +@click.option('--remote', default='upstream', help='Remote, default is "upstream"') +@pass_context +def auto_deploy(context, app, migrate=False, restart=False, remote='upstream'): + '''Pull and migrate sites that have new version''' + from frappe.utils.gitutils import get_app_branch + from frappe.utils import get_sites + + branch = get_app_branch(app) + app_path = frappe.get_app_path(app) + + # fetch + subprocess.check_output(['git', 'fetch', remote, branch], cwd = app_path) + + # get diff + if subprocess.check_output(['git', 'diff', '{0}..upstream/{0}'.format(branch)], cwd = app_path): + print('✔ Updates found for {0}'.format(app)) + if app=='frappe': + # run bench update + subprocess.check_output(['bench', 'update', '--no-backup'], cwd = '..') + else: + updated = False + subprocess.check_output(['git', 'pull', '--rebase', 'upstream', branch], + cwd = app_path) + # find all sites with that app + for site in get_sites(): + frappe.init(site) + if app in frappe.get_installed_apps(): + print('Updating {0}'.format(site)) + updated = True + subprocess.check_output(['bench', '--site', site, 'clear-cache'], cwd = '..') + if migrate: + subprocess.check_output(['bench', '--site', site, 'migrate'], cwd = '..') + frappe.destroy() + + if updated and restart: + subprocess.check_output(['bench', 'restart'], cwd = '..') + else: + print('No Updates') commands = [ build, @@ -537,5 +581,6 @@ commands = [ add_to_email_queue, setup_global_help, setup_help, - rebuild_global_search + rebuild_global_search, + auto_deploy ] diff --git a/frappe/utils/change_log.py b/frappe/utils/change_log.py index 322b241d03..182a2a10b2 100644 --- a/frappe/utils/change_log.py +++ b/frappe/utils/change_log.py @@ -3,10 +3,11 @@ from __future__ import unicode_literals from six.moves import range -import json, subprocess, os +import json, os from semantic_version import Version import frappe from frappe.utils import cstr +from frappe.utils.gitutils import get_app_last_commit_ref, get_app_branch def get_change_log(user=None): if not user: user = frappe.session.user @@ -109,18 +110,3 @@ def get_versions(): versions[app]["version"] = '0.0.1' return versions - -def get_app_branch(app): - '''Returns branch of an app''' - try: - return subprocess.check_output('cd ../apps/{0} && git rev-parse --abbrev-ref HEAD'.format(app), - shell=True).strip() - except Exception as e: - return '' - -def get_app_last_commit_ref(app): - try: - return subprocess.check_output('cd ../apps/{0} && git rev-parse HEAD'.format(app), - shell=True).strip()[:7] - except Exception as e: - return '' diff --git a/frappe/utils/gitutils.py b/frappe/utils/gitutils.py new file mode 100644 index 0000000000..0d672885c0 --- /dev/null +++ b/frappe/utils/gitutils.py @@ -0,0 +1,18 @@ +from __future__ import unicode_literals + +import subprocess + +def get_app_branch(app): + '''Returns branch of an app''' + try: + return subprocess.check_output('cd ../apps/{0} && git rev-parse --abbrev-ref HEAD'.format(app), + shell=True).strip() + except Exception: + return '' + +def get_app_last_commit_ref(app): + try: + return subprocess.check_output('cd ../apps/{0} && git rev-parse HEAD'.format(app), + shell=True).strip()[:7] + except Exception: + return ''