Blame SOURCES/bz1135026-introducing-docker-agent.patch

c608c9
From 6d4180b5ed46cda544e008b242f024b2ab143a83 Mon Sep 17 00:00:00 2001
c608c9
From: David Vossel <dvossel@redhat.com>
c608c9
Date: Thu, 23 Oct 2014 09:37:18 -0500
c608c9
Subject: [PATCH] introducing docker agent
c608c9
c608c9
---
c608c9
 doc/man/Makefile.am   |   1 +
c608c9
 heartbeat/Makefile.am |   1 +
c608c9
 heartbeat/docker      | 330 ++++++++++++++++++++++++++++++++++++++++++++++++++
c608c9
 3 files changed, 332 insertions(+)
c608c9
 create mode 100755 heartbeat/docker
c608c9
c608c9
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
c608c9
index e97c7e9..ee29756 100644
c608c9
--- a/doc/man/Makefile.am
c608c9
+++ b/doc/man/Makefile.am
c608c9
@@ -98,6 +98,7 @@ man_MANS	       = ocf_heartbeat_AoEtarget.7 \
c608c9
                           ocf_heartbeat_conntrackd.7 \
c608c9
                           ocf_heartbeat_db2.7 \
c608c9
                           ocf_heartbeat_dhcpd.7 \
c608c9
+                          ocf_heartbeat_docker.7 \
c608c9
                           ocf_heartbeat_eDir88.7 \
c608c9
                           ocf_heartbeat_ethmonitor.7 \
c608c9
                           ocf_heartbeat_exportfs.7 \
c608c9
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
c608c9
index aab521f..f763533 100644
c608c9
--- a/heartbeat/Makefile.am
c608c9
+++ b/heartbeat/Makefile.am
c608c9
@@ -65,6 +65,7 @@ ocf_SCRIPTS	     =  ClusterMon		\
c608c9
 			conntrackd		\
c608c9
 			db2			\
c608c9
 			dhcpd		\
c608c9
+			docker		\
c608c9
 			Delay			\
c608c9
 			eDir88			\
c608c9
 			EvmsSCC			\
c608c9
diff --git a/heartbeat/docker b/heartbeat/docker
c608c9
new file mode 100755
c608c9
index 0000000..546c423
c608c9
--- /dev/null
c608c9
+++ b/heartbeat/docker
c608c9
@@ -0,0 +1,330 @@
c608c9
+#!/bin/sh
c608c9
+#
c608c9
+# The docker HA resource agent creates and launches a docker container
c608c9
+# based off a supplied docker image. Containers managed by this agent
c608c9
+# are both created and removed upon the agent's start and stop actions.
c608c9
+#
c608c9
+# Copyright (c) 2014 David Vossel <dvossel@redhat.com>
c608c9
+#                    All Rights Reserved.
c608c9
+#
c608c9
+# This program is free software; you can redistribute it and/or modify
c608c9
+# it under the terms of version 2 of the GNU General Public License as
c608c9
+# published by the Free Software Foundation.
c608c9
+#
c608c9
+# This program is distributed in the hope that it would be useful, but
c608c9
+# WITHOUT ANY WARRANTY; without even the implied warranty of
c608c9
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
c608c9
+#
c608c9
+# Further, this software is distributed without any warranty that it is
c608c9
+# free of the rightful claim of any third person regarding infringement
c608c9
+# or the like.  Any license provided herein, whether implied or
c608c9
+# otherwise, applies only to this software file.  Patent licenses, if
c608c9
+# any, provided herein do not apply to combinations of this program with
c608c9
+# other software, or any other product whatsoever.
c608c9
+#
c608c9
+# You should have received a copy of the GNU General Public License
c608c9
+# along with this program; if not, write the Free Software Foundation,
c608c9
+# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
c608c9
+#
c608c9
+
c608c9
+#######################################################################
c608c9
+# Initialization:
c608c9
+
c608c9
+: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
c608c9
+. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
c608c9
+
c608c9
+#######################################################################
c608c9
+
c608c9
+meta_data()
c608c9
+{
c608c9
+	cat <
c608c9
+
c608c9
+
c608c9
+<resource-agent name="docker" version="0.9">
c608c9
+<version>1.0</version>
c608c9
+
c608c9
+<longdesc lang="en">
c608c9
+The docker HA resource agent creates and launches a docker container
c608c9
+based off a supplied docker image. Containers managed by this agent
c608c9
+are both created and removed upon the agent's start and stop actions.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">Docker container resource agent.</shortdesc>
c608c9
+
c608c9
+<parameters>
c608c9
+<parameter name="image" required="1" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+The docker image to base this container off of.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">docker image</shortdesc>
c608c9
+<content type="string"/>
c608c9
+</parameter>
c608c9
+
c608c9
+<parameter name="container" required="0" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+The name to give the created container. By default this will 
c608c9
+be that resource's instance name.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">docker container name</shortdesc>
c608c9
+<content type="string"/>
c608c9
+</parameter>
c608c9
+
c608c9
+<parameter name="allow_pull" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+Allow the image to be pulled from the configured docker registry when
c608c9
+the image does not exist locally. NOTE, this can drastically increase
c608c9
+the time required to start the container if the image repository is 
c608c9
+pulled over the network.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">Allow pulling non-local images</shortdesc>
c608c9
+<content type="boolean"/>
c608c9
+</parameter>
c608c9
+
c608c9
+<parameter name="run_opts" required="0" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+Add options to be appended to the 'docker run' command which is used
c608c9
+when creating the container during the start action. This option allows
c608c9
+users to do things such as setting a custom entry point and injecting
c608c9
+environment variables into the newly created container. Note the '-d' 
c608c9
+option is supplied regardless of this value to force containers to run 
c608c9
+in the background.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">run options</shortdesc>
c608c9
+<content type="string"/>
c608c9
+</parameter>
c608c9
+
c608c9
+<parameter name="run_cmd" required="0" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+Specifiy a command to launch within the container once 
c608c9
+it has initialized.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">run command</shortdesc>
c608c9
+<content type="string"/>
c608c9
+</parameter>
c608c9
+
c608c9
+<parameter name="force_kill" required="0" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+Kill a container immediately rather than waiting for it to gracefully
c608c9
+shutdown
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">force kill</shortdesc>
c608c9
+<content type="boolean"/>
c608c9
+</parameter>
c608c9
+
c608c9
+<parameter name="reuse" required="0" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+Allow the container to be reused after stopping the container. By default
c608c9
+containers are removed after stop. With the reuse option containers
c608c9
+will persist after the container stops.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">reuse container</shortdesc>
c608c9
+<content type="boolean"/>
c608c9
+</parameter>
c608c9
+
c608c9
+</parameters>
c608c9
+
c608c9
+<actions>
c608c9
+<action name="start"        timeout="90" />
c608c9
+<action name="stop"         timeout="90" />
c608c9
+<action name="monitor"      timeout="30" interval="30" depth="0" />
c608c9
+<action name="meta-data"    timeout="5" />
c608c9
+<action name="validate-all"   timeout="30" />
c608c9
+</actions>
c608c9
+</resource-agent>
c608c9
+END
c608c9
+}
c608c9
+
c608c9
+#######################################################################
c608c9
+REQUIRE_IMAGE_PULL=0
c608c9
+
c608c9
+docker_usage()
c608c9
+{
c608c9
+	cat <
c608c9
+usage: $0 {start|stop|monitor|validate-all|meta-data}
c608c9
+
c608c9
+Expects to have a fully populated OCF RA-compliant environment set.
c608c9
+END
c608c9
+}
c608c9
+
c608c9
+container_exists()
c608c9
+{
c608c9
+	docker inspect $CONTAINER > /dev/null 2>&1
c608c9
+}
c608c9
+
c608c9
+remove_container()
c608c9
+{
c608c9
+	if ocf_is_true "$OCF_RESKEY_reuse"; then
c608c9
+		# never remove the container if we have reuse enabled.
c608c9
+		return 0
c608c9
+	fi
c608c9
+
c608c9
+	ocf_log notice "Cleaning up inactive container, ${CONTAINER}."
c608c9
+	ocf_run docker rm $CONTAINER
c608c9
+}
c608c9
+
c608c9
+docker_monitor()
c608c9
+{
c608c9
+	local val
c608c9
+
c608c9
+	container_exists
c608c9
+	if [ $? -ne 0 ]; then
c608c9
+		return $OCF_NOT_RUNNING
c608c9
+	fi
c608c9
+
c608c9
+	# retrieve the 'Running' attribute for the container
c608c9
+	val=$(docker inspect --format {{.State.Running}} $CONTAINER 2>/dev/null)
c608c9
+	if [ $? -ne 0 ]; then
c608c9
+		#not running as a result of container not being found
c608c9
+		return $OCF_NOT_RUNNING
c608c9
+	fi
c608c9
+
c608c9
+	if ocf_is_true "$val"; then
c608c9
+		# container exists and is running
c608c9
+		return $OCF_SUCCESS
c608c9
+	fi
c608c9
+
c608c9
+	return $OCF_NOT_RUNNING
c608c9
+}
c608c9
+
c608c9
+docker_start()
c608c9
+{
c608c9
+	local run_opts="-d --name=${CONTAINER}"
c608c9
+	# check to see if the container has already started
c608c9
+	docker_monitor
c608c9
+	if [ $? -eq $OCF_SUCCESS ]; then
c608c9
+		return $OCF_SUCCESS
c608c9
+	fi
c608c9
+
c608c9
+	if [ -n "$OCF_RESKEY_run_opts" ]; then
c608c9
+		run_opts="$run_opts $OCF_RESKEY_run_opts"
c608c9
+	fi
c608c9
+
c608c9
+	if [ $REQUIRE_IMAGE_PULL -eq 1 ]; then
c608c9
+		ocf_log notice "Beginning pull of image, ${OCF_RESKEY_image}"
c608c9
+		docker pull "${OCF_RESKEY_image}"
c608c9
+		if [ $? -ne 0 ]; then
c608c9
+			ocf_exit_reason "failed to pull image ${OCF_RESKEY_image}"
c608c9
+			return $OCF_ERR_GENERIC
c608c9
+		fi
c608c9
+	fi
c608c9
+
c608c9
+	if ocf_is_true "$OCF_RESKEY_reuse" && container_exists; then
c608c9
+		ocf_log info "starting existing container $CONTAINER."
c608c9
+		ocf_run docker start $CONTAINER
c608c9
+	else 
c608c9
+		ocf_log info "running container $CONTAINER for the first time"
c608c9
+		ocf_run docker run $run_opts $OCF_RESKEY_image $OCF_RESKEY_run_cmd
c608c9
+	fi
c608c9
+
c608c9
+	if [ $? -ne 0 ]; then
c608c9
+		ocf_exit_reason "docker failed to launch container"
c608c9
+		return $OCF_ERR_GENERIC
c608c9
+	fi
c608c9
+
c608c9
+	docker_monitor
c608c9
+	if [ $? -ne $OCF_SUCCESS ]; then
c608c9
+		ocf_exit_reason "Newly created docker container exited after start"
c608c9
+		return $OCF_ERR_GENERIC
c608c9
+	fi
c608c9
+
c608c9
+	return $OCF_SUCCESS
c608c9
+}
c608c9
+
c608c9
+docker_stop()
c608c9
+{
c608c9
+	local timeout=60
c608c9
+	docker_monitor
c608c9
+	if [ $? -eq  $OCF_NOT_RUNNING ]; then
c608c9
+		return $OCF_SUCCESS
c608c9
+	fi
c608c9
+
c608c9
+	if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
c608c9
+		timeout=$((($OCF_RESKEY_CRM_meta_timeout/1000) -10 ))
c608c9
+		if [ $timeout -lt 10 ]; then
c608c9
+			timeout=10
c608c9
+		fi
c608c9
+	fi
c608c9
+
c608c9
+	if ocf_is_true "$OCF_RESKEY_force_kill"; then
c608c9
+		ocf_run docker kill $CONTAINER
c608c9
+	else 	
c608c9
+		ocf_log debug "waiting $timeout second[s] before killing container"
c608c9
+		ocf_run docker stop -t=$timeout $CONTAINER
c608c9
+	fi
c608c9
+
c608c9
+	if [ $? -ne 0 ]; then
c608c9
+		ocf_exit_reason "Failed to stop container, ${CONTAINER}, based on image, ${OCF_RESKEY_image}."
c608c9
+		return $OCF_ERR_GENERIC
c608c9
+	fi
c608c9
+
c608c9
+	remove_container
c608c9
+	if [ $? -ne 0 ]; then
c608c9
+		ocf_exit_reason "Failed to remove stopped container, ${CONTAINER}, based on image, ${OCF_RESKEY_image}."
c608c9
+		return $OCF_ERR_GENERIC
c608c9
+	fi
c608c9
+
c608c9
+	return $OCF_SUCCESS
c608c9
+}
c608c9
+
c608c9
+image_exists()
c608c9
+{
c608c9
+	local res=1
c608c9
+
c608c9
+
c608c9
+	echo "${OCF_RESKEY_image}" | grep -q ":"
c608c9
+	if [ $? -eq 0 ]; then
c608c9
+		docker images | awk '{print $1 ":" $2}' |  grep "^${OCF_RESKEY_image}\$" > /dev/null 2>&1
c608c9
+	else
c608c9
+		docker images | awk '{print $1}' | grep "^${OCF_RESKEY_image}\$" > /dev/null 2>&1
c608c9
+	fi
c608c9
+	if [ $? -eq 0 ]; then
c608c9
+		return 0
c608c9
+	fi
c608c9
+	if ocf_is_true "$OCF_RESKEY_allow_pull"; then
c608c9
+		REQUIRE_IMAGE_PULL=1
c608c9
+		ocf_log notice "Image (${OCF_RESKEY_image}) does not exist locally but will be pulled during start"
c608c9
+		return 0
c608c9
+	fi 
c608c9
+	# image not found.
c608c9
+	return 1
c608c9
+}
c608c9
+
c608c9
+docker_validate()
c608c9
+{
c608c9
+	check_binary docker
c608c9
+	if [ -z "$OCF_RESKEY_image" ]; then
c608c9
+		ocf_exit_reason "'image' option is required"
c608c9
+		exit $OCF_ERR_CONFIGURED
c608c9
+	fi 
c608c9
+
c608c9
+	image_exists
c608c9
+	if [ $? -ne 0 ]; then
c608c9
+		ocf_exit_reason "base image, ${OCF_RESKEY_image}, could not be found."
c608c9
+		exit $OCF_ERR_CONFIGURED
c608c9
+	fi
c608c9
+
c608c9
+	return $OCF_SUCCESS
c608c9
+}
c608c9
+
c608c9
+: ${OCF_RESKEY_container=${OCF_RESOURCE_INSTANCE}}
c608c9
+CONTAINER=$OCF_RESKEY_container
c608c9
+
c608c9
+case $__OCF_ACTION in
c608c9
+meta-data) meta_data
c608c9
+		exit $OCF_SUCCESS;;
c608c9
+start)
c608c9
+	docker_validate
c608c9
+	docker_start;;
c608c9
+stop)		docker_stop;;
c608c9
+monitor)	docker_monitor;;
c608c9
+validate-all)	docker_validate;;
c608c9
+usage|help)	docker_usage
c608c9
+		exit $OCF_SUCCESS
c608c9
+		;;
c608c9
+*)		docker_usage
c608c9
+		exit $OCF_ERR_UNIMPLEMENTED
c608c9
+		;;
c608c9
+esac
c608c9
+rc=$?
c608c9
+ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
c608c9
+exit $rc
c608c9
+
c608c9
-- 
c608c9
1.8.4.2
c608c9