diff --git a/src/openec2/actions/describe_images.py b/src/openec2/actions/describe_images.py index d223b39..b17645d 100644 --- a/src/openec2/actions/describe_images.py +++ b/src/openec2/actions/describe_images.py @@ -1,14 +1,36 @@ +import uuid + +from fastapi import Response from fastapi.datastructures import QueryParams from sqlmodel import select from openec2.config import OpenEC2Config from openec2.db import DatabaseDep from openec2.db.image import AMI +from openec2.api.describe_images import DescribeImagesResponse, ImagesSet, Image + def describe_images( params: QueryParams, config: OpenEC2Config, db: DatabaseDep, ): - amis = db.exec(select(AMI)).all() - return ", ".join(ami.id for ami in amis) + images: list[Image] = [] + for ami in db.exec(select(AMI)).all(): + images.append( + Image( + imageId=ami.id, + imageState="available", + name=ami.originalFilename, + ), + ) + + return Response( + DescribeImagesResponse( + requestId=uuid.uuid4().hex, + imagesSet=ImagesSet( + items=images, + ) + ).to_xml(), + media_type="application/xml", + ) diff --git a/src/openec2/actions/run_instances.py b/src/openec2/actions/run_instances.py index 27657a5..09166ce 100644 --- a/src/openec2/actions/run_instances.py +++ b/src/openec2/actions/run_instances.py @@ -1,15 +1,13 @@ -import shutil import base64 from typing import cast import uuid -import os from fastapi import HTTPException from fastapi.datastructures import QueryParams from sqlmodel import select from openec2.libvirt import LibvirtSingleton -from openec2.config import OpenEC2Config +from openec2.config import OpenEC2Config, ConfigSingleton from openec2.utils.qemu import create_cow_copy from openec2.db import DatabaseDep from openec2.db.instance import Instance @@ -26,8 +24,18 @@ def create_libvirt_domain( vcpu: int, ami_path: str, mac: str, - user_data: str | None, ) -> str: + extra_devices = "" + if ConfigSingleton.of().config.debug: + extra_devices += """ + + + + +""" + return f""" {name} @@ -58,18 +66,12 @@ def create_libvirt_domain( - {user_data or ''} + {extra_devices} - - - - /dev/urandom @@ -153,7 +155,6 @@ def run_instances( int(instance_type.vcpu), str(config.instances.location / instance_id), mac, - None, ), ) domain.create() diff --git a/src/openec2/api/describe_images.py b/src/openec2/api/describe_images.py new file mode 100644 index 0000000..c17bf57 --- /dev/null +++ b/src/openec2/api/describe_images.py @@ -0,0 +1,20 @@ +from pydantic_xml import BaseXmlModel, element + +class Image(BaseXmlModel): + imageId: str = element() + + imageState: str = element() + + name: str = element() + +class ImagesSet(BaseXmlModel, tag="imagesSet"): + items: list[Image] = element(tag="item") + +class DescribeImagesResponse( + BaseXmlModel, + tag="DescribeImagesResponse", + nsmap={"": "http://ec2.amazonaws.com/doc/2016-11-15/"} +): + requestId: str = element() + + imagesSet: ImagesSet = element() diff --git a/src/openec2/config.py b/src/openec2/config.py index 4415a14..3e653e3 100644 --- a/src/openec2/config.py +++ b/src/openec2/config.py @@ -16,11 +16,20 @@ class _OpenEC2InstanceConfig(BaseModel): class _OpenEC2LibvirtConfig(BaseModel): connection: str +class _OpenEC2DatabaseConfig(BaseModel): + # DB URL for sqlmodel + url: str + + # Print SQL statements + debug: bool + class _OpenEC2Config(BaseModel): images: Path seed: Path instances: _OpenEC2InstanceConfig libvirt: _OpenEC2LibvirtConfig + database: _OpenEC2DatabaseConfig + debug: bool def _get_config() -> _OpenEC2Config: # TODO: Read from disk @@ -40,6 +49,11 @@ def _get_config() -> _OpenEC2Config: libvirt=_OpenEC2LibvirtConfig( connection="qemu:///system" ), + debug=True, + database=_OpenEC2DatabaseConfig( + url="sqlite:////home/alexander/openec2/db2.sqlite3", + debug=True, + ), ) class ConfigSingleton: diff --git a/src/openec2/db/__init__.py b/src/openec2/db/__init__.py index 83f9db0..7c2137a 100644 --- a/src/openec2/db/__init__.py +++ b/src/openec2/db/__init__.py @@ -2,12 +2,14 @@ from typing import Generator, Annotated from fastapi import Depends from sqlmodel import Session, create_engine +from openec2.config import ConfigSingleton + engine = create_engine( - "sqlite:////home/alexander/openec2/db2.sqlite3", + ConfigSingleton.of().config.database.url, connect_args={ "check_same_thread": False, }, - echo=True, + echo=ConfigSingleton.of().config.database.debug, ) def get_session() -> Generator[Session]: