diff --git a/bin/build_image.sh b/bin/build_image.sh index bf87e91..a3b3543 100644 --- a/bin/build_image.sh +++ b/bin/build_image.sh @@ -32,12 +32,16 @@ function build_image() #fi image_name="matrixone" - commitid_full=`get_cid less | head -n 2 | tail -n 1` - commitid_less=`echo "${commitid_full:0:8}"` + #commitid_full=`get_cid less | head -n 2 | tail -n 1` + #commitid_less=`echo "${commitid_full:0:8}"` + commitid_less=`get_cid less` - branch=`get_branch | grep "current branch" | awk -F "current branch: " '{print $2}'` - add_log "D" "Commit id full: ${commitid_full}, commit id less: ${commitid_less}, branch: ${branch}" + #branch=`get_branch | grep "current branch" | awk -F "current branch: " '{print $2}'` + branch=`get_branch less` + + + add_log "D" "Commit id: ${commitid_less}, branch: ${branch}" if [[ "${commitid_less}" == "" ]] || [[ "${branch}" == "" ]]; then add_log "E" "Commit id or branch is empty, exiting" diff --git a/bin/deploy.sh b/bin/deploy.sh index 3d18290..034fca9 100644 --- a/bin/deploy.sh +++ b/bin/deploy.sh @@ -104,11 +104,10 @@ function git_clone() rc=1 break; fi - - # git clone and checkout all ok, breaking the loop - rc=0 - break; fi + # git clone and checkout all ok, breaking the loop + rc=0 + break; fi done diff --git a/bin/get_branch.sh b/bin/get_branch.sh index 07729ce..6985fb1 100644 --- a/bin/get_branch.sh +++ b/bin/get_branch.sh @@ -14,7 +14,10 @@ function get_branch() return 1 fi - add_log "I" "Try get mo branch" + if [[ "${option}" != "less" ]]; then + add_log "I" "Try get mo branch" + fi + if [[ ! -d ${MO_PATH}/matrixone ]]; then add_log "E" "Path ${MO_PATH}/matrixone does not exist, please make sure mo is deployed properly" add_log "E" "Get branch failed, exiting" @@ -25,20 +28,35 @@ function get_branch() cd ${MO_PATH}/matrixone if echo "${current_branch}" | grep "HEAD" >/dev/null 2>&1; then - add_log "I" "current_branch is ${current_branch}, contains \"HEAD\" info, thus it's a commit id, trying to find it's real branch" + if [[ "${option}" != "less" ]]; then + add_log "I" "current_branch is ${current_branch}, contains \"HEAD\" info, thus it's a commit id, trying to find it's real branch" + fi commitid_full=`git log | head -n 1 | awk {'print $2'}` commitid_less=`echo "${commitid_full:0:8}"` current_branch=`git branch --contains ${cid_less} | grep -v HEAD | sed 's/ //g'` fi if [[ "${current_branch}" != "" ]]; then - add_log "I" "Get branch succeeded, current branch: ${current_branch}" + MO_V_TYPE="branch" + if [[ "${option}" != "less" ]]; then + add_log "I" "Get branch succeeded, current branch: ${current_branch}" + else + echo "${current_branch}" + fi else - add_log "I" "No branch contains this commit, try to match a tag" + if [[ "${option}" != "less" ]]; then + add_log "I" "No branch contains this commit, try to match a tag" + fi current_tag=`git tag --contains ${cid_less} | grep -v HEAD | sed 's/ //g' | sort | head -1` if [[ "${current_tag}" != "" ]]; then - add_log "I" "Get tag succeeded, current tag: ${current_tag}" + MO_V_TYPE="tag" + if [[ "${option}" != "less" ]]; then + add_log "I" "Get tag succeeded, current tag: ${current_tag}" + else + echo "${current_tag}" + fi else + MO_V_TYPE="unkown" add_log "E" "Get tag failed" fi return 1 diff --git a/bin/get_cid.sh b/bin/get_cid.sh index c3ce19f..bf978e0 100644 --- a/bin/get_cid.sh +++ b/bin/get_cid.sh @@ -14,7 +14,10 @@ function get_cid() return 1 fi - add_log "I" "Try get mo commit id" + if [[ "${option}" != "less" ]]; then + add_log "I" "Try get mo commit id" + fi + if [[ ! -d ${MO_PATH}/matrixone ]]; then add_log "E" "Path ${MO_PATH}/matrixone does not exist, please make sure mo is deployed properly" add_log "E" "Get commit id failed, exiting" @@ -24,6 +27,7 @@ function get_cid() # better way to get commit id cid_full=`cd ${MO_PATH}/matrixone && git log -n 1` cid_less=`cd ${MO_PATH}/matrixone && git log -n 1 --format='%H'` + cid_less=`echo "${cid_less:0:8}"` #deprecated: #cid_full="$(cd ${MO_PATH}/matrixone && git log | head -n 6)" @@ -34,7 +38,10 @@ function get_cid() else echo "${cid_full}" fi - add_log "I" "Get commit id succeeded" + + if [[ "${option}" != "less" ]]; then + add_log "I" "Get commit id succeeded" + fi else add_log "E" "Get commit id failed" return 1 diff --git a/bin/upgrade-old.sh b/bin/upgrade-old.sh new file mode 100644 index 0000000..8faedb1 --- /dev/null +++ b/bin/upgrade-old.sh @@ -0,0 +1,346 @@ +#!/bin/bash +################################################################ +# Copyright (C) 2023 Matrix Origin. All Rights Reserved +# Visit us at https://www.matrixorigin.cn/ +################################################################ +# upgrade + +#global vars +current_branch="" +current_cid="" +target_branch="" +target_cid="" +RUN_TAG="" +MO_UPGRADE_PATH="" +action_type="" +is_cid_notlatest_notstable_valid="" +#declare -A stable_list + + + +function init_global_vars() +{ + current_cid=`get_cid less | sed -n '2p'` + current_branch=`get_branch | grep "current branch" | head -n 1 | awk -F "current branch: " '{print $2}'` + target_branch="${current_branch}" + RUN_TAG="$(date "+%Y%m%d_%H%M%S")" + MO_UPGRADE_PATH="${MO_PATH}/matrixone-bk-${RUN_TAG}" + is_cid_notlatest_notstable_valid="1" + # deprecated: this only works on bash v4 but not bash v3, so might raise issues on MacOS + # see: https://github.com/matrixorigin/mo_ctl_standalone/issues/42 + #stable_list=( + # ["0.8.0"]="3bd05cb14f32dbca4cf57abe03fec4907450c7e7" + # ["0.7.0"]="6d4bd173514990032372310f7b3d9d803781074a" + # ["0.6.0"]="c3262c1b58d030b00534283b9bd22cc83c888a2a" + # ["0.5.1"]="c9491645c681c9e239817a6fa71fb71df25003e2" + # ["0.4.0"]="aefc440bf6d6c2a5e96ba411fb0c98ae0b8bd657" + # ["0.3.0"]="56fcd3ff8e4aa3b5a8b9d08c420fa90f7462c579" + # ["0.2.0"]="c22aa58f948cef7e59acef1ebabb8f8dfd4154cd" + # ["0.1.0"]="19cc0453b573e23ae643bea492bc43c5df4758db" + #) + +} + + + +function check_upgrade_pre_requisites() +{ + rc=0 + if [[ "${target_cid}" == "" ]]; then + add_log "E" "Please specify a commit id to upgrade" + help_upgrade + rc=1 + return ${rc} + fi + + if status; then + add_log "E" "Please make sure no mo-service is running." + add_log "I" "You may use 'mo_ctl stop [force]' to stop mo-service" + rc=1 + fi + + if watchdog; then + add_log "E" "Please make sure mo-watchdog is disabled before upgrading." + add_log "I" "You may use 'mo_ctl watchdog disable' to disable mo-watchdog" + rc=1 + fi + return ${rc} +} + +function copy_mo_path() +{ + add_log "I" "Copying upgrade path from ${MO_PATH}/matrixone/ to ${MO_UPGRADE_PATH}/" + mkdir -p ${MO_UPGRADE_PATH}/ + if ls -a ${MO_PATH}/matrixone/ | grep -vE "logs|^.$|^..$|mo-data|mo-service|mo-dump" | xargs -I{} cp -r ${MO_PATH}/matrixone/{} ${MO_UPGRADE_PATH}/ >/dev/null 2>&1; then + add_log "I" "Succeeded" + else + add_log "E" "Failed, exiting" + return 1 + fi +} + + +function validate_target_cid() +{ + + cd ${MO_UPGRADE_PATH} + + # 0. Output info + add_log "I" "Specified info:" + add_log "I" "1. current branch: ${current_branch}, current commit id: ${current_cid}" + add_log "I" "2. target branch: ${target_branch}, target commit id: ${target_cid}" + + + # 1. check if target commit id is a valid stable version + s_cid=`get_stable_cid "${target_cid}"` + if [[ "${s_cid}" != "${target_cid}" ]]; then + add_log "I" "Target commit id ${target_cid} is a stable version, whose last commit id is ${s_cid}" + target_branch="${target_cid}" + before_t_cid="${target_cid}" + target_cid="${s_cid}" + fi + + # deprecated + #if [ -v stable_list["${target_cid}"] ] ; then + # add_log "I" "Target commit id ${target_cid} is a stable version, whose last commit id is ${stable_list[${target_cid}]}" + # target_branch="${target_cid}" + # target_cid="${stable_list[${target_cid}]}" + #fi + + # 2. currently mo is already on this commit id + if echo "${current_cid}" | grep "${target_cid}" >/dev/null 2>&1 || echo "${target_cid}" | grep "${current_cid}" >/dev/null 2>&1 ; then + add_log "I" "Current commit id seems to match target, thus no need to perform any upgrade, exiting" + exit 0 + fi + + # 3 checkout target branch + if [[ "${current_branch}" != "${target_branch}" ]]; then + # 3.1. switch to target branch + # e.g. branch not the same: 0.8.0 -> main, main -> 0.8.0, 0.8.0 -> 0.7.0 + add_log "I" "Current and target branch are not the same, switching to target: git checkout ${target_branch}" + if [[ "${target_branch}" == "main" ]] || [[ "${target_branch}" > "${current_branch}" ]]; then + add_log "I" "Target branch is main or newer than current, thus it's an UPGRADE" + action_type="upgrade" + else + add_log "I" "Target branch is older than current, thus it's a DOWNGRADE" + action_type="downgrade" + fi + if ! git checkout ${target_branch}; then + add_log "E" "Failed, exiting" + return 1 + fi + else + # 3.2. git fetch to update codes on local repository + # e.g. branch=main, but switch commit id de596817 -> d3661e7d + add_log "I" "Git fetching: git fetch" + if ! git fetch; then + add_log "E" "Failed, exiting" + return 1 + fi + fi + + # 4. If target branch is main + if [[ "${target_branch}" == "main" ]]; then + if [[ "${current_branch}" != "main" ]]; then + add_log "I" "Current branch ${current_branch} is not on main, checking out to main: git checkout main" + ! git checkout main && return 1 + fi + if [[ "${target_cid}" == "latest" ]]; then + # 4.1. get latest commit id if target cid is set to latest + before_t_cid="${target_cid}" + target_cid=`git rev-parse origin/${target_branch} | head -n 1` + add_log "I" "Target commit id is latest, thus it's an UPGRADE" + action_type="upgrade" + add_log "I" "Latest commit id on remote repository is ${target_cid}" + if echo "${current_cid}" | grep "${target_cid}" >/dev/null 2>&1; then + add_log "I" "Target commit id ${target_cid} seems to match current commit id ${current_cid}" + add_log "I" "No need to perform any upgrade, exiting" + exit 0 + fi + + else + # 4.2. check if target commit id is submitted, that is, a valid commit id + add_log "I" "Check if the given commit id ${target_cid} is valid: git merge-base --is-ancestor ${current_cid} ${target_cid}" + git merge-base --is-ancestor ${current_cid} ${target_cid} >/dev/null 2>&1 + check_cid_result=`echo $?` + if [[ "${check_cid_result}" == "0" ]]; then + if [[ "action_type" != "" ]]; then + + add_log "I" "Succeeded, valid target commit id is newer than current, thus it's an UPGRADE" + action_type="upgrade" + fi + elif [[ "${check_cid_result}" == "1" ]]; then + if [[ "action_type" != "" ]]; then + add_log "I" "Succeeded, valid target commit id is older than current, thus it's a DOWNGRADE" + action_type="downgrade" + fi + else + + add_log "E" "Failed, commit id ${target_cid} seems to be invalid, exiting" + return 1 + fi + + is_cid_notlatest_notstable_valid="0" + fi + fi + + + # 5. print action info + add_log "I" "Actual info:" + add_log "I" "1. current branch: ${current_branch}, current commit id: ${current_cid}" + add_log "I" "2. target branch: ${target_branch}, target commit id: ${target_cid}" + add_log "I" "3. action_type: ${action_type}" +} + +function update_src_codes() +{ + + cd ${MO_UPGRADE_PATH} + + # 1. pull : merge codes from local repository to local workdir + add_log "I" "Git pulling: git pull" + if git pull; then + add_log "I" "Succeeded" + else + add_log "E" "Failed, exiting" + return 1 + fi + + # 2. checkout to target cid + if [[ "${is_cid_notlatest_notstable_valid}" == "0" ]]; then + add_log "I" "Checking out to target commit id ${target_cid}: git checkout ${target_cid}" + git checkout ${target_cid} >/dev/null 2>&1 + fi + +} + +function upgrade_build_mo_service() +{ + # mo-service + add_log "I" "Try to build mo-service: make build" + if cd ${MO_UPGRADE_PATH}/ && make build ; then + add_log "I" "Build succeeded" + else + add_log "E" "Build failed" + return 1 + fi + + +} +function upgrade_build_mo_dump() +{ + add_log "I" "Try to build mo-dump: make cgo && make modump" +# if cd ${MO_UPGRADE_PATH}/ && make build modump; then + if cd ${MO_UPGRADE_PATH}/ && make cgo && make modump; then + add_log "I" "Build succeeded" + else + add_log "E" "Build failed" + return 1 + fi +} + + +function upgrade_build_all() +{ + rc=0 + if [[ "${GOPROXY}" != "" ]]; then + add_log "I" "GOPROXY is set, setting go proxy to GOPROXY=${GOPROXY}" + go env -w GOPROXY=${GOPROXY} + fi + + if ! upgrade_build_mo_service; then + rc=1 + fi + + #if ! upgrade_build_mo_dump; then + # rc=1 + #fi + + return ${rc} + +} + +function upgrade_rollback() +{ + add_log "I" "Rolling back ${action_type} actions by moving below folder" + add_log "I" "${MO_UPGRADE_PATH} -> ${MO_PATH}/matrixone-${action_type}-FAILED-${RUN_TAG}" + action_type=`to_upper "${action_type}"` + mv ${MO_UPGRADE_PATH} ${MO_PATH}/matrixone-${action_type}-FAILED-${RUN_TAG} +} + +function upgrade_commit() +{ + add_log "I" "Committing ${action_type} actions by moving below folders" + action_type=`to_upper "${action_type}"` + add_log "I" "1. ${MO_PATH}/matrixone/mo-data -> ${MO_UPGRADE_PATH}/" + add_log "I" "2. ${MO_PATH}/matrixone -> ${MO_PATH}/matrixone-${action_type}-BACKUP-${RUN_TAG}" + add_log "I" "3. ${MO_UPGRADE_PATH} -> ${MO_PATH}/matrixone" + # copy mo logs + if [[ -d "${MO_LOG_PATH}" ]] ; then + mv ${MO_LOG_PATH} ${MO_UPGRADE_PATH}/logs + fi + + # move mo-data + mv ${MO_PATH}/matrixone/mo-data ${MO_UPGRADE_PATH}/ + + # move original mo folder to backup + mv ${MO_PATH}/matrixone ${MO_PATH}/matrixone-${action_type}-BACKUP-${RUN_TAG} + + # move upgraded mo folder to current mo path + mv ${MO_UPGRADE_PATH} ${MO_PATH}/matrixone + +} + + +function upgrade() +{ + target_cid=$1 + + if [[ "${MO_DEPLOY_MODE}" != "git" ]]; then + add_log "E" "Currently upgrade is only supported when mo is deployed in git mode. Please check MO_DEPLOY_MODE again" + return 1 + fi + + # 0. initialize global variables + init_global_vars + + + + # 1. check if target_cid is not empty and mo-service not running and mo-watchdog disabled + if ! check_upgrade_pre_requisites; then + return 1 + fi + + # 2. copy mo path + if ! copy_mo_path; then + return 1 + fi + + # 3. validate commit id + if ! validate_target_cid; then + upgrade_rollback + return 1 + fi + + # 4. update codes in local workdir from remote repository + if ! update_src_codes; then + upgrade_rollback + return 1 + fi + + # 5. rebuild mo-service and mo-dump + if ! upgrade_build_all; then + upgrade_rollback + return 1 + fi + + # 5. commit all actions + if ! upgrade_commit; then + return 1 + fi + + add_log "I" "All ${action_type} actions succeeded. Please use 'mo_ctl start' or 'mo_ctl restart' to restart your mo-service" + + + return 0 +} diff --git a/bin/upgrade.sh b/bin/upgrade.sh index 2c70b66..e3b0f91 100644 --- a/bin/upgrade.sh +++ b/bin/upgrade.sh @@ -6,44 +6,45 @@ # upgrade #global vars + + current_branch="" +current_tag="" current_cid="" +current_cid_full="" + +target_type="" +actual_target="" +target_cid_full="" target_branch="" -target_cid="" + + RUN_TAG="" MO_UPGRADE_PATH="" -action_type="" -is_cid_notlatest_notstable_valid="" +#action_type="" #declare -A stable_list - -function init_global_vars() +function upgrade_init_vars() { - current_cid=`get_cid less | sed -n '2p'` - current_branch=`get_branch | grep "current branch" | head -n 1 | awk -F "current branch: " '{print $2}'` - target_branch="${current_branch}" - RUN_TAG="$(date "+%Y%m%d_%H%M%S")" - MO_UPGRADE_PATH="${MO_PATH}/matrixone-bk-${RUN_TAG}" - is_cid_notlatest_notstable_valid="1" - # deprecated: this only works on bash v4 but not bash v3, so might raise issues on MacOS - # see: https://github.com/matrixorigin/mo_ctl_standalone/issues/42 - #stable_list=( - # ["0.8.0"]="3bd05cb14f32dbca4cf57abe03fec4907450c7e7" - # ["0.7.0"]="6d4bd173514990032372310f7b3d9d803781074a" - # ["0.6.0"]="c3262c1b58d030b00534283b9bd22cc83c888a2a" - # ["0.5.1"]="c9491645c681c9e239817a6fa71fb71df25003e2" - # ["0.4.0"]="aefc440bf6d6c2a5e96ba411fb0c98ae0b8bd657" - # ["0.3.0"]="56fcd3ff8e4aa3b5a8b9d08c420fa90f7462c579" - # ["0.2.0"]="c22aa58f948cef7e59acef1ebabb8f8dfd4154cd" - # ["0.1.0"]="19cc0453b573e23ae643bea492bc43c5df4758db" - #) + cd ${MO_PATH}/matrixone + current_cid=$(get_cid less) + current_cid_full=$(get_cid) + current_branch=$(get_branch less) + current_tag="" + if [[ "${MO_V_TYPE}" == "tag" ]]; then + current_tag="${current_branch}" + current_branch="" + fi + + RUN_TAG="$(date "+%Y%m%d_%H%M%S")" + MO_UPGRADE_PATH="${MO_PATH}/matrixone-UPGRADE-BK-${RUN_TAG}" } -function check_upgrade_pre_requisites() +function upgrade_check_pre_requisites() { rc=0 if [[ "${target_cid}" == "" ]]; then @@ -67,280 +68,230 @@ function check_upgrade_pre_requisites() return ${rc} } -function copy_mo_path() + +# Step_2. Validate target commit id +function upgrade_valid_target() { - add_log "I" "Copying upgrade path from ${MO_PATH}/matrixone/ to ${MO_UPGRADE_PATH}/" - mkdir -p ${MO_UPGRADE_PATH}/ - if ls -a ${MO_PATH}/matrixone/ | grep -vE "logs|^.$|^..$|mo-data|mo-service|mo-dump" | xargs -I{} cp -r ${MO_PATH}/matrixone/{} ${MO_UPGRADE_PATH}/ >/dev/null 2>&1; then - add_log "I" "Succeeded" - else - add_log "E" "Failed, exiting" + target_cid=$1 + add_log "I" "Target: ${target_cid}" + if [[ "${MO_DEPLOY_MODE}" != "git" ]]; then + add_log "E" "Currently upgrade is only supported when mo is deployed in git mode. Please check MO_DEPLOY_MODE again" return 1 fi -} + add_log "D" "Try to get current commit id" -function validate_target_cid() -{ - - cd ${MO_UPGRADE_PATH} - - # 0. Output info - add_log "I" "Specified info:" - add_log "I" "1. current branch: ${current_branch}, current commit id: ${current_cid}" - add_log "I" "2. target branch: ${target_branch}, target commit id: ${target_cid}" + cd ${MO_PATH}/matrixone - - # 1. check if target commit id is a valid stable version - s_cid=`get_stable_cid "${target_cid}"` - if [[ "${s_cid}" != "${target_cid}" ]]; then - add_log "I" "Target commit id ${target_cid} is a stable version, whose last commit id is ${s_cid}" - target_branch="${target_cid}" - before_t_cid="${target_cid}" - target_cid="${s_cid}" - fi - - # deprecated - #if [ -v stable_list["${target_cid}"] ] ; then - # add_log "I" "Target commit id ${target_cid} is a stable version, whose last commit id is ${stable_list[${target_cid}]}" - # target_branch="${target_cid}" - # target_cid="${stable_list[${target_cid}]}" - #fi - - # 2. currently mo is already on this commit id - if echo "${current_cid}" | grep "${target_cid}" >/dev/null 2>&1 || echo "${target_cid}" | grep "${current_cid}" >/dev/null 2>&1 ; then - add_log "I" "Current commit id seems to match target, thus no need to perform any upgrade, exiting" - exit 0 - fi - - # 3 checkout target branch - if [[ "${current_branch}" != "${target_branch}" ]]; then - # 3.1. switch to target branch - # e.g. branch not the same: 0.8.0 -> main, main -> 0.8.0, 0.8.0 -> 0.7.0 - add_log "I" "Current and target branch are not the same, switching to target: git checkout ${target_branch}" - if [[ "${target_branch}" == "main" ]] || [[ "${target_branch}" > "${current_branch}" ]]; then - add_log "I" "Target branch is main or newer than current, thus it's an UPGRADE" - action_type="upgrade" - else - add_log "I" "Target branch is older than current, thus it's a DOWNGRADE" - action_type="downgrade" - fi - if ! git checkout ${target_branch}; then - add_log "E" "Failed, exiting" + add_log "I" "Current info:" + add_log "I" "Commit id: ${current_cid}, branch: ${current_branch}, tag: ${current_tag}" + + if [[ "${target_cid}" == "latest" ]]; then + # 1. is current commit a tag? + if [[ "${current_tag}" != "" ]]; then + add_log "E" "MO is currently on a tag but not a branch, thus upgrade to latest is not possible. Please specify a commit id, a branch or a tag" return 1 fi - else - # 3.2. git fetch to update codes on local repository - # e.g. branch=main, but switch commit id de596817 -> d3661e7d - add_log "I" "Git fetching: git fetch" - if ! git fetch; then - add_log "E" "Failed, exiting" + add_log "D" "Command: git fetch" + + # 2. current commit id a branch commit id, so is current commit id the newest? + git fetch + latest_cid=`git rev-parse origin/${current_branch} | head -n 1` + add_log "I" "Latest commit id on current branch ${current_branch} is ${latest_cid}" + if echo "${latest_cid}" | grep "${current_cid}" >/dev/null 2>&1 ; then + #if [[ "${latest_cid}" == "${current_cid}" ]]; then + add_log "W" "Current commit id is already latest, no need to perform upgrade, exiting" return 1 fi - fi - - # 4. If target branch is main - if [[ "${target_branch}" == "main" ]]; then - if [[ "${current_branch}" != "main" ]]; then - add_log "I" "Current branch ${current_branch} is not on main, checking out to main: git checkout main" - ! git checkout main && return 1 - fi - if [[ "${target_cid}" == "latest" ]]; then - # 4.1. get latest commit id if target cid is set to latest - before_t_cid="${target_cid}" - target_cid=`git rev-parse origin/${target_branch} | head -n 1` - add_log "I" "Target commit id is latest, thus it's an UPGRADE" - action_type="upgrade" - add_log "I" "Latest commit id on remote repository is ${target_cid}" - if echo "${current_cid}" | grep "${target_cid}" >/dev/null 2>&1; then - add_log "I" "Target commit id ${target_cid} seems to match current commit id ${current_cid}" - add_log "I" "No need to perform any upgrade, exiting" - exit 0 - fi - else - # 4.2. check if target commit id is submitted, that is, a valid commit id - add_log "I" "Check if the given commit id ${target_cid} is valid: git merge-base --is-ancestor ${current_cid} ${target_cid}" - git merge-base --is-ancestor ${current_cid} ${target_cid} >/dev/null 2>&1 - check_cid_result=`echo $?` - if [[ "${check_cid_result}" == "0" ]]; then - if [[ "action_type" != "" ]]; then + # 3. target is latest commid id + actual_target="${current_branch}" + target_type="branch" - add_log "I" "Succeeded, valid target commit id is newer than current, thus it's an UPGRADE" - action_type="upgrade" - fi - elif [[ "${check_cid_result}" == "1" ]]; then - if [[ "action_type" != "" ]]; then - add_log "I" "Succeeded, valid target commit id is older than current, thus it's a DOWNGRADE" - action_type="downgrade" - fi - else + else + # check if target cid is a valid branch, commit id or tag + remote_tags=`git tag` + remote_branches=`git branch -r | awk -F'origin/' '{print $2}' | grep -v HEAD` + + + add_log "D" "List of remote tags:" + add_log "D" "${remote_tags}" "l" + add_log "D" "List of remote branches:" + add_log "D" "${remote_branches}" "l" + + target_type="unknown" + actual_target="${target_cid}" + # 1. is it a tag? + for tag in ${remote_tags}; do + if [[ "${tag}" == "${target_cid}" ]]; then + add_log "I" "Tag ${tag} mathces target ${target_cid}" + target_type="tag" - add_log "E" "Failed, commit id ${target_cid} seems to be invalid, exiting" - return 1 + if [[ "${target_cid}" == "${current_tag}" ]]; then + add_log "W" "Currently on a tag ${current_tag} which is the same as given target ${target_cid}, no need to perform upgrade, exiting" + return 2 + fi + break fi + done + + # 2. is it a branch? + if [[ "${target_type}" == "unknown" ]]; then + add_log "D" "No tag mathces target ${target_cid}, trying to match a branch" + for branch in ${remote_branches}; do + if [[ "${branch}" == "${target_cid}" ]]; then + target_type="branch" + add_log "I" "Branch ${branch} mathces target ${target_cid}" + git fetch + latest_cid=`git rev-parse origin/${branch} | head -n 1` + add_log "I" "Latest commit id on target branch ${branch} is ${latest_cid}" + + if echo "${latest_cid}" | grep "${current_cid}" >/dev/null 2>&1 ; then + #if [[ "${latest_cid}" == "${current_cid}" ]]; then + add_log "W" "Current commit id is already latest, no need to perform upgrade, exiting" + return 2 + fi + break + fi + done + fi - is_cid_notlatest_notstable_valid="0" + # 3. is it a commit id? + if [[ "${target_type}" == "unknown" ]]; then + target_type="commit" + add_log "D" "No branch mathces target ${target_cid}, will take it as a commit id" fi + fi + add_log "I" "target_type: ${target_type}, actual_target: ${actual_target}" - # 5. print action info - add_log "I" "Actual info:" - add_log "I" "1. current branch: ${current_branch}, current commit id: ${current_cid}" - add_log "I" "2. target branch: ${target_branch}, target commit id: ${target_cid}" - add_log "I" "3. action_type: ${action_type}" + return 0 } -function update_src_codes() -{ - - cd ${MO_UPGRADE_PATH} - - # 1. pull : merge codes from local repository to local workdir - add_log "I" "Git pulling: git pull" - if git pull; then +function upgrade_bk_old_mo() +{ + add_log "I" "Back up mo path from ${MO_PATH}/matrixone to ${MO_UPGRADE_PATH}" + add_log "D" "cmd: mv ${MO_PATH}/matrixone ${MO_UPGRADE_PATH}" + if mv ${MO_PATH}/matrixone ${MO_UPGRADE_PATH}; then + #if ls -a ${MO_PATH}/matrixone/ | grep -vE "logs|^.$|^..$|mo-data|mo-service|mo-dump" | xargs -I{} cp -r ${MO_PATH}/matrixone/{} ${MO_UPGRADE_PATH}/ >/dev/null 2>&1; then add_log "I" "Succeeded" else add_log "E" "Failed, exiting" return 1 fi - - # 2. checkout to target cid - if [[ "${is_cid_notlatest_notstable_valid}" == "0" ]]; then - add_log "I" "Checking out to target commit id ${target_cid}: git checkout ${target_cid}" - git checkout ${target_cid} >/dev/null 2>&1 - fi - } -function upgrade_build_mo_service() +function upgrade_deploy_new_mo() { - # mo-service - add_log "I" "Try to build mo-service: make build" - if cd ${MO_UPGRADE_PATH}/ && make build ; then - add_log "I" "Build succeeded" - else - add_log "E" "Build failed" - return 1 - fi - -} -function upgrade_build_mo_dump() -{ - add_log "I" "Try to build mo-dump: make cgo && make modump" -# if cd ${MO_UPGRADE_PATH}/ && make build modump; then - if cd ${MO_UPGRADE_PATH}/ && make cgo && make modump; then - add_log "I" "Build succeeded" - else - add_log "E" "Build failed" + add_log "I" "Deploying new mo on target ${target_type} ${actual_target}" + if ! deploy ${actual_target}; then + add_log "E" "Failed, exiting" return 1 fi } -function upgrade_build_all() -{ - rc=0 - if [[ "${GOPROXY}" != "" ]]; then - add_log "I" "GOPROXY is set, setting go proxy to GOPROXY=${GOPROXY}" - go env -w GOPROXY=${GOPROXY} - fi - - if ! upgrade_build_mo_service; then - rc=1 +function upgrade_copy_conf_and_data() +{ + + add_log "I" "Backup new mo confs and copy confs from old mo to new mo" + add_log "D" "cmd: mv ${MO_PATH}/matrixone/etc ${MO_PATH}/matrixone/etc-default && cp -rp ${MO_UPGRADE_PATH}/etc ${MO_PATH}/matrixone/etc" + mv ${MO_PATH}/matrixone/etc ${MO_PATH}/matrixone/etc-default && cp -rp ${MO_UPGRADE_PATH}/etc ${MO_PATH}/matrixone/etc + + if [[ -d ${MO_UPGRADE_PATH}/mo-data ]]; then + add_log "I" "Copy mo-data from old mo to new mo" + add_log "D" "cmd: mv ${MO_UPGRADE_PATH}/mo-data ${MO_PATH}/matrixone/" + mv ${MO_UPGRADE_PATH}/mo-data ${MO_PATH}/matrixone/ + else + add_log "I" "${MO_UPGRADE_PATH}/mo-data does not exist, skipping moving it" fi - #if ! upgrade_build_mo_dump; then - # rc=1 - #fi - - return ${rc} - } + function upgrade_rollback() { - add_log "I" "Rolling back ${action_type} actions by moving below folder" - add_log "I" "${MO_UPGRADE_PATH} -> ${MO_PATH}/matrixone-${action_type}-FAILED-${RUN_TAG}" - action_type=`to_upper "${action_type}"` - mv ${MO_UPGRADE_PATH} ${MO_PATH}/matrixone-${action_type}-FAILED-${RUN_TAG} + add_log "E" "Rolling back upgrade action" + add_log "D" "cmd: mv ${MO_UPGRADE_PATH} ${MO_PATH}/matrixone" + #action_type=`to_upper "${action_type}"` + cd ${MO_PATH} && rm -rf ./matrixone + mv "${MO_UPGRADE_PATH}" "${MO_PATH}/matrixone" } -function upgrade_commit() -{ - add_log "I" "Committing ${action_type} actions by moving below folders" - action_type=`to_upper "${action_type}"` - add_log "I" "1. ${MO_PATH}/matrixone/mo-data -> ${MO_UPGRADE_PATH}/" - add_log "I" "2. ${MO_PATH}/matrixone -> ${MO_PATH}/matrixone-${action_type}-BACKUP-${RUN_TAG}" - add_log "I" "3. ${MO_UPGRADE_PATH} -> ${MO_PATH}/matrixone" - # copy mo logs - if [[ -d "${MO_LOG_PATH}" ]] ; then - mv ${MO_LOG_PATH} ${MO_UPGRADE_PATH}/logs - fi - - # move mo-data - mv ${MO_PATH}/matrixone/mo-data ${MO_UPGRADE_PATH}/ - # move original mo folder to backup - mv ${MO_PATH}/matrixone ${MO_PATH}/matrixone-${action_type}-BACKUP-${RUN_TAG} - - # move upgraded mo folder to current mo path - mv ${MO_UPGRADE_PATH} ${MO_PATH}/matrixone +function upgrade_print_report() +{ + target_branch=$(get_branch less) + target_cid_full=$(get_cid) + add_log "I" "Branch or tag before upgrade: ${current_branch}" + add_log "I" "Branch or tag after upgrade: ${target_branch}" + add_log "I" "--------------------------------" + add_log "I" "Commit id before upgrade:" + add_log "I" "${current_cid_full}" "l" + add_log "I" "--------------------------------" + add_log "I" "Commit id after upgrade:" + add_log "I" "${target_cid_full}" "l" } + function upgrade() { target_cid=$1 - if [[ "${MO_DEPLOY_MODE}" == "docker" ]]; then - add_log "E" "Currently mo_ctl does not support upgrade when mo deploy mode is docker" + if [[ "${MO_DEPLOY_MODE}" != "git" ]]; then + add_log "E" "Currently upgrade is only supported when mo is deployed in git mode. Please check MO_DEPLOY_MODE again" return 1 fi - # 0. initialize global variables - init_global_vars - + # 2. check pre-requisites + if ! upgrade_check_pre_requisites; then + return 1 + fi - - # 1. check if target_cid is not empty and mo-service not running and mo-watchdog disabled - if ! check_upgrade_pre_requisites; then + # 1. init vars + if ! upgrade_init_vars; then + add_log "E" "Upgrade failed, exiting" return 1 fi - # 2. copy mo path - if ! copy_mo_path; then + # 2. validate target + if ! upgrade_valid_target "${target_cid}"; then + add_log "E" "Upgrade failed, exiting" return 1 fi - # 3. validate commit id - if ! validate_target_cid; then - upgrade_rollback + # 3. backup old mo + if ! upgrade_bk_old_mo; then + add_log "E" "Upgrade failed, exiting" return 1 fi - # 4. update codes in local workdir from remote repository - if ! update_src_codes; then + # 4. deploy new mo + if ! upgrade_deploy_new_mo; then upgrade_rollback + add_log "E" "Upgrade failed, exiting" return 1 fi - # 5. rebuild mo-service and mo-dump - if ! upgrade_build_all; then + # 5. copy confs + if ! upgrade_copy_conf_and_data; then upgrade_rollback + add_log "E" "Upgrade failed, exiting" return 1 fi - # 5. commit all actions - if ! upgrade_commit; then - return 1 + # 6. print repot + if ! upgrade_print_report; then + add_log "E" "Upgrade failed, exiting" fi - add_log "I" "All ${action_type} actions succeeded. Please use 'mo_ctl start' or 'mo_ctl restart' to restart your mo-service" + add_log "I" "Upgrade succeeded. Please use 'mo_ctl start' or 'mo_ctl restart' to restart your mo-service" return 0 -} \ No newline at end of file +} + diff --git a/mo_ctl.sh b/mo_ctl.sh index 1d20ed0..aa099f6 100644 --- a/mo_ctl.sh +++ b/mo_ctl.sh @@ -30,6 +30,7 @@ LOG_DIR="${WORK_DIR}/log" # "build_image" "monitor" "restore" \ #) PIDS="" +MO_V_TYPE="unknown" function main() {