2014-01-15 14:52:17 +00:00
#!/usr/bin/python
2013-11-03 20:48:03 +00:00
# (c) 2013, Cove Schneider
2014-02-25 23:24:46 +00:00
# (c) 2014, Joshua Conner <joshua.conner@gmail.com>
2014-01-22 12:04:19 +00:00
# (c) 2014, Pavel Antonov <antonov@adwz.ru>
2013-10-13 22:07:27 +00:00
#
2013-11-03 20:48:03 +00:00
# This file is part of Ansible,
2013-10-13 22:07:27 +00:00
#
2013-11-03 20:48:03 +00:00
# Ansible 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.
2013-10-13 22:07:27 +00:00
#
2013-11-03 20:48:03 +00:00
# Ansible 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.
2013-10-13 22:07:27 +00:00
#
2013-11-03 20:48:03 +00:00
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
######################################################################
2013-10-13 22:07:27 +00:00
DOCUMENTATION = '''
---
module: docker
2013-11-18 23:55:49 +00:00
version_added: "1.4"
2013-10-13 22:07:27 +00:00
short_description: manage docker containers
description:
2013-10-14 04:37:42 +00:00
- Manage the life cycle of docker containers.
2013-10-13 22:07:27 +00:00
options:
count:
description:
- Set number of containers to run
required: False
default: 1
aliases: []
image:
description:
- Set container image to use
required: true
default: null
aliases: []
command:
description:
- Set command to run in a container on startup
required: false
default: null
aliases: []
2014-01-22 12:04:19 +00:00
name:
description:
- Set name for container (used to find single container or to provide links)
required: false
default: null
aliases: []
2014-02-26 01:04:01 +00:00
version_added: "1.5"
2013-10-13 22:07:27 +00:00
ports:
description:
2014-02-25 23:24:46 +00:00
- Set private to public port mapping specification using docker CLI-style syntax [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp]
2013-10-13 22:07:27 +00:00
required: false
default: null
aliases: []
2014-02-26 01:04:01 +00:00
version_added: "1.5"
2014-02-25 23:24:46 +00:00
expose:
2014-01-22 12:04:19 +00:00
description:
2014-02-25 23:24:46 +00:00
- Set container ports to expose for port mappings or links. (If the port is already exposed using EXPOSE in a Dockerfile, you don't need to expose it again.)
2014-01-22 12:04:19 +00:00
required: false
default: null
aliases: []
2014-02-26 01:04:01 +00:00
version_added: "1.5"
2014-02-25 23:24:46 +00:00
publish_all_ports:
description:
- Publish all exposed ports to the host interfaces
2014-02-26 01:04:01 +00:00
required: false
default: false
aliases: []
version_added: "1.5"
2013-10-13 22:07:27 +00:00
volumes:
description:
- Set volume(s) to mount on the container
required: false
default: null
aliases: []
volumes_from:
description:
- Set shared volume(s) from another container
required: false
default: null
aliases: []
2014-02-25 23:24:46 +00:00
links:
description:
- Link container(s) to other container(s) (e.g. links=redis,postgresql:db)
required: false
default: null
aliases: []
2014-02-26 01:04:01 +00:00
version_added: "1.5"
2013-10-13 22:07:27 +00:00
memory_limit:
description:
- Set RAM allocated to container
required: false
default: null
aliases: []
default: 256MB
docker_url:
description:
- URL of docker host to issue commands to
required: false
default: unix://var/run/docker.sock
aliases: []
username:
description:
- Set remote API username
required: false
default: null
aliases: []
password:
description:
- Set remote API password
required: false
default: null
aliases: []
hostname:
description:
- Set container hostname
required: false
default: null
aliases: []
env:
description:
2014-02-26 01:04:01 +00:00
- Set environment variables (e.g. env="PASSWORD=sEcRe7,WORKERS=4")
2013-10-13 22:07:27 +00:00
required: false
default: null
aliases: []
dns:
description:
- Set custom DNS servers for the container
required: false
default: null
aliases: []
detach:
description:
- Enable detached mode on start up, leaves container running in background
required: false
default: true
aliases: []
state:
description:
- Set the state of the container
required: false
default: present
2014-03-14 10:11:56 +00:00
choices: [ "present", "running", "stopped", "absent", "killed", "restarted" ]
2013-10-13 22:07:27 +00:00
aliases: []
privileged:
description:
- Set whether the container should run in privileged mode
required: false
default: false
aliases: []
lxc_conf:
description:
- LXC config parameters, e.g. lxc.aa_profile:unconfined
required: false
default:
aliases: []
2014-02-25 23:24:46 +00:00
name:
description:
- Set the name of the container (cannot use with count)
required: false
default: null
aliases: []
2014-02-26 01:04:01 +00:00
version_added: "1.5"
2014-03-18 11:57:01 +00:00
stdin_open:
description:
- Keep stdin open
required: false
default: false
aliases: []
version_added: "1.6"
tty:
description:
- Allocate a pseudo-tty
required: false
default: false
aliases: []
version_added: "1.6"
2014-02-25 23:24:46 +00:00
author: Cove Schneider, Joshua Conner, Pavel Antonov
2014-04-22 10:21:51 +00:00
requirements: [ "docker-py >= 0.3.0", "docker >= 0.10.0" ]
2013-10-13 22:07:27 +00:00
'''
2013-10-14 03:34:14 +00:00
EXAMPLES = '''
Start one docker container running tomcat in each host of the web group and bind tomcat's listening port to 8080
on the host:
- hosts: web
sudo: yes
tasks:
- name: run tomcat servers
2014-02-25 23:24:46 +00:00
docker: image=centos command="service tomcat6 start" ports=8080
2013-10-14 03:34:14 +00:00
The tomcat server's port is NAT'ed to a dynamic port on the host, but you can determine which port the server was
2013-10-14 20:49:53 +00:00
mapped to using docker_containers:
2013-10-14 03:34:14 +00:00
- hosts: web
sudo: yes
tasks:
- name: run tomcat servers
docker: image=centos command="service tomcat6 start" ports=8080 count=5
- name: Display IP address and port mappings for containers
2014-02-25 23:24:46 +00:00
debug: msg={{inventory_hostname}}:{{item['HostConfig']['PortBindings']['8080/tcp'][0]['HostPort']}}
2013-10-14 20:49:53 +00:00
with_items: docker_containers
2013-10-14 03:34:14 +00:00
Just as in the previous example, but iterates over the list of docker containers with a sequence:
- hosts: web
sudo: yes
vars:
start_containers_count: 5
tasks:
- name: run tomcat servers
docker: image=centos command="service tomcat6 start" ports=8080 count={{start_containers_count}}
- name: Display IP address and port mappings for containers
2014-02-25 23:24:46 +00:00
debug: msg="{{inventory_hostname}}:{{docker_containers[{{item}}]['HostConfig']['PortBindings']['8080/tcp'][0]['HostPort']}}"
2013-10-14 03:34:14 +00:00
with_sequence: start=0 end={{start_containers_count - 1}}
2013-10-14 20:49:53 +00:00
Stop, remove all of the running tomcat containers and list the exit code from the stopped containers:
2013-10-14 03:34:14 +00:00
- hosts: web
sudo: yes
tasks:
- name: stop tomcat servers
docker: image=centos command="service tomcat6 start" state=absent
2013-10-14 20:49:53 +00:00
- name: Display return codes from stopped containers
debug: msg="Returned {{inventory_hostname}}:{{item}}"
with_items: docker_containers
2014-02-25 23:24:46 +00:00
Create a named container:
- hosts: web
sudo: yes
tasks:
- name: run tomcat server
docker: image=centos name=tomcat command="service tomcat6 start" ports=8080
Create multiple named containers:
- hosts: web
sudo: yes
tasks:
- name: run tomcat servers
docker: image=centos name={{item}} command="service tomcat6 start" ports=8080
with_items:
- crookshank
- snowbell
- heathcliff
- felix
- sylvester
Create containers named in a sequence:
- hosts: web
sudo: yes
tasks:
- name: run tomcat servers
docker: image=centos name={{item}} command="service tomcat6 start" ports=8080
with_sequence: start=1 end=5 format=tomcat_%d.example.com
Create two linked containers:
- hosts: web
sudo: yes
tasks:
- name: ensure redis container is running
docker: image=crosbymichael/redis name=redis
- name: ensure redis_ambassador container is running
docker: image=svendowideit/ambassador ports=6379:6379 links=redis:redis name=redis_ambassador_ansible
Create containers with options specified as key-value pairs and lists:
- hosts: web
sudo: yes
tasks:
- docker:
image: namespace/image_name
links:
- postgresql:db
- redis:redis
Create containers with options specified as strings and lists as comma-separated strings:
- hosts: web
sudo: yes
tasks:
docker: image=namespace/image_name links=postgresql:db,redis:redis
2013-10-14 03:34:14 +00:00
'''
2014-02-25 23:24:46 +00:00
HAS_DOCKER_PY = True
import sys
from urlparse import urlparse
2013-10-13 22:07:27 +00:00
try:
import docker.client
2014-04-11 16:08:55 +00:00
import docker.utils
2013-10-13 22:07:27 +00:00
from requests.exceptions import *
except ImportError, e:
2014-02-25 23:24:46 +00:00
HAS_DOCKER_PY = False
2014-04-16 22:42:56 +00:00
try:
from docker.errors import APIError as DockerAPIError
except ImportError:
from docker.client import APIError as DockerAPIError
2013-10-13 22:07:27 +00:00
def _human_to_bytes(number):
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
if isinstance(number, int):
return number
if number[-1] == suffixes[0] and number[-2].isdigit():
return number[:-1]
i = 1
for each in suffixes[1:]:
if number[-len(each):] == suffixes[i]:
return int(number[:-len(each)]) * (1024 ** i)
i = i + 1
print "failed=True msg='Could not convert %s to integer'" % (number)
sys.exit(1)
def _ansible_facts(container_list):
2013-10-14 20:49:53 +00:00
return {"docker_containers": container_list}
2013-10-13 22:07:27 +00:00
2013-10-24 05:56:02 +00:00
def _docker_id_quirk(inspect):
# XXX: some quirk in docker
if 'ID' in inspect:
inspect['Id'] = inspect['ID']
del inspect['ID']
return inspect
2013-10-14 20:49:53 +00:00
class DockerManager:
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
counters = {'created':0, 'started':0, 'stopped':0, 'killed':0, 'removed':0, 'restarted':0, 'pull':0}
def __init__(self, module):
self.module = module
2014-02-25 23:24:46 +00:00
2013-10-24 05:56:02 +00:00
self.binds = None
self.volumes = None
if self.module.params.get('volumes'):
self.binds = {}
self.volumes = {}
2014-03-18 20:13:12 +00:00
vols = self.module.params.get('volumes')
2013-10-24 05:56:02 +00:00
for vol in vols:
parts = vol.split(":")
# host mount (e.g. /mnt:/tmp, bind mounts host's /tmp to /mnt in the container)
if len(parts) == 2:
self.volumes[parts[1]] = {}
self.binds[parts[0]] = parts[1]
# docker mount (e.g. /www, mounts a docker volume /www on the container at the same location)
else:
self.volumes[parts[0]] = {}
self.lxc_conf = None
if self.module.params.get('lxc_conf'):
self.lxc_conf = []
2014-03-18 20:13:12 +00:00
options = self.module.params.get('lxc_conf')
2013-10-24 05:56:02 +00:00
for option in options:
parts = option.split(':')
self.lxc_conf.append({"Key": parts[0], "Value": parts[1]})
2014-02-25 23:24:46 +00:00
self.exposed_ports = None
if self.module.params.get('expose'):
2014-03-18 20:13:12 +00:00
self.exposed_ports = self.get_exposed_ports(self.module.params.get('expose'))
2014-01-22 12:04:19 +00:00
self.port_bindings = None
2014-02-25 23:24:46 +00:00
if self.module.params.get('ports'):
2014-03-18 20:13:12 +00:00
self.port_bindings = self.get_port_bindings(self.module.params.get('ports'))
2014-01-22 12:04:19 +00:00
self.links = None
if self.module.params.get('links'):
2014-06-10 12:30:27 +00:00
self.links = self.get_links(self.module.params.get('links'))
2013-10-24 05:56:02 +00:00
2013-11-03 20:48:03 +00:00
self.env = None
if self.module.params.get('env'):
2014-05-14 14:11:43 +00:00
self.env = dict(map(lambda x: x.split("=", 1), self.module.params.get('env')))
2013-11-03 20:48:03 +00:00
2013-10-13 22:07:27 +00:00
# connect to docker server
docker_url = urlparse(module.params.get('docker_url'))
self.client = docker.Client(base_url=docker_url.geturl())
2014-02-25 23:24:46 +00:00
2014-06-10 12:30:27 +00:00
def get_links(self, links):
"""
Parse the links passed, if a link is specified without an alias then just create the alias of the same name as the link
"""
processed_links = {}
for link in links:
parsed_link = link.split(':', 1)
if(len(parsed_link) == 2):
processed_links[parsed_link[0]] = parsed_link[1]
else:
processed_links[parsed_link[0]] = parsed_link[0]
return processed_links
2014-02-25 23:24:46 +00:00
def get_exposed_ports(self, expose_list):
"""
Parse the ports and protocols (TCP/UDP) to expose in the docker-py `create_container` call from the docker CLI-style syntax.
"""
if expose_list:
exposed = []
for port in expose_list:
if port.endswith('/tcp') or port.endswith('/udp'):
port_with_proto = tuple(port.split('/'))
else:
# assume tcp protocol if not specified
port_with_proto = (port, 'tcp')
exposed.append(port_with_proto)
return exposed
else:
return None
def get_port_bindings(self, ports):
"""
Parse the `ports` string into a port bindings dict for the `start_container` call.
"""
binds = {}
for port in ports:
2014-03-18 20:13:12 +00:00
# ports could potentially be an array like [80, 443], so we make sure they're strings
# before splitting
parts = str(port).split(':')
2014-02-25 23:24:46 +00:00
container_port = parts[-1]
if '/' not in container_port:
container_port = int(parts[-1])
p_len = len(parts)
if p_len == 1:
# Bind `container_port` of the container to a dynamically
# allocated TCP port on all available interfaces of the host
# machine.
bind = ('0.0.0.0',)
elif p_len == 2:
# Bind `container_port` of the container to port `parts[0]` on
# all available interfaces of the host machine.
bind = ('0.0.0.0', int(parts[0]))
elif p_len == 3:
# Bind `container_port` of the container to port `parts[1]` on
# IP `parts[0]` of the host machine. If `parts[1]` empty bind
# to a dynamically allocacted port of IP `parts[0]`.
bind = (parts[0], int(parts[1])) if parts[1] else (parts[0],)
if container_port in binds:
old_bind = binds[container_port]
if isinstance(old_bind, list):
# append to list if it already exists
old_bind.append(bind)
else:
# otherwise create list that contains the old and new binds
binds[container_port] = [binds[container_port], bind]
else:
binds[container_port] = bind
return binds
2013-10-24 05:56:02 +00:00
def get_split_image_tag(self, image):
2013-11-16 21:15:03 +00:00
if '/' in image:
image = image.split('/')[1]
2013-10-24 05:56:02 +00:00
tag = None
if image.find(':') > 0:
return image.split(':')
else:
2014-02-25 23:24:46 +00:00
return image, tag
2013-10-13 22:07:27 +00:00
def get_summary_counters_msg(self):
msg = ""
for k, v in self.counters.iteritems():
msg = msg + "%s %d " % (k, v)
return msg
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
def increment_counter(self, name):
self.counters[name] = self.counters[name] + 1
def has_changed(self):
for k, v in self.counters.iteritems():
if v > 0:
return True
return False
2014-02-25 23:24:46 +00:00
2013-10-24 05:56:02 +00:00
def get_inspect_containers(self, containers):
inspect = []
for i in containers:
details = self.client.inspect_container(i['Id'])
details = _docker_id_quirk(details)
inspect.append(details)
return inspect
2013-10-13 22:07:27 +00:00
def get_deployed_containers(self):
2014-04-17 23:10:53 +00:00
"""determine which images/commands are running already"""
2014-01-22 12:04:19 +00:00
image = self.module.params.get('image')
command = self.module.params.get('command')
2013-10-24 05:56:02 +00:00
if command:
command = command.strip()
2014-01-22 12:04:19 +00:00
name = self.module.params.get('name')
if name and not name.startswith('/'):
name = '/' + name
deployed = []
2013-10-13 22:07:27 +00:00
2013-10-24 05:56:02 +00:00
# if we weren't given a tag with the image, we need to only compare on the image name, as that
# docker will give us back the full image name including a tag in the container list if one exists.
image, tag = self.get_split_image_tag(image)
2014-01-22 12:04:19 +00:00
2014-04-17 23:10:53 +00:00
for i in self.client.containers(all=True):
2013-11-03 20:48:03 +00:00
running_image, running_tag = self.get_split_image_tag(i['Image'])
2013-10-24 05:56:02 +00:00
running_command = i['Command'].strip()
2014-05-12 17:27:07 +00:00
name_matches = False
if i["Names"]:
name_matches = (name and name in i['Names'])
2014-04-17 23:10:53 +00:00
image_matches = (running_image == image)
tag_matches = (not tag or running_tag == tag)
# if a container has an entrypoint, `command` will actually equal
# '{} {}'.format(entrypoint, command)
command_matches = (not command or running_command.endswith(command))
2014-05-15 14:04:30 +00:00
if name_matches or (name is None and image_matches and tag_matches and command_matches):
2013-10-13 22:07:27 +00:00
details = self.client.inspect_container(i['Id'])
2013-10-24 05:56:02 +00:00
details = _docker_id_quirk(details)
2013-10-13 22:07:27 +00:00
deployed.append(details)
return deployed
def get_running_containers(self):
running = []
for i in self.get_deployed_containers():
2014-05-08 21:17:48 +00:00
if i['State']['Running'] == True and i['State'].get('Ghost', False) == False:
2013-10-13 22:07:27 +00:00
running.append(i)
return running
def create_containers(self, count=1):
params = {'image': self.module.params.get('image'),
'command': self.module.params.get('command'),
2014-02-25 23:24:46 +00:00
'ports': self.exposed_ports,
2013-10-24 05:56:02 +00:00
'volumes': self.volumes,
2013-10-13 22:07:27 +00:00
'mem_limit': _human_to_bytes(self.module.params.get('memory_limit')),
2013-11-03 20:48:03 +00:00
'environment': self.env,
2013-10-13 22:07:27 +00:00
'hostname': self.module.params.get('hostname'),
'detach': self.module.params.get('detach'),
2014-02-25 23:24:46 +00:00
'name': self.module.params.get('name'),
2014-03-18 11:57:01 +00:00
'stdin_open': self.module.params.get('stdin_open'),
'tty': self.module.params.get('tty'),
2013-10-13 22:07:27 +00:00
}
2014-04-11 16:08:55 +00:00
if docker.utils.compare_version('1.10', self.client.version()['ApiVersion']) < 0:
params['dns'] = self.module.params.get('dns')
params['volumes_from'] = self.module.params.get('volumes_from')
2013-10-13 22:07:27 +00:00
def do_create(count, params):
results = []
2013-10-14 04:34:58 +00:00
for _ in range(count):
2013-10-13 22:07:27 +00:00
result = self.client.create_container(**params)
self.increment_counter('created')
results.append(result)
return results
try:
containers = do_create(count, params)
except:
self.client.pull(params['image'])
self.increment_counter('pull')
containers = do_create(count, params)
return containers
def start_containers(self, containers):
2014-02-25 23:24:46 +00:00
params = {
'lxc_conf': self.lxc_conf,
'binds': self.binds,
'port_bindings': self.port_bindings,
'publish_all_ports': self.module.params.get('publish_all_ports'),
'privileged': self.module.params.get('privileged'),
'links': self.links,
}
2014-04-17 23:05:50 +00:00
if docker.utils.compare_version('1.10', self.client.version()['ApiVersion']) >= 0 and hasattr(docker, '__version__') and docker.__version__ > '0.3.0':
2014-04-11 16:08:55 +00:00
params['dns'] = self.module.params.get('dns')
params['volumes_from'] = self.module.params.get('volumes_from')
2013-10-13 22:07:27 +00:00
for i in containers:
2014-02-25 23:24:46 +00:00
self.client.start(i['Id'], **params)
self.increment_counter('started')
2013-10-13 22:07:27 +00:00
def stop_containers(self, containers):
for i in containers:
self.client.stop(i['Id'])
self.increment_counter('stopped')
return [self.client.wait(i['Id']) for i in containers]
def remove_containers(self, containers):
for i in containers:
self.client.remove_container(i['Id'])
self.increment_counter('removed')
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
def kill_containers(self, containers):
for i in containers:
self.client.kill(i['Id'])
self.increment_counter('killed')
def restart_containers(self, containers):
for i in containers:
self.client.restart(i['Id'])
self.increment_counter('restarted')
2014-02-25 23:24:46 +00:00
def check_dependencies(module):
"""
Ensure `docker-py` >= 0.3.0 is installed, and call module.fail_json with a
helpful error message if it isn't.
"""
if not HAS_DOCKER_PY:
module.fail_json(msg="`docker-py` doesn't seem to be installed, but is required for the Ansible Docker module.")
else:
HAS_NEW_ENOUGH_DOCKER_PY = False
if hasattr(docker, '__version__'):
# a '__version__' attribute was added to the module but not until
# after 0.3.0 was added pushed to pip. If it's there, use it.
if docker.__version__ >= '0.3.0':
HAS_NEW_ENOUGH_DOCKER_PY = True
else:
# HACK: if '__version__' isn't there, we check for the existence of
# `_get_raw_response_socket` in the docker.Client class, which was
# added in 0.3.0
if hasattr(docker.Client, '_get_raw_response_socket'):
HAS_NEW_ENOUGH_DOCKER_PY = True
if not HAS_NEW_ENOUGH_DOCKER_PY:
module.fail_json(msg="The Ansible Docker module requires `docker-py` >= 0.3.0.")
2013-10-13 22:07:27 +00:00
def main():
module = AnsibleModule(
argument_spec = dict(
count = dict(default=1),
image = dict(required=True),
command = dict(required=False, default=None),
2014-03-18 20:13:12 +00:00
expose = dict(required=False, default=None, type='list'),
ports = dict(required=False, default=None, type='list'),
2014-02-25 23:24:46 +00:00
publish_all_ports = dict(default=False, type='bool'),
2014-03-18 20:13:12 +00:00
volumes = dict(default=None, type='list'),
2013-10-13 22:07:27 +00:00
volumes_from = dict(default=None),
2014-03-18 20:13:12 +00:00
links = dict(default=None, type='list'),
2013-10-13 22:07:27 +00:00
memory_limit = dict(default=0),
memory_swap = dict(default=0),
docker_url = dict(default='unix://var/run/docker.sock'),
user = dict(default=None),
password = dict(),
email = dict(),
hostname = dict(default=None),
2014-03-18 20:13:12 +00:00
env = dict(type='list'),
2013-10-13 22:07:27 +00:00
dns = dict(),
detach = dict(default=True, type='bool'),
2014-03-14 10:11:56 +00:00
state = dict(default='running', choices=['absent', 'present', 'running', 'stopped', 'killed', 'restarted']),
2013-10-13 22:07:27 +00:00
debug = dict(default=False, type='bool'),
privileged = dict(default=False, type='bool'),
2014-03-18 11:57:01 +00:00
stdin_open = dict(default=False, type='bool'),
tty = dict(default=False, type='bool'),
2014-03-18 20:13:12 +00:00
lxc_conf = dict(default=None, type='list'),
name = dict(default=None)
2013-10-13 22:07:27 +00:00
)
)
2014-02-25 23:24:46 +00:00
check_dependencies(module)
2013-10-13 22:07:27 +00:00
try:
2013-10-14 20:49:53 +00:00
manager = DockerManager(module)
2013-10-13 22:07:27 +00:00
state = module.params.get('state')
count = int(module.params.get('count'))
2014-02-25 23:24:46 +00:00
name = module.params.get('name')
if count < 0:
module.fail_json(msg="Count must be greater than zero")
if count > 1 and name:
module.fail_json(msg="Count and name must not be used together")
2013-10-13 22:07:27 +00:00
2013-10-14 20:49:53 +00:00
running_containers = manager.get_running_containers()
2013-10-13 22:07:27 +00:00
running_count = len(running_containers)
delta = count - running_count
2013-10-14 20:49:53 +00:00
deployed_containers = manager.get_deployed_containers()
2013-10-13 22:07:27 +00:00
facts = None
failed = False
changed = False
# start/stop containers
2014-03-14 10:11:56 +00:00
if state in [ "running", "present" ]:
2014-04-14 22:05:42 +00:00
2014-03-14 10:11:56 +00:00
# make sure a container with `name` exists, if not create and start it
if name and "/" + name not in map(lambda x: x.get('Name'), deployed_containers):
2014-02-25 23:24:46 +00:00
containers = manager.create_containers(1)
2014-04-14 22:05:42 +00:00
if state == "present": #otherwise it get (re)started later anyways..
2014-03-14 10:11:56 +00:00
manager.start_containers(containers)
running_containers = manager.get_running_containers()
deployed_containers = manager.get_deployed_containers()
if state == "running":
# make sure a container with `name` is running
if name and "/" + name not in map(lambda x: x.get('Name'), running_containers):
manager.start_containers(deployed_containers)
2014-04-14 22:05:42 +00:00
2014-03-14 10:11:56 +00:00
# start more containers if we don't have enough
elif delta > 0:
containers = manager.create_containers(delta)
manager.start_containers(containers)
2014-04-14 22:05:42 +00:00
2014-03-14 10:11:56 +00:00
# stop containers if we have too many
elif delta < 0:
containers_to_stop = running_containers[0:abs(delta)]
containers = manager.stop_containers(containers_to_stop)
manager.remove_containers(containers_to_stop)
2014-04-14 22:05:42 +00:00
2014-03-14 10:11:56 +00:00
facts = manager.get_running_containers()
else:
2014-04-21 15:06:00 +00:00
facts = manager.get_deployed_containers()
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
# stop and remove containers
elif state == "absent":
2013-10-14 20:49:53 +00:00
facts = manager.stop_containers(deployed_containers)
2013-10-24 05:56:02 +00:00
manager.remove_containers(deployed_containers)
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
# stop containers
elif state == "stopped":
2013-10-14 20:49:53 +00:00
facts = manager.stop_containers(running_containers)
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
# kill containers
elif state == "killed":
2013-10-14 20:49:53 +00:00
manager.kill_containers(running_containers)
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
# restart containers
elif state == "restarted":
2013-10-24 05:56:02 +00:00
manager.restart_containers(running_containers)
facts = manager.get_inspect_containers(running_containers)
2013-10-13 22:07:27 +00:00
msg = "%s container(s) running image %s with command %s" % \
2013-10-14 20:49:53 +00:00
(manager.get_summary_counters_msg(), module.params.get('image'), module.params.get('command'))
changed = manager.has_changed()
2014-02-25 23:24:46 +00:00
2013-10-13 22:07:27 +00:00
module.exit_json(failed=failed, changed=changed, msg=msg, ansible_facts=_ansible_facts(facts))
2014-04-16 22:42:56 +00:00
except DockerAPIError, e:
2013-10-14 20:49:53 +00:00
changed = manager.has_changed()
2013-10-13 22:07:27 +00:00
module.exit_json(failed=True, changed=changed, msg="Docker API error: " + e.explanation)
2014-02-02 17:33:27 +00:00
except RequestException, e:
2013-10-14 20:49:53 +00:00
changed = manager.has_changed()
2013-10-13 22:07:27 +00:00
module.exit_json(failed=True, changed=changed, msg=repr(e))
2014-02-25 23:24:46 +00:00
2013-12-02 20:13:49 +00:00
# import module snippets
2013-12-02 20:11:23 +00:00
from ansible.module_utils.basic import *
2013-10-13 22:07:27 +00:00
main()