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