|
21 | 21 | This module provides the REST API for the image gateway. |
22 | 22 | """ |
23 | 23 |
|
24 | | -import json |
25 | | -import os |
26 | 24 | import logging |
27 | | -import shifter_imagegw |
28 | 25 | from shifter_imagegw.errors import AuthenticationError |
29 | 26 | from shifter_imagegw.imagemngr import ImageMngr |
30 | 27 | from contextlib import asynccontextmanager |
31 | 28 | from fastapi import FastAPI, HTTPException, Request, Header, Query |
32 | 29 | from fastapi.responses import JSONResponse, PlainTextResponse |
33 | 30 | from pydantic import BaseModel |
| 31 | +from shifter_imagegw.config import Config |
34 | 32 |
|
35 | 33 |
|
36 | 34 | mgr = None |
37 | | -logger = logging.getLogger("fastapi.root") |
| 35 | +logger = logging.getLogger("imagegwapi") |
38 | 36 |
|
39 | 37 |
|
40 | 38 | @asynccontextmanager |
41 | 39 | async def lifespan(app: FastAPI): |
42 | 40 | global config |
43 | | - if 'GWCONFIG' in os.environ: |
44 | | - CONFIG_FILE = os.environ['GWCONFIG'] |
45 | | - else: |
46 | | - CONFIG_FILE = f'{shifter_imagegw.CONFIG_PATH}/imagemanager.json' |
47 | | - # Configure logging |
48 | | - logger.debug('Initializing api image manager') |
49 | | - |
50 | | - logger.info(f"initializing with {CONFIG_FILE}") |
51 | | - with open(CONFIG_FILE) as config_file: |
52 | | - config = json.load(config_file) |
53 | | - if 'LogLevel' in config: |
54 | | - LOG_STRING = config['LogLevel'].lower() |
55 | | - if LOG_STRING == 'debug': |
56 | | - logger.setLevel(logging.DEBUG) |
57 | | - elif LOG_STRING == 'info': |
58 | | - logger.setLevel(logging.INFO) |
59 | | - elif LOG_STRING == 'warn': |
60 | | - logger.setLevel(logging.WARN) |
61 | | - elif LOG_STRING == 'error': |
62 | | - logger.setLevel(logging.ERROR) |
63 | | - elif LOG_STRING == 'critical': |
64 | | - logger.setLevel(logging.CRITICAL) |
65 | | - else: |
66 | | - logger.critical('Unrecongnized Log Level specified') |
| 41 | + config = Config() |
| 42 | + logger.setLevel(config.LogLevel) |
67 | 43 | global mgr |
68 | 44 | mgr = ImageMngr(config, logname="fastapi.root") |
69 | 45 | yield |
@@ -264,10 +240,10 @@ async def doimport(system: str, imgtype: str, tag: str, data: ImportImage, |
264 | 240 | # Convert to integers |
265 | 241 | i['groupACL'] = list(map(lambda x: int(x), |
266 | 242 | data.allowed_gids.split(','))) |
267 | | - if 'ImportUsers' not in config: |
| 243 | + if not config.ImportUsers: |
268 | 244 | raise HTTPException(status_code=403, detail="User image import from file disabled.") |
269 | 245 |
|
270 | | - iusers = config['ImportUsers'] |
| 246 | + iusers = config.ImportUsers |
271 | 247 | # If ImportUsers is None, no one can do this |
272 | 248 | if iusers.lower() == "none": |
273 | 249 | raise HTTPException(status_code=403, detail="User image import from file disabled.") |
|
0 commit comments