Coretex
__init__.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from pathlib import Path
19 
20 import os
21 import json
22 import shutil
23 import logging
24 
25 from .user import UserConfiguration
26 from .node import NodeConfiguration
27 from .base import CONFIG_DIR, DEFAULT_VENV_PATH, InvalidConfiguration, ConfigurationNotFound
28 from ..utils import isCliRuntime
29 
30 
31 def configMigration(configPath: Path) -> None:
32  with configPath.open("r") as file:
33  oldConfig = json.load(file)
34 
35  UserConfiguration({
36  "username": oldConfig.get("username"),
37  "password": oldConfig.get("password"),
38  "token": oldConfig.get("token"),
39  "refreshToken": oldConfig.get("refreshToken"),
40  "tokenExpirationDate": oldConfig.get("tokenExpirationDate"),
41  "refreshTokenExpirationDate": oldConfig.get("refreshTokenExpirationDate"),
42  "serverUrl": oldConfig.get("serverUrl"),
43  "projectId": oldConfig.get("projectId"),
44  }).save()
45 
46  NodeConfiguration({
47  "name": oldConfig.get("nodeName"),
48  "accessToken": oldConfig.get("nodeAccessToken"),
49  "storagePath": oldConfig.get("storagePath"),
50  "image": oldConfig.get("image"),
51  "allowGpu": oldConfig.get("allowGpu"),
52  "ram": oldConfig.get("nodeRam"),
53  "sharedMemory": oldConfig.get("nodeSharedMemory"),
54  "swap": oldConfig.get("nodeSwap"),
55  "cpuCount": oldConfig.get("cpuCount"),
56  "mode": oldConfig.get("nodeMode"),
57  "allowDocker": oldConfig.get("allowDocker"),
58  "secret": oldConfig.get("nodeSecret"),
59  "initScript": oldConfig.get("initScript"),
60  "modelId": oldConfig.get("modelId"),
61  "id": oldConfig.get("nodeId")
62  }).save()
63 
64  configPath.unlink()
65  if DEFAULT_VENV_PATH.exists():
66  shutil.rmtree(DEFAULT_VENV_PATH)
67 
68 
69 def _syncConfigWithEnv() -> None:
70  # If configuration doesn't exist default one will be created
71  # Initialization of User and Node Configuration classes will do
72  # the necessary sync between config properties and corresponding
73  # environment variables (e.g. storagePath -> CTX_STORAGE_PATH)
74 
75  # old configuration exists, fill out new config files with old configuration
76  oldConfigPath = CONFIG_DIR / "config.json"
77  if oldConfigPath.exists():
78  logging.warning(
79  f">> [Coretex] Old configuration found at path: {oldConfigPath}. Migrating to new configuration."
80  )
81  configMigration(oldConfigPath)
82 
83  try:
84  userConfig = UserConfiguration.load()
85  if not "CTX_API_URL" in os.environ:
86  os.environ["CTX_API_URL"] = userConfig.serverUrl
87  except (ConfigurationNotFound, InvalidConfiguration):
88  if not "CTX_API_URL" in os.environ:
89  os.environ["CTX_API_URL"] = "https://api.coretex.ai/"
90 
91  if not "CTX_STORAGE_PATH" in os.environ or isCliRuntime():
92  os.environ["CTX_STORAGE_PATH"] = f"{CONFIG_DIR}/data"