2021-06-06 07:22:47 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
API routes for images.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import urllib.parse
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
from fastapi import APIRouter, Request, Depends, status
|
2023-09-25 07:51:14 +00:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
2022-03-20 06:20:17 +00:00
|
|
|
from starlette.requests import ClientDisconnect
|
2021-08-30 07:23:41 +00:00
|
|
|
from sqlalchemy.orm.exc import MultipleResultsFound
|
2021-10-18 07:34:30 +00:00
|
|
|
from typing import List, Optional
|
2021-06-06 07:22:47 +00:00
|
|
|
from gns3server import schemas
|
|
|
|
|
2022-03-20 06:20:17 +00:00
|
|
|
from gns3server.config import Config
|
2023-09-25 07:51:14 +00:00
|
|
|
from gns3server.compute.qemu import Qemu
|
|
|
|
from gns3server.utils.images import InvalidImageError, write_image, read_image_info, default_images_directory
|
2021-06-06 07:22:47 +00:00
|
|
|
from gns3server.db.repositories.images import ImagesRepository
|
2021-10-10 07:05:11 +00:00
|
|
|
from gns3server.db.repositories.templates import TemplatesRepository
|
|
|
|
from gns3server.db.repositories.rbac import RbacRepository
|
|
|
|
from gns3server.controller import Controller
|
2021-06-06 07:22:47 +00:00
|
|
|
from gns3server.controller.controller_error import (
|
|
|
|
ControllerError,
|
|
|
|
ControllerNotFoundError,
|
|
|
|
ControllerForbiddenError,
|
|
|
|
ControllerBadRequestError
|
|
|
|
)
|
|
|
|
|
2021-10-10 07:05:11 +00:00
|
|
|
from .dependencies.authentication import get_current_active_user
|
2021-06-06 07:22:47 +00:00
|
|
|
from .dependencies.database import get_repository
|
2023-09-02 10:54:24 +00:00
|
|
|
from .dependencies.rbac import has_privilege
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2023-09-25 07:51:14 +00:00
|
|
|
@router.post(
|
2023-09-25 11:08:23 +00:00
|
|
|
"/qemu/{image_path:path}",
|
2023-09-25 07:51:14 +00:00
|
|
|
response_model=schemas.Image,
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
dependencies=[Depends(has_privilege("Image.Allocate"))]
|
|
|
|
)
|
2023-09-25 11:08:23 +00:00
|
|
|
async def create_qemu_image(
|
2023-09-25 07:51:14 +00:00
|
|
|
image_path: str,
|
|
|
|
image_data: schemas.QemuDiskImageCreate,
|
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
|
|
|
|
|
|
|
) -> schemas.Image:
|
|
|
|
"""
|
2023-09-25 11:08:23 +00:00
|
|
|
Create a new blank Qemu image.
|
2023-09-25 07:51:14 +00:00
|
|
|
|
|
|
|
Required privilege: Image.Allocate
|
|
|
|
"""
|
|
|
|
|
|
|
|
allow_raw_image = Config.instance().settings.Server.allow_raw_images
|
|
|
|
if image_data.format == schemas.QemuDiskImageFormat.raw and not allow_raw_image:
|
|
|
|
raise ControllerBadRequestError("Raw images are not allowed")
|
|
|
|
|
|
|
|
disk_image_path = urllib.parse.unquote(image_path)
|
|
|
|
image_dir, image_name = os.path.split(disk_image_path)
|
|
|
|
# check if the path is within the default images directory
|
|
|
|
base_images_directory = os.path.expanduser(Config.instance().settings.Server.images_path)
|
|
|
|
full_path = os.path.abspath(os.path.join(base_images_directory, image_dir, image_name))
|
|
|
|
if os.path.commonprefix([base_images_directory, full_path]) != base_images_directory:
|
|
|
|
raise ControllerForbiddenError(f"Cannot write disk image, '{disk_image_path}' is forbidden")
|
|
|
|
|
|
|
|
if not image_dir:
|
2023-09-25 11:08:23 +00:00
|
|
|
# put the image in the default images directory for Qemu
|
2023-09-25 07:51:14 +00:00
|
|
|
directory = default_images_directory(image_type="qemu")
|
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
disk_image_path = os.path.abspath(os.path.join(directory, disk_image_path))
|
|
|
|
|
|
|
|
if await images_repo.get_image(disk_image_path):
|
|
|
|
raise ControllerBadRequestError(f"Disk image '{disk_image_path}' already exists")
|
|
|
|
|
|
|
|
options = jsonable_encoder(image_data, exclude_unset=True)
|
|
|
|
# FIXME: should we have the create_disk_image in the compute code since
|
|
|
|
# this code is used to create images on the controller?
|
|
|
|
await Qemu.instance().create_disk_image(disk_image_path, options)
|
|
|
|
|
|
|
|
image_info = await read_image_info(disk_image_path, "qemu")
|
|
|
|
return await images_repo.add_image(**image_info)
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"",
|
|
|
|
response_model=List[schemas.Image],
|
|
|
|
dependencies=[Depends(has_privilege("Image.Audit"))]
|
|
|
|
)
|
2021-06-06 07:22:47 +00:00
|
|
|
async def get_images(
|
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
2022-04-14 10:01:54 +00:00
|
|
|
image_type: Optional[schemas.ImageType] = None
|
2021-06-06 07:22:47 +00:00
|
|
|
) -> List[schemas.Image]:
|
|
|
|
"""
|
|
|
|
Return all images.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Image.Audit
|
2021-06-06 07:22:47 +00:00
|
|
|
"""
|
|
|
|
|
2022-04-14 10:01:54 +00:00
|
|
|
return await images_repo.get_images(image_type)
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"/upload/{image_path:path}",
|
|
|
|
response_model=schemas.Image,
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
dependencies=[Depends(has_privilege("Image.Allocate"))]
|
|
|
|
)
|
2021-06-06 07:22:47 +00:00
|
|
|
async def upload_image(
|
2021-08-30 07:23:41 +00:00
|
|
|
image_path: str,
|
2021-06-06 07:22:47 +00:00
|
|
|
request: Request,
|
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
2021-10-10 07:05:11 +00:00
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
|
|
|
current_user: schemas.User = Depends(get_current_active_user),
|
2021-10-18 07:34:30 +00:00
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository)),
|
2022-07-22 10:39:52 +00:00
|
|
|
install_appliances: Optional[bool] = False,
|
2021-06-06 07:22:47 +00:00
|
|
|
) -> schemas.Image:
|
|
|
|
"""
|
|
|
|
Upload an image.
|
2021-10-10 07:05:11 +00:00
|
|
|
|
2022-03-20 06:20:17 +00:00
|
|
|
Example: curl -X POST http://host:port/v3/images/upload/my_image_name.qcow2 \
|
2021-10-10 07:05:11 +00:00
|
|
|
-H 'Authorization: Bearer <token>' --data-binary @"/path/to/image.qcow2"
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Image.Allocate
|
2021-06-06 07:22:47 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
image_path = urllib.parse.unquote(image_path)
|
|
|
|
image_dir, image_name = os.path.split(image_path)
|
2022-03-20 06:20:17 +00:00
|
|
|
# check if the path is within the default images directory
|
|
|
|
base_images_directory = os.path.expanduser(Config.instance().settings.Server.images_path)
|
|
|
|
full_path = os.path.abspath(os.path.join(base_images_directory, image_dir, image_name))
|
|
|
|
if os.path.commonprefix([base_images_directory, full_path]) != base_images_directory:
|
2021-10-10 07:05:11 +00:00
|
|
|
raise ControllerForbiddenError(f"Cannot write image, '{image_path}' is forbidden")
|
2021-06-06 07:22:47 +00:00
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
if await images_repo.get_image(image_path):
|
|
|
|
raise ControllerBadRequestError(f"Image '{image_path}' already exists")
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
try:
|
2022-12-26 03:28:51 +00:00
|
|
|
allow_raw_image = Config.instance().settings.Server.allow_raw_images
|
2022-07-22 10:39:52 +00:00
|
|
|
image = await write_image(image_path, full_path, request.stream(), images_repo, allow_raw_image=allow_raw_image)
|
2022-03-20 06:20:17 +00:00
|
|
|
except (OSError, InvalidImageError, ClientDisconnect) as e:
|
|
|
|
raise ControllerError(f"Could not save image '{image_path}': {e}")
|
2021-06-06 07:22:47 +00:00
|
|
|
|
2021-10-18 07:34:30 +00:00
|
|
|
if install_appliances:
|
|
|
|
# attempt to automatically create templates based on image checksum
|
|
|
|
await Controller.instance().appliance_manager.install_appliances_from_image(
|
|
|
|
image_path,
|
2021-10-10 07:05:11 +00:00
|
|
|
image.checksum,
|
|
|
|
images_repo,
|
2021-10-18 07:34:30 +00:00
|
|
|
templates_repo,
|
|
|
|
rbac_repo,
|
|
|
|
current_user,
|
2022-03-20 06:20:17 +00:00
|
|
|
os.path.dirname(image.path)
|
2021-10-10 07:05:11 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 07:22:47 +00:00
|
|
|
return image
|
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"/{image_path:path}",
|
|
|
|
response_model=schemas.Image,
|
|
|
|
dependencies=[Depends(has_privilege("Image.Audit"))]
|
|
|
|
)
|
2021-06-06 07:22:47 +00:00
|
|
|
async def get_image(
|
2021-08-30 07:23:41 +00:00
|
|
|
image_path: str,
|
2021-06-06 07:22:47 +00:00
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
|
|
|
) -> schemas.Image:
|
|
|
|
"""
|
|
|
|
Return an image.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Image.Audit
|
2021-06-06 07:22:47 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
image_path = urllib.parse.unquote(image_path)
|
|
|
|
image = await images_repo.get_image(image_path)
|
2021-06-06 07:22:47 +00:00
|
|
|
if not image:
|
2021-08-30 07:23:41 +00:00
|
|
|
raise ControllerNotFoundError(f"Image '{image_path}' not found")
|
2021-06-06 07:22:47 +00:00
|
|
|
return image
|
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.delete(
|
|
|
|
"/{image_path:path}",
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
dependencies=[Depends(has_privilege("Image.Allocate"))]
|
|
|
|
)
|
2021-06-06 07:22:47 +00:00
|
|
|
async def delete_image(
|
2021-08-30 07:23:41 +00:00
|
|
|
image_path: str,
|
2021-06-06 07:22:47 +00:00
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
2022-07-15 22:12:18 +00:00
|
|
|
) -> None:
|
2021-06-06 07:22:47 +00:00
|
|
|
"""
|
|
|
|
Delete an image.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Image.Allocate
|
2021-06-06 07:22:47 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
image_path = urllib.parse.unquote(image_path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
image = await images_repo.get_image(image_path)
|
|
|
|
except MultipleResultsFound:
|
|
|
|
raise ControllerBadRequestError(f"Image '{image_path}' matches multiple images. "
|
|
|
|
f"Please include the relative path of the image")
|
|
|
|
|
2021-06-06 07:22:47 +00:00
|
|
|
if not image:
|
2021-08-30 07:23:41 +00:00
|
|
|
raise ControllerNotFoundError(f"Image '{image_path}' not found")
|
2021-06-06 07:22:47 +00:00
|
|
|
|
2021-10-18 07:34:30 +00:00
|
|
|
templates = await images_repo.get_image_templates(image.image_id)
|
|
|
|
if templates:
|
|
|
|
template_names = ", ".join([template.name for template in templates])
|
|
|
|
raise ControllerError(f"Image '{image_path}' is used by one or more templates: {template_names}")
|
2021-06-06 07:22:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(image.path)
|
|
|
|
except OSError:
|
|
|
|
log.warning(f"Could not delete image file {image.path}")
|
|
|
|
|
2021-08-30 07:23:41 +00:00
|
|
|
success = await images_repo.delete_image(image_path)
|
2021-06-06 07:22:47 +00:00
|
|
|
if not success:
|
2021-08-30 07:23:41 +00:00
|
|
|
raise ControllerError(f"Image '{image_path}' could not be deleted")
|
2021-08-20 06:28:41 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"/prune",
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
dependencies=[Depends(has_privilege("Image.Allocate"))]
|
|
|
|
)
|
2021-08-20 06:28:41 +00:00
|
|
|
async def prune_images(
|
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
2022-07-15 22:12:18 +00:00
|
|
|
) -> None:
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
|
|
|
Prune images not attached to any template.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Image.Allocate
|
2021-08-20 06:28:41 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
await images_repo.prune_images()
|