Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ pip install saagieapi==<version>

### Compatibility with your Saagie platform

| **Saagie platform version** | **saagie-api release** |
|-----------------------------|------------------------|
| < 2.2.0 | < 0.6.0 |
| >= 2.2.0 | >= 0.6.0 |
When instantiating a SaagieApi instance, a check is made to ensure that your Saagie platform version is compatible with
the version of SaagieApi you are using.
This check is based on the `saagieapi/projects/compatibility_matrix.json` file.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion saagieapi/projects/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def _authenticate(realm, url, login, password):
r = s.post(url + '/authentication/api/open/authenticate',
json={'login': login, 'password': password},
verify=False)
return r.text
return r.text
12 changes: 12 additions & 0 deletions saagieapi/projects/compatibility_matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"2.1.0": {
"min_api_saagie_version": "0.5.1",
"max_api_saagie_version": "0.5.8"
},
"2.2.0": {
"min_api_saagie_version": "0.6.0"
},
"2.2.1": {
"min_api_saagie_version": "0.6.0"
}
}
83 changes: 75 additions & 8 deletions saagieapi/projects/saagie_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
Projects & Jobs - to interact with the manager API, see the manager subpackage)

"""
import importlib.metadata
import json
import logging
import time
import re
import pytz
import time
from json import JSONDecodeError
from pathlib import Path
from croniter import croniter

from gql import gql
import deprecation
import pytz
from croniter import croniter
from gql import Client
from gql import gql
from gql.transport.requests import RequestsHTTPTransport
from packaging import version
from packaging.version import Version

from .auth import *
from .gql_template import *
from .graph_pipeline import *
import deprecation

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", datefmt="%d/%m/%Y %H:%M:%S")
logging.getLogger("requests").setLevel(logging.WARN)
logging.getLogger("gql").setLevel(logging.WARN)

dir_path = Path(__file__).resolve().parent


class SaagieApi:
Expand Down Expand Up @@ -85,13 +96,16 @@ def __init__(self, url_saagie, id_platform, user, password, realm, retries=0):
# Valid status list of alerting
self.valid_status_list = ["REQUESTED", "QUEUED", "RUNNING", "FAILED", "KILLED",
"KILLING", "SUCCEEDED", "UNKNOWN", "AWAITING", "SKIPPED"]
self.saagie_current_version = self.__get_saagie_current_version()
logging.info(f"Current saagie version : {self.saagie_current_version}")
self.check_saagie_version_compatibility()

@classmethod
def easy_connect(cls, url_saagie_platform, user, password):
"""
Alternative constructor which uses the complete URL (eg:
https://saagie-workspace.prod.saagie.io/projects/platform/6/) and will
parse it in order to retrieve the platform URL, platform id and the
Alternative constructor which uses the complete URL (eg:
https://saagie-workspace.prod.saagie.io/projects/platform/6/) and will
parse it in order to retrieve the platform URL, platform id and the
realm.

Parameters
Expand Down Expand Up @@ -119,6 +133,59 @@ def easy_connect(cls, url_saagie_platform, user, password):
# ### env vars ####
# ######################################################

def __get_saagie_current_version(self):
"""
Retrieve the current Saagie version on the target platform
:return: Saagie major version
"""
try:
saagie_versions = self.auth(requests.session()).get(f"{self.url_saagie}version.json").json()
return saagie_versions['major']
except (JSONDecodeError, KeyError):
logging.warning("Could not get Saagie version")
return "unknown-version "

def __get_min_max_saagie_versions(self) -> (Version, Version):
"""
Retrieve the minimum and maximum Saagie version supported by the Saagie API
based on the compatibility matrix
:return: (min_version, max_version)
"""
saagie_compatibility_matrix = json.load(open(dir_path.joinpath('compatibility_matrix.json')))
logging.debug(f"Compatibility matrix : {saagie_compatibility_matrix}")
try:
compatible_versions = saagie_compatibility_matrix[self.saagie_current_version]
minimal_version = version.parse(compatible_versions.get("min_api_saagie_version", "0"))
maximal_version = version.parse(compatible_versions.get("max_api_saagie_version", "9.9"))
return minimal_version, maximal_version
except KeyError:
logging.warning(
f"Could not find your Saagie version ({self.saagie_current_version}) in the compatibility matrix")
return version.parse("0"), version.parse("9.9")

def check_saagie_version_compatibility(self):
"""
Check if the Saagie version is compatible with the Saagie API version and display warnings if not
"""
saagie_api_current_version = version.parse(importlib.metadata.version("saagieapi"))

logging.info(f"Current api-saagie version : {saagie_api_current_version}")
min_version, max_version = self.__get_min_max_saagie_versions()
if saagie_api_current_version < min_version:
logging.warning(
f"You are using a saagie-api version ({saagie_api_current_version}) "
f"not compatible with your Saagie platform (Saagie v.{self.saagie_current_version})")
logging.warning(
f"Your Saagie platform requires at least the version {min_version} "
f"of saagieapi. Please consider upgrading.")
if saagie_api_current_version > max_version:
logging.warning(
f"You are using a saagie-api version ({saagie_api_current_version}) "
f"not compatible with your Saagie platform (Saagie v.{self.saagie_current_version})")
logging.warning(
f"Your Saagie platform is not compatible with versions > {max_version} "
f"of saagieapi. Please consider downgrading.")

def get_global_env_vars(self):
"""Get global environment variables
NB: You can only list environment variables if you have at least the
Expand Down