def log_archive(self): """ Dumps the active log to the archive. Returns True on successful logging, false otherwise. """ self._check_and_add(self.path, self.LOGNAME_ARCHIVE) cmd = "cat {0}/{1} >> {2}/{3}".format(self.path, self.LOGNAME_ACTIVE, self.path, self.LOGNAME_ARCHIVE) # Write remote log line try: ssh_command_target(cmd, self.target, self.user, self.key_path) except: log.error("Failed to append active log to archive.") return False cmd = "rm {0}/{1}".format(self.path, self.LOGNAME_ACTIVE) # Write remote log line try: ssh_command_target(cmd, self.target, self.user, self.key_path) except: log.error("Failed to append active log to archive.") return False return True
def _check_and_add(self, path, filename): """ Checks for a file and adds it if it's not there """ cmd = "ls {0} | grep -c {1}".format(path, filename) ret = ssh_command_target(cmd, self.target, self.user, self.key_path) # Parse the count and add the file if it's missing if int(ret["stdout"][0].strip()) > 0: cmd = "touch {0}/{1}".format(path, filename) ssh_command_target(cmd, self.target, self.user, self.key_path)
def _check_and_add(self, path, filename): """ Checks for a file and adds it if it's not there """ cmd = 'ls {0} | grep -c {1}'.format(path, filename) ret = ssh_command_target(cmd, self.target, self.user, self.key_path) # Parse the count and add the file if it's missing if int(ret['stdout'][0].strip()) > 0: cmd = 'touch {0}/{1}'.format(path, filename) ssh_command_target(cmd, self.target, self.user, self.key_path)
def add_lock(self): """ Write the lock file """ cmd = "touch {0}/{1}".format(self.deploy_path, self.get_lock_name()) try: ssh_command_target(cmd, self.target, self.user, self.key_path) except Exception as e: log.error(__name__ + ' :: Failed to create lock -> ' + e.message) raise DeployLockerError(message=exit_codes[16], exit_code=16) # Logging log.info('{0} :: Created lock file at {1}:{2}/{3}.'.format( __name__, self.target, self.deploy_path, self.get_lock_name())) deploy_log.log('Created lock.')
def check_lock(self): """ Returns boolean flag on lock file existence """ cmd = "ls {0}/{1}".format(self.deploy_path, self.get_lock_name()) # log.debug('{0} :: Executing - "{1}"'.format(__name__, cmd)) log.info('{0} :: Checking for lock file at {1}.'.format( __name__, self.target)) try: ret = ssh_command_target(cmd, self.target, self.user, self.key_path) except Exception as e: log.error(__name__ + ' :: ' + e.message) raise DeployLockerError(message=exit_codes[16], exit_code=16) # Pull the lock file handle from try: file_handle = ret['stdout'][0].split('/')[-1].strip() except (IndexError, ValueError): log.info('{0} :: No lock file exists.'.format( __name__, self.target)) return False if file_handle == self.get_lock_name(): log.info('{0} :: {1} has lock.'.format(__name__, self.user)) return True else: log.info('{0} :: Another user has lock.'.format(__name__)) return False
def remove_lock(self): """ Remove the lock file """ log.info('{0} :: SSH Lock destroy.'.format(__name__)) cmd = "rm {0}/{1}".format(self.deploy_path, self.get_lock_name()) try: ssh_command_target(cmd, self.target, self.user, self.key_path) except Exception as e: log.error(__name__ + ' :: Failed ot remove lock -> ' + e.message) raise DeployLockerError(message=exit_codes[16], exit_code=16) # Logging log.info('{0} :: Removed lock file at {1}:{2}/{3}.'.format( __name__, self.target, self.deploy_path, self.get_lock_name())) deploy_log.log('Removed lock.')
def check_lock(self): """ Returns boolean flag on lock file existence """ cmd = "ls {0}/{1}".format(self.deploy_path, self.get_lock_name()) # log.debug('{0} :: Executing - "{1}"'.format(__name__, cmd)) log.info('{0} :: Checking for lock file at {1}.'.format( __name__, self.target)) try: ret = ssh_command_target(cmd, self.target, self.user, self.key_path) except Exception as e: log.error(__name__ + ' :: ' + e.message) raise DeployLockerError(message=exit_codes[16], exit_code=16) # Pull the lock file handle from try: file_handle = ret['stdout'][0].split('/')[-1].strip() except (IndexError, ValueError): log.info('{0} :: No lock file exists.'.format(__name__, self.target)) return False if file_handle == self.get_lock_name(): log.info('{0} :: {1} has lock.'.format(__name__, self.user)) return True else: log.info('{0} :: Another user has lock.'.format(__name__)) return False
def log(self, line): """ Handles log writing to remote file :param line: string; the line to be logged Returns True on successful logging, false otherwise. """ self._check_and_add(self.path, self.LOGNAME_ACTIVE) # escape logline re.escape(line) cmd = "echo '{0}' >> {1}/{2}".format(line, self.path, self.LOGNAME_ACTIVE) # Write remote log line try: ssh_command_target(cmd, self.target, self.user, self.key_path) except: log.error("Failed to log '{0}'".format(line)) return False return True