Beispiel #1
0
    def pull(self):
        """
        Pull the image of the container from the registry.
        """

        exec_command(['docker', 'pull', self.image],
                     stderr=STDOUT,
                     universal_newlines=True)
        self.change_reason.append("Executed 'docker pull'")
        self.changed = True
Beispiel #2
0
    def remove(self):
        """
        Remove the docker container.
        """

        self.change_reason.append("Executed 'docker rm'")
        exec_command(['docker', 'rm', self.name],
                     stderr=STDOUT,
                     universal_newlines=True)
        self.changed = True
Beispiel #3
0
    def start(self):
        """
        Starts an existing container.
        """

        self.change_reason.append("Executed 'docker start'")
        exec_command(['docker', 'start', self.name],
                     stderr=STDOUT,
                     universal_newlines=True)
        self.changed = True
Beispiel #4
0
    def stop(self):
        """
        Stop the docker container.
        """

        self.change_reason.append("Executed 'docker stop'")
        exec_command(['docker', 'stop', self.name],
                     stderr=STDOUT,
                     universal_newlines=True)
        self.changed = True
Beispiel #5
0
    def build(self):
        """
        Build the docker image of the container.
        """

        # Makes sure this runs in the directory where the Dockerfile is,
        # otherwise the command would be wrong.
        exec_command(self.build_command,
                     cwd=self.path,
                     stderr=STDOUT,
                     universal_newlines=True)
        self.change_reason.append("Executed 'docker build'")
        self.changed = True
Beispiel #6
0
    def restart(self):
        """
        Restart the docker container (or start it if it wasn't running).
        """

        if self.changed:
            self.stop()
            self.remove()
            self.run()
        else:
            self.change_reason.append("Executed 'docker restart'")
            exec_command(['docker', 'restart', self.name],
                         stderr=STDOUT,
                         universal_newlines=True)
            self.changed = True
Beispiel #7
0
    def run(self):
        """
        Run the container.
        """

        self.run_stdout = exec_command(self.run_command,
                                       stderr=STDOUT,
                                       universal_newlines=True)
        self.change_reason.append("Executed 'docker run'")
        self.changed = True
Beispiel #8
0
    def running(self):
        """
        Check, whether the container is running or not.

        :return: True if it is running, False otherwise.
        """

        try:
            # This will either be 'true' (container runs), 'false' (container
            # is stopped) or throw an exception (container doesn't exist).
            return 'true' in exec_command([
                'docker', 'inspect', '--format', '"{{.State.Running}}"',
                self.name
            ],
                                          stderr=STDOUT,
                                          universal_newlines=True)
        except CalledProcessError as e:
            # If that command fails, the container is not just not running, but it also doesn't exist
            return None
Beispiel #9
0
    def needs_pull(self):
        """
        Check, whether the image for the container already exists locally or
        if it has to be pulled first. This function can currently not check,
        if the local version is also the newest one, it only checks whether
        it exists locally or not.

        :return: True if it needs to be pulled and doesn't exist locally, False otherwise.
        """

        try:
            return not exec_command(
                ['docker', 'inspect', '--format', '{{.ID}}', self.image],
                stderr=STDOUT,
                universal_newlines=True)
        except CalledProcessError:
            # If that command fails, we assume that the image was not found locally
            self.change_reason.append("Image not found, needs pull")
            return True
Beispiel #10
0
    def needs_rebuild(self):
        """
        Check, whether the image needs to be rebuilt. This function looks at
        the path to the Dockerfile and checks, whether any files there are
        newer than the docker image.

        :return: True if the image needs to be rebuilt, False otherwise.
        """

        # If the build command changed, we definitely have to rebuild it
        if self.build_command_str != self.prev_build_command:
            self.change_reason.append("Arguments changed for build command")
            self.change_reason.append(self.build_command_str)
            self.change_reason.append(self.prev_build_command)
            return True

        # Get the creation time of the docker image
        try:
            time_str = exec_command(
                ['docker', 'inspect', '--format', '{{.Created}}', self.image],
                stderr=STDOUT,
                universal_newlines=True)
        except CalledProcessError:
            # If that command fails, we assume that the image was not found
            self.change_reason.append("Image not found, needs rebuild")
            return True

        # Convert the time to a format we can use for comparisons
        image_creation_time = datetime.datetime.strptime(
            time_str[:26], "%Y-%m-%dT%H:%M:%S.%f")

        # Iterate over all files in the image path to see if any of those files
        # were more recently modified than the image creation time.
        for root, subdirs, files in os.walk(self.path):
            for filename in files:
                file_mtime = datetime.datetime.utcfromtimestamp(
                    os.path.getmtime(os.path.join(root, filename)))
                if image_creation_time < file_mtime:
                    self.change_reason.append("File changed: " + filename)
                    return True
        return False