Add ability to revert tags via time machine

This commit is contained in:
Joseph Schorr 2015-04-16 17:18:00 -04:00
parent 2a77bd2c92
commit f19d2f684e
16 changed files with 277 additions and 19 deletions

View file

@ -497,6 +497,7 @@ class RepositoryTag(BaseModel):
lifetime_start_ts = IntegerField(default=get_epoch_timestamp)
lifetime_end_ts = IntegerField(null=True, index=True)
hidden = BooleanField(default=False)
reversion = BooleanField(default=False)
class Meta:
database = db

View file

@ -0,0 +1,29 @@
"""Add revert_tag log entry kind
Revision ID: 1c3decf6b9c4
Revises: 4ce2169efd3b
Create Date: 2015-04-16 17:14:11.154856
"""
# revision identifiers, used by Alembic.
revision = '1c3decf6b9c4'
down_revision = '4ce2169efd3b'
from alembic import op
import sqlalchemy as sa
def upgrade(tables):
op.bulk_insert(tables.logentrykind,
[
{'id': 47, 'name':'revert_tag'},
])
def downgrade(tables):
op.execute(
(tables.logentrykind.delete()
.where(tables.logentrykind.c.name == op.inline_literal('revert_tag')))
)

View file

@ -0,0 +1,26 @@
"""Add reversion column to the tags table
Revision ID: 4ce2169efd3b
Revises: 2b4dc0818a5e
Create Date: 2015-04-16 17:10:16.039835
"""
# revision identifiers, used by Alembic.
revision = '4ce2169efd3b'
down_revision = '2b4dc0818a5e'
from alembic import op
import sqlalchemy as sa
def upgrade(tables):
### commands auto generated by Alembic - please adjust! ###
op.add_column('repositorytag', sa.Column('reversion', sa.Boolean(), nullable=False))
### end Alembic commands ###
def downgrade(tables):
### commands auto generated by Alembic - please adjust! ###
op.drop_column('repositorytag', 'reversion')
### end Alembic commands ###

View file

@ -1762,13 +1762,17 @@ def _tag_alive(query, now_ts=None):
(RepositoryTag.lifetime_end_ts > now_ts))
def list_repository_tag_history(repository, limit=100):
def list_repository_tag_history(repository, limit=100, specific_tag=None):
query = (RepositoryTag
.select(RepositoryTag, Image)
.join(Image)
.where(RepositoryTag.repository == repository)
.order_by(RepositoryTag.lifetime_start_ts.desc())
.limit(limit))
if specific_tag:
query = query.where(RepositoryTag.name == specific_tag)
return query
@ -1990,7 +1994,7 @@ def get_parent_images(namespace_name, repository_name, image_obj):
def create_or_update_tag(namespace_name, repository_name, tag_name,
tag_docker_image_id):
tag_docker_image_id, reversion=False):
try:
repo = _get_repository(namespace_name, repository_name)
except Repository.DoesNotExist:
@ -2015,7 +2019,7 @@ def create_or_update_tag(namespace_name, repository_name, tag_name,
raise DataModelException('Invalid image with id: %s' % tag_docker_image_id)
return RepositoryTag.create(repository=repo, image=image, name=tag_name,
lifetime_start_ts=now_ts)
lifetime_start_ts=now_ts, reversion=reversion)
def delete_tag(namespace_name, repository_name, tag_name):
now_ts = get_epoch_timestamp()
@ -2823,3 +2827,14 @@ def repository_is_starred(user, repository):
return True
except Star.DoesNotExist:
return False
def revert_tag(namespace_name, repository_name, tag_name, docker_image_id):
""" Reverts a tag to a specific image ID. """
image = get_image_by_id(namespace_name, repository_name, docker_image_id)
if image is None:
raise DataModelException('Cannot revert to unknown image')
return create_or_update_tag(namespace_name, repository_name, tag_name, docker_image_id,
reversion=True)