Advertisement
Guest User

IPv6addrLO

a guest
Oct 28th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.27 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. #       OCF Resource Agent compliant resource script.
  4. #   Arturo Borrero <aborrero@cica.es> || October 2011
  5. #
  6. #   Based on the anything RA.
  7. #
  8. #   GPLv3 Licensed. You can read the license in
  9. #   http://www.gnu.org/licenses/gpl-3.0.html
  10. #
  11. # Initialization:
  12.  
  13. : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
  14. . ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
  15.  
  16. # Custom vars:
  17. IFCONFIG_BIN="/sbin/ifconfig"
  18. GREP_BIN="grep"
  19. IFACE="lo"
  20. process=$OCF_RESOURCE_INSTANCE
  21. ipv6addr=$OCF_RESKEY_ipv6addr
  22. cidr_netmask=$OCF_RESKEY_cidr_netmask
  23. pidfile=$OCF_RESKEY_pidfile ; [ -z "$pidfile" ] && pidfile=${HA_VARRUN}IPv6addrLO_${process}.pid
  24. logfile=$OCF_RESKEY_logfile ; [ -z "$logfile" ] && logfile="/var/log/syslog"
  25. errlogfile=$OCF_RESKEY_errlogfile ; [ -z "$errlogfile" ] && errlogfile="/var/log/syslog"
  26.  
  27.  
  28. validate_ipv6(){
  29.     ocf_log debug "Validating IPv6 addr: [\"$1\"]."
  30.  
  31.     echo "$1" | $GREP_BIN -E "^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))(%.+)?\s*$" > /dev/null
  32.     if [ $? -eq 0 ]
  33.     then
  34.         # the ipv6 is valid
  35.         ocf_log debug "IPv6 addr: [\"$1\"] is valid."
  36.         return 1
  37.     fi
  38.     # the ipv6 is invalid
  39.     ocf_log err "IPv6 addr: [\"$1\"] is not valid."
  40.     return 0
  41. }
  42. validate_cidr(){
  43.     ocf_log debug "Validating cidr: \"$1\"."
  44.  
  45.     if [ $1 -lt 129 ]
  46.     then
  47.         if [ $1 -gt 0 ]
  48.         then
  49.             # the cidr is valid
  50.             ocf_log debug "Cidr: \"$1\" is valid."
  51.             return 1
  52.         fi
  53.     fi
  54.     ocf_log err "Cidr: \"$1\" is not valid."
  55.     return 0
  56. }
  57.  
  58. iface_has_ipv6()
  59. {
  60.     ocf_log debug "Checking if iface \"$IFACE\" has the ipv6 [\"$ipv6addr\"]."
  61.     if [ ! -z $1 ]
  62.     then
  63.         $IFCONFIG_BIN $IFACE | $GREP_BIN $1 2> /dev/null > /dev/null
  64.         if [ $? -eq 0 ]
  65.         then
  66.             # the iface has the IPv6
  67.             ocf_log info "The iface \"$IFACE\" has the ipv6 [\"$ipv6addr\"]."
  68.             return 1
  69.         fi
  70.         ocf_log info "The iface \"$IFACE\" does not have the ipv6 [\"$ipv6addr\"]."
  71.     fi
  72.     return 0
  73. }
  74.  
  75. IPv6addrLO_status() {
  76.     # Will check that the system has the ipv6 saved in the pidfile
  77.     if [ -r $pidfile ]
  78.     then
  79.         ocf_log debug "STATUS: The pidfile \"$pidfile\" exists."
  80.  
  81.         validate_ipv6 `cat $pidfile | awk -F'/' '{print $1}'`
  82.         if [ $? -eq 1 ]
  83.         then
  84.             # the ipv6 stored in pidfile is valid, then check if the system has that ip
  85.             iface_has_ipv6 `cat $pidfile`
  86.             if [ $? -eq 1 ]
  87.             then
  88.                 ocf_log info "The iface \"$IFACE\" has the IPv6 \"[`cat $pidfile`]\" stored in \"$pidfile\"."
  89.                 return $OCF_RUNNING
  90.             else
  91.                 ocf_log err "When checking status, the iface \"$IFACE\" has nor the IPv6 of the \"$pidfile\" nor \"$ipv6addr\"."
  92.                 return $OCF_ERR_GENERIC
  93.             fi
  94.         else
  95.             ocf_log err "The ipv6addr in \"$pidfile\" is not valid: [\"`cat $pidfile`\"]."
  96.             return $OCF_ERR_GENERIC
  97.         fi
  98.     fi
  99.     ocf_log debug  "The pidfile \"$pidfile\" don't exists."
  100.     return $OCF_NOT_RUNNING
  101. }
  102.  
  103. IPv6addrLO_start() {
  104.     if ! IPv6addrLO_status
  105.     then
  106.         # First, validate the input parameteres, ipv6addr and cidr_netmaks
  107.         validate_ipv6 $ipv6addr
  108.         if [ $? -ne 1 ]
  109.         then
  110.             ocf_log err "$process: The ipv6 addr: \"$ipv6addr\" is not a valid one."
  111.             return $OCF_ERR_GENERIC
  112.         fi
  113.         validate_cidr $cidr_netmask
  114.         if [ $? -ne 1 ]
  115.         then
  116.             ocf_log err "$process: The cidr netmask \"$cidr_netmask\" is not valid."
  117.             return $OCF_ERR_GENERIC
  118.         fi
  119.  
  120.  
  121.         # Before assign the ip, check if we already have that ip
  122.         # because maybe we had a sudden reboot and the ipv6 is still on lo.
  123.         iface_has_ipv6 $ipv6addr
  124.         if [ $? -eq 1 ]
  125.         then
  126.             # we have the IPv6addr on loopback
  127.             ocf_log info "The iface \"$IFACE\" had the IPv6 addr [\"$ipv6addr/$cidr_netmask\"], don't assigning again."
  128.             touch $pidfile
  129.             if [ $? -ne 0 ]
  130.             then
  131.                 ocf_log war "Could not create the pidfile \"$pidfile\"."
  132.             fi
  133.             echo "$ipv6addr/$cidr_netmask" > $pidfile
  134.             if [ $? -ne 0 ]
  135.             then
  136.                 ocf_log err "Failed to manage the new pidfile for \"$ipv6addr/\$cidr_netmask\"."
  137.             fi
  138.         else
  139.             # we don't have the IPv6addr on loopback
  140.             ocf_log info "Starting $process"
  141.  
  142.             # Doing different depending on what logfile we have.
  143.             if [ -n "$logfile" -a -n "$errlogfile" ]
  144.             then
  145.                 # We have logfile and errlogfile, so redirect STDOUT und STDERR to different files
  146.                 $IFCONFIG_BIN $IFACE add $ipv6addr/$cidr_netmask >> $logfile 2>> $errlogfile
  147.             else
  148.                 if [ -n "$logfile" ]
  149.                 then
  150.                     # We only have logfile so redirect STDOUT and STDERR to the same file
  151.                     $IFCONFIG_BIN $IFACE add $ipv6addr/$cidr_netmask >> $logfile 2>&1
  152.                 else
  153.                     # We have neither logfile nor errlogfile, so we're not going to redirect anything
  154.                     $IFCONFIG_BIN $IFACE add $ipv6addr/$cidr_netmask
  155.                 fi
  156.             fi
  157.             echo "$ipv6addr/$cidr_netmask" > $pidfile
  158.         fi
  159.  
  160.         # Check what happened here.
  161.         if IPv6addrLO_status
  162.         then
  163.             ocf_log info "$process: Started successfully."
  164.             return $OCF_SUCCESS
  165.         else
  166.             ocf_log err "$process: Could not be started: ipv6addr[\"$ipv6addr\"] cidr_netmask[\"$cidr_netmask\"]."
  167.             return $OCF_ERR_GENERIC
  168.         fi
  169.     else
  170.         # If already running, consider start successful
  171.         ocf_log debug "$process: is already running"
  172.         return $OCF_SUCCESS
  173.     fi
  174. }
  175.  
  176. IPv6addrLO_stop() {
  177.  
  178.     ocf_log debug "$process: Running STOP function."
  179.  
  180.         if [ -n "$OCF_RESKEY_stop_timeout" ]
  181.         then
  182.                 stop_timeout=$OCF_RESKEY_stop_timeout
  183.         elif [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
  184.                 # Allow 2/3 of the action timeout for the orderly shutdown
  185.                 # (The origin unit is ms, hence the conversion)
  186.                 stop_timeout=$((OCF_RESKEY_CRM_meta_timeout/1500))
  187.         else
  188.                 stop_timeout=10
  189.         fi
  190.     if IPv6addrLO_status
  191.     then
  192.         $IFCONFIG_BIN $IFACE del `cat $pidfile`
  193.                 i=0
  194.                 while [ $i -lt $stop_timeout ]
  195.                 do
  196.                         if ! IPv6addrLO_status
  197.                         then
  198.                             rm -f $pidfile
  199.                                 return $OCF_SUCCESS
  200.                         fi
  201.                         sleep 1
  202.                         i=`expr $i + 1`
  203.                 done
  204.                 ocf_log warn "Stop failed. Trying again."
  205.                 $IFCONFIG_BIN $IFACE del `cat $pidfile`
  206.                 rm -f $pidfile
  207.                 if ! IPv6addrLO_status
  208.                 then
  209.                         ocf_log warn "Stop success."
  210.                         return $OCF_SUCCESS
  211.                 else
  212.                         ocf_log err "Failed to stop."
  213.                         return $OCF_ERR_GENERIC
  214.                 fi
  215.     else
  216.         # was not running, so stop can be considered successful
  217.         $ICONFIG_BIN $IFACE del `cat $pidfile`
  218.         rm -f $pidfile
  219.         return $OCF_SUCCESS
  220.     fi
  221. }
  222.  
  223. IPv6addrLO_monitor() {
  224.     IPv6addrLO_status
  225.     ret=$?
  226.     if [ $ret -eq $OCF_SUCCESS ]
  227.     then
  228.         if [ -n "$OCF_RESKEY_monitor_hook" ]; then
  229.             eval "$OCF_RESKEY_monitor_hook"
  230.                         if [ $? -ne $OCF_SUCCESS ]; then
  231.                                 return ${OCF_ERR_GENERIC}
  232.                         fi
  233.             return $OCF_SUCCESS
  234.         else
  235.             true
  236.         fi
  237.     else
  238.         return $ret
  239.     fi
  240. }
  241.  
  242.  
  243. IPv6addrLO_validate() {
  244.  
  245.     ocf_log debug "IPv6addrLO validating: args:[\"$*\"]"
  246.  
  247.     if [ -x $IFCONFIG_BIN ]
  248.     then
  249.         ocf_log debug "Binary \"$IFCONFIG_BIN\" exist and is executable."
  250.         return $OCF_SUCCESS
  251.     else
  252.         ocf_log err "Binary \"$IFCONFIG_BIN\" does not exist or isn't executable."
  253.         return $OCF_ERR_INSTALLED
  254.     fi
  255.     ocf_log err "Error while validating."
  256.     return $OCF_ERR_GENERIC
  257. }
  258.  
  259. IPv6addrLO_meta(){
  260. cat <<END
  261. <?xml version="1.0"?>
  262. <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
  263. <resource-agent name="IPv6addrLO">
  264. <version>0.1</version>
  265. <longdesc lang="en">
  266. OCF RA to manage IPv6addr on loopback interface Linux
  267. </longdesc>
  268. <shortdesc lang="en">IPv6 addr on loopback linux</shortdesc>
  269.  
  270. <parameters>
  271. <parameter name="ipv6addr" required="1">
  272. <longdesc lang="en">
  273. The ipv6 addr to asign to the loopback interface.
  274. </longdesc>
  275. <shortdesc lang="en">Ipv6 addr to the loopback interface.</shortdesc>
  276. <content type="string" default=""/>
  277. </parameter>
  278. <parameter name="cidr_netmask" required="1">
  279. <longdesc lang="en">
  280. The cidr netmask of the ipv6 addr.
  281. </longdesc>
  282. <shortdesc lang="en">netmask of the ipv6 addr.</shortdesc>
  283. <content type="string" default="128"/>
  284. </parameter>
  285. <parameter name="logfile" required="0">
  286. <longdesc lang="en">
  287. File to write STDOUT to
  288. </longdesc>
  289. <shortdesc lang="en">File to write STDOUT to</shortdesc>
  290. <content type="string" />
  291. </parameter>
  292. <parameter name="errlogfile" required="0">
  293. <longdesc lang="en">
  294. File to write STDERR to
  295. </longdesc>
  296. <shortdesc lang="en">File to write STDERR to</shortdesc>
  297. <content type="string" />
  298. </parameter>
  299. </parameters>
  300. <actions>
  301. <action name="start"   timeout="20s" />
  302. <action name="stop"    timeout="20s" />
  303. <action name="monitor" depth="0"  timeout="20s" interval="10" />
  304. <action name="meta-data"  timeout="5" />
  305. <action name="validate-all"  timeout="5" />
  306. </actions>
  307. </resource-agent>
  308. END
  309. exit 0
  310. }
  311.  
  312. case "$1" in
  313.     meta-data|metadata|meta_data|meta)
  314.         IPv6addrLO_meta
  315.     ;;
  316.     start)
  317.         IPv6addrLO_start
  318.     ;;
  319.     stop)
  320.         IPv6addrLO_stop
  321.     ;;
  322.     monitor)
  323.         IPv6addrLO_monitor
  324.     ;;
  325.     validate-all)
  326.         IPv6addrLO_validate
  327.     ;;
  328.     *)
  329.         ocf_log err "$0 was called with unsupported arguments:"
  330.         exit $OCF_ERR_UNIMPLEMENTED
  331.     ;;
  332. esac
  333.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement