Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback shortcuts #369

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions solvebio/resource/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .task import Task
from .datasetfield import DatasetField
from .datasetexport import DatasetExport
from .datasetcommit import DatasetCommit
from .datasetmigration import DatasetMigration


Expand Down Expand Up @@ -292,6 +293,18 @@ def activity(self, follow=False, limit=1,

return list(activity)

def revert(self):
"""Revert the most recent commit"""
commit_ids = [c.id for c in self.commits()]
if not commit_ids:
print("WARNING: No commits exist on this dataset")
return

# Sort commits by oldest
commit_ids = sorted(commit_ids, reverse=True)
commit = DatasetCommit(id=commit_ids[0])
return commit.rollback()

#
# Vault properties
#
Expand Down
42 changes: 41 additions & 1 deletion solvebio/resource/datasetcommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,51 @@ def dataset(self):

@property
def parent_object(self):
""" Get the commit objects parent Import or Migration """
""" Get the commit objects parent Import, Migration or Commit """
from . import types
parent_klass = types.get(self.parent_job_model.split('.')[1])
return parent_klass.retrieve(self.parent_job_id, client=self._client)

def _rollback_url(self):
return self.instance_url() + '/rollback'

def _can_rollback(self):
"""Check if this commit can be reverted"""
# NOTE this always returns a 400....
try:
resp = self._client.get(self._rollback_url(), {})
except Exception:
# TODO
# how to get the valid details?
# add special kwarg to client.request() to allow 400 as valid response?
raise

return resp['is_blocked'], resp['detail'], \
[convert_to_solve_object(dc) for dc in resp['blocking_commits']]

def can_rollback(self):
rollback_status = self._can_rollback()
is_blocked, reason, blocking_commits = rollback_status
if is_blocked:
print("Could not revert commit: {}".format(reason))
if blocking_commits:
print('The following commits are blocking and '
'must be reverted first: {}'.format(
', '.join([bc.id for bc in blocking_commits]))
)
return is_blocked

def rollback(self):
"""Reverts this commit by creating a rollback"""
rollback_status = self._can_rollback()
is_blocked, reason, blocking_commits = rollback_status
if not is_blocked:
resp = self._client.post(self._beacon_url(), {})
return convert_to_solve_object(resp)

# TODO what do return in this scenario
raise Exception("Unable to rollback")

def follow(self, loop=True, sleep_seconds=Task.SLEEP_WAIT_DEFAULT):
# Follow unfinished commits
while self.status in ['queued', 'running']:
Expand Down
6 changes: 3 additions & 3 deletions solvebio/resource/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def add_file(self, path, **kwargs):
'name': file_.filename,
'md5': file_.md5,
'size': file_.size,
'reader_params': kwargs.get('reader_params'),
'entity_params': kwargs.get('entity_params'),
'validation_params': kwargs.get('validation_params')
'reader_params': kwargs.get('reader_params', {}),
'entity_params': kwargs.get('entity_params', {}),
'validation_params': kwargs.get('validation_params', {})
})

def add_url(self, url, **kwargs):
Expand Down