4 资源代理结构

一个典型的(基于shell的)资源代理有一些标准的结构元素。本章将按顺序说明。在说明里将描述资源代理每一个支持的行为的细节内容。本章将以一个假想的foobar函数为例子。


4.1 资源代理解释器


每个资源代理都以标准的“shebang”#!)抬头


1

#!/bin/sh


如果资源代理使用shell编写,最好是指定通用的shell解释器(#!/bin/sh),但也不强求。使用/bin/sh兼容的资源代理一定不要使用某种特别shell的方言(如bash中的 ${!variable}),建议对资源代理使用检查器检查一下,如checkbashisms


有人考虑使用一个patch来使得之前的sh兼容的资源代理只用于bashksh或者其他的非通用shell。然而,对于新完成的资源代理,最好显式的定义一个指定的shell解释器,比如/bin/bash


4.2 作者和授权信息


资源代理应该包含一段注释,来说明作者和版权信息,并说明资源代理的授权信息,如:


1

2

3

4

5

6

#

#   Resource Agent for managing  foobar resources.

#

#    License:      GNU General Public License (GPL)

#   (c) 2008-2010 John Doe,  Jane Roe,

#                  and Linux-HA contributors


如果资源代理的使用的授权有不同版本,缺省可假设是当前版本。


4.3 初始化


任何 shell 资源代理都应该运行.ocf_shellfuncs函数库。按下面的语法,如$OCF_FUNCTION_DIR,这里是为了测试和写文档,这个变量也许重复定义了。


1

2

3

# Initialization:

:  ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}

. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs


4.4  资源行为的函数实现


下面将说明的是资源代理公布的行为的函数实现。每个行为的细节将在第五章描述。


4.5 执行块


下面是一个资源代理实际执行时的部分代码。通常遵循如下的标准结构:


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

# Make sure meta-data and usage always  succeed

case $__OCF_ACTION in

meta-data)       foobar_meta_data

               exit $OCF_SUCCESS

               ;;

usage|help)      foobar_usage

               exit $OCF_SUCCESS

               ;;

esac


# Anything other than meta-data and usage  must pass validation

foobar_validate_all || exit $?


# Translate each action into the  appropriate function call

case $__OCF_ACTION in

start)           foobar_start;;

stop)            foobar_stop;;

status|monitor) foobar_monitor;;

promote)         foobar_promote;;

demote)          foobar_demote;;

reload)          ocf_log info "Reloading..."

               foobar_start

               ;;

validate-all)   ;;

*)               foobar_usage

               exit $OCF_ERR_UNIMPLEMENTED

               ;;

esac

rc=$?


# The resource agent may optionally log a  debug message

ocf_log debug "${OCF_RESOURCE_INSTANCE}  $__OCF_ACTION returned $rc"

exit $rc