18 from typing
import Optional
19 from pathlib
import Path
23 from ..modules
import ui
24 from ..modules
import node
as node_module
25 from ..modules.node
import NodeStatus, getNodeStatus
26 from ..modules.user
import initializeUserSession
27 from ..modules.utils
import onBeforeCommandExecute, checkEnvironment
28 from ..modules.update
import activateAutoUpdate
29 from ...utils
import docker
30 from ...configuration
import NodeConfiguration, InvalidConfiguration, ConfigurationNotFound
34 @click.option("--image", type = str, help = "Docker image url")
35 @onBeforeCommandExecute(node_module.initializeNodeConfiguration)
36 def start(image: Optional[str]) ->
None:
37 nodeConfig = NodeConfiguration.load()
39 if node_module.isRunning():
40 if not ui.clickPrompt(
41 "Node is already running. Do you wish to restart the Node? (Y/n)",
48 node_module.stop(nodeConfig.id)
50 if node_module.exists():
55 nodeConfig.image = image
58 dockerImage = nodeConfig.image
60 if node_module.shouldUpdate(dockerImage):
61 node_module.pull(dockerImage)
63 node_module.start(dockerImage, nodeConfig)
64 docker.removeDanglingImages(node_module.getRepoFromImageUrl(dockerImage), node_module.getTagFromImageUrl(dockerImage))
70 nodeConfig = NodeConfiguration.load()
72 if not node_module.isRunning():
73 ui.errorEcho(
"Node is already offline.")
76 node_module.stop(nodeConfig.id)
80 @click.option("-y", "autoAccept", is_flag = True, help = "Accepts all prompts.")
81 @click.option("-n", "autoDecline", is_flag = True, help = "Declines all prompts.")
82 @onBeforeCommandExecute(node_module.initializeNodeConfiguration)
83 def update(autoAccept: bool, autoDecline: bool) ->
None:
84 if autoAccept
and autoDecline:
85 ui.errorEcho(
"Only one of the flags (\"-y\" or \"-n\") can be used at the same time.")
88 nodeConfig = NodeConfiguration.load()
89 nodeStatus = node_module.getNodeStatus()
91 if nodeStatus == NodeStatus.inactive:
92 ui.errorEcho(
"Node is not running. To update Node you need to start it first.")
95 if nodeStatus == NodeStatus.reconnecting:
96 ui.errorEcho(
"Node is reconnecting. Cannot update now.")
99 if nodeStatus == NodeStatus.busy
and not autoAccept:
103 if not ui.clickPrompt(
104 "Node is busy, do you wish to terminate the current execution to perform the update? (Y/n)",
111 node_module.stop(nodeConfig.id)
113 if not node_module.shouldUpdate(nodeConfig.image):
114 ui.successEcho(
"Node is already up to date.")
117 ui.stdEcho(
"Fetching latest node image...")
118 node_module.pull(nodeConfig.image)
120 if getNodeStatus() == NodeStatus.busy
and not autoAccept:
124 if not ui.clickPrompt(
125 "Node is busy, do you wish to terminate the current execution to perform the update? (Y/n)",
132 node_module.stop(nodeConfig.id)
134 ui.stdEcho(
"Updating node...")
135 node_module.start(nodeConfig.image, nodeConfig)
137 docker.removeDanglingImages(
138 node_module.getRepoFromImageUrl(nodeConfig.image),
139 node_module.getTagFromImageUrl(nodeConfig.image)
145 @click.option("--advanced", is_flag = True, help = "Configure node settings manually.")
146 def config(advanced: bool) ->
None:
147 if node_module.isRunning():
148 if not ui.clickPrompt(
149 "Node is already running. Do you wish to stop the Node? (Y/n)",
154 ui.errorEcho(
"If you wish to reconfigure your node, use coretex node stop commands first.")
158 nodeConfig = NodeConfiguration.load()
159 node_module.stop(nodeConfig.id)
160 except (ConfigurationNotFound, InvalidConfiguration):
164 nodeConfig = NodeConfiguration.load()
165 if not ui.clickPrompt(
166 "Node configuration already exists. Would you like to update? (Y/n)",
172 except (ConfigurationNotFound, InvalidConfiguration):
175 nodeConfig = node_module.configureNode(advanced)
177 ui.previewNodeConfig(nodeConfig)
179 ui.successEcho(
"Node successfully configured.")
184 def status() -> None:
185 nodeStatus = getNodeStatus()
190 "reconnecting":
"yellow"
192 statusEcho = click.style(nodeStatus.name, fg = statusColors[nodeStatus.name])
193 click.echo(f
"Node is {statusEcho}.")
197 @click.option("--tail", "-n", type = int, help = "Shows N last logs.")
198 @click.option("--follow", "-f", is_flag = True, help = "Displays logs realtime.")
199 @click.option("--timestamps", "-t", is_flag = True, help = "Displays timestamps for logs.")
200 def logs(tail: Optional[int], follow: bool, timestamps: bool) ->
None:
201 if not node_module.isRunning():
202 ui.errorEcho(
"There is no currently running Node on the machine.")
205 node_module.showLogs(tail, follow, timestamps)
209 @onBeforeCommandExecute(docker.isDockerAvailable, excludeSubcommands = ["status"])
210 @onBeforeCommandExecute(initializeUserSession)
211 @onBeforeCommandExecute(node_module.checkResourceLimitations, excludeSubcommands = ["status"])
212 @onBeforeCommandExecute(checkEnvironment)
217 node.add_command(start,
"start")
218 node.add_command(stop,
"stop")
219 node.add_command(update,
"update")
220 node.add_command(config,
"config")
221 node.add_command(status,
"status")
222 node.add_command(logs,
"logs")