Skip to content

Commit

Permalink
use config.settings attributes instead of os.getenv
Browse files Browse the repository at this point in the history
APISettings() will load all values from .env file as
APISettings's attributes. These can be imported in all
other modules, instead of using load_dotenv in every module.
ref: https://fastapi.tiangolo.com/advanced/settings/#reading-a-env-file

This commit also fixes the issue of missing value of PADDLES_URL
in services/helpers.py

Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
  • Loading branch information
VallariAg committed Nov 7, 2023
1 parent f92d3f3 commit 08ecc9c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ GH_FETCH_MEMBERSHIP_URL='https://api.github.com/user/memberships/orgs/ceph'
## SESSION_SECRET_KEY is used to encrypt session data
## and it's prod value should be kept secret.
SESSION_SECRET_KEY=my-secret-key

# Path where all logs for teuthology-suite or teuthology-kill would be collected
ARCHIVE_DIR=
13 changes: 13 additions & 0 deletions src/teuthology_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ class APISettings(BaseSettings):
Class for API settings.
"""

deployment: str = ""
pulpito_url: str = ""
paddles_url: str = ""

gh_client_id: str = ""
gh_client_secret: str = ""
gh_token_url: str = ""
gh_authorization_base_url: str = ""
gh_fetch_membership_url: str = ""

session_secret_key: str = ""
archive_dir: str = ""

model_config = SettingsConfigDict(
env_file=".env", env_file_encoding="utf-8", extra="ignore"
)
Expand Down
11 changes: 5 additions & 6 deletions src/teuthology_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from dotenv import load_dotenv

from teuthology_api.config import settings
from teuthology_api.routes import suite, kill, login, logout

load_dotenv()

DEPLOYMENT = os.getenv("DEPLOYMENT")
SESSION_SECRET_KEY = os.getenv("SESSION_SECRET_KEY")
PULPITO_URL = os.getenv("PULPITO_URL")
PADDLES_URL = os.getenv("PADDLES_URL")
DEPLOYMENT = settings.deployment
SESSION_SECRET_KEY = settings.session_secret_key
PULPITO_URL = settings.pulpito_url
PADDLES_URL = settings.paddles_url

log = logging.getLogger(__name__)
app = FastAPI()
Expand Down
15 changes: 7 additions & 8 deletions src/teuthology_api/routes/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import os
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import RedirectResponse
from dotenv import load_dotenv
import httpx
from teuthology_api.config import settings

load_dotenv()

GH_CLIENT_ID = os.getenv("GH_CLIENT_ID")
GH_CLIENT_SECRET = os.getenv("GH_CLIENT_SECRET")
GH_AUTHORIZATION_BASE_URL = os.getenv("GH_AUTHORIZATION_BASE_URL")
GH_TOKEN_URL = os.getenv("GH_TOKEN_URL")
GH_FETCH_MEMBERSHIP_URL = os.getenv("GH_FETCH_MEMBERSHIP_URL")
PULPITO_URL = os.getenv("PULPITO_URL")
GH_CLIENT_ID = settings.gh_client_id
GH_CLIENT_SECRET = settings.gh_client_secret
GH_AUTHORIZATION_BASE_URL = settings.gh_authorization_base_url
GH_TOKEN_URL = settings.gh_token_url
GH_FETCH_MEMBERSHIP_URL = settings.gh_fetch_membership_url
PULPITO_URL = settings.pulpito_url

log = logging.getLogger(__name__)
router = APIRouter(
Expand Down
5 changes: 3 additions & 2 deletions src/teuthology_api/routes/logout.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging, os
import logging
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import RedirectResponse
from teuthology_api.config import settings

PULPITO_URL = os.getenv("PULPITO_URL")
PULPITO_URL = settings.pulpito_url
log = logging.getLogger(__name__)

router = APIRouter(
Expand Down
7 changes: 5 additions & 2 deletions src/teuthology_api/services/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import uuid
from pathlib import Path

from fastapi import HTTPException, Request

Expand All @@ -11,7 +12,8 @@
import requests # Note: import requests after teuthology
from requests.exceptions import HTTPError

PADDLES_URL = os.getenv("PADDLES_URL")
PADDLES_URL = settings.paddles_url
ARCHIVE_DIR = settings.archive_dir

log = logging.getLogger(__name__)

Expand All @@ -22,7 +24,8 @@ def logs_run(func, args):
and return logs printed during the execution of the function.
"""
_id = str(uuid.uuid4())
log_file = f"/archive_dir/{_id}.log"
archive = Path(ARCHIVE_DIR)
log_file = archive / f"{_id}.log"

teuthology_process = Process(target=_execute_with_logs, args=(func, args, log_file))
teuthology_process.start()
Expand Down

0 comments on commit 08ecc9c

Please sign in to comment.