2021-06-06 07:22:47 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2021 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-08-20 06:28:41 +00:00
|
|
|
import os
|
|
|
|
|
2021-06-06 07:22:47 +00:00
|
|
|
from typing import Optional, List
|
|
|
|
from sqlalchemy import select, delete
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
from .base import BaseRepository
|
|
|
|
|
|
|
|
import gns3server.db.models as models
|
|
|
|
|
2021-08-20 06:28:41 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
class ImagesRepository(BaseRepository):
|
|
|
|
|
|
|
|
def __init__(self, db_session: AsyncSession) -> None:
|
|
|
|
|
|
|
|
super().__init__(db_session)
|
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
async def get_image(self, image_path: str) -> Optional[models.Image]:
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
2021-08-30 07:23:41 +00:00
|
|
|
Get an image by its path.
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
2021-06-06 07:22:47 +00:00
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
image_dir, image_name = os.path.split(image_path)
|
|
|
|
if image_dir:
|
|
|
|
query = select(models.Image).\
|
|
|
|
where(models.Image.filename == image_name, models.Image.path.endswith(image_path))
|
|
|
|
else:
|
|
|
|
query = select(models.Image).where(models.Image.filename == image_name)
|
2021-06-06 07:22:47 +00:00
|
|
|
result = await self._db_session.execute(query)
|
2021-08-30 07:23:41 +00:00
|
|
|
return result.scalars().one_or_none()
|
2021-06-06 07:22:47 +00:00
|
|
|
|
2021-08-20 06:28:41 +00:00
|
|
|
async def get_image_by_checksum(self, checksum: str) -> Optional[models.Image]:
|
|
|
|
"""
|
|
|
|
Get an image by its checksum.
|
|
|
|
"""
|
|
|
|
|
|
|
|
query = select(models.Image).where(models.Image.checksum == checksum)
|
|
|
|
result = await self._db_session.execute(query)
|
|
|
|
return result.scalars().first()
|
|
|
|
|
2021-06-06 07:22:47 +00:00
|
|
|
async def get_images(self) -> List[models.Image]:
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
|
|
|
Get all images.
|
|
|
|
"""
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
query = select(models.Image)
|
|
|
|
result = await self._db_session.execute(query)
|
|
|
|
return result.scalars().all()
|
|
|
|
|
|
|
|
async def get_image_templates(self, image_id: int) -> Optional[List[models.Template]]:
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
|
|
|
Get all templates that an image belongs to.
|
|
|
|
"""
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
query = select(models.Template).\
|
2021-08-20 06:28:41 +00:00
|
|
|
join(models.Template.images).\
|
2021-09-23 01:35:03 +00:00
|
|
|
filter(models.Image.image_id == image_id)
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
result = await self._db_session.execute(query)
|
|
|
|
return result.scalars().all()
|
|
|
|
|
2021-08-11 07:28:23 +00:00
|
|
|
async def add_image(self, image_name, image_type, image_size, path, checksum, checksum_algorithm) -> models.Image:
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
|
|
|
Create a new image.
|
|
|
|
"""
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
db_image = models.Image(
|
2021-09-23 01:35:03 +00:00
|
|
|
image_id=None,
|
2021-06-06 07:22:47 +00:00
|
|
|
filename=image_name,
|
|
|
|
image_type=image_type,
|
2021-08-11 07:28:23 +00:00
|
|
|
image_size=image_size,
|
2021-06-06 07:22:47 +00:00
|
|
|
path=path,
|
|
|
|
checksum=checksum,
|
|
|
|
checksum_algorithm=checksum_algorithm
|
|
|
|
)
|
|
|
|
|
|
|
|
self._db_session.add(db_image)
|
|
|
|
await self._db_session.commit()
|
|
|
|
await self._db_session.refresh(db_image)
|
|
|
|
return db_image
|
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
async def delete_image(self, image_path: str) -> bool:
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
|
|
|
Delete an image.
|
|
|
|
"""
|
2021-06-06 07:22:47 +00:00
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
image_dir, image_name = os.path.split(image_path)
|
|
|
|
if image_dir:
|
|
|
|
query = delete(models.Image).\
|
|
|
|
where(models.Image.filename == image_name, models.Image.path.endswith(image_path)).\
|
|
|
|
execution_options(synchronize_session=False)
|
|
|
|
else:
|
|
|
|
query = delete(models.Image).where(models.Image.filename == image_name)
|
2021-06-06 07:22:47 +00:00
|
|
|
result = await self._db_session.execute(query)
|
|
|
|
await self._db_session.commit()
|
|
|
|
return result.rowcount > 0
|
2021-08-20 06:28:41 +00:00
|
|
|
|
|
|
|
async def prune_images(self) -> int:
|
|
|
|
"""
|
|
|
|
Prune images not attached to any template.
|
|
|
|
"""
|
|
|
|
|
|
|
|
query = select(models.Image).\
|
|
|
|
filter(~models.Image.templates.any())
|
|
|
|
result = await self._db_session.execute(query)
|
|
|
|
images = result.scalars().all()
|
|
|
|
images_deleted = 0
|
|
|
|
for image in images:
|
|
|
|
try:
|
2021-08-23 00:57:10 +00:00
|
|
|
log.debug(f"Deleting image '{image.path}'")
|
2021-08-20 06:28:41 +00:00
|
|
|
os.remove(image.path)
|
|
|
|
except OSError:
|
|
|
|
log.warning(f"Could not delete image file {image.path}")
|
|
|
|
if await self.delete_image(image.filename):
|
|
|
|
images_deleted += 1
|
2021-08-23 00:57:10 +00:00
|
|
|
log.info(f"{images_deleted} image(s) have been deleted")
|
2021-08-20 06:28:41 +00:00
|
|
|
return images_deleted
|