def hum_readable_list_devices(full_info=False): if full_info: cmd="lsblk" exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: print("[CMD] " + str(cmd)) print(stdout) print("\n") cmd="blkid" exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: print("[CMD] " + str(cmd)) print(stdout) print("\n") cmd="sudo fdisk -l | grep -v grep | grep /dev/sd" exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: print("[CMD] " + str(cmd)) print(stdout) devices_list = BlockDeviceHandler.list_connected_devices() for device in devices_list: device_info = BlockDeviceHandler.get_device_info_data(device) for key, value in device_info.items(): sep_len = 12 - len(key) sep = " "*sep_len print("\t{}:{}{}".format(key, sep, value))
def premount_device(dev): """ dev example: /dev/sda1 """ dev_info_dict = get_device_info_data(dev) premount_path = "/media/pre_" + dev_info_dict['label'] if not os.path.exists(premount_path): module_printer("mount point not exists - create: " + str(premount_path)) cmd = "sudo mkdir -p " + str(premount_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr) if not device_is_mounted(premount_path): module_printer("premount device " + str(dev_info_dict['path'])) cmd = "sudo mount -t {} {} {}".format(dev_info_dict['filesystem'], dev_info_dict['path'], premount_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) if exitcode != 0: # retry without explicit filesystem parameter cmd = "sudo mount {} {}".format(dev_info_dict['path'], premount_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr) else: module_printer("already mounted " + str(dev_info_dict['path'])) return premount_path
def get_rpitools_services(color=Colors.CYAN): global rpitools_services, linux_services services = rpitools_services data = color + " RPITOOLS SERVICES:\n" + Colors.NC for service in services: is_active = LocalMachine.run_command("systemctl is-active " + str(service))[1] is_enabled = LocalMachine.run_command("systemctl is-enabled " + str(service))[1] is_active, is_enabled = service_state_coloring(isactive=is_active, isenabled=is_enabled, service_name=service) data += "\t" + color + str( service) + Colors.NC + " active status: " + str(is_active) + "\n" data += "\t" + str(service) + " enabled status: " + str( is_enabled) + "\n" services = linux_services data += color + " LINUX SERVICES:\n" + Colors.NC for service in services: is_active = LocalMachine.run_command("systemctl is-active " + str(service))[1] is_enabled = LocalMachine.run_command("systemctl is-enabled " + str(service))[1] is_active, is_enabled = service_state_coloring(isactive=is_active, isenabled=is_enabled, service_name=service) data += "\t" + color + str( service) + Colors.NC + " active status: " + str(is_active) + "\n" data += "\t" + str(service) + " enabled status: " + str( is_enabled) + "\n" return data
def restore_diskconf_file(path, confname="diskconf.json"): json_path = str(path) + "/" + str(confname) save_path = "/tmp/" + str(confname) cmd = "sudo cp {} {}".format(save_path, json_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) BlockDeviceHandler.check_exitcode(cmd, exitcode, stderr) cmd = "sudo rm -f {}".format(save_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) BlockDeviceHandler.check_exitcode(cmd, exitcode, stderr)
def device_is_mounted(device): """ device example: /dev/sda1 or mount point example: /media/device """ cmd = "grep -qs '{} ' /proc/mounts".format(device) exitcode, stdout, stderr = LocalMachine.run_command(cmd) if exitcode == 0: return True else: return False
def unmount_device(dev): """ device example: /dev/sda1 or mount point example: /media/device """ cmd = "sudo umount " + str(dev) exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr) time.sleep(1) if device_is_mounted(dev): time.sleep(1) unmount_device(dev)
def mount_all_devices(): media_dirs = [] for dirname, dirnames, filenames in os.walk('/media/'): media_dirs = dirnames break for actualdir in media_dirs: is_mounted_cmd = "grep -qs '/media/{}' /proc/mounts".format(actualdir) exitcode, stdout, stderr = LocalMachine.run_command(is_mounted_cmd) if exitcode == 0: print("/media/{} is already mounted".format(actualdir)) elif exitcode == 1: exitcode, stdout, stderr = LocalMachine.run_command( "cat /etc/fstab") if exitcode == 0 and actualdir not in stdout: print(actualdir + " not in /etc/fstab") print("\tSkipping mount...") return print("/media/{} mount".format(actualdir)) mount_cmd = "mount /media/" + str(actualdir) exitcode, stdout, stderr = LocalMachine.run_command(mount_cmd) if exitcode == 0: is_mounted_cmd = "grep -qs '/media/{}' /proc/mounts".format( actualdir) exitcode, stdout, stderr = LocalMachine.run_command( is_mounted_cmd) if exitcode == 0: print("Successfully mounted") elif exitcode == 1: print("Fail to mount - may device is not found") else: module_printer( "EXITCODE: {}\nSTDOUT: {}\nSTDERR:{}".format( exitcode, stdout, stderr)) else: module_printer("Mount failed!") module_printer("Command: {} return with error code: {}".format( mount_cmd, exitcode)) module_printer("STDOUT: {}\nSTDERR:{}".format(stdout, stderr)) else: module_printer("Unexpected error!") module_printer("Command: {} return with error code: {}".format( is_mounted_cmd, exitcode)) module_printer("STDOUT: {}\nSTDERR:{}".format(stdout, stderr))
def get_rpienv_status(color=Colors.CYAN): rpienv_log = LocalMachine.run_command( "if [ -f /var/log/rpienv ]; then cat /var/log/rpienv; else echo ''; fi" )[1] print(rpienv_log) if "[ ENV ERROR ]" in rpienv_log: rpienv_status = 'warning' else: rpienv_status = 'ok' return process_state_coloring(rpienv_status, "rpienv_log")
def reset_dumped_database_and_restart_service(): global dumped_dotdictbackup_json_path cmd = "rm -f " + str(dumped_dotdictbackup_json_path) exit_code, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True) if exit_code == 0: cmd = "sudo systemctl restart memDictCore" exit_code, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True) if exit_code == 0: cmd = "sudo systemctl is-active memDictCore" exit_code, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True) if stdout == "active": print("Reset memDictCore databse was SUCCESSFUL") else: print("Reset memDictCore databse FAILED: " + str(stdout)) else: print("Reset memDictCore databse FAILED: " + str(stderr)) else: print("Reset memDictCore databse FAILED: " + str(stderr)) return exit_code
def format_ex4(device, label): """ format disk to ext4 linux file format """ cmd = "sudo mkfs.ext4 {} -L {}".format(device, label) exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: module_printer("[CMD] " + str(cmd)) module_printer(stdout) module_printer("\n") else: module_printer("EXITCODE:{}\nSTDOUT:{}\nSTDERR:{}\n".format( exit_code, stdout, stderr))
def unmount_all_premounted_devices(): """ serach under /media/ with pre_ prefix and unmount """ premounted_devices_list = [] cmd = "ls -1 /media/" exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr) devices_list = stdout.split('\n') for dev in devices_list: dev = "/media/" + str(dev) if "pre_" in dev: premounted_devices_list.append(dev) module_printer("premounted devices: " + str(premounted_devices_list)) for dev in premounted_devices_list: if device_is_mounted(dev): unmount_device(dev) else: module_printer("{} not mounted".format(dev)) cmd = "rm -rf " + str(dev) exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr)
def is_any_device_avaible(): """ search under /dev with sd prefix for any available devices """ cmd = "ls /dev/sd*" textmatrix = "" devices = [] exitcode, stdout, stderr = LocalMachine.run_command(cmd) msg = "STDOUT: {} STDERR: {}".format(stdout, stderr) if exitcode == 0: return True, msg else: return False, msg
def unmount_all_devices(del_mount_point=False): """ unmount all mounted devices """ mounted_devices_list = [] cmd = "ls -1 /media/" exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr) devices_list = stdout.split('\n') for dev in devices_list: if device_is_mounted(dev): dev = "/media/" + str(dev) mounted_devices_list.append(dev) module_printer("mounted devices: " + str(mounted_devices_list)) for dev in mounted_devices_list: if device_is_mounted(dev): unmount_device(dev) else: module_printer("{} not mounted".format(dev)) if del_mount_point: cmd = "rm -rf " + str(dev) exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr)
def set_get_device_name(device, name=None): if os.path.exists(device): if name is None: exitcode, stdout, stderr = LocalMachine.run_command( "sudo e2label " + str(device)) #device=/dev/sda1 if exitcode == 0 and stderr == "" and stdout != "": print("Device {} name: {}".format(device, stdout)) return stdout else: print("exitcode:{}\nstdout:{}\nstderr:{}".format( exitcode, stdout, stderr)) else: exitcode, stdout, stderr = LocalMachine.run_command( "sudo e2label " + str(device) + " " + str(name)) if exitcode == 0 and stderr == "": set_get_device_name(device) else: print("exitcode:{}\nstdout:{}\nstderr:{}".format( exitcode, stdout, stderr)) else: error_msg("Device is not found " + str(device))
def edit_fstab_create_mount_point(device_info_dict, fstab_path="/etc/fstab"): global USER uuid = device_info_dict["uuid"] label = device_info_dict["label"] cmd = "UUID={} /media/{} auto defaults,auto,noatime,nofail,users,rw, 0 1".format( uuid, label) # ext4 mount - done # check device is already added to the system, and make override protection enable = False uuid_is_exists = "cat {} | grep -v grep | grep '{}'".format( fstab_path, uuid + " ") print(uuid_is_exists) label_is_exists = "cat {} | grep -v grep | grep '{}'".format( fstab_path, label + " ") exitcode, stdout, stderr = LocalMachine.run_command(uuid_is_exists) if exitcode != 0 and stderr == "" and stdout == "": print("\tnew device unique id {}".format(uuid)) exitcode, stdout, stderr = LocalMachine.run_command(label_is_exists) if exitcode != 0 and stderr == "" and stdout == "": print("\tnew device name {}".format(label)) enable = True else: print("\tdevice name is already exists in fstab {} !!!".format( label)) else: print("\tdevice unique id is already exists in fstab {} !!!".format( uuid)) if enable: print("+++> ADDING NEW DEVICE IS ENABLE") check_cmd = "cat {} | grep -v grep | grep '{}'".format(fstab_path, cmd) exitcode, stdout, stderr = LocalMachine.run_command(check_cmd) if exitcode != 0 and stdout == "": print("line not exists: {}".format(cmd)) print("ADD TO " + str(fstab_path)) # edit fstab file add_cmd = "sudo echo '{}' >> {}".format(cmd, fstab_path) exitcode, stdout, stderr = LocalMachine.run_command(add_cmd) if exitcode == 0: print("{} added succesfully!".format(cmd)) else: error_msg("Command: {} return with error code: {}".format( cmd, exitcode)) error_msg("STDOUT: {}\nSTDERR:{}".format(stdout, stderr)) else: print("line exists: {}".format(cmd)) # create mount point if not os.path.isdir("/media/" + str(label)): print("Create /media/" + str(label)) os.makedirs("/media/" + str(label)) print("chmod 0765 /media/" + str(label)) os.chmod("/media/" + str(label), 0765) print("chmod " + str(USER)) LocalMachine.run_command("chown " + USER + " /media/" + str(label)) else: print("/media/" + str(label) + " already exists")
def map_wlan_devices(): devices = [] host_address_list = SearchDevices.online_device_scanner() for device in host_address_list: devip = device macaddr = "n/a" try: exitcode, stdout, stderr = LocalMachine.run_command('arp -n {}'.format(device), shell=True) if exitcode == 0: macaddr = stdout.split(' ')[3] except Exception: pass devices.append([devip, macaddr]) return devices
def get_other_monitored_processes(color=Colors.CYAN): process_name_list = ["Xorg", "vncserver", "kodi"] data = color + " MONITORED PROCESSES:\n" + Colors.NC for process in process_name_list: exitcode, process_state, stderr = LocalMachine.run_command( "ps aux | grep -v grep | grep '" + str(process) + "'") if process_state == "": process_state = "inactive" else: process_state = "active" process_state, is_enabled = service_state_coloring( isactive=process_state, service_name=process) data += "\t" + color + str(process) + Colors.NC + " state: " + str( process_state) + "\n" return data
def migrate_from_internal_to_external_storage(default_storage_root, external_storage_root, default_storage_folders): if not os.path.exists(default_storage_root): return False default_storage_folder_content = os.listdir(default_storage_root) linking_is_required = False for content in default_storage_folder_content: abs_path = default_storage_root + os.sep + content if os.path.isdir(abs_path) and not os.path.islink(abs_path): cmd = "mv " + str(abs_path) + " " + str(external_storage_root) console_out("Migrate storage from {} to {}".format( default_storage_root, external_storage_root)) exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code != 0: console_out("\tFAIL") linking_is_required = True if linking_is_required: console_out("Create symlinks for backward compatibility") for folder in default_storage_folders: from_ = str(external_storage_root) + os.sep + str(folder) to_ = str(default_storage_root) + os.sep + str(folder) cmd = "ln -sf {} {}".format(from_, to_) exit_code, stdout, stderr = LocalMachine.run_command(cmd) return True
def create_base_strucure(storage_root_path, path_list): cmd = "mkdir -p " for path in path_list: full_cmd = cmd + path if not os.path.exists(path) and not os.path.islink(path): console_out("CMD: " + str(full_cmd)) exit_code, stdout, stderr = LocalMachine.run_command(full_cmd) if exit_code == 0: console_out("\tCREATE DIR DONE") else: console_out("\tCREATE DIR FAIL" + str(stderr)) else: console_out("Already exists: " + str(path)) for index, path in enumerate(path_list): if get_user_and_group(path)[1] != str(storage_folders_groups[index]): cmd_grp = "sudo chgrp " + str( storage_folders_groups[index]) + " " + str(path) console_out("CMD: " + str(cmd_grp)) exit_code, stdout, stderr = LocalMachine.run_command(cmd_grp) if exit_code == 0: console_out("\tSET GROUP DONE") else: console_out("\tSET GROUP FAIL" + str(stderr))
def network_mapper_raw(self, debug_print=False): print("[ClusterMemberDiscover] [arp-scan] get raw local network data") exitcode, stdout, stderr = LocalMachine.run_command("sudo arp-scan -l") local_machines_info_list = stdout.split("\n") for local_machines_info in local_machines_info_list: values = local_machines_info.split("\t") if self.validate_ip(values[0]): try: self.devices_on_network_raw[str(values[0]).strip()] = [str(values[1]).strip(),\ str(values[2]).strip(), "hostname"] except: pass if debug_print: for key, value in self.devices_on_network_raw.items(): print("IP: {}\n\tDATA: {}".format(key, value))
def set_value_MemDict(key, value, namespace="system", field=None): global clientMemDict_path if field is not None: cmd = "{} -md -n {} -f {} -k {} -v {} -s True".format( clientMemDict_path, namespace, field, key, value) else: cmd = "{} -md -n {} -k {} -v {} -s True".format( clientMemDict_path, namespace, key, value) __debug_print(cmd) exitcode, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True) if exitcode == 0: state = stdout else: print("Get data from memdict failed: {}\n{}".format(cmd, stderr)) state = None return state
def map_wlan_devices(service_port=9008): devices = [] print("[NWSCN] Get online host list on port {}".format(service_port)) host_address_list = SearchDevices.online_device_scanner( service_port=service_port) print("[NWSCN] Get macaddress by ip ...") for device in host_address_list: devip = device macaddr = "n/a" try: exitcode, stdout, stderr = LocalMachine.run_command( 'arp -n {}'.format(device), shell=True, debug=False) if exitcode == 0: macaddr = stdout.split(' ')[3] except Exception: pass devices.append([devip, macaddr]) return devices
def device_mapper_inject_hostname_data(self, debug_print=True): print("[ClusterMemberDiscover] [nbtscan] get hostname by ip, create compact network data") for key, value in self.devices_on_network_raw.items(): exitcode, stdout, stderr = LocalMachine.run_command("nbtscan " + str(key)) stdout_lines = stdout.split("\n") cnt = 0 for index, line in enumerate(stdout_lines): line_worlds = re.split('\s+', line) if key in line_worlds[0]: hostname = line_worlds[1] self.devices_on_network[hostname] = {"ip": str(key), "mac": str(value[0]), "device": str(value[1])} else: hostname = "UNKNOWN-" + str(cnt) cnt+=1 self.devices_on_network[hostname] = {"ip": str(key), "mac": str(value[0]), "device": str(value[1])} if debug_print: for key, value in self.devices_on_network.items(): print("{}\n\t{}".format(key, value))
def get_storage_structure_folders(set_extarnal_storage, external_storage_label): global default_storage_root storage_root_path, path_list = get_storage_root_and_base_path_list( set_extarnal_storage, external_storage_label) if set_extarnal_storage: if not BlockDeviceHandler.device_is_mounted(storage_root_path): return "External device is not available: " + str( storage_root_path) create_source_file_for_bash_scripts(path_list) text = "Storage structure folders:" text += "\n(default internal storage root: " + default_storage_root + ")" for path in path_list: text += "\n" + Colors.YELLOW + path + Colors.NC + "\tgroup: " + get_user_and_group( path)[1] cmd = "tree -L 2 " + str(path) exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: text += stdout console_out(text) return text
def list_connected_devices(): """ get block devices from /dev """ cmd = "ls /dev/sd*" textmatrix = "" devices = [] exitcode, stdout, stderr = LocalMachine.run_command(cmd) if exitcode != 0 and "No such file or directory" in stderr: module_printer("\tNO DEVICES WAS FOUND.") return [] if exitcode == 0: textmatrix = LocalMachine.text_to_matrix_converter(stdout) for line in textmatrix: digit = line[0][ -1] # check if didgit is the last character ex. /dev/sda1 if digit.isdigit(): devices.append(line[0]) else: module_printer("Command: {} return with error code: {}".format( cmd, exitcode)) module_printer("STDOUT: {}\nSTDERR:{}".format(stdout, stderr)) sys.exit(1) return devices
def get_device_info_data(device): """ device example: /dev/sda1 """ device_info_dict = {} cmd = "sudo blkid " + str(device) exitcode, stdout, stderr = LocalMachine.run_command(cmd) check_exitcode(cmd, exitcode, stderr) try: dev_info = stdout.split(" ") device_info_dict["path"] = dev_info[0].replace(':', '', 1) device_info_dict["label"] = dev_info[1].split("=")[1].replace( '"', '', 2) device_info_dict["uuid"] = dev_info[2].split("=")[1].replace( '"', '', 2) device_info_dict["filesystem"] = dev_info[3].split("=")[1].replace( '"', '', 2) except Exception as e: print("get_device_info_data: " + str(e)) device_info_dict = { "path": "", "label": "", "uuid": "", "filesystem": "" } return device_info_dict
value = value.rstrip() os.environ[name] = value except Exception as e: if "name 'unicode' is not defined" != str(e): print(e) proc.communicate() rpienv_source() try: confighandler_path = os.environ['CONFIGHANDLER'] cmd = "{} -s {} -o {}".format(confighandler_path, "TEMP_CONTROLL_FAN", "activate") exit_code, stdout, stderr = LocalMachine.run_command(cmd) is_activated = stdout.decode("utf-8") cmd = "{} -s {} -o {}".format(confighandler_path, "TEMP_CONTROLL_FAN", "temperature_trigger_celsius") exit_code, stdout, stderr = LocalMachine.run_command(cmd) temperature_trigger_celsius = stdout.decode("utf-8") cmd = "{} -s {} -o {}".format(confighandler_path, "TEMP_CONTROLL_FAN", "temperature_inertia_celsius") exit_code, stdout, stderr = LocalMachine.run_command(cmd) temperature_inertia_celsius = stdout.decode("utf-8") cmd = "{} -s {} -o {}".format(confighandler_path, "TEMP_CONTROLL_FAN", "pin_channel") exit_code, stdout, stderr = LocalMachine.run_command(cmd)
if sys.argv[1] == "-h" or sys.argv[1] == "--help": print("(1) RUN COMMAND: clientMemDict -md -n xx -k yy -v zz\n\tOR: clientMemDict --memdict --namespace xx --key yy --value zz") print("(2) RUN COMMAND: clientMemDict -md -n xx -f mm -k yy -v zz\n\tOR: clientMemDict --memdict --namespace xx --field mm --key yy --value zz") print("(3) RUN INTERACTIVE MODE: clientMemDict") print("TOOLS: RESET AND RESTART MEMDICT SERVICE: clientMemDict -r | --reset") print("TOOLS: RUN TEST MODULE: clientMemDict -tm | --testmodule") sys.exit(0) if sys.argv[1] == "-r" or sys.argv[1] == "--reset": print("Reset memory dict core") exit_code = reset_dumped_database_and_restart_service() sys.exit(exit_code) if sys.argv[1] == "-tm" or sys.argv[1] == "--testmodule": print("Run service test module, pls wait...") print("CMD: " + str(myfolder) + "/../testmodule/test_memdict_cmdline_interface.bash") if str(raw_input("RUN? Y|N > ")).lower() == "y": exit_code, stdout, stderr = LocalMachine.run_command(str(myfolder) + "/../testmodule/test_memdict_cmdline_interface.bash") print(stdout) else: exit_code = 0 sys.exit(exit_code) # handle command line arhuments - non interactive mode if len(sys.argv) > 1: arg_list = sys.argv[1:] cmd = "" for par in arg_list: cmd += " " + str(par) try: socketdictclient.receive_data() console_output_raw = socketdictclient.run_command(cmd) try:
def get_backuphandler_status(color=Colors.CYAN): backuphandler_status = LocalMachine.run_command( "cat ~/rpitools/tools/backuphandler/.status")[1] return process_state_coloring(backuphandler_status, "backuphandler")
def get_autosync_status(color=Colors.CYAN): autosync_status = LocalMachine.run_command( "cat ~/rpitools/tools/autosync/.status")[1] return process_state_coloring(autosync_status, "autosync")