From aaff8e451636abf88977167107db8325a6d30ef2 Mon Sep 17 00:00:00 2001 From: Ondrej Dockal Date: Mon, 24 Jul 2017 16:31:33 +0200 Subject: [PATCH] Making script runnable on windows platform - add .gitattributes file - preserves LF on Windows - added setup flag (setup-cdk [params]) - home folder decisioning between shell ($HOME) vs cygwin ($USERPROFILE) - variable BASEFILE (minishift vs minishift.exe) Signed-off-by: Ondrej Dockal --- .gitattributes | 1 + scripts/cdk3-cleanup.sh | 49 +++++++++++++++--------------- scripts/cdk3-install.sh | 67 ++++++++++++++++++++++------------------- scripts/cdk3-stop.sh | 13 +++++--- scripts/script-utils.sh | 67 +++++++++++++++++++++++++++++++++-------- 5 files changed, 124 insertions(+), 73 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfdb8b7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sh text eol=lf diff --git a/scripts/cdk3-cleanup.sh b/scripts/cdk3-cleanup.sh index 7a540db..3c8b1fe 100755 --- a/scripts/cdk3-cleanup.sh +++ b/scripts/cdk3-cleanup.sh @@ -28,8 +28,6 @@ function usage { echo " path with minishift binary or directory where new minishift will be downloaded" echo " -u, --url (optional)" echo " CDK/minishift binary url to download" - echo " -h, --home" - echo " minishift home path, overrides MINISHIFT_HOME" echo " -e, --erase" echo " Will erase minishift binary, created folders and minishift home folder" echo " Use it on your own risk!" @@ -59,9 +57,11 @@ function check_minishift_bin() { MINISHIFT_PATH= MINISHIFT_URL= +USER_HOME= HOME_FOLDER= EXISTING=1 ERASE=0 +BASEFILE= # At least one parameter is required if [ $# -lt 1 ] @@ -78,6 +78,7 @@ while [ $# -gt 0 ]; do log_warning "${MINISHIFT_PATH} does not exist, will be created" EXISTING=0 elif [ -f ${1} ] && [ $(minishift_has_status "${MINISHIFT_PATH}") == 1 ]; then + BASEFILE=$(basename ${MINISHIFT_PATH}) log_info "Minishift binary ${MINISHIFT_PATH} will be used..." elif [ -d ${1} ]; then log_info "${MINISHIFT_PATH} is a folder..." @@ -86,25 +87,21 @@ while [ $# -gt 0 ]; do ;; -u | --url) shift - url_status=$(curl -Is -l ${1} | head -n 1 | grep -i ok) + url_status=$(http_status_ok ${1}) log_info "Trying to reach ${1}" log_info "URL status: $url_status" if [ "${url_status}" ]; then - MINISHIFT_URL="${1}" + if [ $(url_has_minishift ${1}) == "1" ]; then + MINISHIFT_URL="${1}" + else + MINISHIFT_URL="$(add_url_suffix ${1})" + fi + BASEFILE=$(basename ${MINISHIFT_URL}) else log_error "Given minishift url ${1} cannot be reached" exit 1 fi ;; - -h | --home) - shift - HOME_FOLDER=${1} - if [ ! -d ${1} ]; then - log_error "${HOME_FOLDER} does not exist..." - exit 1 - fi - export MINISHIFT_HOME=${HOME_FOLDER} - ;; -e | --erase) ERASE=1 log_warning "Erase mode is on!!!" @@ -116,6 +113,12 @@ while [ $# -gt 0 ]; do shift done +HOME_ADDR="${HOME}" +if [ "$(get_os_platform)" == "win" ]; then + HOME_ADDR="${USERPROFILE}" +fi +log_info "User's home folder is at ${HOME_ADDR}" + if [ -z $MINISHIFT_PATH ]; then log_error "--p or --path cannot be empty" usage @@ -126,8 +129,8 @@ fi # without minishift_home defined in previous jenkins job if [ -n "${MINISHIFT_HOME}" ]; then log_info "Minishift home is set with MINISHIFT_HOME env. var., ${MINISHIFT_HOME}" -elif [ -d "${HOME}/.minishift" ]; then - HOME_FOLDER="${HOME}/.minishift" +elif [ -d "${HOME_ADDR}/.minishift" ]; then + HOME_FOLDER="${HOME_ADDR}/.minishift" log_info "Minishift home is at ${HOME_FOLDER}" else log_warning "Minishift home does not exist, cannot clean old minishift configuration" @@ -136,18 +139,12 @@ fi log_info "Script parameters:" log_info "MINISHIFT_PATH: ${MINISHIFT_PATH}" +log_info "Basefile name is: ${BASEFILE}" log_info "MINISHIFT_URL: ${MINISHIFT_URL:-"Not set"}" # Start script main part if [ $EXISTING == 1 ]; then log_info "Checking existence of actual minishift instance..." - # check for every "minishift" file in the given path - #MINISHIFT_BIN=$(find $MINISHIFT_PATH -type f -name "minishift" 2>&1 | grep -i minishift) - # check whether there is multiple minishift files in the path - # check_minishift_bin $MINISHIFT_BIN - # if there is an existing minishift binary, try to clean everything - #if [ -f $MINISHIFT_BIN ] && [ $MINISHIFT_BIN ]; then - # try to stop and/or delete minishift minishift_cleanup $MINISHIFT_PATH # remove the direcotry with minishift if [ $ERASE == 1 ]; then @@ -167,7 +164,7 @@ else log_info "Downloading minishift from ${MINISHIFT_URL}" log_info "to $MINISHIFT_PATH" wget $MINISHIFT_URL - if [ ! -f $MINISHIFT_PATH/minishift ]; then + if [ ! -f "${MINISHIFT_PATH}/${BASEFILE}" ]; then log_info "Content of $MINISHIFT_PATH" log_info "$(ls $MINISHIFT_PATH)" if [ $ERASE == 1 ]; then @@ -176,8 +173,8 @@ else log_error "Minishift file was not downloaded from $MINISHIFT_URL, please, check the given url" exit 1 fi - chmod +x minishift - MINISHIFT_BIN=$(realpath minishift) + chmod +x ${BASEFILE} + MINISHIFT_BIN=$(realpath ${BASEFILE}) # stop/delete minishift minishift_cleanup $MINISHIFT_BIN if [ $ERASE == 1 ]; then @@ -196,3 +193,5 @@ fi if [ $ERASE == 1 ]; then clear_minishift_home fi + +log_info "Script ${__base} was finished..." diff --git a/scripts/cdk3-install.sh b/scripts/cdk3-install.sh index 5395fb3..15cd70a 100755 --- a/scripts/cdk3-install.sh +++ b/scripts/cdk3-install.sh @@ -14,18 +14,21 @@ __base="$(basename ${__file} .sh)" function usage { echo "Minishift install script" echo "Synopsis:" - echo " -u minishift_url -p minishift_path" + echo " -u minishift_url -p minishift_path -s setup_params" echo "Usage $0 -u [-p]" echo " -u, --url (required)" echo " CDK/minishift binary url to download, will overwrite existing minishift file" echo " -p, --path (optional)" echo " path where the file should be downloaded" + echo " -s, --setup (optional)" + echo " runs 'minishift setup-cdk' command, if params are given, it runs it with them" exit 1 } MINISHIFT_PATH=$(pwd) MINISHIFT_URL= EXISTING=1 +SETUP_CDK= # At least one parameter is required if [ $# -lt 2 ] @@ -33,7 +36,7 @@ then usage fi -while [ $# -gt 1 ]; do +while [ $# -gt 0 ]; do case $1 in -p | --path) shift @@ -51,16 +54,27 @@ while [ $# -gt 1 ]; do ;; -u | --url) shift - url_status=$(curl -Is -l ${1} | head -n 1 | grep -i http/1 | awk {'print $2'} | grep -E '2[0-9]{2}|3[0-9]{2}') + url_status=$(http_status_ok ${1}) log_info "Trying to reach ${1}" log_info "URL status: $url_status" if [ "${url_status}" ]; then - MINISHIFT_URL="${1}" + if [ $(url_has_minishift ${1}) == "1" ]; then + MINISHIFT_URL="${1}" + else + MINISHIFT_URL="$(add_url_suffix ${1})" + fi else log_error "Given minishift url ${1} cannot be reached" exit 1 fi ;; + -s | --setup) + shift + SETUP_CDK="setup-cdk" + if [ -n "${1}" ]; then + SETUP_CDK="${SETUP_CDK} ${1}" + fi + ;; *) usage ;; @@ -75,40 +89,31 @@ fi cd ${MINISHIFT_PATH} +BASEFILE=$(basename ${MINISHIFT_URL}) +log_info "Basefile name is: ${BASEFILE}" log_info "Downloading minishift from ${MINISHIFT_URL}" log_info "to $MINISHIFT_PATH" +if [ -n "${SETUP_CDK}" ]; then + log_info "Minishift ${SETUP_CDK} will be called" +fi -BASEFILE=$(basename ${MINISHIFT_URL}) - -wget -O minishift ${MINISHIFT_URL} +wget -O "${BASEFILE}" ${MINISHIFT_URL} if [ $? == 1 ]; then log_error "Downloading ${MINISHIFT_URL} fails to save the file as minishift" exit 1 fi - - -#cd ${WORKSPACE} -#mkdir -p minishift -#cd minishift - -#if [ "${USE_CDK3}" == true ]; then -# wget ${CDK3_MINISHIFT_URL}/minishift -#else -# wget ${MINISHIFT_URL} -# tar -xvf $(ls | grep minishift) -#fi - -#chmod +x minishift - -#if [ "${USE_CDK3}" == true ]; then -# ./minishift setup-cdk -#fi - log_info "Make the file executable" -chmod +x minishift - -if [ $(minishift_not_initialized "${MINISHIFT_PATH}/minishift") == 1 ]; then - log_info "Minishift was not initialized, running 'minishift setup-cdk'" - ${MINISHIFT_PATH}/minishift setup-cdk +chmod +x ${BASEFILE} + +if [ $(minishift_not_initialized "${MINISHIFT_PATH}/${BASEFILE}") == 1 ]; then + log_info "Minishift was not initialized" + if [ -n "${SETUP_CDK}" ]; then + log_info "Running ${MINISHIFT_PATH}/${BASEFILE} ${SETUP_CDK}" + ${MINISHIFT_PATH}/${BASEFILE} ${SETUP_CDK} + else + log_warning "'minishift setup-cdk' will not be called, did you forget to set -s flag?" + fi fi + +log_info "Script $__base was finished successfully" diff --git a/scripts/cdk3-stop.sh b/scripts/cdk3-stop.sh index 3ba5869..2acd085 100755 --- a/scripts/cdk3-stop.sh +++ b/scripts/cdk3-stop.sh @@ -32,11 +32,14 @@ function minishift_cleanup() { local minishift="$(realpath $1)" local status="$(${minishift} status)" log_info "Minishift status: ${status}" - if [ "${status}" == "Running" ]; then - ${minishift} stop - ${minishift} delete + if [ "${status}" == "Running" ] || [ "${status}" == "Paused" ]; then + ${minishift} stop || log_warning "minishift stop failed, proceeding..." + log_info "Executing 'minishift delete --force'" + ${minishift} delete --force elif [ "${status}" == "Stopped" ]; then - ${minishift} delete + ${minishift} stop || log_warning "minishift stop failed, proceeding..." + log_info "Executing 'minishift delete --force'" + ${minishift} delete --force else log_info "Do nothing here" fi @@ -73,3 +76,5 @@ if [ ${IS_SOURCE} == 0 ]; then shift done fi + +log_info "${__base} script finished successfully" diff --git a/scripts/script-utils.sh b/scripts/script-utils.sh index 60da2c6..2a5a522 100755 --- a/scripts/script-utils.sh +++ b/scripts/script-utils.sh @@ -14,12 +14,13 @@ function log_warning { echo "[WARNING] $@" } -STATUS_SETUP="You need to run 'minishift setup-cdk' first to install required CDK components" +STATUS_SETUP="setup-cdk" STATUS_NONEXISTING="Does Not Exist" STATUS_RUNNING="Running" STATUS_STOPPED="Stopped" +STATUS_PAUSED="Paused" -declare -r STATUSES=("${STATUS_SETUP}" "${STATUS_NONEXISTING}" "${STATUS_RUNNING}" "${STATUS_STOPPED}") +declare -r STATUSES=("${STATUS_SETUP}" "${STATUS_NONEXISTING}" "${STATUS_RUNNING}" "${STATUS_STOPPED}" "${STATUS_PAUSED}") # Takes one parameter and checks whether given file is minishift binary file # Beware, minishift version creates minishift folder structure in minishift_home @@ -62,16 +63,12 @@ function minishift_has_status { # checks if minishift setup-cdk was called function minishift_not_initialized { - if [ -f ${1} ]; then - output="$($(realpath ${1}) status)" - if [[ "${output}" == *"${STATUS_SETUP}"* ]]; then - echo 1 - else - echo 0 - fi - else + local output="$($(realpath ${1}) status)" + if [[ "${output}" == *"${STATUS_SETUP}"* ]]; then + echo 1 + else echo 0 - fi + fi } function clear_minishift_home { @@ -81,8 +78,16 @@ function clear_minishift_home { delete_path ${1} elif [ -z ${MINISHIFT_HOME+x} ]; then log_info "MINISHIFT_HOME is not set" - if [ -d ${HOME}/.minishift ]; then - delete_path ${HOME}/.minishift + HOME_ADDRESS=${HOME} + if [ $(get_os_platform) == "win" ]; then + HOME_ADDRESS=${USERPROFILE} + fi + log_info "Searching for .minishift in ${HOME_ADDRESS}" + if [ -d ${HOME_ADDRESS}/.minishift ]; then + log_info ".minishift exists" + delete_path ${HOME_ADDRESS}/.minishift + else + log_info "There is no .minishift folder in user's home: ${HOME_ADDRESS}" fi elif [ -d "${MINISHIFT_HOME}" ]; then log_info "MINISHIFT_HOME is set to ${MINISHIFT_HOME}" @@ -95,3 +100,39 @@ function clear_minishift_home { log_info "Nothing to clear" fi } + +# return os/kernel that script runs on +function get_os_platform { + if [[ "$(uname)" = *CYGWIN* ]]; then + echo "win" + elif [[ "$(uname)" = *Linux* ]]; then + echo "linux" + else + echo $(uname) + fi +} + +function add_url_suffix { + if [ "$(get_os_platform)" == "linux" ]; then + echo "${1}/linux-amd64/minishift" + elif [ "$(get_os_platform)" == "win" ]; then + echo "${1}/windows-amd64/minishift.exe" + else + echo "It is another os: $(get_os_platform)" + exit -1 + fi +} + +# checks whether basename of url contains word minishift +function url_has_minishift { + if [[ $(basename ${1}) = *minishift* ]]; then + echo 1 + else + echo 0 + fi +} + +# returns http status only if status is in class 2 or 3 (2xx - successful or 3xx - redirected) +function http_status_ok { + echo $(curl -Is -l ${1} | head -n 1 | grep -i http/1 | awk {'print $2'} | grep -E '2[0-9]{2}|3[0-9]{2}') +}