Esempio n. 1
0
 def __init__(self, **kargs):
     self.tasks = TasksModel(**kargs)
     self.task = TaskModel(**kargs)
     self.current_taskid = 0
     self.objstore = kargs['objstore']
Esempio n. 2
0
class FirmwareProgressModel(object):
    def __init__(self, **kargs):
        self.tasks = TasksModel(**kargs)
        self.task = TaskModel(**kargs)
        self.current_taskid = 0
        self.objstore = kargs['objstore']

    def is_update_flash_running(self):
        for task in self.tasks.get_list():
            info = self.task.lookup(task)
            if info['target_uri'] == '/plugins/ginger/firmware/upgrade':
                if info['status'] == 'running':
                    return True
        self.current_taskid = 0
        return False

    def tailUpdateLogs(self, cb, tee_log_file=None):
        """
        Read the log_file to return it's content (the output of update_flash
        command). If the file is not found, a simple '*' is displayed to track
        progress.
        """
        if not self.is_update_flash_running():
            msg = "Error flashing firmware.\n"
            msg = msg+"Please see /usr/sbin/update_flash for rc reasons."
            return cb(msg, True)

        fd = None
        try:
            fd = os.open(tee_log_file, os.O_RDONLY)

        # cannot open file, print something to let users know that the
        # system is being upgrading until the package manager finishes its
        # job
        except (TypeError, OSError):
            msgs = []
            while self.is_update_flash_running():
                msgs.append('*')
                cb(''.join(msgs))
                time.sleep(1)
            msgs.append('\n')
            return cb(''.join(msgs), True)

        # go to the end of logfile and starts reading, if nothing is read or
        # a pattern is not found in the message just wait and retry until
        # the package manager finishes
        os.lseek(fd, 0, os.SEEK_END)
        msgs = []
        progress = []
        while True:
            read = os.read(fd, 1024)
            if not read:
                if not self.is_update_flash_running():
                    break

                if not msgs:
                    progress.append('*')
                    cb(''.join(progress))

                time.sleep(1)
                continue

            msgs.append(read)
            cb(''.join(msgs))

        os.close(fd)
        return cb(''.join(msgs), True)

    def lookup(self, *name):
        if self.current_taskid == 0:
            self.current_taskid = add_task('/plugins/ginger/fwprogress',
                                           self.tailUpdateLogs, self.objstore,
                                           fw_tee_log)
        return self.task.lookup(self.current_taskid)