From e5a0c176d51a772f82ce4391754e2cb8173024f0 Mon Sep 17 00:00:00 2001 From: imdjh <20123225@student.shiep.edu.cn> Date: Thu, 8 Oct 2015 14:44:38 +0800 Subject: [PATCH 1/5] Simplify function of checking OS init system Renamed this function to check_initsystem for better explanation via its own name. Remove useless logic. --- hyper-bootstrap.sh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/hyper-bootstrap.sh b/hyper-bootstrap.sh index b1f743f..e399d1f 100755 --- a/hyper-bootstrap.sh +++ b/hyper-bootstrap.sh @@ -100,7 +100,7 @@ check_deps() { check_deps_platform check_deps_distro check_deps_qemu || check_deps_xen || exit ${${ERR_NO_HYPERVISOR[0]}} - check_deps_initsystem + check_initsystem show_message done " Done" } check_deps_platform() { @@ -222,10 +222,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" From cbbf1d5e1ac3d8e8d678f7b880a23e0b22fea493 Mon Sep 17 00:00:00 2001 From: imdjh <20123225@student.shiep.edu.cn> Date: Thu, 8 Oct 2015 15:04:55 +0800 Subject: [PATCH 2/5] Remove redundancy logic on Mint distro checking. --- hyper-bootstrap.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hyper-bootstrap.sh b/hyper-bootstrap.sh index e399d1f..660e2c7 100755 --- a/hyper-bootstrap.sh +++ b/hyper-bootstrap.sh @@ -144,9 +144,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]} From 563159b2852d3fe38b6d786c79b767edb0c1584f Mon Sep 17 00:00:00 2001 From: Jiahao Dai <20123225@student.shiep.edu.cn> Date: Thu, 8 Oct 2015 18:37:22 +0800 Subject: [PATCH 3/5] Fix #2, tput would work without $TERM environ --- hyper-bootstrap.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/hyper-bootstrap.sh b/hyper-bootstrap.sh index 660e2c7..3395cf5 100755 --- a/hyper-bootstrap.sh +++ b/hyper-bootstrap.sh @@ -25,6 +25,7 @@ DEBIAN_CODE=(jessie wheezy) CENTOS_VER=(6 7) FEDORA_VER=(20 21 22) #Color Constant +[ ! -z ${TERM} ] || TERM="xterm" # tput would complain without $TERM environ RED=`tput setaf 1` GREEN=`tput setaf 2` YELLOW=`tput setaf 3` From 0aac726b76be53426cc17713d543fe6b9bfb7a9e Mon Sep 17 00:00:00 2001 From: Jiahao Dai <20123225@student.shiep.edu.cn> Date: Thu, 8 Oct 2015 21:09:19 +0800 Subject: [PATCH 4/5] show_message error can exit itself, add show_message fatal, fix typo I have notice that `show_message error` has always comes with an exit, so I merge the exit func into show_message. You can use it with `show_message error ${ERR_TWO_ELEMENT_ARRAY[@]}` I have noticed that the original implement on display_message always comesafter an error message. Since it's not the case user can easily solve themself, why not implement a fatal message and it would give the (almost) same result. You can use it with `show_message fatal ${ERR_EXIT_CODE}` Also fixed some typo and missleading message in this commit. Install from remote -> Start fetching hyper package Installing -> Installing hyper and hyperd Since __done__ is a reserve keyword in bash, I personally recommand remove it(as I did). --- hyper-bootstrap.sh | 68 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/hyper-bootstrap.sh b/hyper-bootstrap.sh index 3395cf5..999a68f 100755 --- a/hyper-bootstrap.sh +++ b/hyper-bootstrap.sh @@ -61,7 +61,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 @@ -90,7 +90,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" @@ -102,12 +102,12 @@ check_deps() { check_deps_distro check_deps_qemu || check_deps_xen || exit ${${ERR_NO_HYPERVISOR[0]}} check_initsystem - show_message done " Done" + 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 "." } @@ -157,8 +157,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) @@ -168,14 +168,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 "." @@ -275,30 +274,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 @@ -309,12 +308,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="" @@ -336,9 +333,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() { @@ -384,12 +379,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 } @@ -405,11 +394,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 } ################# From d5c211ec83691b0b1f3fdaddf932e7566ba66006 Mon Sep 17 00:00:00 2001 From: Jiahao Dai <20123225@student.shiep.edu.cn> Date: Fri, 9 Oct 2015 12:15:33 +0800 Subject: [PATCH 5/5] Better solution on #4 --- hyper-bootstrap.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hyper-bootstrap.sh b/hyper-bootstrap.sh index 999a68f..e8085e5 100755 --- a/hyper-bootstrap.sh +++ b/hyper-bootstrap.sh @@ -25,7 +25,15 @@ DEBIAN_CODE=(jessie wheezy) CENTOS_VER=(6 7) FEDORA_VER=(20 21 22) #Color Constant -[ ! -z ${TERM} ] || TERM="xterm" # tput would complain without $TERM environ +# reset virables +RED='' +GREEN='' +YELLOW='' +BLUE='' +WHITE='' +LIGHT='' +RESET='' +[ -z ${TERM} ] || { RED=`tput setaf 1` GREEN=`tput setaf 2` YELLOW=`tput setaf 3` @@ -33,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!")