Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTX-5430: Coretex configuration centralization refactor. #167

Merged
merged 38 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
71bff33
CTX-5430: Created new configuration for user, saving changes...
Feb 15, 2024
51369b8
Merge branch 'develop' into CTX-5430n
Mar 27, 2024
3b9c70d
CTX-5430: Saving changes related to configuration refactor.
Mar 27, 2024
f785e2d
CTX-5430: First part of refactor finished, saving changes (stop and s…
Mar 29, 2024
2092ecf
CTX-5430: Refactored rest of the commands. Saving changes.
Mar 29, 2024
0a6ee75
Merge branch 'develop' into CTX-5430n
Apr 16, 2024
d885bc2
CTX-5430: Finished merge of develop into branch. This should fix all …
Apr 17, 2024
c81cd6a
CTX-5430: Cleanup changes saving.
Apr 17, 2024
521d7d6
CTX-5430: Saving changes.
Apr 17, 2024
a782934
Merge branch 'develop' into CTX-5430n
Apr 17, 2024
0c731ff
CTX-5430: Saving changes, cleaning code (have issue with imports and …
Apr 17, 2024
35b94bc
CTX-5430: Saving last changes.
Apr 17, 2024
0466801
CTX-5430: Saving changes ramLimit must be int not float.
Apr 17, 2024
4a80f55
CTX-5430: More code cleanup.
Apr 17, 2024
15d758c
Merge branch 'develop' into CTX-5430n
May 15, 2024
f3bb4b7
CTX-5430: import fix.
May 15, 2024
fa9a0f1
CTX-5430: Unit tests fix.
May 16, 2024
b8bca8b
Merge branch 'develop' into CTX-5430n
May 16, 2024
e1aa18a
Merge branch 'develop' into CTX-5430n
Jul 23, 2024
4ca0a8e
CTX-5430: Mypy errors fixed, fixing bugs while testing. Saving changes.
Jul 23, 2024
9b28f5e
CTX-5430: Rest of cleanup and possible bug fixes. Tests were failing …
Jul 24, 2024
e51959e
CTX-5430: Code cleanup, removed userdata.
Jul 24, 2024
f1e2b7c
CTX-5430n: testing cli on machines
Jul 24, 2024
82edb68
CTX-5430: Code cleanup testing finished.
Jul 24, 2024
0c258ad
CTX-5430: More cleaning, large string formatting.
Jul 24, 2024
981eacc
CTX-5430: Code cleanup based on comments from PR.
Jul 26, 2024
d7595f0
CTX-5430: Save changes, need to test further.
Aug 6, 2024
a0594c3
CTX-5430: Removed isConfigured() method since it was just causing tro…
Aug 6, 2024
9d57d18
Merge branch 'develop' into CTX-5430n
Aug 7, 2024
2d8f4ed
CTX-5430: Pipeline fix.
Aug 7, 2024
df5fa55
Merge branch 'develop' into CTX-5430n
Aug 7, 2024
596d3e7
CTX-5430: Cleanup, changes regarding discussions on PR.
Aug 9, 2024
a761b91
CTX-5430: Changes regarding pr discussions.
Aug 9, 2024
1899389
Merge branch 'develop' into CTX-5430n
Aug 9, 2024
702baa0
CTX-5430: Code and pipeline fix, imports issue solved.
Aug 9, 2024
5dfbf07
Merge branch 'develop' into CTX-5430n
Aug 12, 2024
c1b8e77
CTX-5430: Discussion changes and minor bug fixes (variables not decla…
Aug 12, 2024
2d5c774
CTX-5430: replaced modified files with ones from dev.
Aug 13, 2024
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
2 changes: 1 addition & 1 deletion coretex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# Internal - not for outside use
from ._logger import _initializeDefaultLogger, _initializeCLILogger
from .configuration import isCliRuntime
from .utils import isCliRuntime


if isCliRuntime():
Expand Down
23 changes: 12 additions & 11 deletions coretex/cli/commands/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@

import click

from ..modules.user import configUser
from ..modules.ui import clickPrompt, stdEcho, successEcho
from ...configuration import loadConfig, saveConfig, isUserConfigured
from ..modules import user, ui
from ...configuration import UserConfiguration, InvalidConfiguration, ConfigurationNotFound


@click.command()
def login() -> None:
config = loadConfig()
if isUserConfigured(config):
if not clickPrompt(
f"User already logged in with username {config['username']}.\nWould you like to log in with a different user (Y/n)?",
try:
userConfig = UserConfiguration.load()
if not ui.clickPrompt(
f"User already logged in with username {userConfig.username}.\nWould you like to log in with a different user (Y/n)?",
type = bool,
default = True,
show_default = False
):
return

stdEcho("Please enter your credentials:")
configUser(config)
saveConfig(config)
except (ConfigurationNotFound, InvalidConfiguration):
pass

successEcho(f"User {config['username']} successfully logged in.")
ui.stdEcho("Please enter your credentials:")
userConfig = user.configUser()
userConfig.save()
dule1322 marked this conversation as resolved.
Show resolved Hide resolved
ui.successEcho(f"User {userConfig.username} successfully logged in.")
13 changes: 8 additions & 5 deletions coretex/cli/commands/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import click

from ..modules import project_utils, user, utils, ui
from ..modules import ui
from ..modules.project_utils import getProject
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute
from ...entities import Model
from ...configuration import loadConfig
from ...configuration import UserConfiguration


@click.command()
Expand All @@ -13,12 +16,12 @@
@click.option("-p", "--project", type = str, required = False, default = None)
@click.option("-a", "--accuracy", type = click.FloatRange(0, 1), required = False, default = 1)
def create(name: str, path: str, project: Optional[str], accuracy: float) -> None:
config = loadConfig()
userConfig = UserConfiguration.load()

# If project was provided used that, otherwise get the one from config
# If project that was provided does not exist prompt user to create a new
# one with that name
ctxProject = project_utils.getProject(project, config)
ctxProject = getProject(project, userConfig)
if ctxProject is None:
return

Expand All @@ -32,7 +35,7 @@ def create(name: str, path: str, project: Optional[str], accuracy: float) -> Non


@click.group()
@utils.onBeforeCommandExecute(user.initializeUserSession)
@onBeforeCommandExecute(initializeUserSession)
def model() -> None:
pass

Expand Down
99 changes: 53 additions & 46 deletions coretex/cli/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,60 @@

import click

from ..modules import ui
from ..modules import node as node_module
from ..modules.ui import clickPrompt, stdEcho, successEcho, errorEcho, previewConfig
from ..modules.update import NodeStatus, getNodeStatus, activateAutoUpdate
from ..modules.utils import onBeforeCommandExecute, checkEnvironment
from ..modules.node import NodeStatus
from ..modules.user import initializeUserSession
from ...configuration import loadConfig, saveConfig, isNodeConfigured
from ..modules.utils import onBeforeCommandExecute, checkEnvironment
from ..modules.update import activateAutoUpdate, getNodeStatus
from ...utils import docker
from ...configuration import UserConfiguration, NodeConfiguration, InvalidConfiguration, ConfigurationNotFound


@click.command()
@click.option("--image", type = str, help = "Docker image url")
@onBeforeCommandExecute(node_module.initializeNodeConfiguration)
def start(image: Optional[str]) -> None:
config = loadConfig()
nodeConfig = NodeConfiguration.load()

if node_module.isRunning():
if not clickPrompt(
if not ui.clickPrompt(
"Node is already running. Do you wish to restart the Node? (Y/n)",
type = bool,
default = True,
show_default = False
):
return

node_module.stop(config.get("nodeId", node_module.fetchNodeId(config["nodeName"])))
node_module.stop(nodeConfig.id)

if node_module.exists():
node_module.clean()


if image is not None:
config["image"] = image # store forced image (flagged) so we can run autoupdate afterwards
saveConfig(config)
nodeConfig.image = image # store forced image (flagged) so we can run autoupdate afterwards
nodeConfig.save()

dockerImage = config["image"]
dockerImage = nodeConfig.image

if node_module.shouldUpdate(dockerImage):
node_module.pull(dockerImage)

node_module.start(dockerImage, config)
node_module.start(dockerImage, nodeConfig)
docker.removeDanglingImages(node_module.getRepoFromImageUrl(dockerImage), node_module.getTagFromImageUrl(dockerImage))

activateAutoUpdate()


@click.command()
def stop() -> None:
config = loadConfig()
nodeConfig = NodeConfiguration.load()

if not node_module.isRunning():
errorEcho("Node is already offline.")
ui.errorEcho("Node is already offline.")
return

node_module.stop(config.get("nodeId", node_module.fetchNodeId(config["nodeName"])))
node_module.stop(nodeConfig.id)


@click.command()
Expand All @@ -80,95 +81,101 @@ def stop() -> None:
@onBeforeCommandExecute(node_module.initializeNodeConfiguration)
def update(autoAccept: bool, autoDecline: bool) -> None:
if autoAccept and autoDecline:
errorEcho("Only one of the flags (\"-y\" or \"-n\") can be used at the same time.")
ui.errorEcho("Only one of the flags (\"-y\" or \"-n\") can be used at the same time.")
return

config = loadConfig()
dockerImage = config["image"]

nodeStatus = getNodeStatus()
nodeConfig = NodeConfiguration.load()
nodeStatus = node_module.getNodeStatus()

if nodeStatus == NodeStatus.inactive:
errorEcho("Node is not running. To update Node you need to start it first.")
ui.errorEcho("Node is not running. To update Node you need to start it first.")
return

if nodeStatus == NodeStatus.reconnecting:
errorEcho("Node is reconnecting. Cannot update now.")
ui.errorEcho("Node is reconnecting. Cannot update now.")
return

if nodeStatus == NodeStatus.busy and not autoAccept:
if autoDecline:
return

if not clickPrompt(
if not ui.clickPrompt(
"Node is busy, do you wish to terminate the current execution to perform the update? (Y/n)",
type = bool,
default = True,
show_default = False
):
return

node_module.stop(config.get("nodeId", node_module.fetchNodeId(config["nodeName"])))
node_module.stop(nodeConfig.id)

if not node_module.shouldUpdate(dockerImage):
successEcho("Node is already up to date.")
if not node_module.shouldUpdate(nodeConfig.image):
ui.successEcho("Node is already up to date.")
return

stdEcho("Updating node...")
node_module.pull(dockerImage)
ui.stdEcho("Fetching latest node image...")
node_module.pull(nodeConfig.image)

if getNodeStatus() == NodeStatus.busy and not autoAccept:
if autoDecline:
return

if not clickPrompt(
if not ui.clickPrompt(
"Node is busy, do you wish to terminate the current execution to perform the update? (Y/n)",
type = bool,
default = True,
show_default = False
):
return

node_module.stop(config.get("nodeId", node_module.fetchNodeId(config["nodeName"])))
node_module.stop(nodeConfig.id)

stdEcho("Updating node...")
node_module.start(dockerImage, config)
docker.removeDanglingImages(node_module.getRepoFromImageUrl(dockerImage), node_module.getTagFromImageUrl(dockerImage))
ui.stdEcho("Updating node...")
node_module.start(nodeConfig.image, nodeConfig)

docker.removeDanglingImages(
node_module.getRepoFromImageUrl(nodeConfig.image),
node_module.getTagFromImageUrl(nodeConfig.image)
)
activateAutoUpdate()


@click.command()
@click.option("--verbose", is_flag = True, help = "Configure node settings manually.")
def config(verbose: bool) -> None:
config = loadConfig()

@click.option("--advanced", is_flag = True, help = "Configure node settings manually.")
def config(advanced: bool) -> None:
if node_module.isRunning():
if not clickPrompt(
if not ui.clickPrompt(
"Node is already running. Do you wish to stop the Node? (Y/n)",
type = bool,
default = True,
show_default = False
):
errorEcho("If you wish to reconfigure your node, use coretex node stop commands first.")
ui.errorEcho("If you wish to reconfigure your node, use coretex node stop commands first.")
return

node_module.stop(config.get("nodeId", node_module.fetchNodeId(config["nodeName"])))
try:
nodeConfig = NodeConfiguration.load()
node_module.stop(nodeConfig.id)
except (ConfigurationNotFound, InvalidConfiguration):
node_module.stop()

if isNodeConfigured(config):
if not clickPrompt(
try:
nodeConfig = NodeConfiguration.load()
if not ui.clickPrompt(
"Node configuration already exists. Would you like to update? (Y/n)",
type = bool,
default = True,
show_default = False
):
return
except (ConfigurationNotFound, InvalidConfiguration):
pass

node_module.configureNode(config, verbose)
saveConfig(config)
previewConfig(config)
nodeConfig = node_module.configureNode(advanced)
nodeConfig.save()
ui.previewNodeConfig(nodeConfig)

successEcho("Node successfully configured.")
ui.successEcho("Node successfully configured.")
activateAutoUpdate()


Expand Down
20 changes: 12 additions & 8 deletions coretex/cli/commands/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

import click

from ..modules import ui, project_utils, utils, user
from ..modules import ui, project_utils
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute
from ...entities import Project, ProjectVisibility
from ...networking import RequestFailedError
from ...configuration import loadConfig
from ...configuration import UserConfiguration


@click.command()
Expand All @@ -31,10 +33,11 @@
@click.option("--description", "-d", type = str, help = "Project description")
def create(name: Optional[str], projectType: Optional[int], description: Optional[str]) -> None:
project = project_utils.createProject(name, projectType, description)
userConfig = UserConfiguration.load()

selectNewProject = ui.clickPrompt("Do you want to select the new project as default? (Y/n)", type = bool, default = True)
if selectNewProject:
project_utils.selectProject(project.id)
userConfig.selectProject(project.id)
ui.successEcho(f"Project \"{project.name}\" successfully selected.")


Expand All @@ -43,8 +46,8 @@ def create(name: Optional[str], projectType: Optional[int], description: Optiona
@click.option("--name", "-n", type = str, help = "New Project name")
@click.option("--description", "-d", type = str, help = "New Project description")
def edit(project: Optional[str], name: Optional[str], description: Optional[str]) -> None:
config = loadConfig()
defaultProjectId = config.get("projectId")
userConfiguration = UserConfiguration.load()
defaultProjectId = userConfiguration.projectId
if defaultProjectId is None and project is None:
ui.errorEcho(f"To use edit command you need to specifiy project name using \"--project\" or \"-p\" flag, or you can select default project using \"coretex project select\" command.")
return
Expand Down Expand Up @@ -73,24 +76,25 @@ def edit(project: Optional[str], name: Optional[str], description: Optional[str]
@click.argument("name", type = str)
def select(name: str) -> None:
project: Optional[Project] = None
userConfig = UserConfiguration.load()

ui.progressEcho("Validating project...")

try:
project = Project.fetchOne(name = name)
ui.successEcho(f"Project \"{name}\" selected successfully!")
project_utils.selectProject(project.id)
userConfig.selectProject(project.id)
except ValueError:
ui.errorEcho(f"Project \"{name}\" not found.")
project = project_utils.promptProjectCreate("Do you want to create a project with that name?", name)
if project is None:
return

project_utils.selectProject(project.id)
userConfig.selectProject(project.id)


@click.group()
@utils.onBeforeCommandExecute(user.initializeUserSession)
@onBeforeCommandExecute(initializeUserSession)
def project() -> None:
pass

Expand Down
Loading
Loading