Rename log functions for consistency
This commit is contained in:
parent
5e88ee5819
commit
943f5d35c5
|
@ -9,13 +9,13 @@ from threading import Thread
|
|||
log = logging.getLogger('mynode')
|
||||
log.setLevel(logging.INFO)
|
||||
|
||||
def print_and_log(msg):
|
||||
def log_message(msg):
|
||||
global log
|
||||
print(msg)
|
||||
log.info(msg)
|
||||
|
||||
def set_clone_state(state):
|
||||
print_and_log("Clone State: {}".format(state))
|
||||
log_message("Clone State: {}".format(state))
|
||||
try:
|
||||
with open("/tmp/.clone_state", "w") as f:
|
||||
f.write(state)
|
||||
|
@ -35,7 +35,7 @@ def reset_clone_rescan():
|
|||
os.system("rm /tmp/.clone_rescan")
|
||||
|
||||
def set_clone_error(error_msg):
|
||||
print_and_log("Clone Error: {}".format(error_msg))
|
||||
log_message("Clone Error: {}".format(error_msg))
|
||||
try:
|
||||
with open("/tmp/.clone_error", "w") as f:
|
||||
f.write(error_msg)
|
||||
|
@ -57,7 +57,7 @@ def get_drive_size(drive):
|
|||
size = int(parts[3])
|
||||
except:
|
||||
pass
|
||||
print_and_log(f"Drive {drive} size: {size}")
|
||||
log_message(f"Drive {drive} size: {size}")
|
||||
return size
|
||||
|
||||
|
||||
|
@ -131,12 +131,12 @@ def main():
|
|||
|
||||
# Detect drives
|
||||
drives = find_drives()
|
||||
print_and_log(f"Drives: {drives}")
|
||||
log_message(f"Drives: {drives}")
|
||||
|
||||
# Check exactly two drives found
|
||||
drive_count = len(drives)
|
||||
if drive_count != 2:
|
||||
print_and_log("Clone tool did not find 2 drives!")
|
||||
log_message("Clone tool did not find 2 drives!")
|
||||
set_clone_state("error")
|
||||
set_clone_error("Clone tool needs 2 drives! Found {}.".format(drive_count))
|
||||
wait_on_clone_error_dismiss()
|
||||
|
@ -150,7 +150,7 @@ def main():
|
|||
both_drives_have_mynode = False
|
||||
for d in drives:
|
||||
partitions = find_partitions_for_drive(d)
|
||||
print_and_log(f"Drive {d} paritions: {partitions}")
|
||||
log_message(f"Drive {d} paritions: {partitions}")
|
||||
|
||||
if len(partitions) == 0:
|
||||
# No partition found - must be target drive since its empty
|
||||
|
@ -189,7 +189,7 @@ def main():
|
|||
mynode_drive = d
|
||||
target_found = True
|
||||
else:
|
||||
print_and_log(f"myNode Partition Found: {p}")
|
||||
log_message(f"myNode Partition Found: {p}")
|
||||
mynode_drive = d
|
||||
mynode_found = True
|
||||
else:
|
||||
|
@ -203,11 +203,11 @@ def main():
|
|||
target_drive = d
|
||||
b = round(time.time() * 1000)
|
||||
total_time = b - a
|
||||
print_and_log(f"Checked partition {p} in {total_time}ms")
|
||||
log_message(f"Checked partition {p} in {total_time}ms")
|
||||
|
||||
# Successfully found source and target, wait for confirm
|
||||
print_and_log(f"Source Drive: {mynode_drive}")
|
||||
print_and_log(f"Target Drive: {target_drive}")
|
||||
log_message(f"Source Drive: {mynode_drive}")
|
||||
log_message(f"Target Drive: {target_drive}")
|
||||
if both_drives_have_mynode:
|
||||
os.system("touch /tmp/.clone_target_drive_has_mynode")
|
||||
os.system(f"echo {mynode_drive} > /tmp/.clone_source")
|
||||
|
@ -228,7 +228,7 @@ def main():
|
|||
os.system(f"umount /dev/{target_drive}1")
|
||||
|
||||
# Update partitions (removes all + makes new without removing data)
|
||||
print_and_log("Formatting Drive...")
|
||||
log_message("Formatting Drive...")
|
||||
os.system("echo 'Formatting drive...' > /tmp/.clone_progress")
|
||||
subprocess.check_output(f"wipefs -a /dev/{target_drive}", shell=True)
|
||||
time.sleep(2)
|
||||
|
@ -236,13 +236,13 @@ def main():
|
|||
time.sleep(2)
|
||||
|
||||
# Make new partition on dest drive
|
||||
print_and_log("Creating Partition...")
|
||||
log_message("Creating Partition...")
|
||||
os.system("echo 'Creating Partition...' > /tmp/.clone_progress")
|
||||
subprocess.check_output(f"mkfs.ext4 -F -L myNode /dev/{target_drive}1", shell=True)
|
||||
time.sleep(2)
|
||||
|
||||
# Mounting Partitions
|
||||
print_and_log("Mounting Partitions...")
|
||||
log_message("Mounting Partitions...")
|
||||
os.system("echo 'Mounting Partitions...' > /tmp/.clone_progress")
|
||||
subprocess.check_output(f"mount /dev/{mynode_drive}1 /tmp/drive1", shell=True)
|
||||
subprocess.check_output(f"mount /dev/{target_drive}1 /tmp/drive2", shell=True)
|
||||
|
@ -254,7 +254,7 @@ def main():
|
|||
#cmd = ["dd","bs=512",f"if=/dev/zero",f"of=/dev/null","count=5999999","conv=sync,noerror"]
|
||||
cmd = ["rsync","-avxHAX","--info=progress2",f"/tmp/drive1/","/tmp/drive2/"]
|
||||
clone_process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
print_and_log("CLONE PID: {}".format(clone_process.pid))
|
||||
log_message("CLONE PID: {}".format(clone_process.pid))
|
||||
for l in clone_process.stdout:
|
||||
l = l.decode("utf-8")
|
||||
if 'xfr#' in l:
|
||||
|
@ -271,32 +271,32 @@ def main():
|
|||
out_fd.write(logline)
|
||||
out_fd.close()
|
||||
except Exception as e:
|
||||
print_and_log("Write Exception: " + str(e))
|
||||
log_message("Write Exception: " + str(e))
|
||||
|
||||
while clone_process.poll() is None:
|
||||
time.sleep(5)
|
||||
print_and_log("Waiting on rsync exit...")
|
||||
log_message("Waiting on rsync exit...")
|
||||
|
||||
print_and_log("CLONE RET CODE: {}".format(clone_process.returncode))
|
||||
log_message("CLONE RET CODE: {}".format(clone_process.returncode))
|
||||
if clone_process.returncode != 0:
|
||||
# Clone had an error - log it
|
||||
if clone_process.stderr != None:
|
||||
for l in clone_process.stderr:
|
||||
print_and_log("CLONE STDERR: "+l.decode("utf-8"))
|
||||
log_message("CLONE STDERR: "+l.decode("utf-8"))
|
||||
if clone_process.stdout != None:
|
||||
for l in clone_process.stdout:
|
||||
print_and_log("CLONE STDOUT: "+l.decode("utf-8"))
|
||||
log_message("CLONE STDOUT: "+l.decode("utf-8"))
|
||||
set_clone_state("error")
|
||||
set_clone_error("Clone failed with return code {}".format(clone_process.returncode))
|
||||
wait_on_clone_error_dismiss()
|
||||
return
|
||||
|
||||
print_and_log("CLONE IS COMPLETE")
|
||||
log_message("CLONE IS COMPLETE")
|
||||
time.sleep(2)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print_and_log("CalledProcessError")
|
||||
print_and_log(e.stderr)
|
||||
print_and_log(e.stdout)
|
||||
log_message("CalledProcessError")
|
||||
log_message(e.stderr)
|
||||
log_message(e.stdout)
|
||||
set_clone_state("error")
|
||||
set_clone_error("Clone failed: {}".format(e))
|
||||
wait_on_clone_error_dismiss()
|
||||
|
@ -309,8 +309,8 @@ def main():
|
|||
|
||||
# Complete - wait for reboot
|
||||
set_clone_state("complete")
|
||||
print_and_log("Clone Complete!")
|
||||
print_and_log("Waiting for reboot...")
|
||||
log_message("Clone Complete!")
|
||||
log_message("Waiting for reboot...")
|
||||
while True:
|
||||
time.sleep(60)
|
||||
|
||||
|
@ -320,6 +320,6 @@ if __name__ == "__main__":
|
|||
try:
|
||||
main()
|
||||
except Exception as e:
|
||||
print_and_log("Exception: {}".format(str(e)))
|
||||
log_message("Exception: {}".format(str(e)))
|
||||
set_clone_error("Exception: {}".format(str(e)))
|
||||
wait_on_clone_error_dismiss()
|
|
@ -55,13 +55,13 @@ def write_usb_devices_json():
|
|||
## Utility Functions
|
||||
################################
|
||||
|
||||
def print_and_log(msg):
|
||||
def log_message(msg):
|
||||
global log
|
||||
print(msg)
|
||||
log.info(msg)
|
||||
|
||||
def set_usb_extras_state(state):
|
||||
print_and_log("USB Extras State: {}".format(state))
|
||||
log_message("USB Extras State: {}".format(state))
|
||||
try:
|
||||
with open("/tmp/.usb_extras_state", "w") as f:
|
||||
f.write(state)
|
||||
|
@ -79,7 +79,7 @@ def get_drive_size(drive):
|
|||
size = int(parts[3])
|
||||
except:
|
||||
pass
|
||||
print_and_log(f"Drive {drive} size: {size}")
|
||||
log_message(f"Drive {drive} size: {size}")
|
||||
return size
|
||||
|
||||
def mount_partition(partition, folder_name, permissions="ro"):
|
||||
|
@ -216,17 +216,17 @@ class OpendimeHandler(UsbDeviceHandler):
|
|||
content = f.read()
|
||||
if "This Opendime is fresh and unused. It hasn't picked a private key yet." in content:
|
||||
self.state = "new"
|
||||
print_and_log(" Opendime in state 'new'")
|
||||
log_message(" Opendime in state 'new'")
|
||||
|
||||
if os.path.isfile(private_key_file):
|
||||
with open(private_key_file) as f:
|
||||
content = f.read()
|
||||
if "SEALED" in content:
|
||||
self.state = "sealed"
|
||||
print_and_log(" Opendime in state 'sealed'")
|
||||
log_message(" Opendime in state 'sealed'")
|
||||
else:
|
||||
self.state = "unsealed"
|
||||
print_and_log(" Opendime in state 'unsealed'")
|
||||
log_message(" Opendime in state 'unsealed'")
|
||||
|
||||
except Exception as e:
|
||||
self.state = "error_reading_opendime"
|
||||
|
@ -238,11 +238,11 @@ class OpendimeHandler(UsbDeviceHandler):
|
|||
self.http_server_thread.start()
|
||||
return True
|
||||
else:
|
||||
print_and_log("Error mounting partition for opendime")
|
||||
log_message("Error mounting partition for opendime")
|
||||
return False
|
||||
except Exception as e:
|
||||
unmount_partition(self.folder_name)
|
||||
print_and_log("Opendime Start Exception: {}".format(str(e)))
|
||||
log_message("Opendime Start Exception: {}".format(str(e)))
|
||||
return False
|
||||
|
||||
def stop(self):
|
||||
|
@ -251,7 +251,7 @@ class OpendimeHandler(UsbDeviceHandler):
|
|||
self.http_server.shutdown()
|
||||
unmount_partition(self.folder_name)
|
||||
except Exception as e:
|
||||
print_and_log("Opendime Stop Exception: {}".format(str(e)))
|
||||
log_message("Opendime Stop Exception: {}".format(str(e)))
|
||||
|
||||
################################
|
||||
## check_usb_devices()
|
||||
|
@ -267,47 +267,47 @@ def check_usb_devices():
|
|||
|
||||
# Detect drives
|
||||
drives = find_unmounted_drives()
|
||||
print_and_log(f"Drives: {drives}")
|
||||
log_message(f"Drives: {drives}")
|
||||
|
||||
# Check exactly one extra drive found
|
||||
drive_count = len(drives)
|
||||
if drive_count == 0:
|
||||
print_and_log("No USB extras found.")
|
||||
log_message("No USB extras found.")
|
||||
else:
|
||||
set_usb_extras_state("processing")
|
||||
for drive in drives:
|
||||
# Check drive for partitions
|
||||
drive = drives[0]
|
||||
partitions = find_partitions_for_drive(drive)
|
||||
print_and_log(f"Drive {drive} paritions: {partitions}")
|
||||
log_message(f"Drive {drive} paritions: {partitions}")
|
||||
|
||||
num_partitions = len(partitions)
|
||||
if num_partitions == 0:
|
||||
print_and_log("No partitions found. Nothing to do.")
|
||||
log_message("No partitions found. Nothing to do.")
|
||||
elif num_partitions == 1:
|
||||
# Process partition
|
||||
partition = partitions[0]
|
||||
print_and_log("One partition found! Scanning...")
|
||||
log_message("One partition found! Scanning...")
|
||||
if check_partition_for_opendime(partition):
|
||||
print_and_log("Found Opendime!")
|
||||
log_message("Found Opendime!")
|
||||
opendime = OpendimeHandler(drive, partition)
|
||||
if opendime.start():
|
||||
add_usb_device(opendime)
|
||||
else:
|
||||
opendime.stop()
|
||||
else:
|
||||
print_and_log(f"Drive {drive} could not be detected.")
|
||||
log_message(f"Drive {drive} could not be detected.")
|
||||
else:
|
||||
print_and_log(f"{num_partitions} partitions found. Not sure what to do.")
|
||||
log_message(f"{num_partitions} partitions found. Not sure what to do.")
|
||||
|
||||
# Successful scan post init or usb action detected, mark homepage refresh
|
||||
os.system("touch /tmp/homepage_needs_refresh")
|
||||
|
||||
except Exception as e:
|
||||
print_and_log("Exception: {}".format(str(e)))
|
||||
log_message("Exception: {}".format(str(e)))
|
||||
set_usb_extras_state("error")
|
||||
reset_usb_devices()
|
||||
print_and_log("Caught exception. Delaying 30s.")
|
||||
log_message("Caught exception. Delaying 30s.")
|
||||
time.sleep(30)
|
||||
|
||||
|
||||
|
@ -328,29 +328,29 @@ def main():
|
|||
monitor = pyudev.Monitor.from_netlink(context)
|
||||
monitor.filter_by(subsystem='usb')
|
||||
# this is module level logger, can be ignored
|
||||
print_and_log("Starting to monitor for usb")
|
||||
log_message("Starting to monitor for usb")
|
||||
monitor.start()
|
||||
print_and_log("Waiting on USB Event...")
|
||||
log_message("Waiting on USB Event...")
|
||||
set_usb_extras_state("waiting")
|
||||
for device in iter(monitor.poll, None):
|
||||
print_and_log("")
|
||||
print_and_log("Got USB event: %s", device.action)
|
||||
log_message("")
|
||||
log_message("Got USB event: %s", device.action)
|
||||
if device.action == 'add':
|
||||
check_usb_devices()
|
||||
else:
|
||||
# HANDLE DEVICE REMOVAL BETTER? This resets all and re-scans
|
||||
reset_usb_devices()
|
||||
check_usb_devices()
|
||||
print_and_log("Waiting on USB Event...")
|
||||
log_message("Waiting on USB Event...")
|
||||
set_usb_extras_state("waiting")
|
||||
|
||||
|
||||
@atexit.register
|
||||
def goodbye():
|
||||
print_and_log("ATEXIT: Resetting devices")
|
||||
log_message("ATEXIT: Resetting devices")
|
||||
unmount_partition("*")
|
||||
reset_usb_devices()
|
||||
print_and_log("ATEXIT: Done")
|
||||
log_message("ATEXIT: Done")
|
||||
|
||||
# This is the main entry point for the program
|
||||
if __name__ == "__main__":
|
||||
|
@ -359,8 +359,8 @@ if __name__ == "__main__":
|
|||
main()
|
||||
except Exception as e:
|
||||
set_usb_extras_state("error")
|
||||
print_and_log("Main Exception: {}".format(str(e)))
|
||||
print_and_log("Caught exception. Delaying 30s.")
|
||||
log_message("Main Exception: {}".format(str(e)))
|
||||
log_message("Caught exception. Delaying 30s.")
|
||||
unmount_partition("*")
|
||||
reset_usb_devices()
|
||||
time.sleep(30)
|
||||
|
|
Loading…
Reference in New Issue
Block a user