Skip to content

Commit

Permalink
Fix (master/develop) branch checkout from config-general (#8399)
Browse files Browse the repository at this point in the history
* Add apiv2 config operation route, for switching branch.

* Do not initiate a restart.

* Change notification message.

* Remove updating from log message.
* It's a backup, not necessarily used for updates.

* github_updater.py: Add set-remote, to also be able to update other branches then master / develop.
* Fix missing import.

* Fix checking out branch.
* Only check dbUpgrade for master and develop (current) branch.

* Disable the actual switch branch.

* Move Restart to vue component
* Add apiv2 route for system operation calls.
* Add restart as first operation

* Move shutdown to apiv2.

* Remove all the commended code.
This should make it end-2-end functional.

* Add modal.css

* increase timeout.

* Remove result, as i'm not using it "yet".

* remove restart.mako

* Add restart.vue component.
* Fix lint errors

* Move operation 'CHECKOUT_BRANCH' to system handler.

* Fix remote github branches sometime not loading.
Race issue.

* Fix system handler.

* Fix lint errors

* Revert trailing comma.

* Fix git branches not shown when navigating from other page through router to config-general.vue

* bundle runtime

* (re)load the default page after a restart.
For the theme reload. Reload and force getting data from backend.

* fix hard browser reload on chrome
  • Loading branch information
p0psicles authored Sep 1, 2020
1 parent 7be4f95 commit 71449b5
Show file tree
Hide file tree
Showing 21 changed files with 708 additions and 380 deletions.
7 changes: 3 additions & 4 deletions medusa/server/api/v2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ConfigHandler(BaseRequestHandler):
#: path param
path_param = ('path_param', r'\w+')
#: allowed HTTP methods
allowed_methods = ('GET', 'PATCH', )
allowed_methods = ('GET', 'PATCH',)
#: patch mapping
patches = {
# Main
Expand Down Expand Up @@ -552,11 +552,10 @@ def patch(self, identifier, *args, **kwargs):
app.instance.save_config()

# Push an update to any open Web UIs through the WebSocket
msg = ws.Message('configUpdated', {
ws.Message('configUpdated', {
'section': identifier,
'config': DataGenerator.get_data(identifier)
})
msg.push()
}).push()

return self._ok(data=accepted)

Expand Down
70 changes: 70 additions & 0 deletions medusa/server/api/v2/system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# coding=utf-8
"""Request handler for statistics."""
from __future__ import unicode_literals

from medusa import app, ui
from medusa.server.api.v2.base import BaseRequestHandler
from medusa.system.restart import Restart
from medusa.system.shutdown import Shutdown
from medusa.updater.version_checker import CheckVersion

from tornado.escape import json_decode


class SystemHandler(BaseRequestHandler):
"""System operation calls request handler."""

#: resource name
name = 'system'
#: identifier
identifier = ('identifier', r'\w+')
#: path param
path_param = None
#: allowed HTTP methods
allowed_methods = ('POST', )

def post(self, identifier, *args, **kwargs):
"""Perform an operation on the config."""
if identifier != 'operation':
return self._bad_request('Invalid operation')

data = json_decode(self.request.body)

if data['type'] == 'RESTART' and data['pid']:
if not Restart.restart(data['pid']):
self._not_found('Pid does not match running pid')
return self._created()

if data['type'] == 'SHUTDOWN' and data['pid']:
if not Shutdown.stop(data['pid']):
self._not_found('Pid does not match running pid')
return self._created()

if data['type'] == 'CHECKOUT_BRANCH' and data['branch']:
if app.BRANCH != data['branch']:
app.BRANCH = data['branch']
ui.notifications.message('Checking out branch: ', data['branch'])

if self._update(data['branch']):
return self._created()
else:
return self._bad_request('Update failed')
else:
ui.notifications.message('Already on branch: ', data['branch'])
return self._bad_request('Already on branch')

return self._bad_request('Invalid operation')

def _update(self, branch):
checkversion = CheckVersion()
backup = checkversion.updater and checkversion._runbackup() # pylint: disable=protected-access

if backup is True:
if branch:
checkversion.updater.branch = branch

if checkversion.updater.need_update() and checkversion.updater.update():
return True
else:
ui.notifications.message("Update wasn't successful. Check your log for more information.", branch)
return False
4 changes: 4 additions & 0 deletions medusa/server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from medusa.server.api.v2.series_legacy import SeriesLegacyHandler
from medusa.server.api.v2.series_operation import SeriesOperationHandler
from medusa.server.api.v2.stats import StatsHandler
from medusa.server.api.v2.system import SystemHandler
from medusa.server.web import (
CalendarHandler,
KeyHandler,
Expand Down Expand Up @@ -127,6 +128,9 @@ def get_apiv2_handlers(base):
# /api/v2/alias
AliasHandler.create_app_handler(base),

# /api/v2/system
SystemHandler.create_app_handler(base),

# /api/v2/authenticate
AuthHandler.create_app_handler(base),

Expand Down
28 changes: 12 additions & 16 deletions medusa/server/web/home/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@
WebRoot,
)
from medusa.show.show import Show
from medusa.system.restart import Restart
from medusa.system.shutdown import Shutdown
from medusa.tv.cache import Cache
from medusa.tv.series import Series, SeriesIdentifier
from medusa.updater.version_checker import CheckVersion
Expand Down Expand Up @@ -596,23 +594,21 @@ def status(self):
tvdirFree=tv_dir_free, rootDir=root_dir,
controller='home', action='status')

def shutdown(self, pid=None):
if not Shutdown.stop(pid):
return self.redirect('/{page}/'.format(page=app.DEFAULT_PAGE))

title = 'Shutting down'
message = 'Medusa is shutting down...'

return self._genericMessage(title, message)
def restart(self):
"""
Render the restart page.
def restart(self, pid=None):
if not Restart.restart(pid):
return self.redirect('/{page}/'.format(page=app.DEFAULT_PAGE))
[Converted to VueRouter]
"""
return PageTemplate(rh=self, filename='index.mako').render()

t = PageTemplate(rh=self, filename='restart.mako')
def shutdown(self):
"""
Render the shutdown page.
return t.render(title='Home', header='Restarting Medusa',
controller='home', action='restart')
[Converted to VueRouter]
"""
return PageTemplate(rh=self, filename='index.mako').render()

def updateCheck(self, pid=None):
if text_type(pid) != text_type(app.PID):
Expand Down
16 changes: 11 additions & 5 deletions medusa/updater/github_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ def update_newest_commit_hash(self):
# update remote origin url
self.update_remote_origin()

# Configure local branch with upstream
self.set_upstream_branch()

# get all new info from github
output, _, exit_status = self._run_git(self._git_path, 'fetch --prune %s' % app.GIT_REMOTE)
output, _, exit_status = self._run_git(self._git_path, 'fetch --prune {0}'.format(app.GIT_REMOTE))
if not exit_status == 0:
log.warning(u"Unable to contact github, can't check for update")
return False
Expand Down Expand Up @@ -308,7 +311,7 @@ def update(self):
self.clean()

if self.branch == self._find_installed_branch():
_, _, exit_status = self._run_git(self._git_path, 'pull -f %s %s' % (app.GIT_REMOTE, self.branch))
_, _, exit_status = self._run_git(self._git_path, 'pull -f {0} {1}'.format(app.GIT_REMOTE, self.branch))
else:
_, _, exit_status = self._run_git(self._git_path, 'checkout -f ' + self.branch)

Expand Down Expand Up @@ -363,12 +366,15 @@ def list_remote_branches(self):
self.update_remote_origin()
app.BRANCH = self._find_installed_branch()

branches, _, exit_status = self._run_git(self._git_path, 'ls-remote --heads %s' % app.GIT_REMOTE)
branches, _, exit_status = self._run_git(self._git_path, 'ls-remote --heads {0}'.format(app.GIT_REMOTE))
if exit_status == 0 and branches:
if branches:
return re.findall(r'refs/heads/(.*)', branches)
return []

def update_remote_origin(self):
self._run_git(self._git_path, 'config remote.%s.url %s' % (app.GIT_REMOTE, app.GIT_REMOTE_URL))
self._run_git(self._git_path, 'config remote.%s.pushurl %s' % (app.GIT_REMOTE, app.GIT_REMOTE_URL))
self._run_git(self._git_path, 'config remote.{0}.url {1}'.format(app.GIT_REMOTE, app.GIT_REMOTE_URL))
self._run_git(self._git_path, 'config remote.{0}.pushurl {1}'.format(app.GIT_REMOTE, app.GIT_REMOTE_URL))

def set_upstream_branch(self):
self._run_git(self._git_path, 'branch {0} --set-upstream-to origin/{1}'.format(app.BRANCH, app.BRANCH))
10 changes: 5 additions & 5 deletions medusa/updater/version_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ def _runbackup(self):
os.mkdir(backupDir)

if self._keeplatestbackup(backupDir) and self._backup(backupDir):
log.info(u'Config backup successful, updating...')
ui.notifications.message('Backup', 'Config backup successful, updating...')
log.info(u'Config backup successful')
ui.notifications.message('Backup', 'Config backup successful')
return True
else:
log.warning(u'Config backup failed, aborting update')
ui.notifications.message('Backup', 'Config backup failed, aborting update')
log.warning(u'Config backup failed')
ui.notifications.message('Backup', 'Config backup failed')
return False
except Exception as e:
log.error(u'Update: Config backup failed. Error: {0!r}', e)
ui.notifications.message('Backup', 'Config backup failed, aborting update')
ui.notifications.message('Backup', 'Config backup failed')
return False

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions themes-default/slim/src/components/app-header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
<li><app-link href="errorlogs/viewlog/"><i class="menu-icon-viewlog" />&nbsp;View Log</app-link></li>
<li role="separator" class="divider" />
<li><app-link :href="`home/updateCheck?pid=${system.pid}`"><i class="menu-icon-update" />&nbsp;Check For Updates</app-link></li>
<li><app-link :href="`home/restart/?pid=${system.pid}`" @click.native.prevent="confirmDialog($event, 'restart')"><i class="menu-icon-restart" />&nbsp;Restart</app-link></li>
<li><app-link :href="`home/shutdown/?pid=${system.pid}`" @click.native.prevent="confirmDialog($event, 'shutdown')"><i class="menu-icon-shutdown" />&nbsp;Shutdown</app-link></li>
<li><app-link :href="'home/restart'"><i class="menu-icon-restart" />&nbsp;Restart</app-link></li>
<li><app-link :href="'home/shutdown'" @click.prevent="$router.push({ name: 'shutdown' });"><i class="menu-icon-shutdown" />&nbsp;Shutdown</app-link></li>
<li v-if="username"><app-link href="logout" @click.native.prevent="confirmDialog($event, 'logout')"><i class="menu-icon-shutdown" />&nbsp;Logout</app-link></li>
<li role="separator" class="divider" />
<li><app-link href="home/status/"><i class="menu-icon-info" />&nbsp;Server Status</app-link></li>
Expand Down
Loading

0 comments on commit 71449b5

Please sign in to comment.