diff --git a/hyper-bootstrap.sh b/hyper-bootstrap.sh index b1f743f..e8085e5 100755 --- a/hyper-bootstrap.sh +++ b/hyper-bootstrap.sh @@ -25,6 +25,15 @@ DEBIAN_CODE=(jessie wheezy) CENTOS_VER=(6 7) FEDORA_VER=(20 21 22) #Color Constant +# reset virables +RED='' +GREEN='' +YELLOW='' +BLUE='' +WHITE='' +LIGHT='' +RESET='' +[ -z ${TERM} ] || { RED=`tput setaf 1` GREEN=`tput setaf 2` YELLOW=`tput setaf 3` @@ -32,6 +41,7 @@ BLUE=`tput setaf 4` WHITE=`tput setaf 7` LIGHT=`tput bold ` RESET=`tput sgr0` +} #Error Message ERR_ROOT_PRIVILEGE_REQUIRED=(10 "This install script need root privilege, please retry use 'sudo' or root user!") ERR_NOT_SUPPORT_PLATFORM=(20 "Sorry, Hyper only support x86_64 platform!") @@ -60,7 +70,7 @@ main() { show_message debug "Install from local ${BASE_DIR}/" BOOTSTRAP_DIR="${BASE_DIR}" else - show_message debug "Install from remote" + show_message info "Start fetching hyper package" fetch_hyper_package fi stop_running_hyperd @@ -89,7 +99,7 @@ check_user() { if (command_exist sudo);then BASH_C="sudo -E bash -c" else - show_message error "${ERR_ROOT_PRIVILEGE_REQUIRED[1]}" && exit ${ERR_ROOT_PRIVILEGE_REQUIRED[0]} + show_message error "${ERR_ROOT_PRIVILEGE_REQUIRED[@]}" fi show_message info "${WHITE}Hint: Hyper installer need root privilege\n" ${BASH_C} "echo -n" @@ -100,13 +110,13 @@ check_deps() { check_deps_platform check_deps_distro check_deps_qemu || check_deps_xen || exit ${${ERR_NO_HYPERVISOR[0]}} - check_deps_initsystem - show_message done " Done" + check_initsystem + show_message success " Done!" } check_deps_platform() { ARCH="$(uname -m)" if [ "${ARCH}" != "x86_64" ];then - show_message error "${ERR_NOT_SUPPORT_PLATFORM[1]}" && exit ${ERR_NOT_SUPPORT_PLATFORM[0]} + show_message error "${ERR_NOT_SUPPORT_PLATFORM[@]}" fi echo -n "." } @@ -144,9 +154,7 @@ check_deps_distro() { fi case "${LSB_DISTRO}" in linuxmint) - if [ "${LSB_DISTRO}" == "linuxmint" ] - then SUPPORT_CODE_LIST="${LINUX_MINT_CODE[@]}"; - fi + SUPPORT_CODE_LIST="${LINUX_MINT_CODE[@]}"; if (echo "${SUPPORT_CODE_LIST}" | grep -vqw "${LSB_CODE}");then show_message error "Hyper support ${LSB_DISTRO}( ${SUPPORT_CODE_LIST} ), but current is ${LSB_CODE}(${LSB_VER})" exit ${ERR_NOT_SUPPORT_DISTRO_VERSION[0]} @@ -158,8 +166,8 @@ check_deps_distro() { else SUPPORT_CODE_LIST="${DEBIAN_CODE[@]}"; fi if (echo "${SUPPORT_CODE_LIST}" | grep -vqw "${LSB_CODE}");then - show_message error "Hyper support ${LSB_DISTRO}( ${SUPPORT_CODE_LIST} ), but current is ${LSB_CODE}(${LSB_VER})" - exit ${ERR_NOT_SUPPORT_DISTRO_VERSION[0]} + error_list=(${ERR_NOT_SUPPORT_DISTRO_VERSION[0]} "Hyper support ${LSB_DISTRO}( ${SUPPORT_CODE_LIST} ), but current is ${LSB_CODE}(${LSB_VER})") + show_message error ${error_list} fi ;; centos|fedora) @@ -169,14 +177,13 @@ check_deps_distro() { else SUPPORT_VER_LIST="${FEDORA_VER[@]}"; fi if (echo "${SUPPORT_VER_LIST}" | grep -qvw "${CMAJOR}");then - show_message error "Hyper support ${LSB_DISTRO}( ${SUPPORT_VER_LIST} ), but current is ${LSB_VER}" - exit ${ERR_NOT_SUPPORT_DISTRO_VERSION[0]} + error_list=(${ERR_NOT_SUPPORT_DISTRO_VERSION[0]} "Hyper support ${LSB_DISTRO}( ${SUPPORT_VER_LIST} ), but current is ${LSB_VER}") + show_message error ${error_list} fi ;; *) if [ ! -z ${LSB_DISTRO} ];then echo -e -n "\nCurrent OS is '${LSB_DISTRO} ${LSB_VER}(${LSB_CODE})'"; else echo -e -n "\nCan not detect OS type"; fi - show_message error "${ERR_NOT_SUPPORT_DISTRO[1]}" - exit ${ERR_NOT_SUPPORT_DISTRO[0]} + show_message error "${ERR_NOT_SUPPORT_DISTRO[@]}" ;; esac echo -n "." @@ -222,10 +229,8 @@ check_deps_qemu() { #QEMU 2.0+ should be installed echo -n "." return 0 } -check_deps_initsystem() { - if [ "${LSB_DISTRO}" == "ubuntu" -a "${LSB_CODE}" == "utopic" ];then - INIT_SYSTEM="sysvinit" - elif (command_exist systemctl);then +check_initsystem() { + if (command_exist systemctl);then INIT_SYSTEM="systemd" else INIT_SYSTEM="sysvinit" @@ -278,30 +283,30 @@ fetch_hyper_package() { ${CURL_C} ${TGT_FILE} ${SRC_URL} fi if [ $? -ne 0 ];then - show_message error "${ERR_FETCH_INST_PKG_FAILED[1]}" && exit "${ERR_FETCH_INST_PKG_FAILED[0]}" + show_message error "${ERR_FETCH_INST_PKG_FAILED[@]}" else MD5_REMOTE=$(cat ${TGT_FILE}.md5 | awk '{print $1}'); MD5_LOCAL=$(md5sum ${TGT_FILE} | awk '{print $1}') if [ ${MD5_REMOTE} != ${MD5_LOCAL} ];then echo "required checksum: ${MD5_REMOTE}, but downloaded package is ${MD5_LOCAL}" - show_message error "${ERR_INST_PKG_MD5_ERROR[1]}" && exit "${ERR_INST_PKG_MD5_ERROR[0]}" + show_message error "${ERR_INST_PKG_MD5_ERROR[@]}" fi fi fi ${BASH_C} "cd ${BOOTSTRAP_DIR} && tar xzf ${PKG_FILE}" if [ $? -ne 0 ];then - show_message error "${ERR_UNTAR_PKG_FAILED[1]}" && exit "${ERR_UNTAR_PKG_FAILED[0]}" + show_message error "${ERR_UNTAR_PKG_FAILED[@]}" fi BOOTSTRAP_DIR="${BOOTSTRAP_DIR}/${UNTAR_DIR}" - show_message done " Done" + show_message success " success!" set -e } install_hyper() { - show_message info "Installing " + show_message info "Installing hyper and hyperd" set +e cd ${BOOTSTRAP_DIR} ${BASH_C} "./install.sh" 1>/dev/null if [ $? -ne 0 ];then - show_message error "${ERR_EXEC_INSTALL_FAILED[1]}" && exit "${ERR_EXEC_INSTALL_FAILED[0]}" + show_message error "${ERR_EXEC_INSTALL_FAILED[@]}" fi echo -n "." if [[ -f /usr/local/bin/hyper ]] && [[ -f /usr/local/bin/hyperd ]] && [[ ! -f /usr/bin/hyper ]] && [[ ! -f /usr/bin/hyperd ]] ;then @@ -312,12 +317,10 @@ install_hyper() { install_hyperd_service echo -n "." else - show_message error "${ERR_HYPER_NOT_FOUND[1]}" - display_support ${ERR_HYPER_NOT_FOUND[0]} - exit ${ERR_HYPER_NOT_FOUND[0]} + show_message fatal "${ERR_HYPER_NOT_FOUND[0]}" fi set -e - show_message done " Done" + show_message success " success!" } install_hyperd_service() { local SRC_INIT_FILE="" @@ -339,9 +342,7 @@ install_hyperd_service() { ${BASH_C} "cp ${SRC_INIT_FILE} ${TGT_INIT_FILE}" ${BASH_C} "chmod +x ${TGT_INIT_FILE}" else - show_message error "${ERR_INSTALL_SERVICE_FAILED[1]}" - display_support ${ERR_INSTALL_SERVICE_FAILED[1]} - exit ${ERR_INSTALL_SERVICE_FAILED[0]} + show_message fatal "${ERR_INSTALL_SERVICE_FAILED[0]}" fi } stop_running_hyperd() { @@ -387,12 +388,6 @@ COMMENT fi set -e } -display_support() { - echo "Sorry, we are suffering from some technical issue($1), please contact ${SUPPORT_EMAIL}" - if [ $# -eq 0 ];then exit ${ERR_UNKNOWN} - else exit $1 - fi -} command_exist() { type "$@" > /dev/null 2>&1 } @@ -408,11 +403,22 @@ get_curl() { } show_message() { case "$1" in - debug) echo -e "\n[${BLUE}DEBUG${RESET}] : $2";; - info) echo -e -n "\n${WHITE}$2${RESET}" ;; - warn) echo -e "\n[${YELLOW}WARN${RESET}] : $2" ;; - done|success) echo -e "${LIGHT}${GREEN}$2${RESET}" ;; - error|failed) echo -e "\n[${RED}ERROR${RESET}] : $2" ;; + debug) echo -e "\n [${BLUE}DEBUG${RESET}] : $2";; + info) echo -e -n "\n ${WHITE}$2${RESET}" ;; + warn) echo -e "\n [${YELLOW}WARN${RESET}] : $2" ;; + success) echo -e "${LIGHT}${GREEN}$2${RESET}" ;; + fatal) + echo -e "\n [${RED}FATAL${RESET}] : Sorry, we are suffering from some technical issue($2), please contact ${SUPPORT_EMAIL}" + exit $2 + ;; + error|failed) + shift # remove ``error'' from param + exit_code=$1 + shift + error_message=$@ + echo -e "\n [${RED}ERROR${RESET}] : ${error_message}" + exit ${exit_code} + ;; esac } #################