Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
5f7e1fc3
Commit
5f7e1fc3
authored
Oct 14, 2013
by
Cove Schneider
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Un-camelcase DockerContainers, rename class to DockerManager().
parent
565db64a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
23 deletions
+26
-23
library/cloud/docker
+26
-23
No files found.
library/cloud/docker
View file @
5f7e1fc3
...
@@ -148,7 +148,7 @@ on the host:
...
@@ -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
$DockerC
ontainers:
mapped to using
docker_c
ontainers:
- hosts: web
- hosts: web
sudo: yes
sudo: yes
...
@@ -157,7 +157,7 @@ mapped to using $DockerContainers:
...
@@ -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:
$DockerC
ontainers
with_items:
docker_c
ontainers
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
...
@@ -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}}:{{
DockerC
ontainers[{{item}}].NetworkSettings.PortMapping.Tcp['8080']}}"
debug: msg="Mapped to {{inventory_hostname}}:{{
docker_c
ontainers[{{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):
...
@@ -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
{
"
DockerC
ontainers"
:
container_list
}
return
{
"
docker_c
ontainers"
:
container_list
}
class
AnsibleDock
er
:
class
DockerManag
er
:
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():
...
@@ -367,17 +370,17 @@ def main():
)
)
try
:
try
:
docker_client
=
AnsibleDock
er
(
module
)
manager
=
DockerManag
er
(
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():
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment