Un-camelcase DockerContainers, rename class to DockerManager().
This commit is contained in:
parent
21e31722f9
commit
502fe3cf5f
1 changed files with 26 additions and 23 deletions
49
cloud/docker
49
cloud/docker
|
@ -148,7 +148,7 @@ on the host:
|
||||||
docker: image=centos command="service tomcat6 start" ports=:8080
|
docker: image=centos command="service tomcat6 start" ports=:8080
|
||||||
|
|
||||||
The tomcat server's port is NAT'ed to a dynamic port on the host, but you can determine which port the server was
|
The tomcat server's port is NAT'ed to a dynamic port on the host, but you can determine which port the server was
|
||||||
mapped to using $DockerContainers:
|
mapped to using docker_containers:
|
||||||
|
|
||||||
- hosts: web
|
- hosts: web
|
||||||
sudo: yes
|
sudo: yes
|
||||||
|
@ -157,7 +157,7 @@ mapped to using $DockerContainers:
|
||||||
docker: image=centos command="service tomcat6 start" ports=8080 count=5
|
docker: image=centos command="service tomcat6 start" ports=8080 count=5
|
||||||
- name: Display IP address and port mappings for containers
|
- name: Display IP address and port mappings for containers
|
||||||
debug: msg="Mapped to {{inventory_hostname}}:{{item.NetworkSettings.PortMapping.Tcp['8080']}}"
|
debug: msg="Mapped to {{inventory_hostname}}:{{item.NetworkSettings.PortMapping.Tcp['8080']}}"
|
||||||
with_items: $DockerContainers
|
with_items: docker_containers
|
||||||
|
|
||||||
Just as in the previous example, but iterates over the list of docker containers with a sequence:
|
Just as in the previous example, but iterates over the list of docker containers with a sequence:
|
||||||
|
|
||||||
|
@ -169,16 +169,19 @@ Just as in the previous example, but iterates over the list of docker containers
|
||||||
- name: run tomcat servers
|
- name: run tomcat servers
|
||||||
docker: image=centos command="service tomcat6 start" ports=8080 count={{start_containers_count}}
|
docker: image=centos command="service tomcat6 start" ports=8080 count={{start_containers_count}}
|
||||||
- name: Display IP address and port mappings for containers
|
- name: Display IP address and port mappings for containers
|
||||||
debug: msg="Mapped to {{inventory_hostname}}:{{DockerContainers[{{item}}].NetworkSettings.PortMapping.Tcp['8080']}}"
|
debug: msg="Mapped to {{inventory_hostname}}:{{docker_containers[{{item}}].NetworkSettings.PortMapping.Tcp['8080']}}"
|
||||||
with_sequence: start=0 end={{start_containers_count - 1}}
|
with_sequence: start=0 end={{start_containers_count - 1}}
|
||||||
|
|
||||||
Stop and remove all of the running tomcat containers:
|
Stop, remove all of the running tomcat containers and list the exit code from the stopped containers:
|
||||||
|
|
||||||
- hosts: web
|
- hosts: web
|
||||||
sudo: yes
|
sudo: yes
|
||||||
tasks:
|
tasks:
|
||||||
- name: stop tomcat servers
|
- name: stop tomcat servers
|
||||||
docker: image=centos command="service tomcat6 start" state=absent
|
docker: image=centos command="service tomcat6 start" state=absent
|
||||||
|
- name: Display return codes from stopped containers
|
||||||
|
debug: msg="Returned {{inventory_hostname}}:{{item}}"
|
||||||
|
with_items: docker_containers
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -208,9 +211,9 @@ def _human_to_bytes(number):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def _ansible_facts(container_list):
|
def _ansible_facts(container_list):
|
||||||
return {"DockerContainers": container_list}
|
return {"docker_containers": container_list}
|
||||||
|
|
||||||
class AnsibleDocker:
|
class DockerManager:
|
||||||
|
|
||||||
counters = {'created':0, 'started':0, 'stopped':0, 'killed':0, 'removed':0, 'restarted':0, 'pull':0}
|
counters = {'created':0, 'started':0, 'stopped':0, 'killed':0, 'removed':0, 'restarted':0, 'pull':0}
|
||||||
|
|
||||||
|
@ -367,17 +370,17 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
docker_client = AnsibleDocker(module)
|
manager = DockerManager(module)
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
count = int(module.params.get('count'))
|
count = int(module.params.get('count'))
|
||||||
|
|
||||||
if count < 1:
|
if count < 1:
|
||||||
module.fail_json(msg="Count must be positive number")
|
module.fail_json(msg="Count must be positive number")
|
||||||
|
|
||||||
running_containers = docker_client.get_running_containers()
|
running_containers = manager.get_running_containers()
|
||||||
running_count = len(running_containers)
|
running_count = len(running_containers)
|
||||||
delta = count - running_count
|
delta = count - running_count
|
||||||
deployed_containers = docker_client.get_deployed_containers()
|
deployed_containers = manager.get_deployed_containers()
|
||||||
facts = None
|
facts = None
|
||||||
failed = False
|
failed = False
|
||||||
changed = False
|
changed = False
|
||||||
|
@ -387,45 +390,45 @@ def main():
|
||||||
|
|
||||||
# start more containers if we don't have enough
|
# start more containers if we don't have enough
|
||||||
if delta > 0:
|
if delta > 0:
|
||||||
containers = docker_client.create_containers(delta)
|
containers = manager.create_containers(delta)
|
||||||
docker_client.start_containers(containers)
|
manager.start_containers(containers)
|
||||||
|
|
||||||
# stop containers if we have too many
|
# stop containers if we have too many
|
||||||
elif delta < 0:
|
elif delta < 0:
|
||||||
docker_client.stop_containers(running_containers[0:abs(delta)])
|
manager.stop_containers(running_containers[0:abs(delta)])
|
||||||
docker_client.remove_containers(running_containers[0:abs(delta)])
|
manager.remove_containers(running_containers[0:abs(delta)])
|
||||||
|
|
||||||
facts = docker_client.get_running_containers()
|
facts = manager.get_running_containers()
|
||||||
|
|
||||||
# stop and remove containers
|
# stop and remove containers
|
||||||
elif state == "absent":
|
elif state == "absent":
|
||||||
facts = docker_client.stop_containers(deployed_containers)
|
facts = manager.stop_containers(deployed_containers)
|
||||||
docker_client.remove_containers(containers)
|
manager.remove_containers(containers)
|
||||||
|
|
||||||
# stop containers
|
# stop containers
|
||||||
elif state == "stopped":
|
elif state == "stopped":
|
||||||
facts = docker_client.stop_containers(running_containers)
|
facts = manager.stop_containers(running_containers)
|
||||||
|
|
||||||
# kill containers
|
# kill containers
|
||||||
elif state == "killed":
|
elif state == "killed":
|
||||||
docker_client.kill_containers(running_containers)
|
manager.kill_containers(running_containers)
|
||||||
|
|
||||||
# restart containers
|
# restart containers
|
||||||
elif state == "restarted":
|
elif state == "restarted":
|
||||||
docker_client.restart_containers(running_containers)
|
manager.restart_containers(running_containers)
|
||||||
|
|
||||||
msg = "%s container(s) running image %s with command %s" % \
|
msg = "%s container(s) running image %s with command %s" % \
|
||||||
(docker_client.get_summary_counters_msg(), module.params.get('image'), module.params.get('command'))
|
(manager.get_summary_counters_msg(), module.params.get('image'), module.params.get('command'))
|
||||||
changed = docker_client.has_changed()
|
changed = manager.has_changed()
|
||||||
|
|
||||||
module.exit_json(failed=failed, changed=changed, msg=msg, ansible_facts=_ansible_facts(facts))
|
module.exit_json(failed=failed, changed=changed, msg=msg, ansible_facts=_ansible_facts(facts))
|
||||||
|
|
||||||
except docker.client.APIError as e:
|
except docker.client.APIError as e:
|
||||||
changed = docker_client.has_changed()
|
changed = manager.has_changed()
|
||||||
module.exit_json(failed=True, changed=changed, msg="Docker API error: " + e.explanation)
|
module.exit_json(failed=True, changed=changed, msg="Docker API error: " + e.explanation)
|
||||||
|
|
||||||
except RequestException as e:
|
except RequestException as e:
|
||||||
changed = docker_client.has_changed()
|
changed = manager.has_changed()
|
||||||
module.exit_json(failed=True, changed=changed, msg=repr(e))
|
module.exit_json(failed=True, changed=changed, msg=repr(e))
|
||||||
|
|
||||||
# this is magic, see lib/ansible/module_common.py
|
# this is magic, see lib/ansible/module_common.py
|
||||||
|
|
Loading…
Reference in a new issue