[feature] add bench auto-deploy command (#4894)

This commit is contained in:
Rushabh Mehta 2018-01-23 15:18:51 +05:30 committed by GitHub
parent 1fbde5c85d
commit 7965deaae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 18 deletions

View file

@ -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
]

View file

@ -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 ''

18
frappe/utils/gitutils.py Normal file
View file

@ -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 ''