From 6529fa599c9ee2a4576fe8fab92f461f0fba798d Mon Sep 17 00:00:00 2001 From: Frank Sachsenheim Date: Sat, 4 Mar 2017 00:18:56 +0100 Subject: [PATCH 1/5] Makes docs builds faster and ensures proper ownership Signed-off-by: Frank Sachsenheim --- .dockerignore | 3 +-- Dockerfile-docs | 15 ++++++++------- Makefile | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.dockerignore b/.dockerignore index 050b8bce1a..198b23ecbb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -13,5 +13,4 @@ html/* __pycache__ # Compiled Documentation -site/ -Makefile +docs/_build diff --git a/Dockerfile-docs b/Dockerfile-docs index 6f4194009a..105083e8cb 100644 --- a/Dockerfile-docs +++ b/Dockerfile-docs @@ -1,12 +1,13 @@ FROM python:3.5 -RUN mkdir /src -WORKDIR /src +ARG uid=1000 +ARG gid=1000 -COPY requirements.txt /src/requirements.txt -RUN pip install -r requirements.txt +RUN addgroup --gid $gid sphinx \ + && useradd --uid $uid --gid $gid -M sphinx -COPY docs-requirements.txt /src/docs-requirements.txt -RUN pip install -r docs-requirements.txt +WORKDIR /src +COPY requirements.txt docs-requirements.txt ./ +RUN pip install -r requirements.txt -r docs-requirements.txt -COPY . /src +USER sphinx diff --git a/Makefile b/Makefile index e4c64e71e7..cd1174675b 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ build-py3: .PHONY: build-docs build-docs: - docker build -t docker-sdk-python-docs -f Dockerfile-docs . + docker build -t docker-sdk-python-docs -f Dockerfile-docs --build-arg uid=$(shell id -u) --build-arg gid=$(shell id -g) . .PHONY: build-dind-certs build-dind-certs: @@ -77,7 +77,7 @@ flake8: build .PHONY: docs docs: build-docs - docker run --rm -it -v `pwd`:/code docker-sdk-python-docs sphinx-build docs ./_build + docker run --rm -it -v `pwd`:/src docker-sdk-python-docs sphinx-build docs docs/_build .PHONY: shell shell: build From 1cd56b9f0c85af580c59597643a307b3177ab7c9 Mon Sep 17 00:00:00 2001 From: Frank Sachsenheim Date: Sat, 4 Mar 2017 00:22:19 +0100 Subject: [PATCH 2/5] Adds a 'labels' property to the container model The Docker API seems to respond with a 'null' value for the 'Labels' attribute from containers that were created with older Docker versions. An empty dictionary is returned in this case. Signed-off-by: Frank Sachsenheim --- docker/models/containers.py | 8 ++++++++ docs/containers.rst | 1 + tests/unit/fake_api.py | 2 +- tests/unit/models_containers_test.py | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docker/models/containers.py b/docker/models/containers.py index fb10ba90b3..493f180438 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -18,6 +18,14 @@ def name(self): if self.attrs.get('Name') is not None: return self.attrs['Name'].lstrip('/') + @property + def labels(self): + """ + The labels of a container as dictionary. + """ + result = self.attrs['Config'].get('Labels') + return result or {} + @property def status(self): """ diff --git a/docs/containers.rst b/docs/containers.rst index 20529b0eff..b67a066d0c 100644 --- a/docs/containers.rst +++ b/docs/containers.rst @@ -25,6 +25,7 @@ Container objects .. autoattribute:: short_id .. autoattribute:: name .. autoattribute:: status + .. autoattribute:: labels .. py:attribute:: attrs The raw representation of this object from the server. diff --git a/tests/unit/fake_api.py b/tests/unit/fake_api.py index 2d0a0b4541..2914b63ae6 100644 --- a/tests/unit/fake_api.py +++ b/tests/unit/fake_api.py @@ -134,7 +134,7 @@ def get_fake_inspect_container(tty=False): status_code = 200 response = { 'Id': FAKE_CONTAINER_ID, - 'Config': {'Privileged': True, 'Tty': tty}, + 'Config': {'Labels': {'foo': 'bar'}, 'Privileged': True, 'Tty': tty}, 'ID': FAKE_CONTAINER_ID, 'Image': 'busybox:latest', 'Name': 'foobar', diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index ae1bd12aae..a5ef4a1145 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -390,6 +390,11 @@ def test_kill(self): container.kill(signal=5) client.api.kill.assert_called_with(FAKE_CONTAINER_ID, signal=5) + def test_labels(self): + client = make_fake_client() + container = client.containers.get(FAKE_CONTAINER_ID) + assert container.labels == {'foo': 'bar'} + def test_logs(self): client = make_fake_client() container = client.containers.get(FAKE_CONTAINER_ID) From b585ec59a8a3e7f6f7252d106df0a027eb0b5ab6 Mon Sep 17 00:00:00 2001 From: Frank Sachsenheim Date: Sat, 4 Mar 2017 00:36:11 +0100 Subject: [PATCH 3/5] Adds a 'labels' property to the image model Signed-off-by: Frank Sachsenheim --- docker/models/images.py | 8 ++++++++ docs/images.rst | 1 + tests/unit/fake_api.py | 1 + tests/unit/models_images_test.py | 5 +++++ 4 files changed, 15 insertions(+) diff --git a/docker/models/images.py b/docker/models/images.py index 51ee6f4ab9..3fd3dc1956 100644 --- a/docker/models/images.py +++ b/docker/models/images.py @@ -15,6 +15,14 @@ class Image(Model): def __repr__(self): return "<%s: '%s'>" % (self.__class__.__name__, "', '".join(self.tags)) + @property + def labels(self): + """ + The labels of an image as dictionary. + """ + result = self.attrs['Config'].get('Labels') + return result or {} + @property def short_id(self): """ diff --git a/docs/images.rst b/docs/images.rst index 25fcffc83d..44f006c08e 100644 --- a/docs/images.rst +++ b/docs/images.rst @@ -29,6 +29,7 @@ Image objects .. autoattribute:: id .. autoattribute:: short_id .. autoattribute:: tags + .. autoattribute:: labels .. py:attribute:: attrs The raw representation of this object from the server. diff --git a/tests/unit/fake_api.py b/tests/unit/fake_api.py index 2914b63ae6..ff0f1b65cc 100644 --- a/tests/unit/fake_api.py +++ b/tests/unit/fake_api.py @@ -158,6 +158,7 @@ def get_fake_inspect_image(): 'Parent': "27cf784147099545", 'Created': "2013-03-23T22:24:18.818426-07:00", 'Container': FAKE_CONTAINER_ID, + 'Config': {'Labels': {'bar': 'foo'}}, 'ContainerConfig': { "Hostname": "", diff --git a/tests/unit/models_images_test.py b/tests/unit/models_images_test.py index efb2116660..784717be8e 100644 --- a/tests/unit/models_images_test.py +++ b/tests/unit/models_images_test.py @@ -21,6 +21,11 @@ def test_get(self): assert isinstance(image, Image) assert image.id == FAKE_IMAGE_ID + def test_labels(self): + client = make_fake_client() + image = client.images.get(FAKE_IMAGE_ID) + assert image.labels == {'bar': 'foo'} + def test_list(self): client = make_fake_client() images = client.images.list(all=True) From 659090fc99b87bc9bb4fb5533b09d7ba781ac28d Mon Sep 17 00:00:00 2001 From: Frank Sachsenheim Date: Sat, 4 Mar 2017 01:04:20 +0100 Subject: [PATCH 4/5] Adds an 'image' property to the container model Signed-off-by: Frank Sachsenheim --- docker/models/containers.py | 10 ++++++++++ docs/containers.rst | 1 + tests/unit/models_containers_test.py | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/docker/models/containers.py b/docker/models/containers.py index 493f180438..93f637252f 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -18,6 +18,16 @@ def name(self): if self.attrs.get('Name') is not None: return self.attrs['Name'].lstrip('/') + @property + def image(self): + """ + The image of the container. + """ + image_id = self.attrs['Image'] + if image_id is None: + return None + return self.client.images.get(image_id.split(':')[1]) + @property def labels(self): """ diff --git a/docs/containers.rst b/docs/containers.rst index b67a066d0c..9ea64ad549 100644 --- a/docs/containers.rst +++ b/docs/containers.rst @@ -25,6 +25,7 @@ Container objects .. autoattribute:: short_id .. autoattribute:: name .. autoattribute:: status + .. autoattribute:: image .. autoattribute:: labels .. py:attribute:: attrs diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index a5ef4a1145..c594606170 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -384,6 +384,11 @@ def test_get_archive(self): container.get_archive('foo') client.api.get_archive.assert_called_with(FAKE_CONTAINER_ID, 'foo') + def test_image(self): + client = make_fake_client() + container = client.containers.get(FAKE_CONTAINER_ID) + assert container.image.id == FAKE_IMAGE_ID + def test_kill(self): client = make_fake_client() container = client.containers.get(FAKE_CONTAINER_ID) From 9536c8653dc02624fea01ce9bd6ee01df4369269 Mon Sep 17 00:00:00 2001 From: Frank Sachsenheim Date: Sat, 4 Mar 2017 01:10:40 +0100 Subject: [PATCH 5/5] Sorts model attributes in api docs alphabetically Signed-off-by: Frank Sachsenheim --- docs/containers.rst | 8 ++++---- docs/images.rst | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/containers.rst b/docs/containers.rst index 9ea64ad549..6c895c6b2d 100644 --- a/docs/containers.rst +++ b/docs/containers.rst @@ -21,13 +21,13 @@ Container objects .. autoclass:: Container() + .. py:attribute:: attrs .. autoattribute:: id - .. autoattribute:: short_id - .. autoattribute:: name - .. autoattribute:: status .. autoattribute:: image .. autoattribute:: labels - .. py:attribute:: attrs + .. autoattribute:: name + .. autoattribute:: short_id + .. autoattribute:: status The raw representation of this object from the server. diff --git a/docs/images.rst b/docs/images.rst index 44f006c08e..3ba06010a6 100644 --- a/docs/images.rst +++ b/docs/images.rst @@ -26,11 +26,11 @@ Image objects .. autoclass:: Image() - .. autoattribute:: id - .. autoattribute:: short_id - .. autoattribute:: tags - .. autoattribute:: labels - .. py:attribute:: attrs +.. py:attribute:: attrs +.. autoattribute:: id +.. autoattribute:: labels +.. autoattribute:: short_id +.. autoattribute:: tags The raw representation of this object from the server.