Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
CI stuff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
shore
CI stuff
Commits
080a6307
Commit
080a6307
authored
3 years ago
by
nimrod
Browse files
Options
Downloads
Patches
Plain Diff
Refactor the script a bit.
parent
c310a110
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2397
passed
3 years ago
Stage: .pre
Stage: build
Stage: deploy
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
images/docker/compose-health-check
+43
-34
43 additions, 34 deletions
images/docker/compose-health-check
with
43 additions
and
34 deletions
images/docker/compose-health-check
+
43
−
34
View file @
080a6307
...
...
@@ -26,15 +26,22 @@ def get_project_containers_health(client, project):
}
def
check_project_health
(
project
,
timeout
):
"""
Checks if a Docker Compose project is healthy.
def
get_unhealthy_project_containers
(
client
,
project
):
"""
Returns a list (could be empty) of containers that are not healthy or
not running (in case a health check is not set).
"""
healths
=
get_project_containers_health
(
client
,
project
)
return
[
x
for
x
in
healths
if
healths
[
x
]
not
in
[
"
healthy
"
,
"
running
"
]]
def
wait_for_project_health
(
project
,
timeout
):
"""
Wait for a Docker Compose project to be healthy.
If all of the containers are healthy or running (because there
'
s no
healthcheck set), return
True
. If any of the containers
is starting (health
check hasn
'
t run yet or in the grace period), sleep 10 and
try again
(ignoring the timeout). Otherwise, if
haven
'
t passed the timeout, sleep 10
and try again, if over the timeout, return
if all the containers ar
e
healthy or running.
healthcheck set), return
an empty list
. If any of the containers
are
starting (health
check hasn
'
t run yet or in the grace period), sleep 10 and
try again
(ignoring the timeout). Otherwise, if
the timeout hasn
'
t passed,
sleep 10
and try again, if over the timeout, return
a list of th
e
containers that aren
'
t
healthy or
aren
'
t
running.
"""
starttime
=
datetime
.
datetime
.
now
()
deadline
=
starttime
+
datetime
.
timedelta
(
seconds
=
timeout
)
...
...
@@ -43,8 +50,8 @@ def check_project_health(project, timeout):
while
True
:
healths
=
get_project_containers_health
(
docker_client
,
project
)
# pylint: disable=no-else-return
if
all
(
map
(
lambda
x
:
x
in
[
"
healthy
"
,
"
running
"
],
healths
.
values
())
):
return
True
if
not
get_unhealthy_project_containers
(
docker_client
,
project
):
return
[]
elif
any
(
map
(
lambda
x
:
x
==
"
starting
"
,
healths
.
values
())):
time
.
sleep
(
10
)
continue
...
...
@@ -53,24 +60,26 @@ def check_project_health(project, timeout):
else
:
time
.
sleep
(
10
)
if
all
(
map
(
lambda
x
:
x
in
[
"
healthy
"
,
"
running
"
],
healths
.
values
())):
return
True
unhealthy
=
"
,
"
.
join
(
[
f
"
{
x
.
id
}
(
{
x
.
name
}
)
"
for
x
in
healths
if
healths
[
x
]
not
in
[
"
healthy
"
,
"
running
"
]
]
)
print
(
f
"""
Containers
{
unhealthy
}
are not healthy.
"""
)
return
False
return
get_project_containers_health
(
docker_client
,
project
)
def
get_project_name
(
args
):
"""
Returns the name of the Docker Compose project, or None if unable to.
"""
if
args
.
project
:
return
args
.
project
env
=
dotenv
.
dotenv_values
(
dotenv_path
=
"
.env
"
)
if
"
COMPOSE_PROJECT_NAME
"
in
env
:
return
env
[
"
COMPOSE_PROJECT_NAME
"
]
if
"
COMPOSE_PROJECT_NAME
"
in
os
.
environ
:
return
os
.
environ
[
"
COMPOSE_PROJECT_NAME
"
]
return
None
def
main
():
"""
Main entrypoint.
"""
epilog
=
(
"
The Docker Compose project name is resolved in the following order:
"
# noqa: E501
"
The Docker Compose project name is resolved in the following order:
"
"
\n
1. From the command line parameter.
"
"
\n
2. From the COMPOSE_PROJECT_NAME variable in the .env file.
"
"
\n
3. From the COMPOSE_PROJECT_NAME environment variable.
"
...
...
@@ -91,21 +100,21 @@ def main():
default
=
300
,
)
args
=
arg_parser
.
parse_args
()
if
args
.
project
:
project
=
args
.
project
else
:
env
=
dotenv
.
dotenv_values
(
dotenv_path
=
"
.env
"
)
if
"
COMPOSE_PROJECT_NAME
"
in
env
:
project
=
env
[
"
COMPOSE_PROJECT_NAME
"
]
elif
"
COMPOSE_PROJECT_NAME
"
in
os
.
environ
:
project
=
os
.
environ
[
"
COMPOSE_PROJECT_NAME
"
]
else
:
project
=
get_project_name
(
args
)
if
project
is
None
:
arg_parser
.
error
(
"
Compose project wasn
'
t specified, the COMPOSE_PROJECT_NAME variable is missing from the environment and from the .env file.
"
# noqa: E501
)
if
check_project_health
(
project
,
args
.
timeout
):
unhealthy
=
wait_for_project_health
(
project
,
args
.
timeout
)
if
not
unhealthy
:
print
(
f
"
Project
{
project
}
is healthy.
"
)
return
0
print
(
f
"""
The
{
"
,
"
.
join
(
map
(
lambda
x
:
x
.
name
,
unhealthy
))
}
container for project
{
project
}
are not healthy.
"""
# noqa: E501
)
return
1
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment