2014-01-22 12:04:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
|
|
|
|
# (c) 2014, Pavel Antonov <antonov@adwz.ru>
|
|
|
|
#
|
2014-02-11 16:10:47 +00:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2014-01-22 12:04:19 +00:00
|
|
|
# This module is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# This software is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2014-02-11 16:10:47 +00:00
|
|
|
module: docker_image
|
|
|
|
author: Pavel Antonov
|
|
|
|
version_added: "1.5"
|
|
|
|
short_description: manage docker images
|
2014-01-22 12:04:19 +00:00
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Create, check and remove docker images
|
2014-01-22 12:04:19 +00:00
|
|
|
options:
|
2014-02-11 16:10:47 +00:00
|
|
|
path:
|
2014-01-22 12:04:19 +00:00
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Path to directory with Dockerfile
|
2014-01-22 12:04:19 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2014-02-11 16:10:47 +00:00
|
|
|
name:
|
2014-01-22 12:04:19 +00:00
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Image name to work with
|
|
|
|
required: true
|
2014-01-22 12:04:19 +00:00
|
|
|
default: null
|
|
|
|
aliases: []
|
2014-02-11 16:10:47 +00:00
|
|
|
tag:
|
2014-01-22 12:04:19 +00:00
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Image tag to work with
|
2014-01-22 12:04:19 +00:00
|
|
|
required: false
|
2014-02-11 16:10:47 +00:00
|
|
|
default: ""
|
2014-01-22 12:04:19 +00:00
|
|
|
aliases: []
|
2014-02-11 16:10:47 +00:00
|
|
|
nocache:
|
2014-01-22 12:04:19 +00:00
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Do not use cache with building
|
2014-01-22 12:04:19 +00:00
|
|
|
required: false
|
2014-02-11 16:10:47 +00:00
|
|
|
default: false
|
2014-01-22 12:04:19 +00:00
|
|
|
aliases: []
|
|
|
|
docker_url:
|
|
|
|
description:
|
|
|
|
- URL of docker host to issue commands to
|
|
|
|
required: false
|
|
|
|
default: unix://var/run/docker.sock
|
|
|
|
aliases: []
|
|
|
|
state:
|
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Set the state of the image
|
2014-01-22 12:04:19 +00:00
|
|
|
required: false
|
|
|
|
default: present
|
2014-02-11 16:10:47 +00:00
|
|
|
choices: [ "present", "absent", "build" ]
|
2014-01-22 12:04:19 +00:00
|
|
|
aliases: []
|
2014-02-11 16:10:47 +00:00
|
|
|
timeout:
|
2014-01-22 12:04:19 +00:00
|
|
|
description:
|
2014-02-11 16:10:47 +00:00
|
|
|
- Set image operation timeout
|
2014-01-22 12:04:19 +00:00
|
|
|
required: false
|
2014-02-11 16:10:47 +00:00
|
|
|
default: 600
|
2014-01-22 12:04:19 +00:00
|
|
|
aliases: []
|
|
|
|
requirements: [ "docker-py" ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2014-02-11 16:10:47 +00:00
|
|
|
Build docker image if required. Path should contains Dockerfile to build image:
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
- hosts: web
|
|
|
|
sudo: yes
|
|
|
|
tasks:
|
2014-02-11 16:10:47 +00:00
|
|
|
- name: check or build image
|
|
|
|
docker_image: path="/path/to/build/dir" name="my/app" state=present
|
2014-01-22 12:04:19 +00:00
|
|
|
|
2014-02-11 16:10:47 +00:00
|
|
|
Build new version of image:
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
- hosts: web
|
|
|
|
sudo: yes
|
|
|
|
tasks:
|
2014-02-11 16:10:47 +00:00
|
|
|
- name: check or build image
|
|
|
|
docker_image: path="/path/to/build/dir" name="my/app" state=build
|
2014-01-22 12:04:19 +00:00
|
|
|
|
2014-02-11 16:10:47 +00:00
|
|
|
Remove image from local docker storage:
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
- hosts: web
|
|
|
|
sudo: yes
|
|
|
|
tasks:
|
|
|
|
- name: run tomcat servers
|
2014-02-11 16:10:47 +00:00
|
|
|
docker_image: name="my/app" state=absent
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import sys
|
2014-02-26 20:27:39 +00:00
|
|
|
import re
|
|
|
|
import json
|
2014-01-22 12:04:19 +00:00
|
|
|
import docker.client
|
|
|
|
from requests.exceptions import *
|
|
|
|
from urlparse import urlparse
|
|
|
|
except ImportError, e:
|
|
|
|
print "failed=True msg='failed to import python module: %s'" % e
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
class DockerImageManager:
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.path = self.module.params.get('path')
|
|
|
|
self.name = self.module.params.get('name')
|
|
|
|
self.tag = self.module.params.get('tag')
|
|
|
|
self.nocache = self.module.params.get('nocache')
|
|
|
|
docker_url = urlparse(module.params.get('docker_url'))
|
|
|
|
self.client = docker.Client(base_url=docker_url.geturl(), timeout=module.params.get('timeout'))
|
|
|
|
self.changed = False
|
2014-02-26 20:27:39 +00:00
|
|
|
self.log = []
|
|
|
|
self.error_msg = None
|
|
|
|
|
|
|
|
def get_log(self, as_string=True):
|
|
|
|
return "".join(self.log) if as_string else self.log
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
def build(self):
|
2014-02-26 20:27:39 +00:00
|
|
|
stream = self.client.build(self.path, tag=':'.join([self.name, self.tag]), nocache=self.nocache, rm=True, stream=True)
|
|
|
|
success_search = r'Successfully built ([0-9a-f]+)'
|
|
|
|
image_id = None
|
2014-01-22 12:04:19 +00:00
|
|
|
self.changed = True
|
|
|
|
|
2014-02-26 20:27:39 +00:00
|
|
|
for chunk in stream:
|
2014-03-30 19:27:29 +00:00
|
|
|
if not chunk:
|
|
|
|
continue
|
|
|
|
|
2014-02-26 20:27:39 +00:00
|
|
|
chunk_json = json.loads(chunk)
|
|
|
|
|
|
|
|
if 'error' in chunk_json:
|
|
|
|
self.error_msg = chunk_json['error']
|
|
|
|
return None
|
|
|
|
|
|
|
|
if 'stream' in chunk_json:
|
|
|
|
output = chunk_json['stream']
|
|
|
|
self.log.append(output)
|
|
|
|
match = re.search(success_search, output)
|
|
|
|
if match:
|
|
|
|
image_id = match.group(1)
|
|
|
|
|
|
|
|
return image_id
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
def has_changed(self):
|
|
|
|
return self.changed
|
|
|
|
|
|
|
|
def get_images(self):
|
|
|
|
filtered_images = []
|
|
|
|
images = self.client.images()
|
|
|
|
for i in images:
|
2014-02-26 20:27:39 +00:00
|
|
|
# Docker-py version >= 0.3 (Docker API >= 1.8)
|
|
|
|
if 'RepoTags' in i:
|
2014-03-04 15:07:32 +00:00
|
|
|
repotag = '%s:%s' % (getattr(self, 'name', ''), getattr(self, 'tag', 'latest'))
|
2014-02-26 20:27:39 +00:00
|
|
|
if not self.name or repotag in i['RepoTags']:
|
|
|
|
filtered_images.append(i)
|
|
|
|
# Docker-py version < 0.3 (Docker API < 1.8)
|
|
|
|
elif (not self.name or self.name == i['Repository']) and (not self.tag or self.tag == i['Tag']):
|
2014-01-22 12:04:19 +00:00
|
|
|
filtered_images.append(i)
|
|
|
|
return filtered_images
|
|
|
|
|
|
|
|
def remove_images(self):
|
|
|
|
images = self.get_images()
|
|
|
|
for i in images:
|
|
|
|
try:
|
|
|
|
self.client.remove_image(i['Id'])
|
|
|
|
self.changed = True
|
|
|
|
except docker.APIError as e:
|
|
|
|
# image can be removed by docker if not used
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
path = dict(required=False, default=None),
|
|
|
|
name = dict(required=True),
|
|
|
|
tag = dict(required=False, default=""),
|
|
|
|
nocache = dict(default=False, type='bool'),
|
|
|
|
state = dict(default='present', choices=['absent', 'present', 'build']),
|
|
|
|
docker_url = dict(default='unix://var/run/docker.sock'),
|
|
|
|
timeout = dict(default=600, type='int'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
manager = DockerImageManager(module)
|
|
|
|
state = module.params.get('state')
|
|
|
|
failed = False
|
|
|
|
image_id = None
|
|
|
|
msg = ''
|
2014-02-26 20:27:39 +00:00
|
|
|
do_build = False
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
# build image if not exists
|
|
|
|
if state == "present":
|
|
|
|
images = manager.get_images()
|
|
|
|
if len(images) == 0:
|
2014-02-26 20:27:39 +00:00
|
|
|
do_build = True
|
|
|
|
# build image
|
|
|
|
elif state == "build":
|
|
|
|
do_build = True
|
2014-01-22 12:04:19 +00:00
|
|
|
# remove image or images
|
|
|
|
elif state == "absent":
|
|
|
|
manager.remove_images()
|
|
|
|
|
2014-02-26 20:27:39 +00:00
|
|
|
if do_build:
|
|
|
|
image_id = manager.build()
|
|
|
|
if image_id:
|
|
|
|
msg = "Image builded: %s" % image_id
|
|
|
|
else:
|
2014-01-22 12:04:19 +00:00
|
|
|
failed = True
|
2014-02-26 20:27:39 +00:00
|
|
|
msg = "Error: %s\nLog:%s" % (manager.error_msg, manager.get_log())
|
2014-01-22 12:04:19 +00:00
|
|
|
|
|
|
|
module.exit_json(failed=failed, changed=manager.has_changed(), msg=msg, image_id=image_id)
|
|
|
|
|
|
|
|
except docker.client.APIError as e:
|
|
|
|
module.exit_json(failed=True, changed=manager.has_changed(), msg="Docker API error: " + e.explanation)
|
|
|
|
|
|
|
|
except RequestException as e:
|
|
|
|
module.exit_json(failed=True, changed=manager.has_changed(), msg=repr(e))
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
main()
|