2014-04-01 20:54:37 +02:00
|
|
|
#!/usr/bin/python
|
2014-01-22 13:04:19 +01:00
|
|
|
#
|
2016-04-21 00:23:09 +02:00
|
|
|
# Copyright 2016 Red Hat | Ansible
|
2014-01-22 13:04:19 +01:00
|
|
|
#
|
2014-02-11 17:10:47 +01:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2016-04-21 00:23:09 +02:00
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
2014-01-22 13:04:19 +01:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2016-04-21 00:23:09 +02:00
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
2014-01-22 13:04:19 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2016-04-21 00:23:09 +02:00
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2014-02-11 17:10:47 +01:00
|
|
|
module: docker_image
|
2016-04-21 00:23:09 +02:00
|
|
|
|
|
|
|
short_description: Manage docker images.
|
|
|
|
|
2016-04-23 16:09:44 +02:00
|
|
|
version_added: "1.5"
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Build, load or pull an image, making the image available for creating containers. Also supports tagging an
|
|
|
|
image into a repository and archiving an image to a .tar file.
|
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
options:
|
2016-04-21 00:23:09 +02:00
|
|
|
archive_path:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-08-06 01:14:31 +02:00
|
|
|
- Use with state C(present) to archive an image to a .tar file.
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
2016-04-25 21:17:48 +02:00
|
|
|
version_added: "2.1"
|
2016-08-06 01:14:31 +02:00
|
|
|
load_path:
|
|
|
|
description:
|
|
|
|
- Use with state C(present) to load an image from a .tar file.
|
|
|
|
required: false
|
|
|
|
version_added: "2.2"
|
2015-05-18 09:58:31 +02:00
|
|
|
dockerfile:
|
|
|
|
description:
|
2016-07-12 22:24:56 +02:00
|
|
|
- Use with state C(present) to provide an alternate name for the Dockerfile to use when building an image.
|
2015-05-18 09:58:31 +02:00
|
|
|
default: Dockerfile
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-04-25 21:12:28 +02:00
|
|
|
version_added: "2.0"
|
2016-04-21 00:23:09 +02:00
|
|
|
force:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-08-05 16:47:29 +02:00
|
|
|
- Use with state I(absent) to un-tag and remove all images matching the specified name. Use with state
|
|
|
|
C(present) to build, load or pull an image when the image already exists.
|
2016-04-21 00:23:09 +02:00
|
|
|
default: false
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-04-25 21:17:48 +02:00
|
|
|
version_added: "2.1"
|
2016-04-21 00:23:09 +02:00
|
|
|
http_timeout:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Timeout for HTTP requests during the image build operation. Provide a positive integer value for the number of
|
|
|
|
seconds.
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
2016-04-25 21:17:48 +02:00
|
|
|
version_added: "2.1"
|
2016-04-21 00:23:09 +02:00
|
|
|
name:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- "Image name. Name format will be one of: name, repository/name, registry_server:port/name.
|
|
|
|
When pushing or pulling an image the name can optionally include the tag by appending ':tag_name'."
|
|
|
|
required: true
|
|
|
|
path:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Use with state 'present' to build an image. Will be the path to a directory containing the context and
|
|
|
|
Dockerfile for building an image.
|
|
|
|
aliases:
|
|
|
|
- build_path
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
2016-04-21 00:23:09 +02:00
|
|
|
pull:
|
2015-07-02 02:15:23 +02:00
|
|
|
description:
|
2016-04-23 16:09:44 +02:00
|
|
|
- When building an image downloads any updates to the FROM image in Dockerfile.
|
2016-04-21 00:23:09 +02:00
|
|
|
default: true
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-04-25 21:17:48 +02:00
|
|
|
version_added: "2.1"
|
2016-07-12 22:24:56 +02:00
|
|
|
push:
|
|
|
|
description:
|
|
|
|
- Push the image to the registry. Specify the registry as part of the I(name) or I(repository) parameter.
|
|
|
|
default: false
|
|
|
|
required: false
|
|
|
|
version_added: "2.2"
|
2016-04-21 00:23:09 +02:00
|
|
|
rm:
|
2015-07-02 02:15:23 +02:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Remove intermediate containers after build.
|
|
|
|
default: true
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-04-25 21:17:48 +02:00
|
|
|
version_added: "2.1"
|
2016-04-21 00:23:09 +02:00
|
|
|
nocache:
|
2015-07-02 02:15:23 +02:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Do not use cache when building an image.
|
|
|
|
default: false
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-04-21 00:23:09 +02:00
|
|
|
repository:
|
2015-05-23 22:17:30 +02:00
|
|
|
description:
|
2016-08-05 11:01:55 +02:00
|
|
|
- Full path to a repository. Use with state C(present) to tag the image into the repository. Expects
|
2016-08-05 22:08:11 +02:00
|
|
|
format I(repository:tag). If no tag is provided, will use the value of the C(tag) parameter or I(latest).
|
2016-04-21 00:23:09 +02:00
|
|
|
required: false
|
2016-04-25 21:17:48 +02:00
|
|
|
version_added: "2.1"
|
2014-01-22 13:04:19 +01:00
|
|
|
state:
|
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Make assertions about the state of an image.
|
2016-07-12 22:24:56 +02:00
|
|
|
- When C(absent) an image will be removed. Use the force option to un-tag and remove all images
|
2016-04-21 00:23:09 +02:00
|
|
|
matching the provided name.
|
2016-07-12 22:24:56 +02:00
|
|
|
- When C(present) check if an image exists using the provided name and tag. If the image is not found or the
|
2016-04-21 00:23:09 +02:00
|
|
|
force option is used, the image will either be pulled, built or loaded. By default the image will be pulled
|
|
|
|
from Docker Hub. To build the image, provide a path value set to a directory containing a context and
|
|
|
|
Dockerfile. To load an image, specify load_path to provide a path to an archive file. To tag an image to a
|
2016-04-23 16:09:44 +02:00
|
|
|
repository, provide a repository path. If the name contains a repository path, it will be pushed.
|
2016-08-05 16:47:29 +02:00
|
|
|
- "NOTE: C(build) is DEPRECATED and will be removed in release 2.3. Specifying C(build) will behave the
|
|
|
|
same as C(present)."
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2014-01-22 13:04:19 +01:00
|
|
|
default: present
|
2016-04-21 00:23:09 +02:00
|
|
|
choices:
|
|
|
|
- absent
|
|
|
|
- present
|
2016-08-05 16:47:29 +02:00
|
|
|
- build
|
2016-04-21 00:23:09 +02:00
|
|
|
tag:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2016-04-21 00:23:09 +02:00
|
|
|
- Used to select an image when pulling. Will be added to the image when pushing, tagging or building. Defaults to
|
2016-08-05 20:20:52 +02:00
|
|
|
I(latest).
|
|
|
|
- If C(name) parameter format is I(name:tag), then tag value from C(name) will take precedence.
|
2016-04-21 00:23:09 +02:00
|
|
|
default: latest
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-08-06 01:14:31 +02:00
|
|
|
buildargs:
|
|
|
|
description:
|
|
|
|
- Provide a dictionary of C(key:value) build arguments that map to Dockerfile ARG directive.
|
|
|
|
- Docker expects the value to be a string. For convenience any non-string values will be converted to strings.
|
|
|
|
- Requires Docker API >= 1.21 and docker-py >= 1.7.0.
|
|
|
|
type: complex
|
|
|
|
required: false
|
|
|
|
version_added: "2.2"
|
2016-04-29 06:39:08 +02:00
|
|
|
container_limits:
|
|
|
|
description:
|
|
|
|
- A dictionary of limits applied to each container created by the build process.
|
|
|
|
required: false
|
|
|
|
version_added: "2.1"
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
memory:
|
|
|
|
description: Set memory limit for build
|
|
|
|
type: int
|
|
|
|
memswap:
|
|
|
|
description: Total memory (memory + swap), -1 to disable swap
|
|
|
|
type: int
|
|
|
|
cpushares:
|
|
|
|
description: CPU shares (relative weight)
|
|
|
|
type: int
|
|
|
|
cpusetcpus:
|
|
|
|
description: CPUs in which to allow execution, e.g., "0-3", "0,1"
|
|
|
|
type: str
|
2016-04-21 00:23:09 +02:00
|
|
|
use_tls:
|
|
|
|
description:
|
2016-07-12 22:24:56 +02:00
|
|
|
- "DEPRECATED. Whether to use tls to connect to the docker server. Set to C(no) when TLS will not be used. Set to
|
|
|
|
C(encrypt) to use TLS. And set to C(verify) to use TLS and verify that the server's certificate is valid for the
|
2016-04-21 00:23:09 +02:00
|
|
|
server. NOTE: If you specify this option, it will set the value of the tls or tls_verify parameters."
|
|
|
|
choices:
|
|
|
|
- no
|
|
|
|
- encrypt
|
|
|
|
- verify
|
|
|
|
default: no
|
2016-07-12 22:24:56 +02:00
|
|
|
required: false
|
2016-04-21 00:23:09 +02:00
|
|
|
version_added: "2.0"
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- docker
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- "docker-py >= 1.7.0"
|
|
|
|
- "Docker API >= 1.20"
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
authors:
|
|
|
|
- Pavel Antonov (@softzilla)
|
|
|
|
- Chris Houseknecht (@chouseknecht)
|
2016-04-24 17:39:22 +02:00
|
|
|
- James Tanner (@jctanner)
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
'''
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
EXAMPLES = '''
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
- name: pull an image
|
|
|
|
docker_image:
|
|
|
|
name: pacur/centos-7
|
|
|
|
|
2016-07-12 22:24:56 +02:00
|
|
|
- name: Tag and push to docker hub
|
2016-04-21 00:23:09 +02:00
|
|
|
docker_image:
|
|
|
|
name: pacur/centos-7
|
2016-07-12 22:24:56 +02:00
|
|
|
repository: dcoppenhagan/myimage
|
2016-04-21 00:23:09 +02:00
|
|
|
tag: 7.0
|
2016-07-12 22:24:56 +02:00
|
|
|
push: yes
|
|
|
|
|
|
|
|
- name: Tag and push to local registry
|
|
|
|
docker_image:
|
|
|
|
name: centos
|
|
|
|
repository: localhost:5000/centos
|
|
|
|
tag: 7
|
|
|
|
push: yes
|
2016-04-21 00:23:09 +02:00
|
|
|
|
|
|
|
- name: Remove image
|
|
|
|
docker_image:
|
|
|
|
state: absent
|
|
|
|
name: registry.ansible.com/chouseknecht/sinatra
|
|
|
|
tag: v1
|
|
|
|
|
2016-04-23 16:09:44 +02:00
|
|
|
- name: Build an image ad push it to a private repo
|
2016-04-21 00:23:09 +02:00
|
|
|
docker_image:
|
|
|
|
path: ./sinatra
|
|
|
|
name: registry.ansible.com/chouseknecht/sinatra
|
|
|
|
tag: v1
|
|
|
|
|
|
|
|
- name: Archive image
|
|
|
|
docker_image:
|
|
|
|
name: registry.ansible.com/chouseknecht/sinatra
|
|
|
|
tag: v1
|
|
|
|
archive_path: my_sinatra.tar
|
|
|
|
|
2016-07-12 22:24:56 +02:00
|
|
|
- name: Load image from archive and push to a private registry
|
2016-04-21 00:23:09 +02:00
|
|
|
docker_image:
|
2016-07-12 22:24:56 +02:00
|
|
|
name: localhost:5000/myimages/sinatra
|
2016-04-21 00:23:09 +02:00
|
|
|
tag: v1
|
2016-07-12 22:24:56 +02:00
|
|
|
push: yes
|
2016-04-21 00:23:09 +02:00
|
|
|
load_path: my_sinatra.tar
|
2016-06-08 10:31:46 +02:00
|
|
|
push: True
|
2016-08-06 01:14:31 +02:00
|
|
|
|
|
|
|
- name: Build image and with buildargs
|
|
|
|
docker_image:
|
|
|
|
path: /path/to/build/dir
|
|
|
|
name: myimage
|
|
|
|
buildargs:
|
|
|
|
log_volume: /var/log/myapp
|
|
|
|
listen_port: 8080
|
2014-01-22 13:04:19 +01:00
|
|
|
'''
|
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
RETURN = '''
|
|
|
|
image:
|
2016-06-08 10:31:46 +02:00
|
|
|
description: Image inspection results for the affected image.
|
2016-04-29 06:39:08 +02:00
|
|
|
returned: success
|
|
|
|
type: complex
|
|
|
|
sample: {}
|
2016-04-21 00:23:09 +02:00
|
|
|
'''
|
2015-05-11 21:15:53 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
from ansible.module_utils.docker_common import *
|
2015-05-11 21:15:53 +02:00
|
|
|
|
|
|
|
try:
|
2016-07-12 22:24:56 +02:00
|
|
|
from docker.auth.auth import resolve_repository_name
|
|
|
|
from docker.utils.utils import parse_repository_tag
|
2015-05-11 21:15:53 +02:00
|
|
|
except ImportError:
|
2016-04-21 00:23:09 +02:00
|
|
|
# missing docker-py handled in docker_common
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ImageManager(DockerBaseClass):
|
|
|
|
|
|
|
|
def __init__(self, client, results):
|
|
|
|
|
|
|
|
super(ImageManager, self).__init__()
|
|
|
|
|
|
|
|
self.client = client
|
|
|
|
self.results = results
|
|
|
|
parameters = self.client.module.params
|
|
|
|
self.check_mode = self.client.check_mode
|
|
|
|
|
|
|
|
self.archive_path = parameters.get('archive_path')
|
|
|
|
self.container_limits = parameters.get('container_limits')
|
|
|
|
self.dockerfile = parameters.get('dockerfile')
|
|
|
|
self.force = parameters.get('force')
|
|
|
|
self.load_path = parameters.get('load_path')
|
|
|
|
self.name = parameters.get('name')
|
|
|
|
self.nocache = parameters.get('nocache')
|
|
|
|
self.path = parameters.get('path')
|
|
|
|
self.pull = parameters.get('pull')
|
|
|
|
self.repository = parameters.get('repository')
|
|
|
|
self.rm = parameters.get('rm')
|
|
|
|
self.state = parameters.get('state')
|
|
|
|
self.tag = parameters.get('tag')
|
|
|
|
self.http_timeout = parameters.get('http_timeout')
|
2016-07-12 22:24:56 +02:00
|
|
|
self.push = parameters.get('push')
|
2016-08-06 01:14:31 +02:00
|
|
|
self.buildargs = parameters.get('buildargs')
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2016-08-05 20:20:52 +02:00
|
|
|
# If name contains a tag, it takes precedence over tag parameter.
|
|
|
|
repo, repo_tag = parse_repository_tag(self.name)
|
|
|
|
if repo_tag:
|
|
|
|
self.name = repo
|
|
|
|
self.tag = repo_tag
|
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
if self.state in ['present', 'build']:
|
|
|
|
self.present()
|
|
|
|
elif self.state == 'absent':
|
|
|
|
self.absent()
|
|
|
|
|
|
|
|
def fail(self, msg):
|
|
|
|
self.client.fail(msg)
|
|
|
|
|
|
|
|
def present(self):
|
|
|
|
'''
|
|
|
|
Handles state = 'present', which includes building, loading or pulling an image,
|
|
|
|
depending on user provided parameters.
|
|
|
|
|
|
|
|
:returns None
|
|
|
|
'''
|
|
|
|
image = self.client.find_image(name=self.name, tag=self.tag)
|
|
|
|
|
|
|
|
if not image or self.force:
|
|
|
|
if self.path:
|
2016-04-25 21:12:28 +02:00
|
|
|
# Build the image
|
2016-05-13 06:30:36 +02:00
|
|
|
if not os.path.isdir(self.path):
|
|
|
|
self.fail("Requested build path %s could not be found or you do not have access." % self.path)
|
2016-04-25 21:12:28 +02:00
|
|
|
image_name = self.name
|
2016-04-21 00:23:09 +02:00
|
|
|
if self.tag:
|
2016-04-25 21:12:28 +02:00
|
|
|
image_name = "%s:%s" % (self.name, self.tag)
|
|
|
|
self.log("Building image %s" % image_name)
|
|
|
|
self.results['actions'].append("Built image %s from %s" % (image_name, self.path))
|
2016-04-23 16:09:44 +02:00
|
|
|
self.results['changed'] = True
|
2016-04-21 00:23:09 +02:00
|
|
|
if not self.check_mode:
|
2016-04-25 21:12:28 +02:00
|
|
|
self.results['image'] = self.build_image()
|
2016-04-21 00:23:09 +02:00
|
|
|
elif self.load_path:
|
|
|
|
# Load the image from an archive
|
|
|
|
if not os.path.isfile(self.load_path):
|
|
|
|
self.fail("Error loading image %s. Specified path %s does not exist." % (self.name,
|
|
|
|
self.load_path))
|
2016-04-25 21:12:28 +02:00
|
|
|
image_name = self.name
|
2016-04-21 00:23:09 +02:00
|
|
|
if self.tag:
|
2016-04-25 21:12:28 +02:00
|
|
|
image_name = "%s:%s" % (self.name, self.tag)
|
|
|
|
self.results['actions'].append("Loaded image %s from %s" % (image_name, self.load_path))
|
2016-04-23 16:09:44 +02:00
|
|
|
self.results['changed'] = True
|
2016-04-21 00:23:09 +02:00
|
|
|
if not self.check_mode:
|
2016-04-25 21:12:28 +02:00
|
|
|
self.results['image'] = self.load_image()
|
2015-06-30 08:25:28 +02:00
|
|
|
else:
|
2016-04-21 00:23:09 +02:00
|
|
|
# pull the image
|
2016-04-25 21:12:28 +02:00
|
|
|
self.results['actions'].append('Pulled image %s:%s' % (self.name, self.tag))
|
2016-04-21 00:23:09 +02:00
|
|
|
self.results['changed'] = True
|
2016-04-25 21:12:28 +02:00
|
|
|
if not self.check_mode:
|
|
|
|
self.results['image'] = self.client.pull_image(self.name, tag=self.tag)
|
2016-04-21 00:23:09 +02:00
|
|
|
|
|
|
|
if self.archive_path:
|
|
|
|
self.archive_image(self.name, self.tag)
|
|
|
|
|
|
|
|
if self.push and not self.repository:
|
|
|
|
self.push_image(self.name, self.tag)
|
|
|
|
elif self.repository:
|
2016-07-12 22:24:56 +02:00
|
|
|
self.tag_image(self.name, self.tag, self.repository, force=self.force, push=self.push)
|
2016-04-21 00:23:09 +02:00
|
|
|
|
|
|
|
def absent(self):
|
|
|
|
'''
|
|
|
|
Handles state = 'absent', which removes an image.
|
|
|
|
|
|
|
|
:return None
|
|
|
|
'''
|
|
|
|
image = self.client.find_image(self.name, self.tag)
|
|
|
|
if image:
|
|
|
|
name = self.name
|
|
|
|
if self.tag:
|
|
|
|
name = "%s:%s" % (self.name, self.tag)
|
|
|
|
if not self.check_mode:
|
|
|
|
try:
|
|
|
|
self.client.remove_image(name, force=self.force)
|
2016-04-23 16:12:11 +02:00
|
|
|
except Exception as exc:
|
2016-04-21 00:23:09 +02:00
|
|
|
self.fail("Error removing image %s - %s" % (name, str(exc)))
|
|
|
|
|
|
|
|
self.results['changed'] = True
|
|
|
|
self.results['actions'].append("Removed image %s" % (name))
|
|
|
|
self.results['image']['state'] = 'Deleted'
|
|
|
|
|
|
|
|
def archive_image(self, name, tag):
|
|
|
|
'''
|
|
|
|
Archive an image to a .tar file. Called when archive_path is passed.
|
|
|
|
|
|
|
|
:param name - name of the image. Type: str
|
|
|
|
:return None
|
|
|
|
'''
|
2016-04-25 21:12:28 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
if not tag:
|
|
|
|
tag = "latest"
|
2016-04-25 21:12:28 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
image = self.client.find_image(name=name, tag=tag)
|
2016-04-25 21:12:28 +02:00
|
|
|
if not image:
|
|
|
|
self.log("archive image: image %s:%s not found" % (name, tag))
|
|
|
|
return
|
|
|
|
|
|
|
|
image_name = "%s:%s" % (name, tag)
|
|
|
|
self.results['actions'].append('Archived image %s to %s' % (image_name, self.archive_path))
|
|
|
|
self.results['changed'] = True
|
|
|
|
if not self.check_mode:
|
|
|
|
self.log("Getting archive of image %s" % image_name)
|
|
|
|
try:
|
|
|
|
image = self.client.get_image(image_name)
|
|
|
|
except Exception as exc:
|
|
|
|
self.fail("Error getting image %s - %s" % (image_name, str(exc)))
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2016-04-25 21:12:28 +02:00
|
|
|
try:
|
|
|
|
image_tar = open(self.archive_path, 'w')
|
|
|
|
image_tar.write(image.data)
|
|
|
|
image_tar.close()
|
|
|
|
except Exception as exc:
|
|
|
|
self.fail("Error writing image archive %s - %s" % (self.archive_path, str(exc)))
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2016-04-25 21:12:28 +02:00
|
|
|
image = self.client.find_image(name=name, tag=tag)
|
|
|
|
if image:
|
|
|
|
self.results['image'] = image
|
2016-04-21 00:23:09 +02:00
|
|
|
|
|
|
|
def push_image(self, name, tag=None):
|
|
|
|
'''
|
2016-04-23 16:09:44 +02:00
|
|
|
If the name of the image contains a repository path, then push the image.
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2016-04-23 16:09:44 +02:00
|
|
|
:param name Name of the image to push.
|
|
|
|
:param tag Use a specific tag.
|
2016-04-21 00:23:09 +02:00
|
|
|
:return: None
|
|
|
|
'''
|
2016-04-23 16:09:44 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
repository = name
|
|
|
|
if not tag:
|
2016-07-12 22:24:56 +02:00
|
|
|
repository, tag = parse_repository_tag(name)
|
|
|
|
registry, repo_name = resolve_repository_name(repository)
|
|
|
|
|
|
|
|
self.log("push %s to %s/%s:%s" % (self.name, registry, repo_name, tag))
|
|
|
|
|
|
|
|
if registry:
|
|
|
|
self.results['actions'].append("Pushed image %s to %s/%s:%s" % (self.name, registry, repo_name, tag))
|
2016-04-25 21:12:28 +02:00
|
|
|
self.results['changed'] = True
|
|
|
|
if not self.check_mode:
|
2016-04-23 16:09:44 +02:00
|
|
|
status = None
|
2016-04-25 21:12:28 +02:00
|
|
|
try:
|
2016-04-23 16:09:44 +02:00
|
|
|
for line in self.client.push(repository, tag=tag, stream=True):
|
2016-04-25 21:12:28 +02:00
|
|
|
line = json.loads(line)
|
|
|
|
self.log(line, pretty_print=True)
|
|
|
|
if line.get('errorDetail'):
|
|
|
|
raise Exception(line['errorDetail']['message'])
|
|
|
|
status = line.get('status')
|
|
|
|
except Exception as exc:
|
|
|
|
if re.search('unauthorized', str(exc)):
|
2016-07-12 22:24:56 +02:00
|
|
|
if re.search('authentication required', str(exc)):
|
|
|
|
self.fail("Error pushing image %s/%s:%s - %s. Try logging into %s first." %
|
|
|
|
(registry, repo_name, tag, str(exc), registry))
|
|
|
|
else:
|
|
|
|
self.fail("Error pushing image %s/%s:%s - %s. Does the repository exist?" %
|
|
|
|
(registry, repo_name, tag, str(exc)))
|
2016-04-25 21:12:28 +02:00
|
|
|
self.fail("Error pushing image %s: %s" % (repository, str(exc)))
|
|
|
|
self.results['image'] = self.client.find_image(name=repository, tag=tag)
|
|
|
|
if not self.results['image']:
|
|
|
|
self.results['image'] = dict()
|
2016-04-23 16:09:44 +02:00
|
|
|
self.results['image']['push_status'] = status
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2016-07-12 22:24:56 +02:00
|
|
|
def tag_image(self, name, tag, repository, force=False, push=False):
|
2016-04-21 00:23:09 +02:00
|
|
|
'''
|
|
|
|
Tag an image into a repository.
|
|
|
|
|
|
|
|
:param name: name of the image. required.
|
|
|
|
:param tag: image tag.
|
|
|
|
:param repository: path to the repository. required.
|
|
|
|
:param force: bool. force tagging, even it image already exists with the repository path.
|
|
|
|
:param push: bool. push the image once it's tagged.
|
|
|
|
:return: None
|
|
|
|
'''
|
2016-07-12 22:24:56 +02:00
|
|
|
repo, repo_tag = parse_repository_tag(repository)
|
2016-08-05 11:01:55 +02:00
|
|
|
if not repo_tag:
|
|
|
|
repo_tag = "latest"
|
2016-08-05 22:08:11 +02:00
|
|
|
if tag:
|
|
|
|
repo_tag = tag
|
2016-04-21 00:23:09 +02:00
|
|
|
image = self.client.find_image(name=repo, tag=repo_tag)
|
|
|
|
found = 'found' if image else 'not found'
|
|
|
|
self.log("image %s was %s" % (repo, found))
|
2016-08-05 11:01:55 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
if not image or force:
|
2016-08-05 11:01:55 +02:00
|
|
|
self.log("tagging %s:%s to %s:%s" % (name, tag, repo, repo_tag))
|
2016-04-25 21:12:28 +02:00
|
|
|
self.results['changed'] = True
|
2016-08-05 11:01:55 +02:00
|
|
|
self.results['actions'].append("Tagged image %s:%s to %s:%s" % (name, tag, repo, repo_tag))
|
2016-04-25 21:12:28 +02:00
|
|
|
if not self.check_mode:
|
|
|
|
try:
|
2016-04-21 00:23:09 +02:00
|
|
|
# Finding the image does not always work, especially running a localhost registry. In those
|
|
|
|
# cases, if we don't set force=True, it errors.
|
2016-04-23 16:09:44 +02:00
|
|
|
image_name = name
|
|
|
|
if tag and not re.search(tag, name):
|
|
|
|
image_name = "%s:%s" % (name, tag)
|
2016-08-05 11:01:55 +02:00
|
|
|
tag_status = self.client.tag(image_name, repo, tag=repo_tag, force=True)
|
2016-04-21 00:23:09 +02:00
|
|
|
if not tag_status:
|
|
|
|
raise Exception("Tag operation failed.")
|
2016-04-25 21:12:28 +02:00
|
|
|
except Exception as exc:
|
2016-08-05 11:01:55 +02:00
|
|
|
self.fail("Error: failed to tag image - %s" % str(exc))
|
|
|
|
self.results['image'] = self.client.find_image(name=repo, tag=repo_tag)
|
2016-07-12 22:24:56 +02:00
|
|
|
if push:
|
2016-08-05 11:01:55 +02:00
|
|
|
self.push_image(repo, repo_tag)
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2016-04-25 21:12:28 +02:00
|
|
|
def build_image(self):
|
|
|
|
'''
|
|
|
|
Build an image
|
|
|
|
|
|
|
|
:return: image dict
|
|
|
|
'''
|
|
|
|
params = dict(
|
|
|
|
path=self.path,
|
|
|
|
tag=self.name,
|
|
|
|
rm=self.rm,
|
|
|
|
nocache=self.nocache,
|
|
|
|
stream=True,
|
|
|
|
timeout=self.http_timeout,
|
|
|
|
pull=self.pull,
|
|
|
|
forcerm=self.rm,
|
|
|
|
dockerfile=self.dockerfile,
|
|
|
|
decode=True
|
|
|
|
)
|
|
|
|
if self.tag:
|
|
|
|
params['tag'] = "%s:%s" % (self.name, self.tag)
|
|
|
|
if self.container_limits:
|
2016-04-29 06:39:08 +02:00
|
|
|
params['container_limits'] = self.container_limits
|
2016-08-06 01:14:31 +02:00
|
|
|
if self.buildargs:
|
|
|
|
for key, value in self.buildargs.iteritems():
|
|
|
|
if not isinstance(value, basestring):
|
|
|
|
self.buildargs[key] = str(value)
|
|
|
|
params['buildargs'] = self.buildargs
|
|
|
|
|
2016-04-25 21:12:28 +02:00
|
|
|
for line in self.client.build(**params):
|
|
|
|
# line = json.loads(line)
|
|
|
|
self.log(line, pretty_print=True)
|
|
|
|
if line.get('error'):
|
|
|
|
if line.get('errorDetail'):
|
|
|
|
errorDetail = line.get('errorDetail')
|
|
|
|
self.fail("Error building %s - code: %s message: %s" % (self.name,
|
|
|
|
errorDetail.get('code'),
|
|
|
|
errorDetail.get('message')))
|
|
|
|
else:
|
|
|
|
self.fail("Error building %s - %s" % (self.name, line.get('error')))
|
|
|
|
return self.client.find_image(name=self.name, tag=self.tag)
|
|
|
|
|
|
|
|
def load_image(self):
|
|
|
|
'''
|
|
|
|
Load an image from a .tar archive
|
|
|
|
|
|
|
|
:return: image dict
|
|
|
|
'''
|
|
|
|
try:
|
Stream image data in load_image() to avoid out-of-memory
Reading the entire tar file into memory can result in out-of-memory
conditions such as this traceback:
Traceback (most recent call last):
File "/tmp/ansible_YELTSu/ansible_module_docker_image.py", line 486, in load_image
self.client.load_image(image_data)
File "/usr/local/lib/python2.7/dist-packages/docker/api/image.py", line 147, in load_image
res = self._post(self._url("/images/load"), data=data)
...
File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 848, in _send_output
msg += message_body
MemoryError
Luckily docker-py's load_image(), which calls requests post(), accepts a
file-like object instead of a string. Pass in the file object to avoid
reading the full file into memory. This allows larger tar files to load
succesfully.
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
2016-06-09 00:50:44 +02:00
|
|
|
self.log("Opening image %s" % self.load_path)
|
2016-04-25 21:12:28 +02:00
|
|
|
image_tar = open(self.load_path, 'r')
|
|
|
|
except Exception as exc:
|
Stream image data in load_image() to avoid out-of-memory
Reading the entire tar file into memory can result in out-of-memory
conditions such as this traceback:
Traceback (most recent call last):
File "/tmp/ansible_YELTSu/ansible_module_docker_image.py", line 486, in load_image
self.client.load_image(image_data)
File "/usr/local/lib/python2.7/dist-packages/docker/api/image.py", line 147, in load_image
res = self._post(self._url("/images/load"), data=data)
...
File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 848, in _send_output
msg += message_body
MemoryError
Luckily docker-py's load_image(), which calls requests post(), accepts a
file-like object instead of a string. Pass in the file object to avoid
reading the full file into memory. This allows larger tar files to load
succesfully.
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
2016-06-09 00:50:44 +02:00
|
|
|
self.fail("Error opening image %s - %s" % (self.load_path, str(exc)))
|
2016-04-25 21:12:28 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.log("Loading image from %s" % self.load_path)
|
Stream image data in load_image() to avoid out-of-memory
Reading the entire tar file into memory can result in out-of-memory
conditions such as this traceback:
Traceback (most recent call last):
File "/tmp/ansible_YELTSu/ansible_module_docker_image.py", line 486, in load_image
self.client.load_image(image_data)
File "/usr/local/lib/python2.7/dist-packages/docker/api/image.py", line 147, in load_image
res = self._post(self._url("/images/load"), data=data)
...
File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 848, in _send_output
msg += message_body
MemoryError
Luckily docker-py's load_image(), which calls requests post(), accepts a
file-like object instead of a string. Pass in the file object to avoid
reading the full file into memory. This allows larger tar files to load
succesfully.
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
2016-06-09 00:50:44 +02:00
|
|
|
self.client.load_image(image_tar)
|
2016-04-25 21:12:28 +02:00
|
|
|
except Exception as exc:
|
|
|
|
self.fail("Error loading image %s - %s" % (self.name, str(exc)))
|
|
|
|
|
Stream image data in load_image() to avoid out-of-memory
Reading the entire tar file into memory can result in out-of-memory
conditions such as this traceback:
Traceback (most recent call last):
File "/tmp/ansible_YELTSu/ansible_module_docker_image.py", line 486, in load_image
self.client.load_image(image_data)
File "/usr/local/lib/python2.7/dist-packages/docker/api/image.py", line 147, in load_image
res = self._post(self._url("/images/load"), data=data)
...
File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 848, in _send_output
msg += message_body
MemoryError
Luckily docker-py's load_image(), which calls requests post(), accepts a
file-like object instead of a string. Pass in the file object to avoid
reading the full file into memory. This allows larger tar files to load
succesfully.
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
2016-06-09 00:50:44 +02:00
|
|
|
try:
|
|
|
|
image_tar.close()
|
|
|
|
except Exception as exc:
|
|
|
|
self.fail("Error closing image %s - %s" % (self.name, str(exc)))
|
|
|
|
|
2016-04-25 21:12:28 +02:00
|
|
|
return self.client.find_image(self.name, self.tag)
|
2015-06-30 08:25:28 +02:00
|
|
|
|
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
def main():
|
|
|
|
argument_spec = dict(
|
2016-04-29 06:39:08 +02:00
|
|
|
archive_path=dict(type='path'),
|
2016-04-21 00:23:09 +02:00
|
|
|
container_limits=dict(type='dict'),
|
|
|
|
dockerfile=dict(type='str'),
|
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
http_timeout=dict(type='int'),
|
2016-04-29 06:39:08 +02:00
|
|
|
load_path=dict(type='path'),
|
2016-04-21 00:23:09 +02:00
|
|
|
name=dict(type='str', required=True),
|
|
|
|
nocache=dict(type='str', default=False),
|
2016-04-29 06:39:08 +02:00
|
|
|
path=dict(type='path', aliases=['build_path']),
|
2016-04-21 00:23:09 +02:00
|
|
|
pull=dict(type='bool', default=True),
|
2016-07-12 22:24:56 +02:00
|
|
|
push=dict(type='bool', default=False),
|
2016-04-21 00:23:09 +02:00
|
|
|
repository=dict(type='str'),
|
|
|
|
rm=dict(type='bool', default=True),
|
2016-08-05 16:47:29 +02:00
|
|
|
state=dict(type='str', choices=['absent', 'present', 'build'], default='present'),
|
2016-04-21 00:23:09 +02:00
|
|
|
tag=dict(type='str', default='latest'),
|
2016-08-06 01:14:31 +02:00
|
|
|
use_tls=dict(type='str', default='no', choices=['no', 'encrypt', 'verify']),
|
|
|
|
buildargs=dict(type='dict', default=None),
|
2016-04-21 00:23:09 +02:00
|
|
|
)
|
2015-06-30 08:25:28 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
client = AnsibleDockerClient(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2014-03-30 21:27:29 +02:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
results = dict(
|
|
|
|
changed=False,
|
|
|
|
actions=[],
|
|
|
|
image={}
|
|
|
|
)
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2016-04-21 00:23:09 +02:00
|
|
|
ImageManager(client, results)
|
|
|
|
client.module.exit_json(**results)
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2015-05-11 21:15:53 +02:00
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2016-04-21 00:23:09 +02:00
|
|
|
|
2015-05-11 21:15:53 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|