Beispiel #1
0
    def json(self):
        """
        Main sub-function for top
        :return: None
        """
        # Make sure the docker daemon is running
        self.ping()
        # Activate optional columns
        self._activate_optionals()
        proc_info = []
        if len(self.args.containers) < 1:
            try:
                con_ids = [
                    x['Id'] for x in self.get_active_containers(refresh=True)
                ]
            except requests.exceptions.ConnectionError:
                raise NoDockerDaemon()
        else:
            con_ids = []
            self.get_active_containers(refresh=True)
            # verify the inputs are valid
            for user_input in self.args.containers:
                con_ids.append(self.get_input_id(user_input))
        for cid in con_ids:
            proc_info += self.get_pids_by_container(cid)

        return Json.dumps(self.reformat_ps_info(proc_info))
Beispiel #2
0
    def mount(self, identifier, options=None):  # pylint: disable=arguments-differ
        """
        Mounts a container or image referred to by identifier to
        the host filesystem.
        """
        if not options:
            options = []
        try:
            # Check if a container/image is already mounted at the
            # desired mount point.
            cid, _ = self._get_cid_from_mountpoint(self.mountpoint)
            if cid:
                raise ValueError(
                    "container/image '{0}' already mounted at '{1}'".format(
                        cid, self.mountpoint))
        except MountError:
            pass

        try:
            driver = self._info()['Driver']
        except requests.exceptions.ConnectionError:
            raise NoDockerDaemon()

        driver_mount_fn = getattr(self, "_mount_" + driver,
                                  self._unsupported_backend)
        driver_mount_fn(identifier, options)

        # Return mount path so it can be later unmounted by path
        return self.mountpoint
Beispiel #3
0
 def _ping(self):
     '''
     Check if the docker daemon is running; if not, exit with
     message and return code 1
     '''
     try:
         self.d.ping()
     except exceptions.ConnectionError:
         raise NoDockerDaemon()
Beispiel #4
0
 def _get_docker_images(self, get_all=False):
     try:
         images = self.d.images(all=get_all)
         for i in images:
             i["ImageType"] = "Docker"
             i["ImageId"] = i["Id"]
     except requests.exceptions.ConnectionError:
         raise NoDockerDaemon()
     return images
Beispiel #5
0
def get_docker_client():
    """
    Universal method to use docker.client()
    """
    kwargs = {"version": "auto"}
    kwargs.update(docker.utils.kwargs_from_env(assert_hostname=False))
    try:
        client = docker.APIClient #pylint: disable=no-member
    except AttributeError:
        client = docker.Client #pylint: disable=no-member
    try:
        return client(**kwargs)
    except docker.errors.DockerException:
        raise NoDockerDaemon()
Beispiel #6
0
 def Import(self):
     try:
         import_docker(self.graphdir, self.args.import_location,
                       self.args.assumeyes)
     except requests.exceptions.ConnectionError:
         raise NoDockerDaemon()
Beispiel #7
0
    def atomic_top(self):
        """
        Main sub-function for top
        :return: None
        """
        # Make sure the docker daemon is running
        self.ping()
        # Set debug bool
        self.set_debug()
        # Activate optional columns
        self._activate_optionals()
        # Do we have a tty?
        # Can run ./atomic top <&- to replicate no tty
        has_tty = isatty(0)
        # Set up terminal, input handling
        if has_tty:
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)
        sort_vals = [
            x['character'].upper() for x in self.headers
            if x['active'] and x['sort']
        ]
        counter = 0
        while True:
            proc_info = []
            if len(self.args.containers) < 1:
                try:
                    con_ids = [
                        x['Id']
                        for x in self.get_active_containers(refresh=True)
                    ]
                except requests.exceptions.ConnectionError:
                    raise NoDockerDaemon()
            else:
                con_ids = []
                self.get_active_containers(refresh=True)
                # verify the inputs are valid
                for user_input in self.args.containers:
                    con_ids.append(self.get_input_id(user_input))
            for cid in con_ids:
                proc_info += self.get_pids_by_container(cid)
            # Reset screen
            if not self.debug and has_tty:
                util.write_out("\033c")
            sorted_info = self.reformat_ps_info(proc_info)
            self._set_dynamic_column_widths(sorted_info)
            self.output_top(sorted_info)
            if has_tty:
                tty.setraw(sys.stdin.fileno())
            i, _, _ = select.select([sys.stdin], [], [], self.args.d)
            if i and has_tty:
                ch = sys.stdin.read(1)
                # Detect 'q' or Cntrl-c
                if ch.upper() == 'q'.upper() or ord(ch) == 3:
                    # reset terminal
                    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                    raise SystemExit
                # Detect of the character pushed is one in self.headers
                elif ch.upper() in sort_vals:
                    self._sort = next(
                        (header['shortname'] for header in self.headers
                         if header['character'] is not None
                         and header['character'].upper() == ch.upper()), False)

            # reset terminal
            if has_tty:
                termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            counter += 1
            if counter == self.args.n:
                raise SystemExit