496 lines
13 KiB
Bash
Executable File
496 lines
13 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
function_printhelp () {
|
|
|
|
echo -e "-"
|
|
echo -e " Usage: radius [command]"
|
|
echo -e "-"
|
|
|
|
ECO=" :\033[31m\e[1mCOMMAND:DESCRIPTION${NC}\033[0m\n"
|
|
ECO+="-\n"
|
|
ECO+=" :[ update ]:Updates from remote git server.\n"
|
|
ECO+=" :[ list-release ]:List all releases in project.\n"
|
|
ECO+=" :[ list-branches ]:List all branches in project.\n"
|
|
ECO+="-\n"
|
|
|
|
printf "$ECO" | column -t -s ':'
|
|
|
|
}
|
|
|
|
function_app_init () {
|
|
|
|
PHP_BIN="$(type -p "php")"
|
|
|
|
if [ $? -ne 0 ] || [ ! -n ${PHP_BIN} ]; then
|
|
|
|
function_log "error: ${LIGHT_RED}PHP is not installed or not in PATH.${NC}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
COMPOSER_BIN="$(type -p "composer")"
|
|
|
|
if [ $? -ne 0 ] || [ ! -n ${COMPOSER_BIN} ]; then
|
|
|
|
function_log "error: ${LIGHT_RED}composer is not installed or not in PATH.${NC}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
NODE_BIN="$(type -p "node")"
|
|
|
|
if [ $? -ne 0 ] || [ ! -n ${NODE_BIN} ]; then
|
|
|
|
function_log "error: ${LIGHT_RED}node.js is not installed or not in PATH.${NC}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
NPM_BIN="$(type -p "npm")"
|
|
|
|
if [ $? -ne 0 ] || [ ! -n ${NPM_BIN} ]; then
|
|
|
|
function_log "error: ${LIGHT_RED}npm is not installed or not in PATH.${NC}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ ! -d "${DIR_TMP}" ] || [ ! -w "${DIR_TMP}" ]; then
|
|
|
|
function_log "error: ${LIGHT_RED}temporary directory does not exists or not writable.${NC}" true
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
function_app_update () {
|
|
|
|
echo -e "-"
|
|
|
|
function_log "info: ${DARK_GREEN}Gathering information...${NC}" true
|
|
|
|
echo -e "-\n"
|
|
|
|
ECO=" ${LIGHT_RED}SETTING|VALUE${NC}\n"
|
|
ECO+="-\n"
|
|
ECO+=" ${DARK_GREEN}Remote Host${NC}|$REMOTE_HOSTNAME\n";
|
|
ECO+=" ${DARK_GREEN}Remote Port${NC}|$REMOTE_PORT\n";
|
|
ECO+=" ${DARK_GREEN}Enable SSL${NC}|$(function_strtoupper $USE_SSL)\n";
|
|
ECO+=" ${DARK_GREEN}Repo Name${NC}|$PROJECT_NAME\n";
|
|
ECO+=" ${DARK_GREEN}Repo User${NC}|$PROJECT_USER\n";
|
|
ECO+=" ${DARK_GREEN}Repo Branch${NC}|$PROJECT_BRANCH\n";
|
|
ECO+=" ${DARK_GREEN}Temp Directory${NC}|$DIR_TMP\n";
|
|
ECO+=" ${DARK_GREEN}PHP Engine${NC}|$($PHP_BIN -v | head -n 1)\n";
|
|
ECO+=" ${DARK_GREEN}Composer${NC}|v$($COMPOSER_BIN about | sed -n '1 p' | grep -i 'Composer - Depen' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')\n";
|
|
ECO+=" ${DARK_GREEN}Node.js${NC}|$($NODE_BIN -v)\n";
|
|
ECO+=" ${DARK_GREEN}NPM${NC}|v$($NPM_BIN -v)\n";
|
|
|
|
printf "$ECO" | column -t -s '|'
|
|
|
|
echo -e "-"
|
|
|
|
echo -e "Remeber this will download source code from remote git server branch or a release than it will synchronize"
|
|
echo -e "source and root directory using rsync command. This rync command is configured with exclude and include list."
|
|
echo -e "You must configure this exclude and include rsync config files before, only than proceed. If you don't this"
|
|
echo -e "can delete some usefull files and directories too."
|
|
|
|
echo -e "-"
|
|
|
|
ABORT=true
|
|
|
|
if [ "${MODE_QUIET}" = false ]; then
|
|
|
|
read -p $'\e[92m\e[1mDo you want to procced with above mentioned configuartion ?\e[0m\e[0m Y/N default N: ' ASK
|
|
|
|
echo -e "-"
|
|
|
|
if [ "$(function_strtolower "${ASK}")" != "y" ] || [ "$(function_strtolower "${ASK}")" != "yes" ]; then
|
|
|
|
ABORT=false
|
|
|
|
fi
|
|
|
|
if [ "${ABORT}" == true ]; then
|
|
|
|
function_log "info: ${LIGHT_RED}aborted by user.${NC}"
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
function_log "info: ${LIGHT_GREEN}moving on...${NC}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ "${REMOTE_PORT}" == "80" ]; then
|
|
|
|
REMOTE_PORT=""
|
|
|
|
elif [ "${REMOTE_PORT}" == "443" ]; then
|
|
|
|
USE_SSL="YES"
|
|
REMOTE_PORT=""
|
|
|
|
fi
|
|
|
|
if [ "${USE_SSL}" == "" ] || [ "$(function_strtolower $USE_SSL)" == "yes" ]; then
|
|
|
|
REMOTE_URL="https://";
|
|
|
|
else
|
|
|
|
REMOTE_URL="https://"
|
|
|
|
fi
|
|
|
|
REMOTE_URL+="${REMOTE_HOSTNAME}"
|
|
|
|
if [ "${REMOTE_PORT}" != "" ]; then
|
|
|
|
REMOTE_URL+=":${REMOTE_PORT}"
|
|
|
|
fi
|
|
|
|
REMOTE_URL+="/api/v1/repos/${PROJECT_USER}/${PROJECT_NAME}/archive/${PROJECT_BRANCH}.zip"
|
|
|
|
LOCAL_TAG="${PROJECT_NAME}-${PROJECT_BRANCH}"
|
|
LOCAL_DSTI="${DIR_TMP}/${LOCAL_TAG}.zip"
|
|
LOCAL_SORC="${DIR_TMP}/${LOCAL_TAG}"
|
|
|
|
if [ -f "${LOCAL_DSTI}" ]; then
|
|
|
|
echo -e "-"
|
|
|
|
function_log "warning: ${LIGHT_CYAN}found previously downloaded file in destination directory.${NC}" true
|
|
|
|
function_log "info: ${LIGHT_GREEN}removing....${NC}"
|
|
|
|
rm -f "${LOCAL_DSTI}" &
|
|
|
|
BACK_PID=$!
|
|
|
|
wait $BACK_PID
|
|
|
|
function_log "info: ${LIGHT_GREEN}removed successfully.${NC}" true
|
|
|
|
echo -e "-"
|
|
|
|
fi
|
|
|
|
if [ -d "${LOCAL_SORC}" ]; then
|
|
|
|
echo -e "-"
|
|
|
|
function_log "warning: ${LIGHT_CYAN}found previously extracted source directory.${NC}" true
|
|
|
|
function_log "info: ${LIGHT_GREEN}removing....${NC}"
|
|
|
|
rm -fR "${LOCAL_SORC}" &
|
|
|
|
BACK_PID=$!
|
|
|
|
wait $BACK_PID
|
|
|
|
function_log "info: ${LIGHT_GREEN}removed successfully.${NC}" true
|
|
|
|
echo -e "-"
|
|
|
|
fi
|
|
|
|
function_log "info: ${LIGHT_GREEN}Downloading - ${REMOTE_URL}.${NC}" true
|
|
function_log "info: ${LIGHT_GREEN}Destination - ${LOCAL_DSTI}.${NC}" true
|
|
|
|
function_log "info: ${DARK_GREEN}fetching project archive from remote server....${NC}"
|
|
|
|
if [ "${MODE_QUIET}" == false ]; then
|
|
|
|
wget --header="Accept: application/json" \
|
|
--header="Authorization: Bearer ${AUTH_TOKEN}" \
|
|
--header="Cache-Control: no-cache" \
|
|
--no-check-certificate \
|
|
-O "${LOCAL_DSTI}" \
|
|
"${REMOTE_URL}" &
|
|
|
|
else
|
|
|
|
wget --header="Accept: application/json" \
|
|
--header="Authorization: Bearer ${AUTH_TOKEN}" \
|
|
--header="Cache-Control: no-cache" \
|
|
--no-check-certificate \
|
|
--quiet \
|
|
-O "${LOCAL_DSTI}" \
|
|
"${REMOTE_URL}" &
|
|
|
|
fi
|
|
|
|
BACK_PID=$!
|
|
|
|
wait $BACK_PID
|
|
|
|
if [ -f "${LOCAL_DSTI}" ]; then
|
|
|
|
function_log "info: ${LIGHT_GREEN}downloaded successfully.${NC}" true
|
|
|
|
else
|
|
|
|
function_log "error: ${LIGHT_RED}failed to download project repository.${NC}" true
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
function_log "info: ${LIGHT_GREEN}extracting....${NC}"
|
|
|
|
unzip "${LOCAL_DSTI}" -d "$LOCAL_SORC" > /dev/null
|
|
|
|
if [ -d "${LOCAL_SORC}" ]; then
|
|
|
|
function_log "info: ${LIGHT_GREEN}checking extracted files......${NC}" true
|
|
|
|
[ ! -f "${LOCAL_SORC}/$(function_strtolower "${PROJECT_NAME}")/${PROJECT_ENTRY_FILE}" ] && { function_log "error: ${LIGHT_RED}looks like extracted directory is corrupted. Aborting.${NC}" true; exit 1; }
|
|
|
|
function_log "info: ${LIGHT_GREEN}extracted successfully.${NC}" true
|
|
|
|
else
|
|
|
|
function_log "error: ${LIGHT_RED}failed to extract downloaded file.${NC}" true
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
function_log "info: ${DARK_GREEN}Starting updating....${NC}" true
|
|
|
|
if [ ! -d "${DIR_ROOT}" ]; then
|
|
|
|
function_log "info: ${DARK_GREEN}creating root directory....${NC}" true
|
|
|
|
mkdir -p "${DIR_ROOT}"
|
|
|
|
fi
|
|
|
|
function_log "info: ${LIGHT_GREEN}Root Directory - ${DIR_ROOT}.${NC}" true
|
|
|
|
if [ ! -w "${DIR_ROOT}" ]; then
|
|
|
|
function_log "error: ${LIGHT_RED}configured root directory is not writable.${NC}" true
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
function_log "info: ${LIGHT_GREEN}Source Directory - ${LOCAL_SORC}/$(function_strtolower "${PROJECT_NAME}")/.${NC}" true
|
|
|
|
function_log "info: ${DARK_GREEN}Updating...${NC}" true
|
|
|
|
rsync -at --human-readable --stats --delete \
|
|
--include-from "${DIR_CONFIG}/include.rsync.conf" \
|
|
--exclude-from "${DIR_CONFIG}/exclude.rsync.conf" \
|
|
"${LOCAL_SORC}/$(function_strtolower "${PROJECT_NAME}")/" "${DIR_ROOT}" &
|
|
|
|
BACK_PID=$!
|
|
|
|
wait $BACK_PID
|
|
|
|
function_log "info: ${DARK_GREEN}updation done.${NC}" true
|
|
|
|
function_log "info: ${DARK_GREEN}erasing temporary data...${NC}" true
|
|
|
|
[ -f "$LOCAL_DSTI" ] && { rm -f $LOCAL_DSTI; }
|
|
|
|
[ -d "$LOCAL_SORC" ] && { rm -fR $LOCAL_SORC; }
|
|
|
|
function_log "info: ${DARK_GREEN}removed temporary data.${NC}" true
|
|
|
|
function_log "info: ${DARK_GREEN}erasing junk data.....${NC}" true
|
|
|
|
cd "${DIR_ROOT}"
|
|
|
|
find . \( -path ./node_modules -o -path ./vendor \) -prune -false -o \( -name ".gitkeep" -o -name ".gitignore" \) -exec rm -f {} \;
|
|
|
|
# Automatically remove log files older than 30 days
|
|
find "${DIR_LOG}" -name "*.log" -type f -mtime +30 -delete
|
|
|
|
function_log "info: ${DARK_GREEN}removed junk data.${NC}" true
|
|
|
|
function_log "info: ${LIGHT_GREEN}all done.${NC}" true
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
function_app_list_release () {
|
|
|
|
if [ "${REMOTE_PORT}" == "80" ]; then
|
|
|
|
REMOTE_PORT=""
|
|
|
|
elif [ "${REMOTE_PORT}" == "443" ]; then
|
|
|
|
USE_SSL="YES"
|
|
REMOTE_PORT=""
|
|
|
|
fi
|
|
|
|
if [ "${USE_SSL}" == "" ] || [ "$(function_strtolower $USE_SSL)" == "yes" ]; then
|
|
|
|
REMOTE_URL="https://";
|
|
|
|
else
|
|
|
|
REMOTE_URL="https://"
|
|
|
|
fi
|
|
|
|
REMOTE_URL+="${REMOTE_HOSTNAME}"
|
|
|
|
if [ "${REMOTE_PORT}" != "" ]; then
|
|
|
|
REMOTE_URL+=":${REMOTE_PORT}"
|
|
|
|
fi
|
|
|
|
RELEASE_URL="${REMOTE_URL}/api/v1/repos/${PROJECT_USER}/${PROJECT_NAME}/releases"
|
|
|
|
function_log "info: ${DARK_GREEN}fetching list of all releases.${NC}" true
|
|
|
|
RAW_JSON=$(curl -s -k -H "Authorization: Bearer ${AUTH_TOKEN}" "$RELEASE_URL")
|
|
|
|
if [ -z "$RAW_JSON" ] || [ "$RAW_JSON" == "[]" ]; then
|
|
|
|
function_log "info: ${DARK_GREEN}no releases found for project repository.${NC}" true
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
echo -e "-"
|
|
echo -e "${BOLD}Available Repository Releases:${NC}"
|
|
echo -e "--------------------------------------------------"
|
|
|
|
echo "$RAW_JSON" | grep -o '{"id":[^{]*' | while read -r row; do
|
|
|
|
TAG=$(echo "$row" | grep -o '"tag_name":"[^"]*' | cut -d'"' -f4 || true)
|
|
NAME=$(echo "$row" | grep -o '"name":"[^"]*' | cut -d'"' -f4 || true)
|
|
IS_PRE=$(echo "$row" | grep -o '"prerelease":[a-z]*' | cut -d':' -f2 || true)
|
|
|
|
if [ -z "$TAG" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if [ -z "$NAME" ]; then
|
|
|
|
NAME="No Title"
|
|
|
|
fi
|
|
|
|
if [ -z "$FIRST_RELEASE" ]; then
|
|
|
|
FIRST_RELEASE="done"
|
|
echo -e " ${LIGHT_GREEN}➔ ${TAG} - ${NAME} (${BOLD}LATEST / ACTIVE${NC}${LIGHT_GREEN})${NC}"
|
|
|
|
else
|
|
|
|
if [ "$IS_PRE" == "true" ]; then
|
|
|
|
echo -e " ${LIGHT_CYAN}${TAG}${NC} - ${NAME} (Pre-release)"
|
|
|
|
else
|
|
|
|
echo -e " ${TAG} - ${NAME}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo -e "--------------------------------------------------"
|
|
echo -e "\n"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
function_app_list_branches () {
|
|
|
|
if [ "${REMOTE_PORT}" == "80" ]; then
|
|
|
|
REMOTE_PORT=""
|
|
|
|
elif [ "${REMOTE_PORT}" == "443" ]; then
|
|
|
|
USE_SSL="YES"
|
|
REMOTE_PORT=""
|
|
|
|
fi
|
|
|
|
if [ "${USE_SSL}" == "" ] || [ "$(function_strtolower $USE_SSL)" == "yes" ]; then
|
|
|
|
REMOTE_URL="https://";
|
|
|
|
else
|
|
|
|
REMOTE_URL="https://"
|
|
|
|
fi
|
|
|
|
REMOTE_URL+="${REMOTE_HOSTNAME}"
|
|
|
|
if [ "${REMOTE_PORT}" != "" ]; then
|
|
|
|
REMOTE_URL+=":${REMOTE_PORT}"
|
|
|
|
fi
|
|
|
|
RELEASE_URL="${REMOTE_URL}/api/v1/repos/${PROJECT_USER}/${PROJECT_NAME}/branches"
|
|
|
|
function_log "info: ${DARK_GREEN}fetching list of all branches.${NC}" true
|
|
|
|
RAW_JSON=$(curl -s -k -H "Authorization: Bearer ${AUTH_TOKEN}" "$RELEASE_URL")
|
|
|
|
if [ -z "$RAW_JSON" ] || [ "$RAW_JSON" == "[]" ]; then
|
|
|
|
function_log "info: ${DARK_GREEN}no branch found for project repository.${NC}" true
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
echo -e "-"
|
|
echo -e "${BOLD}Available Repository Branches:${NC}"
|
|
echo -e "--------------------------------------------------"
|
|
|
|
echo "$RAW_JSON" | grep -o '{"name":"[^"]*","commit"' | while read -r row; do
|
|
|
|
NAME=$(echo "$row" | grep -o '"name":"[^"]*' | cut -d'"' -f4)
|
|
|
|
if [ "$NAME" == "main" ]; then
|
|
|
|
echo -e " ${LIGHT_GREEN}➔ ${NAME} (${BOLD}DEFAULT / ACTIVE${NC}${LIGHT_GREEN})${NC}"
|
|
|
|
else
|
|
|
|
echo -e " ${NAME}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo -e "--------------------------------------------------"
|
|
echo -e "\n"
|
|
|
|
exit 0
|
|
|
|
} |