Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#570 Implement default env creation #605

Merged
merged 15 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions agenta-backend/agenta_backend/main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import os
import json
from fastapi import FastAPI
import os
from contextlib import asynccontextmanager

from agenta_backend.config import settings
from agenta_backend.routers import app_variant
from agenta_backend.routers import testset_router
from fastapi.middleware.cors import CORSMiddleware
from agenta_backend.routers import container_router
from agenta_backend.routers import evaluation_router
from agenta_backend.services.db_manager import (
add_template,
remove_old_template_from_db,
)
from agenta_backend.services.container_manager import (
pull_image_from_docker_hub,
from agenta_backend.routers import (
app_variant,
container_router,
environment_router,
evaluation_router,
testset_router,
)
from agenta_backend.services.cache_manager import (
retrieve_templates_from_dockerhub_cached,
retrieve_templates_info_from_dockerhub_cached,
)

from contextlib import asynccontextmanager
from agenta_backend.config import settings

from agenta_backend.services.container_manager import pull_image_from_docker_hub
from agenta_backend.services.db_manager import add_template, remove_old_template_from_db
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

origins = [
"http://localhost:3000",
Expand Down Expand Up @@ -89,6 +84,7 @@ async def lifespan(application: FastAPI, cache=True):
app.include_router(evaluation_router.router, prefix="/evaluations")
app.include_router(testset_router.router, prefix="/testsets")
app.include_router(container_router.router, prefix="/containers")
app.include_router(environment_router.router, prefix="/environments")

allow_headers = ["Content-Type"]

Expand Down
8 changes: 7 additions & 1 deletion agenta-backend/agenta_backend/models/api/api_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from typing import Any, Dict, List, Optional

from pydantic import BaseModel
from typing import List, Optional, Dict, Any


class AppVariant(BaseModel):
Expand Down Expand Up @@ -60,3 +61,8 @@ class CreateAppVariant(BaseModel):
image_id: str
image_tag: str
env_vars: Dict[str, str]


class Environment(BaseModel):
name: str
deployed_app_variant: Optional[str]
15 changes: 13 additions & 2 deletions agenta-backend/agenta_backend/models/db_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from typing import Optional, Dict, Any, List
from odmantic import Field, Model, Reference, EmbeddedModel
from typing import Any, Dict, List, Optional

from odmantic import EmbeddedModel, Field, Model, Reference


class OrganizationDB(Model):
Expand Down Expand Up @@ -49,6 +50,16 @@ class Config:
collection = "app_variants"


class EnvironmentDB(Model):
name: str
user_id: UserDB = Reference(key_name="user")
app_name: str
deployed_app_variant: Optional[str]

class Config:
collection = "environments"


class TemplateDB(Model):
template_id: int
name: str
Expand Down
53 changes: 53 additions & 0 deletions agenta-backend/agenta_backend/routers/environment_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from typing import List

from agenta_backend.models.api.api_models import Environment
from agenta_backend.services import db_manager
from fastapi import APIRouter, Depends, HTTPException

if os.environ["FEATURE_FLAG"] in ["cloud", "ee", "demo"]:
from agenta_backend.ee.services.auth_helper import SessionContainer, verify_session
from agenta_backend.ee.services.selectors import get_user_and_org_id
else:
from agenta_backend.services.auth_helper import SessionContainer, verify_session
from agenta_backend.services.selectors import get_user_and_org_id


router = APIRouter()


@router.get("/", response_model=List[Environment])
async def list_environments(
app_name: str,
stoken_session: SessionContainer = Depends(verify_session()),
):
"""
Lists the environments for the given app.
"""
try:
kwargs: dict = await get_user_and_org_id(stoken_session)
app_variants = await db_manager.list_environments(app_name=app_name, **kwargs)
return app_variants
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))


@router.post("/deploy/")
async def deploy_environment(
environment_name: str,
app_name: str,
variant_name: str,
stoken_session: SessionContainer = Depends(verify_session()),
):
print("deploying environment")
print(environment_name, app_name, variant_name)
"""
Deploys the given environment for the given app with the given variant.
"""
try:
kwargs: dict = await get_user_and_org_id(stoken_session)
await db_manager.deploy_environment(
app_name, environment_name, variant_name, **kwargs
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
45 changes: 28 additions & 17 deletions agenta-backend/agenta_backend/services/app_manager.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
"""Main Business logic
"""
import os
import logging
from typing import Optional
import os
from typing import List, Optional

from agenta_backend.config import settings
from agenta_backend.models.api.api_models import (
URI,
App,
AppVariant,
Image,
DockerEnvVars,
Environment,
Image,
ImageExtended,
)
from agenta_backend.models.db_models import AppVariantDB, TestSetDB
from agenta_backend.services import db_manager, docker_utils
from docker.errors import DockerException


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -203,19 +204,29 @@ async def remove_app(app: App, **kwargs: dict):
msg = f"App {app_name} not found in DB"
logger.error(msg)
raise ValueError(msg)
else:
try:
for app_variant in app_variants:
await remove_app_variant(app_variant, **kwargs)
logger.info(
f"App variant {app_variant.app_name}/{app_variant.variant_name} deleted"
)

await remove_app_testsets(app_name, **kwargs)
logger.info(f"Tatasets for {app_name} app deleted")
except Exception as e:
logger.error(f"Error deleting app variants: {str(e)}")
raise

try:
# Delete associated environments
environments: List[Environment] = await db_manager.list_environments(
app_name, **kwargs
)
for environment in environments:
await db_manager.remove_environment(environment.name, app_name, **kwargs)
logger.info(f"Environment {environment.name} deleted")

# Delete associated variants
for app_variant in app_variants:
await remove_app_variant(app_variant, **kwargs)
logger.info(
f"App variant {app_variant.app_name}/{app_variant.variant_name} deleted"
)

# Delete associated testsets
await remove_app_testsets(app_name, **kwargs)
logger.info(f"Tatasets for {app_name} app deleted")
except Exception as e:
logger.error(f"Error deleting app variants: {str(e)}")
raise


async def remove_app_testsets(app_name: str, **kwargs):
Expand Down
Loading