-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the environment provider router (#53)
* Remove the environment provider router We'll now just let the ETOS API save the providers in the database instead of sending HTTP requests to the environment provider.
- Loading branch information
Showing
15 changed files
with
458 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright Axis Communications AB. | ||
# | ||
# For a full list of individual contributors, please see the commit history. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""ETCD helpers.""" | ||
import os | ||
from threading import Event | ||
from typing import Any, Iterator, Optional, Union | ||
|
||
from etcd3gw import client | ||
from etos_lib.lib.config import Config as ETOSConfig | ||
|
||
|
||
class ETCDPath: | ||
"""An ETCD path is like a filesystem path, but it works with keys in ETCD.""" | ||
|
||
def __init__(self, path: Union[str, bytes] = "/") -> None: | ||
"""Initialize.""" | ||
if ETOSConfig().get("database") is None: | ||
ETOSConfig().set( | ||
"database", | ||
client( | ||
host=os.getenv("ETOS_ETCD_HOST", "etcd-client"), | ||
port=int(os.getenv("ETOS_ETCD_PORT", "2379")), | ||
), | ||
) | ||
self.database: client = ETOSConfig().get("database") | ||
if isinstance(path, bytes): | ||
path = path.decode() | ||
self.path = path | ||
|
||
def join(self, new: str) -> "ETCDPath": | ||
"""Join this path with another path. | ||
:param new: New child path 'below' current. | ||
""" | ||
if new.startswith("/"): | ||
new = new[1:] | ||
return ETCDPath("/".join((self.path, new))) | ||
|
||
def write(self, value: Any, expire: Optional[int] = None) -> None: | ||
"""Write a value to an ETCD path. | ||
:param value: Value to write to database. | ||
:param expire: Optional expiration time in seconds. | ||
""" | ||
lease = None | ||
if expire is not None: | ||
lease = self.database.lease(expire) | ||
self.database.put(self.path, value, lease) | ||
|
||
def read(self) -> Optional[bytes]: | ||
"""Read the values from an ETCD path.""" | ||
try: | ||
return self.database.get(self.path)[0] | ||
except IndexError: | ||
return None | ||
|
||
def read_all(self) -> list[tuple[bytes, dict]]: | ||
"""Read values of all keys "below" a path.""" | ||
return self.database.get_prefix(self.path) | ||
|
||
def watch(self) -> tuple[Event, Iterator[dict]]: | ||
"""Watch an ETCD path for any changes.""" | ||
return self.database.watch(self.path) | ||
|
||
def watch_all(self) -> tuple[Event, Iterator[dict]]: | ||
"""Watch an ETCD path for any changes to itself or its children.""" | ||
return self.database.watch(self.path, range_end="\0") | ||
|
||
def delete(self) -> None: | ||
"""Delete the ETCD path.""" | ||
self.database.delete(self.path) | ||
|
||
def delete_all(self) -> None: | ||
"""Delete the ETCD path and paths "below".""" | ||
self.database.delete_prefix(self.path) | ||
|
||
def __str__(self) -> str: | ||
"""Represent the ETCD path as a string.""" | ||
return self.path | ||
|
||
def __repr__(self) -> str: | ||
"""Represent the ETCD path as a string.""" | ||
return self.path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Copyright Axis Communications AB. | ||
# | ||
# For a full list of individual contributors, please see the commit history. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Environment for ETOS testruns.""" | ||
import json | ||
from collections import OrderedDict | ||
from typing import Optional, Union | ||
|
||
from pydantic import BaseModel # pylint:disable=no-name-in-module | ||
|
||
from .database import ETCDPath | ||
|
||
|
||
class Configuration(BaseModel): | ||
"""Model for the ETOS testrun configuration.""" | ||
|
||
suite_id: str | ||
dataset: Union[dict, list] | ||
execution_space_provider: str | ||
iut_provider: str | ||
log_area_provider: str | ||
|
||
|
||
async def configure_testrun(configuration: Configuration) -> None: | ||
"""Configure an ETOS testrun with the configuration passed by user. | ||
:param configuration: The configuration to save. | ||
""" | ||
testrun = ETCDPath(f"/testrun/{configuration.suite_id}") | ||
providers = ETCDPath("/environment/provider") | ||
|
||
await do_configure( | ||
providers.join(f"log-area/{configuration.log_area_provider}"), | ||
configuration.log_area_provider, | ||
testrun.join("provider/log-area"), | ||
) | ||
await do_configure( | ||
providers.join(f"execution-space/{configuration.execution_space_provider}"), | ||
configuration.execution_space_provider, | ||
testrun.join("provider/execution-space"), | ||
) | ||
await do_configure( | ||
providers.join(f"iut/{configuration.iut_provider}"), | ||
configuration.iut_provider, | ||
testrun.join("provider/iut"), | ||
) | ||
await save_json(testrun.join("provider/dataset"), configuration.dataset) | ||
|
||
|
||
async def do_configure(path: ETCDPath, provider_id: str, testrun: ETCDPath) -> None: | ||
"""Configure a provider based on provider ID and save it to a testrun. | ||
:param path: Path to load provider from. | ||
:param provider_id: The ID of the provider to load. | ||
:param testrun: Where to store the loaded provider. | ||
""" | ||
if (provider := await load(path)) is None: | ||
raise AssertionError(f"{provider_id} does not exist") | ||
await save_json(testrun, provider) | ||
|
||
|
||
async def load(path: ETCDPath) -> Optional[dict]: | ||
"""Load a provider from an ETCD path. | ||
:param path: Path to load data from. Will assume it's JSON and load is as such. | ||
""" | ||
provider = path.read() | ||
if provider: | ||
return json.loads(provider, object_pairs_hook=OrderedDict) | ||
return None | ||
|
||
|
||
async def save_json(path: ETCDPath, data: dict, expire=3600) -> None: | ||
"""Save data as json to an ETCD path. | ||
:param path: The path to store data on. | ||
:param data: The data to save. Will be dumped to JSON before saving. | ||
:param expire: How long, in seconds, to set the expiration to. | ||
""" | ||
path.write(json.dumps(data), expire=expire) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
python/src/etos_api/routers/environment_provider/__init__.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.