Make more configurable
This commit is contained in:
parent
a210b47e36
commit
6ee5778878
@ -1,14 +1,36 @@
|
|||||||
|
import uuid
|
||||||
|
|
||||||
|
from fastapi import Response
|
||||||
from fastapi.datastructures import QueryParams
|
from fastapi.datastructures import QueryParams
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
|
|
||||||
from openec2.config import OpenEC2Config
|
from openec2.config import OpenEC2Config
|
||||||
from openec2.db import DatabaseDep
|
from openec2.db import DatabaseDep
|
||||||
from openec2.db.image import AMI
|
from openec2.db.image import AMI
|
||||||
|
from openec2.api.describe_images import DescribeImagesResponse, ImagesSet, Image
|
||||||
|
|
||||||
|
|
||||||
def describe_images(
|
def describe_images(
|
||||||
params: QueryParams,
|
params: QueryParams,
|
||||||
config: OpenEC2Config,
|
config: OpenEC2Config,
|
||||||
db: DatabaseDep,
|
db: DatabaseDep,
|
||||||
):
|
):
|
||||||
amis = db.exec(select(AMI)).all()
|
images: list[Image] = []
|
||||||
return ", ".join(ami.id for ami in amis)
|
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",
|
||||||
|
)
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
import shutil
|
|
||||||
import base64
|
import base64
|
||||||
from typing import cast
|
from typing import cast
|
||||||
import uuid
|
import uuid
|
||||||
import os
|
|
||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from fastapi.datastructures import QueryParams
|
from fastapi.datastructures import QueryParams
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
|
|
||||||
from openec2.libvirt import LibvirtSingleton
|
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.utils.qemu import create_cow_copy
|
||||||
from openec2.db import DatabaseDep
|
from openec2.db import DatabaseDep
|
||||||
from openec2.db.instance import Instance
|
from openec2.db.instance import Instance
|
||||||
@ -26,8 +24,18 @@ def create_libvirt_domain(
|
|||||||
vcpu: int,
|
vcpu: int,
|
||||||
ami_path: str,
|
ami_path: str,
|
||||||
mac: str,
|
mac: str,
|
||||||
user_data: str | None,
|
|
||||||
) -> str:
|
) -> str:
|
||||||
|
extra_devices = ""
|
||||||
|
if ConfigSingleton.of().config.debug:
|
||||||
|
extra_devices += """
|
||||||
|
<video>
|
||||||
|
<model type='qxl' />
|
||||||
|
</video>
|
||||||
|
<graphics type="spice" port="-1" tlsPort="-1" autoport="yes">
|
||||||
|
<image compression="off"/>
|
||||||
|
</graphics>
|
||||||
|
"""
|
||||||
|
|
||||||
return f"""
|
return f"""
|
||||||
<domain type='kvm'>
|
<domain type='kvm'>
|
||||||
<name>{name}</name>
|
<name>{name}</name>
|
||||||
@ -58,18 +66,12 @@ def create_libvirt_domain(
|
|||||||
<suspend-to-disk enabled='no'/>
|
<suspend-to-disk enabled='no'/>
|
||||||
</pm>
|
</pm>
|
||||||
<devices>
|
<devices>
|
||||||
{user_data or ''}
|
{extra_devices}
|
||||||
<disk type='file' device='disk'>
|
<disk type='file' device='disk'>
|
||||||
<driver name='qemu' type='qcow2'/>
|
<driver name='qemu' type='qcow2'/>
|
||||||
<source file='{ami_path}'/>
|
<source file='{ami_path}'/>
|
||||||
<target dev='vda' bus='virtio'/>
|
<target dev='vda' bus='virtio'/>
|
||||||
</disk>
|
</disk>
|
||||||
<video>
|
|
||||||
<model type='qxl' />
|
|
||||||
</video>
|
|
||||||
<graphics type="spice" port="-1" tlsPort="-1" autoport="yes">
|
|
||||||
<image compression="off"/>
|
|
||||||
</graphics>
|
|
||||||
<rng model="virtio">
|
<rng model="virtio">
|
||||||
<backend model="random">/dev/urandom</backend>
|
<backend model="random">/dev/urandom</backend>
|
||||||
</rng>
|
</rng>
|
||||||
@ -153,7 +155,6 @@ def run_instances(
|
|||||||
int(instance_type.vcpu),
|
int(instance_type.vcpu),
|
||||||
str(config.instances.location / instance_id),
|
str(config.instances.location / instance_id),
|
||||||
mac,
|
mac,
|
||||||
None,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
domain.create()
|
domain.create()
|
||||||
|
20
src/openec2/api/describe_images.py
Normal file
20
src/openec2/api/describe_images.py
Normal file
@ -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()
|
@ -16,11 +16,20 @@ class _OpenEC2InstanceConfig(BaseModel):
|
|||||||
class _OpenEC2LibvirtConfig(BaseModel):
|
class _OpenEC2LibvirtConfig(BaseModel):
|
||||||
connection: str
|
connection: str
|
||||||
|
|
||||||
|
class _OpenEC2DatabaseConfig(BaseModel):
|
||||||
|
# DB URL for sqlmodel
|
||||||
|
url: str
|
||||||
|
|
||||||
|
# Print SQL statements
|
||||||
|
debug: bool
|
||||||
|
|
||||||
class _OpenEC2Config(BaseModel):
|
class _OpenEC2Config(BaseModel):
|
||||||
images: Path
|
images: Path
|
||||||
seed: Path
|
seed: Path
|
||||||
instances: _OpenEC2InstanceConfig
|
instances: _OpenEC2InstanceConfig
|
||||||
libvirt: _OpenEC2LibvirtConfig
|
libvirt: _OpenEC2LibvirtConfig
|
||||||
|
database: _OpenEC2DatabaseConfig
|
||||||
|
debug: bool
|
||||||
|
|
||||||
def _get_config() -> _OpenEC2Config:
|
def _get_config() -> _OpenEC2Config:
|
||||||
# TODO: Read from disk
|
# TODO: Read from disk
|
||||||
@ -40,6 +49,11 @@ def _get_config() -> _OpenEC2Config:
|
|||||||
libvirt=_OpenEC2LibvirtConfig(
|
libvirt=_OpenEC2LibvirtConfig(
|
||||||
connection="qemu:///system"
|
connection="qemu:///system"
|
||||||
),
|
),
|
||||||
|
debug=True,
|
||||||
|
database=_OpenEC2DatabaseConfig(
|
||||||
|
url="sqlite:////home/alexander/openec2/db2.sqlite3",
|
||||||
|
debug=True,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
class ConfigSingleton:
|
class ConfigSingleton:
|
||||||
|
@ -2,12 +2,14 @@ from typing import Generator, Annotated
|
|||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlmodel import Session, create_engine
|
from sqlmodel import Session, create_engine
|
||||||
|
|
||||||
|
from openec2.config import ConfigSingleton
|
||||||
|
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
"sqlite:////home/alexander/openec2/db2.sqlite3",
|
ConfigSingleton.of().config.database.url,
|
||||||
connect_args={
|
connect_args={
|
||||||
"check_same_thread": False,
|
"check_same_thread": False,
|
||||||
},
|
},
|
||||||
echo=True,
|
echo=ConfigSingleton.of().config.database.debug,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_session() -> Generator[Session]:
|
def get_session() -> Generator[Session]:
|
||||||
|
Loading…
Reference in New Issue
Block a user