Skip to content

Commit

Permalink
add node_last_snapshot, update remove_snapshots, update execute_starc…
Browse files Browse the repository at this point in the history
…hiver
  • Loading branch information
netmet1 committed May 1, 2024
1 parent b7c847c commit a00c0c4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
110 changes: 87 additions & 23 deletions modules/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from time import sleep, perf_counter
from datetime import datetime, timedelta
from os import system, path, get_terminal_size, remove, walk, chmod, stat, makedirs, SEEK_END, SEEK_CUR
from os import system, path, get_terminal_size, popen, remove, walk, chmod, stat, makedirs, SEEK_END, SEEK_CUR
from sys import exit
from types import SimpleNamespace
from getpass import getpass
Expand Down Expand Up @@ -4965,6 +4965,57 @@ def cli_create_p12(self,command_list):
p12.create_individual_p12(self)


def cli_node_latest_snapshot(self,command_list):
self.functions.check_for_help(command_list,"node_latest_snapshot")
value_only = True if "value_only" in command_list else False

profile = command_list[command_list.index("-p")+1]
snapshot_dir = f"{self.functions.default_tessellation_dir}{profile}/data/incremental_snapshot"

if self.config_obj[profile]["layer"] > 0:
self.error_messages.error_code_messages({
"error_code": "cli-4977",
"line_code": "invalid_layer",
"extra": "1",
})

cmd = f'find {snapshot_dir} -maxdepth 1 -type f -printf "%f\n" '
cmd += "| grep '^[0-9.]\{1,8\}$' | sort -n | tail -n 1"

with ThreadPoolExecutor() as executor:
self.functions.status_dots = True
status_obj = {
"text_start": f"Reviewing snapshots",
"status": "please wait",
"status_color": "yellow",
"dotted_animation": True,
"newline": False,
}
_ = executor.submit(self.functions.print_cmd_status,status_obj)
possible_end = popen(cmd)
possible_end = int(possible_end.read().strip())
self.functions.status_dots = False
self.functions.print_cmd_status({
**status_obj,
"status": "completed",
"status_color": "green",
"dotted_animation": False,
"newline": True,
})

if value_only: return snapshot_dir,possible_end

print_out_list = [{
"PROFILE": profile,
"LAST FOUND LOCAL SNAPSHOT": possible_end,
}]

for header_elements in print_out_list:
self.functions.print_show_output({
"header_elements" : header_elements
})


def cli_remove_snapshots(self,command_list):
self.log.logger.info("cli -> remove_snapshots initiated.")

Expand All @@ -4980,13 +5031,24 @@ def cli_remove_snapshots(self,command_list):
["necessary. This feature can lead to unpredictable and undesired affects on your existing Node.",2,"red"],
])

found = False
profile = command_list[command_list.index("-p")+1]
snapshot_dir, possible_end = self.cli_node_latest_snapshot(["value_only","-p",profile])

int_error = False
missing = []

start = input(colored(" Please enter the start snapshot: ","cyan"))
end = input(colored(" Please enter the end snapshot: ","cyan"))
try:
start = int(start)
end = int(end)
except:
end = input(colored(f" Please enter the end snapshot [{colored(possible_end,'yellow')}]: ","cyan"))
if end == "" or end == None:
try: end = possible_end+1
except: int_error = True
else:
try: end += 1
except: int_error = True
try: start = int(start)
except: int_error = True

if int_error:
self.error_messages.error_code_messages({
"error_code": "cli-4823",
"line_code": "input_error",
Expand All @@ -4996,8 +5058,18 @@ def cli_remove_snapshots(self,command_list):

print("")

profile = command_list[command_list.index("-p")+1]
snapshot_dir = f"{self.functions.default_tessellation_dir}{profile}/data/incremental_snapshot"
self.functions.print_cmd_status({
"text_start": "Starting snapshot",
"status": start,
"status_color": "yellow",
"newline": True,
})
self.functions.print_cmd_status({
"text_start": "Ending snapshot",
"status": end,
"status_color": "yellow",
"newline": True,
})

self.build_node_class()
self.set_profile(profile)
Expand Down Expand Up @@ -5028,22 +5100,13 @@ def cli_remove_snapshots(self,command_list):
["Removing",0,"red"], [str(snap_to_remove),1,"red","bold"],
])
remove(snap_to_remove)
else:
missing.append(snap_to_remove)

# for current_snap in range(start, end):
# file_path = path.join(snapshot_dir, str(current_snap))
# if path.exists(file_path):
# found = True
# inode = stat(file_path).st_ino
# for root, dirs, files in walk(snapshot_dir):
# for name in files:
# if stat(path.join(root, name)).st_ino == inode:
# snap_to_remove = path.join(root, name)
# print(colored(f" Removing {snap_to_remove}","red"))
# remove(snap_to_remove)

if not found:
if len(missing) > 0:
self.functions.print_paragraphs([
["",1], [f"No snapshots found between [{start}] and [{end}]",1],
["",1], [f"Some snapshots were not found between [{start}] and [{end}]",1],
["This can be safely ignored...",2,"white"],
])
self.functions.print_paragraphs([
[f"Removal operations complete, please restart profile [{profile}]",0],
Expand Down Expand Up @@ -5186,6 +5249,7 @@ def send_error(extra2):
data_path = f"/var/tessellation/{profile}/data/"
cluster = self.config_obj[profile]["environment"]
bashCommand = f"{local_path} --data-path {data_path} --cluster {cluster}"
if "--timedate" in command_list: bashCommand += f" --timedate {command_list[command_list.index("--timedate")+1]}"
if "-d" in command_list: bashCommand += " -d"
if "-o" in command_list: bashCommand += " -o"
self.log.logger.debug(f"cli -> execute_starchiver -> executing starchiver | profile [{profile}] | cluster [{cluster}] | command referenced [{bashCommand}]")
Expand Down
1 change: 1 addition & 0 deletions modules/config/valid_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def pull_valid_command():

"nodeid",
"nodeid2dag",
"node_last_snapshot",

"peers",
"passwd12",
Expand Down
8 changes: 4 additions & 4 deletions modules/shell_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,6 @@ def start_cli(self,argv):
self.cli.show_node_states(self.argv)
elif self.called_command == "passwd12":
return_value = self.cli.passwd12(self.argv)
elif self.called_command == "migrate_node":
self.cli.migrate_node(self.argv)
elif self.called_command == "reboot":
self.cli.cli_reboot(self.argv)
elif self.called_command == "remote_access" or self.called_command == "_ra":
Expand Down Expand Up @@ -322,6 +320,8 @@ def start_cli(self,argv):
self.cli.check_connection(self.argv)
elif self.called_command == "remove_snapshots":
self.cli.cli_remove_snapshots(self.argv)
elif self.called_command == "node_last_snapshot":
self.cli.cli_node_latest_snapshot(self.argv)
elif self.called_command == "send_logs" or self.called_command == "_sl":
self.cli.prepare_and_send_logs(self.argv)
elif self.called_command == "check_seedlist_participation" or self.called_command == "_cslp":
Expand Down Expand Up @@ -538,7 +538,7 @@ def check_developer_only_commands(self):
if self.config_obj["global_elements"]["developer_mode"]: return

develop_commands = [
"execute_starchiver","remove_snapshots",
"remove_snapshots",
]
if self.called_command in develop_commands:
self.called_command = "help_only"
Expand Down Expand Up @@ -604,7 +604,7 @@ def send_to_help_method(hint):
"check_seedlist","_csl",
"show_service_log","_ssl","download_status","_ds",
"show_dip_error","_sde","check_consensus","_con",
"check_minority_fork","_cmf",
"check_minority_fork","_cmf","node_last_snapshot",
"execute_starchiver","remove_snapshots"
]

Expand Down

0 comments on commit a00c0c4

Please sign in to comment.