Coretex
update.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 enum import IntEnum
19 from pathlib import Path
20 
21 import requests
22 
23 from .utils import getExecPath
24 from .cron import jobExists, scheduleJob
25 from ..resources import RESOURCES_DIR
26 from ...utils import command
27 from ...configuration import DEFAULT_VENV_PATH
28 
29 
30 UPDATE_SCRIPT_NAME = "update_node.sh"
31 
32 
33 class NodeStatus(IntEnum):
34 
35  inactive = 1
36  active = 2
37  busy = 3
38  deleted = 4
39  reconnecting = 5
40 
41 
42 def generateUpdateScript() -> str:
43  dockerExecPath = getExecPath("docker")
44  gitExecPath = getExecPath("git")
45  bashScriptTemplatePath = RESOURCES_DIR / "update_script_template.sh"
46 
47  with bashScriptTemplatePath.open("r") as scriptFile:
48  bashScriptTemplate = scriptFile.read()
49 
50  return bashScriptTemplate.format(
51  dockerPath = dockerExecPath,
52  gitPath = gitExecPath,
53  venvPath = DEFAULT_VENV_PATH
54  )
55 
56 
57 def dumpScript(updateScriptPath: Path) -> None:
58  with updateScriptPath.open("w") as scriptFile:
59  scriptFile.write(generateUpdateScript())
60 
61  command(["chmod", "+x", str(updateScriptPath)], ignoreStdout = True)
62 
63 
64 def activateAutoUpdate() -> None:
65  updateScriptPath = DEFAULT_VENV_PATH.parent / UPDATE_SCRIPT_NAME
66  dumpScript(updateScriptPath)
67 
68  if not jobExists(str(updateScriptPath)):
69  scheduleJob(UPDATE_SCRIPT_NAME)