Esempio n. 1
0
 def remove_tty_flag(tty_id):
     """Remove tty flag."""
     feedback = self.feedback["tty_file_removal"].format(tty_id)
     with logged(self.logger.error, feedback, (IOError,)):
         with ignored(OSError):
             os.remove(filepath_from_tty_id(tty_id))
         return True
     return False
Esempio n. 2
0
 def remove_tty_flag(tty_id):
     """Remove tty flag."""
     feedback = self.feedback["tty_file_removal"].format(tty_id)
     with logged(self.logger.error, feedback, (IOError, )):
         with ignored(OSError):
             os.remove(filepath_from_tty_id(tty_id))
         return True
     return False
Esempio n. 3
0
    def update_project_stats(self):
        """Thread-safe update of the index in the public WWW folder.

        Index represents the state, resources and logs of the projects.
        """
        def running_instances():
            """Get run dir and container name of every project."""
            for container_name in self.get_containers_in_run_dir():
                run_fp = self.path_of_runfile(container_name)
                lines = safe_read_from_file(
                    run_fp, self.logger.error, lines=True
                ) or [""]
                yield lines[0]

        def get_uptime():
            """Read uptime and replace spaces by commas."""
            seconds = safe_read_from_file("/proc/uptime", self.logger.error)[0]
            return seconds.translate(string.maketrans(' ', ',')) or None

        def get_load():
            """Get load from the uptime command."""
            return check_output(["uptime"]).split("load average: ")[1]

        def get_run_hours():
            """Get run hours of DumbQ."""
            runhours_fp = self.config["dumbq_runhours"]
            hours = safe_read_from_file(
                runhours_fp, self.logger.warning, lines=True)
            return hours[0] if hours else 0

        update_error_message = feedback["update_index_error"]
        now = str(time.time()).split(".")[0]

        # Get updated index from current values
        updated_index = jsonify(instances=list(running_instances()),
                                updated=now,
                                machine_uuid=self.host_uuid,
                                version=self.version,
                                uptime=get_uptime(),
                                load=get_load(),
                                runhours=get_run_hours())

        # Overwrite updated contents to tmp file and update old
        safe_write_to_file(self.temp_index_filepath,
                           updated_index, self.logger.warning)

        with logged(self.logger, update_error_message, (EnvironmentError,)):
            os.rename(self.temp_index_filepath, self.index_filepath)
Esempio n. 4
0
    def update_project_stats(self):
        """Thread-safe update of the index in the public WWW folder.

        Index represents the state, resources and logs of the projects.
        """
        def running_instances():
            """Get run dir and container name of every project."""
            for container_name in self.get_containers_in_run_dir():
                run_fp = self.path_of_runfile(container_name)
                lines = safe_read_from_file(
                    run_fp, self.logger.error, lines=True) or [""]
                yield lines[0]

        def get_uptime():
            """Read uptime and replace spaces by commas."""
            seconds = safe_read_from_file("/proc/uptime", self.logger.error)[0]
            return seconds.translate(string.maketrans(' ', ',')) or None

        def get_load():
            """Get load from the uptime command."""
            return check_output(["uptime"]).split("load average: ")[1]

        def get_run_hours():
            """Get run hours of DumbQ."""
            runhours_fp = self.config["dumbq_runhours"]
            hours = safe_read_from_file(runhours_fp,
                                        self.logger.warning,
                                        lines=True)
            return hours[0] if hours else 0

        update_error_message = feedback["update_index_error"]
        now = str(time.time()).split(".")[0]

        # Get updated index from current values
        updated_index = jsonify(instances=list(running_instances()),
                                updated=now,
                                machine_uuid=self.host_uuid,
                                version=self.version,
                                uptime=get_uptime(),
                                load=get_load(),
                                runhours=get_run_hours())

        # Overwrite updated contents to tmp file and update old
        safe_write_to_file(self.temp_index_filepath, updated_index,
                           self.logger.warning)

        with logged(self.logger, update_error_message, (EnvironmentError, )):
            os.rename(self.temp_index_filepath, self.index_filepath)