sh [Pseduo manual-HA script]用作将资源池从一台机器移动到另一台机器的临时解决方法的工具#ha #racktop #brickstor

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh [Pseduo manual-HA script]用作将资源池从一台机器移动到另一台机器的临时解决方法的工具#ha #racktop #brickstor相关的知识,希望对你有一定的参考价值。

#!/bin/sh
#
# Copyright 2017 RackTop Systems Inc. and/or its affiliates.
# http://www.racktopsystems.com
#
# The contents of this file are subject to the terms of the RackTop Commercial
# License ("RTCL"). You may not use, redistribute, or modify this file in source
# or binary forms except in compliance with the RTCL. You can obtain a copy at
# http://racktopsystems.com/legal/rtcl.txt.
#

DEBUG=0 # Enable debugging by setting > 0

# Assumptions:
# The other node is dead or otherwise disabled
# There are no active IPs or pools on the other node
# Manual cleanup of the other node may be necessary
# The pool being imported here is *NOT* already imported on the other node
# The pool may not have been correctly exported, requiring `-f` flag
# Aggregated interface aggr0 exists and is what vnics are created over


# Cleanup on remote end, if necessary:
# export pool if not exported with `zpool export <poolname>`
# Identify interfaces over which IP(s) is/are assigned with
# `ipadm show-addr`, where the last part will be /v4, and should be ignored
# and name will always begin with `havnic`
# destroy each IP interface with `ipadm delete-if <havnicX>`, where X is
# a number of the IP interface
# Destroy actual vic instance with `dladm delete-vnic <havnicX>`
# As soon as IP interfaces and vnics are gone, you are ready to run this script

[ ${DEBUG} -gt 0 ] && set -o xtrace

DLCMD=/usr/sbin/dladm
IPCMD=/usr/sbin/ipadm
PUBLIC_LINK=aggr0
ENOPUBLINK="Aggregate link ${PUBLIC_LINK} does not exist; cannot continue."
ENOARGS="Not enough arguments; expecting 3: <poolname> <mac addr> <CIDR>."
HAVNIC_NAME=
HAVNIC_MAC=
POOL_NAME=
SVC_IPADDR=
CMDLINE_ARGS=($@)


function print_err {
  printf "[ERROR]: %s\n" "$@" >&2
}

function split_input_into_fields {
  [ ${DEBUG} -gt 0 ] && set -o xtrace
  input=
  # We are assuming here that input arguments are in the following order:
  # 1) pool name
  # 2) mac address
  # 3) Service IP address
  if [[ ${#CMDLINE_ARGS} -lt 3 ]]; then
    print_err "${ENOARGS}"
    return 1
  fi
  
  # Set global variables to values extracted from command line arguments
  # passed to the script and then ask user to confirm that input is as
  # they intended it.
  POOL_NAME=${CMDLINE_ARGS[0]}
  HAVNIC_MAC=${CMDLINE_ARGS[1]}
  SVC_IPADDR=${CMDLINE_ARGS[2]}

  while true; do
    clear
    echo "*********************************************************************"
    echo "*** Please confirm the following is correct by typing 'Y' or 'N'. ***"
    echo "*** Type 'q' to quit. ***********************************************"
    echo "*********************************************************************"
    echo
    printf \
      "Pool: ${POOL_NAME} || MAC ADDR: ${HAVNIC_MAC} || IP ADDR: ${SVC_IPADDR}\n"
    
    # Consume input and keep repeating question until a 
    # suitable answer is provided.
    echo
    printf "Does this look right? "
    read -r input
    
    case ${input} in
    "y"|"Y") return 0
      ;;
    "n"|"N")
      printf "Please re-issue command with corrected arguments.\n"
      return 1
      ;;
    "q"|"Q")
      printf "You chose to quit!\n" ; exit 0
      ;;
    *) 
      continue
      ;;
    esac
  done
}

function create_vnic_over_aggr0 {
  [ ${DEBUG} -gt 0 ] && set -o xtrace
  typeset -A vnics
  vnic_index=0
  vnic_macaddr=$1
  if ! ${DLCMD} show-aggr ${PUBLIC_LINK} >/dev/null 2>&1 ; then 
    print_err "${ENOPUBLINK}"
    return 1
  fi

  # Obtain list of all vnics already configured on the system, then
  # `for loop over` the list and add each to an associative array setting
  # value of the map to 1. The goal is to then check this array against
  # havnic${vnic_index}, and as long as match is found, increment `vnic_index`
  # by 1. Eventually we will reach a point where no match is found and that
  # will be the name of the vnic with which we proceed.
  known_vnics=(`${DLCMD} show-vnic -p -olink | /usr/bin/awk '/havnic/'`)
  for vn in ${known_vnics[@]}; do
    vnics[$vn]=1
  done

  # Loop over already existing vnics and use the next available one.
  # This could be more efficient, but hash table-like lookup is fine here.
  while [ vnics[havnic${vnic_index}] -eq 1 ] ; do
    ((vnic_index++))
  done

  ${DLCMD} create-vnic \
    -t \
    -l ${PUBLIC_LINK} \
    -m ${vnic_macaddr} \
    havnic${vnic_index}
  HAVNIC_NAME=havnic${vnic_index}
}

#
# setup_ipaddress: Generic function responsible for setting-up
# IP Interface and address.
#
function setup_ipaddress {
  [ ${DEBUG} -gt 0 ] && set -o xtrace
  link_name=$1
  # skip_createif=0
  #
  # If we enhance this function to deal with existing vnics, we may need this.
  #
  # if ${IPCMD} show-if ${link_name} >/dev/null 2>&1 ; then
  #   skip_createif=1
  # fi
  # 
  # if ${IPCMD} show-addr "${link_name}/v4" >/dev/null 2>&1 ; then
  #   print_err \
  #     "IP Interface ${link_name}/v4 already exists. Skipping Create."
  # fi
  ${IPCMD} create-if -t ${link_name} || {
    print_err "Failed to create interface." ; return 1
  }
  if ! ${IPCMD} create-addr \
    -t \
    -T static \
    -a local=${SVC_IPADDR} \
    "${link_name}/v4"; then return 1
  fi
}

function rollback_ip_interface {
  [ ${DEBUG} -gt 0 ] && set -o xtrace
  if ! ${IPCMD} delete-if ${HAVNIC_NAME} >/dev/null 2>&1; then
    print_err "Failed to delete IP interface. VNIC will not be dropped."
    return 1
  fi
}

function rollback_vnic {
  [ ${DEBUG} -gt 0 ] && set -o xtrace
  if ! ${DLCMD} delete-vnic ${HAVNIC_NAME} >/dev/null 2>&1; then
    print_err "Failed to delete VNIC. VNIC will not be dropped."
    return 1
  fi
}

function import_pool {
  while true; do
    clear
    echo "*********************************************************************"
    echo "*** Please confirm pool is safe to import by typing 'Y' or 'N'. *****"
    echo "*** Type 'q' to quit. ***********************************************"
    echo "*********************************************************************"
    echo
    echo
    printf "Ready to *FORCE* import pool ${POOL_NAME}? "
    read -r input
    
    case ${input} in
    "y"|"Y")
      if ! /usr/sbin/zpool import -ff ${POOL_NAME} ; then
        print_err \
          "Failed to import pool ${POOL_NAME}. Please contact RackTop Support!"
        return 1
      else
        return 0
      fi
      ;;
    "n"|"N")
      printf "Breaking out.\n"
      return 1
      ;;
    "q"|"Q")
      printf "You chose to quit!\n" ; exit 0
      ;;
    *) 
      continue
      ;;
    esac
  done
}

# Main Body
split_input_into_fields || exit 1
create_vnic_over_aggr0 ${HAVNIC_MAC} || exit 1
setup_ipaddress ${HAVNIC_NAME} || exit 1

if ! import_pool ; then 
  rollback_ip_interface && rollback_vnic
else
  printf "\n\nImported pool ${POOL_NAME} and configured network.\n"
fi
exit $?

以上是关于sh [Pseduo manual-HA script]用作将资源池从一台机器移动到另一台机器的临时解决方法的工具#ha #racktop #brickstor的主要内容,如果未能解决你的问题,请参考以下文章

Redis 主从 keepalived高可用 实现 VIP 自动漂移

SCRY 受邀参加 Blockchain Connect Conference

SCRY: Blockchain technique and Japanese industrial economy

Cooperation on Blockchain Technology between SCRY and CSDN.

shell脚本定时后台执行

How to Catch Ctrl-C in Shell Script