-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added K8S and Rabbit MQ management utilities * added callback on missing k8s config * Fixed backward compatibility issue with pymongo * Added submind state processor * Moved out mq validation utility * incremented subversion * added retry-wait for klatchat observer * Fixed issue with caching * Simplified socket io logic * Added license notice --------- Co-authored-by: NeonKirill <[email protected]>
- Loading branch information
1 parent
3ab7ecd
commit eb85ddc
Showing
17 changed files
with
476 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework | ||
# All trademark and other rights reserved by their respective owners | ||
# Copyright 2008-2022 Neongecko.com Inc. | ||
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, | ||
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo | ||
# BSD-3 License | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# 3. Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from this | ||
# software without specific prior written permission. | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
from fastapi import APIRouter | ||
from starlette.requests import Request | ||
|
||
from utils.logging_utils import LOG | ||
from utils.http_utils import respond | ||
|
||
from chat_server.server_config import k8s_config | ||
from chat_server.server_utils.auth import login_required | ||
from chat_server.server_utils.k8s_utils import restart_deployment | ||
from chat_server.server_utils.admin_utils import run_mq_validation | ||
|
||
router = APIRouter( | ||
prefix="/admin", | ||
responses={"404": {"description": "Unknown authorization endpoint"}}, | ||
) | ||
|
||
|
||
@router.post("/refresh/{service_name}") | ||
@login_required(tmp_allowed=False, required_roles=["admin"]) | ||
async def refresh_state( | ||
request: Request, service_name: str, target_items: str | None = "" | ||
): | ||
""" | ||
Refreshes state of the target | ||
:param request: Starlette Request Object | ||
:param service_name: name of service to refresh | ||
:param target_items: comma-separated list of items to refresh | ||
:returns JSON-formatted response from server | ||
""" | ||
target_items = [x for x in target_items.split(",") if x] | ||
if service_name == "k8s": | ||
if not k8s_config: | ||
return respond("K8S Service Unavailable", 503) | ||
deployments = target_items | ||
if deployments == "*": | ||
deployments = k8s_config.get("MANAGED_DEPLOYMENTS", []) | ||
LOG.info(f"Restarting {deployments=!r}") | ||
for deployment in deployments: | ||
restart_deployment(deployment_name=deployment) | ||
elif service_name == "mq": | ||
run_mq_validation() | ||
else: | ||
return respond(f"Unknown refresh type: {service_name!r}", 404) | ||
return respond("OK") |
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,52 @@ | ||
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System | ||
# All trademark and other rights reserved by their respective owners | ||
# Copyright 2008-2021 Neongecko.com Inc. | ||
# BSD-3 | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# 3. Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from this | ||
# software without specific prior written permission. | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
from chat_server.server_config import mq_api, mq_management_config, LOG | ||
|
||
|
||
def run_mq_validation(): | ||
if mq_api: | ||
for vhost in mq_management_config.get("VHOSTS", []): | ||
status = mq_api.add_vhost(vhost=vhost["name"]) | ||
if not status.ok: | ||
raise ConnectionError(f'Failed to add {vhost["name"]}, {status=}') | ||
for user_creds in mq_management_config.get("USERS", []): | ||
mq_api.add_user( | ||
user=user_creds["name"], | ||
password=user_creds["password"], | ||
tags=user_creds.get("tags", ""), | ||
) | ||
for user_vhost_permissions in mq_management_config.get( | ||
"USER_VHOST_PERMISSIONS", [] | ||
): | ||
mq_api.configure_vhost_user_permissions(**user_vhost_permissions) | ||
else: | ||
LOG.error("MQ API is unavailable") | ||
|
||
|
||
if __name__ == "__main__": | ||
run_mq_validation() |
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
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,80 @@ | ||
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework | ||
# All trademark and other rights reserved by their respective owners | ||
# Copyright 2008-2022 Neongecko.com Inc. | ||
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, | ||
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo | ||
# BSD-3 License | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# 3. Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from this | ||
# software without specific prior written permission. | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import datetime | ||
import os | ||
|
||
from kubernetes import client, config | ||
from kubernetes.client.rest import ApiException | ||
|
||
from chat_server.server_config import k8s_config | ||
from utils.logging_utils import LOG | ||
|
||
k8s_app_api = None | ||
_k8s_default_namespace = "default" | ||
|
||
if _k8s_config_path := k8s_config.get("K8S_CONFIG_PATH"): | ||
_k8s_default_namespace = ( | ||
k8s_config.get("K8S_DEFAULT_NAMESPACE") or _k8s_default_namespace | ||
) | ||
config.load_kube_config(_k8s_config_path) | ||
|
||
k8s_app_api = client.AppsV1Api() | ||
else: | ||
LOG.warning("K8S config is unset!") | ||
|
||
|
||
def restart_deployment(deployment_name: str, namespace: str = _k8s_default_namespace): | ||
""" | ||
Restarts K8S deployment | ||
:param deployment_name: name of the deployment to restart | ||
:param namespace: name of the namespace | ||
""" | ||
if not k8s_app_api: | ||
LOG.error( | ||
f"Failed to restart {deployment_name=!r} ({namespace=!r}) - missing K8S configs" | ||
) | ||
return -1 | ||
now = datetime.datetime.utcnow() | ||
now = str(now.isoformat() + "Z") | ||
body = { | ||
"spec": { | ||
"template": { | ||
"metadata": {"annotations": {"kubectl.kubernetes.io/restartedAt": now}} | ||
} | ||
} | ||
} | ||
try: | ||
k8s_app_api.patch_namespaced_deployment( | ||
deployment_name, namespace, body, pretty="true" | ||
) | ||
except ApiException as e: | ||
LOG.error( | ||
"Exception when calling AppsV1Api->read_namespaced_deployment_status: %s\n" | ||
% e | ||
) |
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
Oops, something went wrong.