Example #1
0
        def post_start_project():
            """Copy metadata, create runfile, update index and open tty."""
            if self.shared_meta_file:
                path_to_copy = (self.config["guest_mountpoint"].format(
                    container_name, self.guest_meta_file))
                parent_path = re.sub("[^/]+$", "", path_to_copy)
                os.makedirs(parent_path)
                shutil.copy(self.shared_meta_file, path_to_copy)

            project_info = jsonify(uuid=container_uuid,
                                   wwwroot="/inst-{0}".format(container_name),
                                   project=project_name,
                                   memory=quota_mem,
                                   swap=quota_swap,
                                   cpus=quota_cpu)

            flag_filepath = os.path.join(self.run_dir, container_name)
            safe_write_to_file(flag_filepath, project_info, self.logger.error)

            if self.www_dir:
                self.update_project_stats()

            # Only open tty if the option is enabled
            if self.base_tty > 0:
                self.open_tty(container_name)

            return True
Example #2
0
        def post_start_project():
            """Copy metadata, create runfile, update index and open tty."""
            if self.shared_meta_file:
                path_to_copy = (self.config["guest_mountpoint"]
                                .format(container_name, self.guest_meta_file))
                parent_path = re.sub("[^/]+$", "", path_to_copy)
                os.makedirs(parent_path)
                shutil.copy(self.shared_meta_file, path_to_copy)

            project_info = jsonify(
                uuid=container_uuid,
                wwwroot="/inst-{0}".format(container_name),
                project=project_name,
                memory=quota_mem,
                swap=quota_swap,
                cpus=quota_cpu
            )

            flag_filepath = os.path.join(self.run_dir, container_name)
            safe_write_to_file(flag_filepath, project_info, self.logger.error)

            if self.www_dir:
                self.update_project_stats()

            # Only open tty if the option is enabled
            if self.base_tty > 0:
                self.open_tty(container_name)

            return True
Example #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)
Example #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)
Example #5
0
 def dump_info_to_file(tty_id, container_name):
     """Write PID of the monitor and the container attach to it."""
     new_tty_fp = filepath_from_tty_id(tty_id)
     tty_info = "{0} {1}".format(os.getpid(), container_name)
     safe_write_to_file(new_tty_fp, tty_info, self.logger.error)
Example #6
0
 def genwrite_uuid():
     # Generate a new UUID and save it
     new_uuid = str(uuid4())
     safe_write_to_file(self.uuid_file, new_uuid, self.logger.warning)
     return new_uuid
Example #7
0
 def dump_info_to_file(tty_id, container_name):
     """Write PID of the monitor and the container attach to it."""
     new_tty_fp = filepath_from_tty_id(tty_id)
     tty_info = "{0} {1}".format(os.getpid(), container_name)
     safe_write_to_file(new_tty_fp, tty_info, self.logger.error)
Example #8
0
 def genwrite_uuid():
     # Generate a new UUID and save it
     new_uuid = str(uuid4())
     safe_write_to_file(self.uuid_file, new_uuid, self.logger.warning)
     return new_uuid