1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#!/bin/sh
# Version: 1.1.2
# Date: 2020-06-24
#
# Resource script for running docker-compose
#
# Description: Manages docker services using docker-compose as an OCF
# resource in an High Availability setup.
# It relies on a well-tested docker compose YAML file which
# distributed on an identical location on all cluster nodes.
#
# Caveat: 1. A YAML file (docker-compose.yml) and an optional Dockerfile
# must be provided in a working directory.
# 2. It is suggested to test run the docker-compose and verify
# on all cluster nodes before enabling this agent.
#
# docker-compose OCF script's Author: Kenny Chen <netman@study-area.org>
# License: GNU General Public License (GPL)
#
# usage: $0 {start|stop|status|monitor|validate-all|meta-data}
#
# The "start" arg starts docker service.
# The "stop" arg stops it.
#
# OCF parameters:
# OCF_RESKEY_binpath
# OCF_RESKEY_dirpath
# OCF_RESKEY_ymlfile
#
##########################################################################
# Initialization:
: ${OCF_ROOT:=/usr/lib/ocf}
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
# Defaults
OCF_RESKEY_binpath_default=/usr/bin/docker-compose
OCF_RESKEY_ymlfile_default=docker-compose.yml
: ${OCF_RESKEY_binpath=${OCF_RESKEY_binpath_default}}
: ${OCF_RESKEY_ymlfile=${OCF_RESKEY_ymlfile_default}}
USAGE="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}"
##########################################################################
usage()
{
echo $USAGE >&2
}
meta_data()
{
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="docker-compose" version="1.0.3">
<version>1.0</version>
<longdesc lang="en">
Manages docker services using docker-compose as an OCF resource in an High Availability setup.
It relies on a well-tested docker compose YAML file which distributed on an identical location on all cluster nodes.
Caveat: 1. A YAML file (docker-compose.yml) and an optional Dockerfile
must be provided in a working directory.
2. It is suggested to test run the docker-compose and verify on all cluster nodes
before enabling this agent.
</longdesc>
<shortdesc lang="en">This script manages docker services using docker-compose.</shortdesc>
<parameters>
<parameter name="binpath">
<longdesc lang="en">
The docker-composer binary path.
For example, "/usr/bin/docker-compose"
</longdesc>
<shortdesc lang="en">The docker-composer binary path</shortdesc>
<content type="string" default="$OCF_RESKEY_binpath_default"/>
</parameter>
<parameter name="dirpath" required="1">
<longdesc lang="en">
The directory contains docker compose yaml file.
For example, "/data/docker"
</longdesc>
<shortdesc lang="en">Directory contains docker compose files</shortdesc>
<content type="string"/>
</parameter>
<parameter name="ymlfile">
<longdesc lang="en">
The docker-compose yaml file.
For example, "docker-compose.yml"
</longdesc>
<shortdesc lang="en">The docker compose yaml</shortdesc>
<content type="string" default="$OCF_RESKEY_ymlfile_default"/>
</parameter>
</parameters>
<actions>
<action name="start" timeout="240s"/>
<action name="stop" timeout="20s"/>
<action name="monitor" depth="0" timeout="10s" interval="60s" />
<action name="validate-all" timeout="5s" />
<action name="meta-data" timeout="5s"/>
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}
if [ -r "$OCF_RESKEY_binpath" -a -x "$OCF_RESKEY_binpath" ]; then
COMMAND="$OCF_RESKEY_binpath"
else
COMMAND="$OCF_RESKEY_binpath_default"
fi
DIR="$OCF_RESKEY_dirpath"
PRE="$(echo ${DIR##*/} | tr A-Z a-z | sed 's/[^a-z0-9]//g')"
YML="$OCF_RESKEY_ymlfile"
docker_kill()
{
for i in $(docker ps --all | awk -e '$NF ~ /\<'"${PRE}"'_.*_[0-9]+\>/ {print $1}'); do
docker kill $i >/dev/null 2>&1
docker rm $i >/dev/null 2>&1 || RTV=false
done
if [ "$RTV" = false ]; then
ocf_log err "failed to kill docker"
return $OCF_ERR_GENERIC
else
RUN=false
fi
}
docker_compose_status()
{
# use docker-compose ps if YML found, otherwise try docker ps and kill containers
if [ -r "$DIR/$YML" ]; then
DKPS=$(cd $DIR; $COMMAND -f $YML ps -q)
# get number of all containers
[ -n "$DKPS" ] && PSNU=$(echo "$DKPS" | wc -l)
# get number of running containers
for UUID in $DKPS; do
UP=$(docker inspect --format='{{.State.Running}}' "$UUID")
[ "$UP" = "true" ] && UPNU=$((UPNU+1))
done
if [ "${PSNU:-0}" -ne 0 ]; then
if [ ${UPNU:-0} -eq 0 ]; then
ocf_log info "docker service is running but not in up state."
return $OCF_NOT_RUNNING
elif [ "$PSNU" -eq $UPNU ]; then
ocf_log info "docker service is up and running"
return $OCF_SUCCESS
else
ocf_log err "docker service is running with partial up state"
return $OCF_ERR_GENERIC
fi
else
RUN=false
fi
else
STAT_MSG=$(docker ps --all | awk -e '$NF ~ /\<'"$PRE"'_.*_[0-9]+\>/ {print $1}')
if [ -z "$STAT_MSG" ]; then
RUN=false
else
ocf_log log "docker service is running without docker-compose, try to kill..."
docker_kill
fi
fi
[ "$RUN" = false ] && {
ocf_log info "docker service is not running"
return $OCF_NOT_RUNNING
}
}
docker_compose_start()
{
docker_compose_validate_all
docker_compose_status >/dev/null 2>&1
retVal=$?
# return success if docker service is running
[ $retVal -eq $OCF_SUCCESS ] && exit $OCF_SUCCESS
cd $DIR
$COMMAND -f $YML up -d || {
ocf_log err "Error. docker-compose returned error $?."
exit $OCF_ERR_GENERIC
}
ocf_log info "docker service started."
exit $OCF_SUCCESS
}
docker_compose_stop()
{
# use docker-compose down if YML found, otherwise try docker kill and rm
if [ -r "$DIR/$YML" ]; then
docker_compose_validate_all
cd $DIR
$COMMAND -f $YML down || {
ocf_log err "Error on shutting down docker service, try docker kill..."
RUN_KILL=true
}
else
RUN_KILL=true
fi
if [ "$RUN_KILL" = true ]; then
docker_kill
[ "$RTV" = false ] && {
ocf_log err "Error. Could not stop docker services."
return $OCF_ERR_GENERIC
}
fi
ocf_log info "docker service stopped."
exit $OCF_SUCCESS
}
docker_compose_monitor()
{
docker_compose_status
}
docker_compose_validate_all()
{
if ! check_binary "$OCF_RESKEY_binpath"; then
ocf_log err "missing binary $OCF_RESKEY_binpath."
exit $OCF_ERR_ARGS
fi
if [ ! -e "$OCF_RESKEY_dirpath" ]; then
ocf_log err "diretory $OCF_RESKEY_dirpath is not found."
exit $OCF_ERR_ARGS
elif [ ! -d "$OCF_RESKEY_dirpath" ]; then
ocf_log err "diretory $OCF_RESKEY_dirpath is not a directory."
exit $OCF_ERR_ARGS
fi
if [ ! -e "$OCF_RESKEY_dirpath/$OCF_RESKEY_ymlfile" ]; then
ocf_log err "yaml file $OCF_RESKEY_dirpath/$OCF_RESKEY_ymlfile is not found."
exit $OCF_ERR_ARGS
fi
return $OCF_SUCCESS
}
#
# Main
#
if [ $# -ne 1 ]; then
usage
exit $OCF_ERR_ARGS
fi
case $1 in
start)
docker_compose_start
;;
stop)
docker_compose_stop
;;
status)
docker_compose_status
;;
monitor)
docker_compose_monitor
;;
validate-all)
docker_compose_validate_all
;;
meta-data)
meta_data
;;
usage) usage
exit $OCF_SUCCESS
;;
*) usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
|