#! /bin/bash # doc cmd_deploy { # # DESCRIPTION # cmd_deploy - Command that deploys IMAGEIDs using deploy_imageid function # # USAGE # cmd_deploy - [] # # DEPENDENCIES bunc/src/* src/print_imageid_du src/deploy_rm src/deploy_cp src/deploy_meta # # } cmd_deploy() { local GIT_DIR="${1}" local TARGET="${2}" local OLD_COMMIT="${3}" local ERR=false local DB_FILE="${TARGET}/${_OPT_DB_FILE}" [[ ! -f "${DB_FILE}" ]] && return 3 [[ ! -d "${GIT_DIR}/.git" ]] && return 2 [[ "$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" != "$(awk 'BEGIN{FS="\t"}{if($1=="REPO_ID"){print $2}}' "${DB_FILE}")" ]] && return 2 if [[ -z "$(grep LAST_DEPLOY ${DB_FILE})" ]]; then echo -e "LAST_DEPLOY\t$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" >> ${DB_FILE} fi local NEW_COMMIT=$(git -C "${GIT_DIR}" rev-parse HEAD) [[ -z "${OLD_COMMIT}" ]] && local OLD_COMMIT=$(awk 'BEGIN{FS="\t"}{if($1=="LAST_DEPLOY"){print $2}}' "${DB_FILE}") local removableidList=($(awk 'BEGIN{FS="\t"}{if(($1=="null" || $1=="false") && $4!="null"){print $2"--"$3}}' "${DB_FILE}")) local deployableidList=($(awk 'BEGIN{FS="\t"}{if($1=="true" && $4=="null"){print $2}}' "${DB_FILE}" | awk '!seen[$0]++')) local changedidList=($(sed 's/\(.*\)\..*/\1/' <<< $(git -C "${GIT_DIR}" diff --name-only ${NEW_COMMIT} ${OLD_COMMIT}) | awk '!seen[$0]++' | grep SHA256)) local updateableidList=($(printf "%s\n" ${deployableidList[@]} ${changedidList[@]} | awk '!seen[$0]++')) # Checks if something needs doing if [[ -z "${removableidList[@]}" ]] && [[ -z "${updateableidList[@]}" ]]; then echo "Nothing to do"; fi # Checks if target has enough space local futureidList=($(awk 'BEGIN{FS="\t"}{if($1=="true"){print $2}}' ${DB_FILE} | awk '!seen[$0]++')) local presentidList=($(awk 'BEGIN{FS="\t"}{if($1=="true" && $4!="null"){print $2}}' ${DB_FILE} | awk '!seen[$0]++')) local DU="$(( $(print_imageid_du ${GIT_DIR} ${futureidList[@]}) - $(print_imageid_du "${GIT_DIR}" ${presentidList[@]}) ))" local DF="$(df ${TARGET} --output=avail -B1 | tail -n -1)" [[ ${DU} -ge ${DF} ]] && { _msg ECHO "Target does not have enough space for deployable IMAGEID. Need ${DU}, has ${DF}."; return 2; } # Cleans target of removable TRACKID local COUNT=1 for removableid in ${removableidList[@]}; do echo "[ ${COUNT} / ${#removableidList[@]} ] Removing ${removableid}" deploy_rm "${TARGET}" "${DB_FILE}" ${removableid} >${STDERR} 2>&1 [[ $? -eq 0 ]] || { echo "[>>>>>>] Error reported"; cat ${STDERR}; local ERR=true; } local COUNT=$(( ${COUNT} + 1 )) done local COUNT=0 for imageid in ${updateableidList[@]}; do local COUNT=$(( ${COUNT} + 1 )) echo "[ ${COUNT} / ${#updateableidList[@]} ] Deploying ${imageid}" # Processes deployableids (IMAGEIDs that have TRACKIDs that are not present in target, but are selected) if _if_array_contains ${imageid} ${deployableidList[@]}; then local GIT_GET=false if [[ ! -f "${GIT_DIR}/${imageid}.flac" ]]; then git -C "${GIT_DIR}" annex get "${imageid}.flac" >${STDERR} 2>&1 local GIT_GET=true [[ $? -ne 0 ]] && { _ansi up 2; echo -en '\033[K'; echo "[ ${COUNT} / ${#updateableidList[@]} ] ${imageid} could not be downloaded from server"; cat ${STDERR}; local ERR=true; continue; } fi deploy_cp "${GIT_DIR}" "${TARGET}" "${DB_FILE}" ${imageid} > ${STDERR} 2>&1 [[ $? -ne 0 ]] && local CP_ERR=true [[ "${GIT_GET}" == "true" ]] && git -C "${GIT_DIR}" annex drop ${imageid}.flac >/dev/null 2>&1 [[ "${CP_ERR}" == "true" ]] && { _ansi up 2; echo -en '\033[K'; echo "[ ${COUNT} / ${#updateableidList[@]} ] Copy of ${imageid} completed with errors"; cat ${STDERR}; local ERR=true; continue; } fi # Processes metadata changes deploy_meta "${GIT_DIR}" "${TARGET}" "${DB_FILE}" ${imageid} >${STDERR} 2>&1 [[ $? -eq 0 ]] && { _ansi up 2; echo -en '\033[K'; echo "[ ${COUNT} / ${#updateableidList[@]} ] Deployment of ${imageid} completed successfully"; } [[ $? -ne 0 ]] && { _ansi up 2; echo -en '\033[K'; echo "[ ${COUNT} / ${#updateableidList[@]} ] Application of metadata of ${imageid} completed with errors"; cat ${STDERR}; } done gawk -i inplace -v newcommit=${NEW_COMMIT} 'BEGIN{FS="\t";OFS="\t"}{if($1=="LAST_DEPLOY"){$2=newcommit}{print $0}}' ${DB_FILE} [[ "${ERR}" == "true" ]] && return 1 || return 0 }