def format_hdd(inDrives): """ Formats the specified drives. Args: inDrives (str): A string of arguments for the format hard drive script. Returns: feedbackOutput (str): Resulting feedback. Raises: IOError, RuntimeError """ # TODO[ash]: Check if echo at end is needed. Pretty sure it isn't consoleOutput = exec_console_command("/root/bin/dfn_setup_data_hdds.sh {0}".format(inDrives) + "echo $?") if "\n127" in consoleOutput: consoleOutput = exec_console_command("/root/bin/dfn_setup_usb_hdds.sh {0}".format(inDrives) + "echo $?") if "\n127" in consoleOutput: raise IOError(constants.scriptNotFound.format("dfn_setup_data_hdds.sh")) elif "is mounted" in consoleOutput: raise RuntimeError("Some drives still mounted, or didn't format properly. Please make sure drives are unmounted and safe to format.") else: feedbackOutput = "\nHarddrives formatted successfully.\n" elif "is mounted" in consoleOutput: raise RuntimeError("Some drives still mounted, or didn't format properly. Please make sure drives are unmounted and safe to format.") else: feedbackOutput = "\nHarddrives formatted successfully.\n" return feedbackOutput
def probe_hdd(): """ Searches for present hard drive's to format. Returns: data (dict): Format:: {/dev/sdxx : /datax/dev/sdxx} Raises: IOError """ # Do command consoleOutput = exec_console_command(constants.probeHardDrives) data = {} # Parse results if "no such file or directory" in consoleOutput: consoleOutput = exec_console_command(constants.probeHardDrivesOLD) if "no such file or directory" in consoleOutput: raise IOError(constants.hddFormatScriptNotFound) firstLine = consoleOutput.split("\n") consoleOutput = firstLine[0] splitOutput = consoleOutput.split(" ") for idx, token in enumerate(splitOutput): if "/" in token: data[splitOutput[idx + 1]] = token + " " + splitOutput[idx + 1] return data
def interval_test(): """ Performs an interval control test on the system. Returns: feedbackOutput (str): Resulting feedback. status (bool): Represents if the test passed or failed. Raises: IOError """ # Do interval test command consoleOutput = exec_console_command(constants.intervalTest + constants.getExitStatus) if "\n127" in consoleOutput: raise IOError(constants.intervalControlTestScriptNotFound) # Check /data0/latest_prev for correct number of NEF files status = False feedbackOutput = constants.intervalTestFailed consoleOutput = exec_console_command(constants.checkIntervalResults) if consoleOutput in ["6", "7", "8" ]: # NOTE: 7 +/- 1 is the required margin of error. status = True feedbackOutput = constants.intervalTestPassed return feedbackOutput, status
def restart_modem(): """Restarts the modem network interface.""" command = "ifdown ppp0; sleep 8; ifup ppp0; sleep 8; ifconfig ppp0" exec_console_command(command) return "Modem restarted successfully."
def format_hdd(inDrives): """ Formats the specified drives. Args: inDrives (str): A string of arguments for the format hard drive script. Returns: feedbackOutput (str): Resulting feedback. Raises: IOError, RuntimeError """ consoleOutput = exec_console_command( constants.formatHardDrive.format(inDrives) + constants.getExitStatus) if "\n127" in consoleOutput: consoleOutput = exec_console_command( constants.formatHardDriveOLD(inDrives) + constants.getExitStatus) if "\n127" in consoleOutput: raise IOError(constants.hddFormatScriptNotFound) elif "is mounted" in consoleOutput: raise RuntimeError(constants.hddFormatFailed) else: feedbackOutput = constants.hddFormatPassed elif "is mounted" in consoleOutput: raise RuntimeError(constants.hddFormatFailed) else: feedbackOutput = constants.hddFormatPassed return feedbackOutput
def restart_vpn(): """Restarts the system's VPN daemon.""" command = "service openvpn restart; sleep 10; ifconfig tun0" exec_console_command(command) return "VPN restarted successfully."
def enable_hdd(): """ Switches the camera's external hard drives on. Returns: constants.hddCommandedOn (str): Represents the success of the operation. Raises: IOError """ # If hardrives already on, get outta here! feedbackOutput, hdd0Status, hdd0Space, hdd1Status, hdd2Status, hdd3Status, hdd1Space, hdd2Space, hdd3Space = check_hdd( ) if hdd1Status != 0 and hdd2Status != 0: return constants.hddAlreadyOn # Do command consoleOutput = exec_console_command(constants.enableHardDrive + constants.getExitStatus) if "\n2" in consoleOutput: raise IOError(constants.scriptNotFound) time.sleep(25) # For EXT, re-scan SATA/SCSI hotswap drives if "EXT" in getHostname(): exec_console_command(constants.scanSATA) time.sleep(2) return constants.hddCommandedOn
def enable_hdd(): """ Switches the camera's external hard drives on. Returns: constants.hddCommandedOn (str): Represents the success of the operation. Raises: IOError """ # If hardrives already on, get outta here! hdd_status = check_hdd() if hdd_status["HDD 1"]["status"] != 0 and hdd_status["HDD 2"]["status"] != 0: return "Hard drives already powered.\n" # Do command __import__("enable_ext-hd") time.sleep(25) # For EXT, re-scan SATA/SCSI hotswap drives if "EXT" in misc.get_hostname(): exec_console_command("for i in $(find /sys/class/scsi_host/ -name host* ); do echo '- - -' > $i/scan") time.sleep(2)
def change_timezone(timezone): """ Changes the system's timezone. Args: timezone (str): Timezone information to change the system's timezone to. """ exec_console_command("timedatectl set-timezone {0}".format(timezone))
def disable_hdd(): """ Switches the camera's external hard drives off. Returns: feedbackOutput (str): Resulting feedback. Raises: RuntimeError, IOError """ devices = ["sdb", "sdc", "sdd"] # Used for deleting devices in EXTs before powering off # If hardrives already off or mounted, get outta here! feedbackOutput, hdd0Status, hdd0Space, hdd1Status, hdd2Status, hdd3Status, hdd1Space, hdd2Space, hdd3Space = check_hdd( ) if hdd1Status != 1 and hdd2Status != 1: return constants.hddNotOnPoweredState # For EXT, delete the devices ONLY if they're all not solid state devices. if "EXT" in getHostname(): for device in devices: # Check if the device is a solid state or HDD driveRotation = exec_console_command( constants.extDeleteDriveDevicesCheck.format(device)) if not re.search("[0-9]", driveRotation): raise RuntimeError( "External drives are not on correct device label. Use the command line to resolve this." ) # No exceptions have been raised by this point, so delete drives for device in devices: exec_console_command(constants.extDeleteDriveDevice.format(device)) time.sleep(1) # Then proceed to power off as normal # Do command consoleOutput = exec_console_command(constants.disableHardDrive + constants.getExitStatus) if "\n2" in consoleOutput: raise IOError(constants.hddOffScriptNotFound) if consoleOutput == "0": feedbackOutput = constants.hddCommandedOff else: feedbackOutput = constants.hddOffFailed # Sleep if EXT, needs time to remove drives. if "EXT" in getHostname(): time.sleep(22) return feedbackOutput
def smart_test(): """ Performs a smart test. Returns: feedbackOutput (str): Resulting feedback. Raises: AssertionError OSError """ smalldrives = ["usbjmicron,00", "usbjmicron,01"] successfuldrives = list(smalldrives) output = {} feedbackOutput = "" # If hardrives off or not mounted, get outta here! hdd_status = check_hdd() try: assert hdd_status["HDD 1"]["status"] == 0 and hdd_status["HDD 2"]["status"] == 0 except AssertionError: raise AssertionError("Smart test failed. Hard drives need to be powered.") # Start all smart tests for drive in smalldrives: consoleOutput = exec_console_command("smartctl -d {0} -t short /dev/sdb".format(drive)) if "\n127" in consoleOutput: raise OSError("Smart test command not installed. Please contact [email protected].") elif "\n0" in consoleOutput: output.update({drive: "\nSmart test for {0} successfully executed.\n".format(drive)}) else: output.update({drive: "\nSmart test {0} failed execution (try re-powering drives).\n".format(drive)}) successfuldrives.remove(drive) # Wait for completion if successfuldrives: # Sleep while smart test performs time.sleep(70) # Evaluate results for drive in successfuldrives: consoleOutput = exec_console_command("smartctl -d {0} -a /dev/sdb".format(drive)) if "No Errors Logged" in consoleOutput: output[drive] += "Smart test for {0} passed.\n".format(drive) else: output[drive] += "Smart test for {0} failed.\n".format(drive) for drive in smalldrives: feedbackOutput += output[drive] return feedbackOutput
def find_pictures(inDate): """ Fetches the filenames of pictures taken on the date specified. Args: inDate (date): The date the requested pictures were taken. Returns: dict. Picture file creation times and paths, the format:: {filecreationtime : filepath} """ data = {} # Let's do some directory searching! day = inDate.day.zfill(2) month = inDate.month.zfill(2) year = inDate.year commandTemplate = constants.findPictures command = commandTemplate.format(year, month, day) foundDirectories = exec_console_command(command) directoriesList = foundDirectories.split('\n') if directoriesList: # Find all dates + times for all directories data = {} for directory in directoriesList: fileList = exec_console_command("ls " + directory).split("\n") for fileName in fileList: if ".NEF" in fileName: # Get filepath for NEF file filePath = (directory + "/" + fileName) # Find timestamp of when photo was taken regexSearch = re.search('(?<!\d)\d{6}(?!\d)', filePath) fileCreationTime = "" if regexSearch: fileCreationTime = regexSearch.group(0) fileCreationTime = fileCreationTime[:2] + ':' + fileCreationTime[2:] fileCreationTime = fileCreationTime[:5] + ':' + fileCreationTime[5:] h, m, s = fileCreationTime.split(':') seconds = int(h) * 3600 + int(m) * 60 + int(s) offset = calendar.timegm(time.localtime()) - calendar.timegm( time.gmtime(time.mktime(time.localtime()))) fileCreationTimeSeconds = seconds + offset fileCreationTimeReadable = time.strftime('%H:%M:%S', time.gmtime(fileCreationTimeSeconds)) data[fileCreationTimeReadable] = filePath return data
def change_timezone(timezone): """ Changes the system's timezone. Args: timezone (str): Timezone information to change the system's timezone to. Returns: constants.timezoneChanged (str): Resulting feedback. """ command = constants.setTimezone exec_console_command(command.format(timezone)) return constants.timezoneChanged.format(timezone)
def check_internet(): """ Delivers a summary of the internet connectivity of the system. Returns: pingOutput (str): Successful output from ping. ipAddress (str): Internet IP. """ pingOutput = exec_console_command("ping -c 1 www.google.com") ipAddress = exec_console_command( "ifconfig | grep eth1 -A 1 | grep -o '\(addr:\|inet \)[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | cut -c6-" ) # curl ipinfo.io/ip return pingOutput, ipAddress
def check_vpn(): """ Delivers a summary of the VPN connectivity of the system. Returns: pingOutput (str): Successful output from ping. vpnIP (str): VPN IP. """ pingOutput = exec_console_command("ping -c 1 10.1.16.1") vpnIP = exec_console_command( "ifconfig | grep tun0 -A 1 | grep -o '\(addr:\|inet \)[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'| cut -c6-" ) return pingOutput, vpnIP
def mount_hdd(): """ Mounts the external hard drive's to the file system. Returns: feedbackOutput (str): Resulting feedback. """ outputDict = {'/data1': "Drive #1", '/data2': "Drive #2", '/data3': "Drive #3"} feedbackOutput = {} hostname = misc.get_hostname() if 'EXT' in hostname: drives = ['/data1', '/data2', '/data3'] else: drives = ['/data1', '/data2'] #Get current status of HDDs hdd_status = check_hdd() poweredArray = [hdd_status["HDD 1"]["status"], hdd_status["HDD 2"]["status"], hdd_status["HDD 3"]["status"]] for idx, drive in enumerate(drives): # Do command for drive try: consoleOutput = exec_console_command("mount {0}".format(drive)) if poweredArray[idx] == 0: feedbackOutput["HDD {0}".format(idx+1)] = "{0} mount error: {1}\n".format(outputDict[drive], "Hard drives need to be powered.") else: feedbackOutput["HDD {0}".format(idx+1)] = "{0} mounted successfully.\n".format(outputDict[drive]) except CommandError as error: feedbackOutput["HDD {0}".format(idx+1)] = "{0} mount error: {1}\n".format(outputDict[drive], "Hard drives may have already been mounted. See status for confirmation.") return feedbackOutput
def system_time(): """ Outputs the current system time to the user. Returns: The current system time. """ return exec_console_command("date")
def get_timezone(): """ Gets the systems current timezone. Returns: timezone (str): The systems timezone. """ return exec_console_command( "timedatectl status | grep -oP '(?<=Time zone: ).*(?= \()'")
def system_time(): """ Outputs the current system time to the user. Returns: consoleOutput (str): Resulting console feedback. """ consoleOutput = exec_console_command(constants.outputTime) return consoleOutput + "\n"
def get_hostname(): """ Gets the hostname of the system. Returns: consoleOutput (str): The hostname of the system. """ consoleOutput = exec_console_command("hostname") return consoleOutput
def check_vpn(): """ Delivers a summary of the VPN connectivity of the system. Returns: feedbackOutput (str): Resulting feedback. vpnStatus (bool): Represents the VPN connectivity of the system. """ consoleOutput = exec_console_command(constants.vpnCheck) # Parse output for results status = False feedbackOutput = constants.vpnCheckFailed if "0" not in re.split(",", consoleOutput)[1]: status = True ipAddress = exec_console_command(constants.getVpnIP) feedbackOutput = constants.vpnCheckPassed.format(ipAddress) return feedbackOutput, status
def disable_hdd(): """ Switches the camera's external hard drives off. Returns: feedbackOutput (str): Resulting feedback. Raises: RuntimeError, IOError """ devices = ["sdb", "sdc", "sdd"] # Used for deleting devices in EXTs before powering off # If hardrives already off or mounted, get outta here! hdd_status = check_hdd() if hdd_status["HDD 1"]["status"] != 1 and hdd_status["HDD 2"]["status"] != 1: return "Hard drives already off, or mounted. Drives must be in a 'powered' state to turn off safely.\n" # For EXT, delete the devices ONLY if they're all not solid state devices. if "EXT" in misc.get_hostname(): for device in devices: # Check if the device is a solid state or HDD driveRotation = exec_console_command("smartctl -i /dev/{0} | grep 'Rotation Rate:'".format(device)) if not re.search("[0-9]", driveRotation): raise RuntimeError( "External drives are not on correct device label. Use the command line to resolve this.") # No exceptions have been raised by this point, so delete drives for device in devices: exec_console_command("echo 1 > /sys/block/{0}/device/delete".format(device)) time.sleep(1) # Then proceed to power off as normal # Do command __import__("disable_ext-hd") # Sleep if EXT, needs time to remove drives. if "EXT" in misc.get_hostname(): time.sleep(22)
def move_data_0(): """ Moves /data0 data to the external drive's. Returns: consoleFeedback (str): Resulting console feedback. Raises: IOError """ if "EXT" in misc.get_hostname(): consoleOutput = exec_console_command("/usr/local/bin/move_data_files_gen3.sh") else: consoleOutput = exec_console_command("/usr/local/bin/move_data_files.sh") if "SUCCESS" in consoleOutput: consoleFeedback = "Move command successful." else: raise IOError("Move command unsuccessful.") return consoleFeedback
def check_internet(): """ Delivers a summary of the internet connectivity of the system. Returns: feedbackOutput (str): Resulting feedback. internetStatus (bool): Represents the internet connectivity of the system. """ consoleOutput = exec_console_command(constants.internetCheck) # Parse output for results status = False feedbackOutput = constants.internetCheckFailed if "unknown" not in consoleOutput and "failure" not in consoleOutput: splitOutput = re.split(",", consoleOutput) if "0" not in splitOutput[1]: status = True ipAddress = exec_console_command(constants.getInternetIP) feedbackOutput = constants.internetCheckPassed.format(ipAddress) return feedbackOutput, status
def get_log(directory): """ Fetches the file path of a text logfile on the file system. Args: directory (str): The directory to get the logfile from. Format:: /data0/ + directory Returns: foundFile (str): The file path of the found logfile. """ filenames = exec_console_command(constants.getLogfileName.format(directory)) foundfile = filenames.split('\n')[0] return foundfile
def cf_check(): """ Checks that a configuration file exists. Returns: consoleOutput (str): Resulting console output. Raises: IOError """ consoleOutput = exec_console_command(constants.cfcheck) if re.search("[0-9]", consoleOutput): return consoleOutput else: raise IOError(constants.cfCheckScriptNotFound)
def mount_hdd(): """ Mounts the external hard drive's to the file system. Returns: feedbackOutput (str): Resulting feedback. """ outputDict = { '/data1': "Drive #1", '/data2': "Drive #2", '/data3': "Drive #3" } smalldrives = ['/data1', '/data2'] extdrives = ['/data1', '/data2', '/data3'] drives = [''] feedbackOutput = "" hostname = getHostname() if 'EXT' in hostname: drives = list(extdrives) else: drives = list(smalldrives) temp, hdd0Status, hdd0Space, hdd1Status, hdd2Status, hdd3Status, hdd1Space, hdd2Space, hdd3Space = check_hdd( ) poweredArray = [hdd1Status, hdd2Status, hdd3Status] for idx, drive in enumerate(drives): # Do command for drive consoleOutput = exec_console_command( constants.mountHardDrive.format(drive)) if "SUCCESS" in consoleOutput: if poweredArray[idx] == 0: feedbackOutput += constants.hddMountFailed.format( outputDict[drive], constants.hddNotPoweredError) else: feedbackOutput += constants.hddMountPassed.format( outputDict[drive]) else: feedbackOutput += constants.hddMountFailed.format( outputDict[drive], constants.hddAlreadyMountedError) return feedbackOutput
def turn_camera_off(): """ Switches the DSLR camera off. Returns: feedbackOutput (str): Resulting feedback. Raises: IOError """ # Do command consoleOutput = exec_console_command(constants.cameraOff + constants.getExitStatus) if "2" in consoleOutput: raise IOError(constants.cameraOffScriptNotFound) # Parse output feedbackOutput = constants.cameraSwitchedOff return feedbackOutput
def camera_status(): """ Delivers a summary of the DSLR's status. Returns: feedbackOutput (str): Resulting feedback. status (bool): On / off state of the DSLR camera. """ # Do command consoleOutput = exec_console_command(constants.cameraCheck) # Parse output for results status = False feedbackOutput = constants.cameraCheckOff if "Nikon Corp." in consoleOutput: status = True feedbackOutput = constants.cameraCheckOn # Encode to JSON return feedbackOutput, status
def remove_thumbnail(inJSON): """ Deletes the specified thumbnail from the camera's filesystem. Args: inJSON (json): A JSON object with the following format:: {filepath : (filepath)} Returns: success (bool): Represents the success of the operation. Raises: IOError """ time.sleep(2) consoleOutput = exec_console_command("rm " + inJSON + ";" + constants.getExitStatus) if "\n1" in consoleOutput: raise IOError("Thumbnail file doesn't exist to delete. No worries though, it was going to be deleted anyway!") return 0