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
53788e4c
Commit
53788e4c
authored
Feb 27, 2014
by
Pavel Antonov
Committed by
James Cammarata
Mar 10, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support docker_py >= 0.3.0, Docker API >= 1.8, extended error reporting
parent
60055348
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
12 deletions
+43
-12
library/cloud/docker_image
+43
-12
No files found.
library/cloud/docker_image
View file @
53788e4c
...
@@ -104,6 +104,8 @@ Remove image from local docker storage:
...
@@ -104,6 +104,8 @@ Remove image from local docker storage:
try
:
try
:
import
sys
import
sys
import
re
import
json
import
docker.client
import
docker.client
from
requests.exceptions
import
*
from
requests.exceptions
import
*
from
urlparse
import
urlparse
from
urlparse
import
urlparse
...
@@ -122,12 +124,33 @@ class DockerImageManager:
...
@@ -122,12 +124,33 @@ class DockerImageManager:
docker_url
=
urlparse
(
module
.
params
.
get
(
'docker_url'
))
docker_url
=
urlparse
(
module
.
params
.
get
(
'docker_url'
))
self
.
client
=
docker
.
Client
(
base_url
=
docker_url
.
geturl
(),
timeout
=
module
.
params
.
get
(
'timeout'
))
self
.
client
=
docker
.
Client
(
base_url
=
docker_url
.
geturl
(),
timeout
=
module
.
params
.
get
(
'timeout'
))
self
.
changed
=
False
self
.
changed
=
False
self
.
log
=
[]
self
.
error_msg
=
None
def
get_log
(
self
,
as_string
=
True
):
return
""
.
join
(
self
.
log
)
if
as_string
else
self
.
log
def
build
(
self
):
def
build
(
self
):
res
=
self
.
client
.
build
(
self
.
path
,
tag
=
":"
.
join
([
self
.
name
,
self
.
tag
]),
nocache
=
self
.
nocache
,
rm
=
True
)
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
self
.
changed
=
True
self
.
changed
=
True
return
res
for
chunk
in
stream
:
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
def
has_changed
(
self
):
def
has_changed
(
self
):
return
self
.
changed
return
self
.
changed
...
@@ -136,7 +159,13 @@ class DockerImageManager:
...
@@ -136,7 +159,13 @@ class DockerImageManager:
filtered_images
=
[]
filtered_images
=
[]
images
=
self
.
client
.
images
()
images
=
self
.
client
.
images
()
for
i
in
images
:
for
i
in
images
:
if
(
not
self
.
name
or
self
.
name
==
i
[
'Repository'
])
and
(
not
self
.
tag
or
self
.
tag
==
i
[
'Tag'
]):
# Docker-py version >= 0.3 (Docker API >= 1.8)
if
'RepoTags'
in
i
:
repotag
=
'
%
s:
%
s'
%
(
getattr
(
self
,
'name'
,
''
),
getattr
(
self
,
'tags'
,
'latest'
))
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'
]):
filtered_images
.
append
(
i
)
filtered_images
.
append
(
i
)
return
filtered_images
return
filtered_images
...
@@ -170,25 +199,27 @@ def main():
...
@@ -170,25 +199,27 @@ def main():
failed
=
False
failed
=
False
image_id
=
None
image_id
=
None
msg
=
''
msg
=
''
do_build
=
False
# build image if not exists
# build image if not exists
if
state
==
"present"
:
if
state
==
"present"
:
images
=
manager
.
get_images
()
images
=
manager
.
get_images
()
if
len
(
images
)
==
0
:
if
len
(
images
)
==
0
:
image_id
,
msg
=
manager
.
build
()
do_build
=
True
if
image_id
is
None
:
# build image
failed
=
True
elif
state
==
"build"
:
do_build
=
True
# remove image or images
# remove image or images
elif
state
==
"absent"
:
elif
state
==
"absent"
:
manager
.
remove_images
()
manager
.
remove_images
()
# build image
if
do_build
:
elif
state
==
"build"
:
image_id
=
manager
.
build
()
image_id
,
msg
=
manager
.
build
()
if
image_id
:
if
image_id
is
None
:
msg
=
"Image builded:
%
s"
%
image_id
else
:
failed
=
True
failed
=
True
msg
=
"Error:
%
s
\n
Log:
%
s"
%
(
manager
.
error_msg
,
manager
.
get_log
())
module
.
exit_json
(
failed
=
failed
,
changed
=
manager
.
has_changed
(),
msg
=
msg
,
image_id
=
image_id
)
module
.
exit_json
(
failed
=
failed
,
changed
=
manager
.
has_changed
(),
msg
=
msg
,
image_id
=
image_id
)
...
...
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