Scott Little ae68691bf0 Enable starlingx mirror
StarlingX needs to download a variety of rpms and tarballs
from various upstream sources.  Unfortunately the upstream sources
are not always dependable. Either servers go down, are unreachable,
or drop older content that we still depend on.

Our proposed solution is to run our own mirror to capture an
independent copy of the content needed by StarlingX.
For this purpose, a server has been set up at
http://mirror.starlingx.cengn.ca/mirror/centos

The mirror will use deterministic paths derived from the upstream
urls.  Scripts will be used to convert an upstream url to
the mirror's equivalent url (see function url_to_stx_mirror_url in
url_utils.sh)

The mirror will be refreshed daily.  New .lst entries will be
processed at that time.  Processing of changes under yum.repos.d
is not automated by this update.  Expect a follow-up update to
address this issue soon.  These scripts are found under the
'stx_mirror_scripts' subdirectory.

Changes are made to the download_mirror.sh script, and it's
supporting scripts.  New arguments have been added to each
script to select the download source.
   -s  StarlingX mirror only
   -S  StarlingX mirror, with upstream source as backup
   -u  Upstream source only
   -U  Upstream source, with StarlingX mirror as backup

You do not need to provide any of these flags.  Continue to
us download_mirror.sh as you always have.  The default
behavior is currently set to '-S', i.e. first try the
StarlingX mirror, with upstream source as backup.
If this proves to place to heavy a load on the existing
server, we might switch the default to '-U', i.e. first
try the upstream source, with StarlingX mirror as backup.
If poor download performance is seen, you might want to try
explicitly adding -U as an argument.

The remaining two options are not recommended for regular use.
Upstream only, i.e. '-u', restores original behaviour, but
you may once again encounter rpms that have aged out, and
been removed from their original repos.  StarlingX only,
i.e. '-s', is vulnerable if a .lst file has been updated,
but the mirror has not yet processed it.

Change-Id: I7e0f3d9fb99253662f9f4bf12457d39250408c0b
Story: 2003906
Task: 26785
Signed-off-by: Scott Little <scott.little@windriver.com>
2018-11-02 13:33:00 -04:00

384 lines
9.7 KiB
Bash
Executable File

#!/bin/bash
#
# SPDX-License-Identifier: Apache-2.0
#
# Daily update script for mirror.starlingx.cengn.ca covering
# tarballs and other files not downloaded from a yum repository.
# This script was originated by Scott Little.
#
# IMPORTANT: This script is only to be run on the StarlingX mirror.
# It is not for use by the general StarlinX developer.
#
# This script was originated by Scott Little.
#
DAILY_DL_SYNC_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
if [ -f "$DAILY_DL_SYNC_DIR/url_utils.sh" ]; then
source "$DAILY_DL_SYNC_DIR/url_utils.sh"
elif [ -f "$DAILY_DL_SYNC_DIR/../url_utils.sh" ]; then
source "$DAILY_DL_SYNC_DIR/../url_utils.sh"
else
echo "Error: Can't find 'url_utils.sh'"
exit 1
fi
LOGFILE=/export/log/daily_dl_sync.log
DOWNLOAD_PATH_ROOT=/export/mirror/centos
STX_TOOLS_BRANCH="master"
STX_TOOLS_BRANCH_ROOT_DIR="$HOME"
STX_TOOLS_GIT_URL="https://git.starlingx.io/stx-tools.git"
STX_TOOLS_OS_SUBDIR="centos-mirror-tools"
usage () {
echo "$0 [-b <branch>] [-d <dir>]"
echo ""
echo "Options:"
echo " -b: Use an alternate branch of stx-tools. Default is 'master'."
echo " -d: Directory where we will clone stx-tools. Default is \$HOME."
echo ""
}
while getopts "b:d:h" opt; do
case "${opt}" in
b)
# branch
STX_TOOLS_BRANCH="${OPTARG}"
if [ $"STX_TOOLS_BRANCH" == "" ]; then
usage
exit 1
fi
;;
d)
# download directory for stx-tools
STX_TOOLS_BRANCH_ROOT_DIR="${OPTARG}"
if [ "$STX_TOOLS_BRANCH_ROOT_DIR" == "" ]; then
usage
exit 1
fi
;;
h)
# Help
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
STX_TOOLS_DL_ROOT_DIR="$STX_TOOLS_BRANCH_ROOT_DIR/$STX_TOOLS_BRANCH"
STX_TOOLS_DL_DIR="$STX_TOOLS_DL_ROOT_DIR/stx-tools"
LST_FILE_DIR="$STX_TOOLS_DL_DIR/$STX_TOOLS_OS_SUBDIR"
dl_git_from_url () {
local GIT_URL="$1"
local BRANCH="$2"
local DL_DIR="$3"
local DL_ROOT_DIR=""
local SAVE_DIR
local CMD=""
SAVE_DIR="$(pwd)"
if [ "$DL_DIR" == "" ]; then
DL_DIR="$DOWNLOAD_PATH_ROOT/$(repo_url_to_sub_path "$GIT_URL" | sed 's#[.]git$##')"
fi
echo "dl_git_from_url GIT_URL='$GIT_URL' BRANCH='$BRANCH' DL_DIR='$DL_DIR'"
DL_ROOT_DIR=$(dirname "$DL_DIR")
if [ ! -d "$DL_DIR" ]; then
if [ ! -d "$DL_ROOT_DIR" ]; then
CMD="mkdir -p '$DL_ROOT_DIR'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
fi
CMD="cd '$DL_ROOT_DIR'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
CMD="git clone --bare '$GIT_URL' '$DL_DIR'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
CMD="cd '$DL_DIR'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
CMD="git --bare update-server-info"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
if [ -f hooks/post-update.sample ]; then
CMD="mv -f hooks/post-update.sample hooks/post-update"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
fi
fi
CMD="cd '$DL_DIR'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
CMD="git fetch"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
CMD="git checkout '$BRANCH'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
cd "$SAVE_DIR"
return 1
fi
cd "$SAVE_DIR"
return 0
}
dl_file_from_url () {
local URL="$1"
local DOWNLOAD_PATH=""
local DOWNLOAD_DIR=""
local PROTOCOL=""
local CMD=""
DOWNLOAD_PATH="$DOWNLOAD_PATH_ROOT/$(repo_url_to_sub_path "$URL")"
DOWNLOAD_DIR="$(dirname "$DOWNLOAD_PATH")"
PROTOCOL=$(url_protocol $URL)
echo "$PROTOCOL $URL $DOWNLOAD_PATH"
if [ -f "$DOWNLOAD_PATH" ]; then
echo "Already have '$DOWNLOAD_PATH'"
return 0
fi
case "$PROTOCOL" in
https|http)
if [ ! -d "$DOWNLOAD_DIR" ]; then
CMD="mkdir -p '$DOWNLOAD_DIR'"
echo "$CMD"
eval "$CMD"
if [ $? -ne 0 ]; then
echo "Error: $CMD"
return 1
fi
fi
CMD="wget '$URL' --tries=5 --wait=15 --output-document='$DOWNLOAD_PATH'"
echo "$CMD"
eval $CMD
if [ $? -ne 0 ]; then
echo "Error: $CMD"
return 1
fi
;;
*)
echo "Error: Unknown protocol '$PROTOCOL' for url '$URL'"
;;
esac
return 0
}
raw_dl_from_rpm_lst () {
local FILE="$1"
local RPM=""
local URL=""
local ERROR_COUNT=0
# Expected format <rpm>#<url>
grep -v '^#' $FILE | while IFS='#' read -r RPM URL; do
echo "Processing: RPM=$RPM URL=$URL"
dl_file_from_url "$URL"
ERR_COUNT=$((ERR_COUNT+$?))
done
return $ERR_COUNT
}
raw_dl_from_non_rpm_lst () {
local FILE="$1"
local TAR=""
local URL=""
local METHOD=""
local UTIL=""
local SCRIPT=""
local BRANCH=""
local SUBDIRS_FILE=""
local TARBALL_NAME=""
local ERROR_COUNT=0
# Expected format <tar-file>#<tar-dir>#<url>
# or !<tar-file>#<tar-dir>#<url>#<method>#[<util>]#[<script>]
grep -v '^#' $FILE | while IFS='#' read -r TAR DIR URL METHOD UTIL SCRIPT; do
if [ "$URL" == "" ]; then
continue
fi
echo "Processing: TAR=$TAR DIR=$DIR URL=$URL METHOD=$METHOD UTIL=$UTIL SCRIPT=$SCRIPT"
TARBALL_NAME="${TAR//!/}"
if [[ "$TAR" =~ ^'!' ]]; then
case $METHOD in
http|http_script)
dl_file_from_url "$URL"
if [ $? -ne 0 ]; then
echo "Error: Failed to download '$URL' while processing '$TARBALL_NAME'"
ERR_COUNT=$((ERR_COUNT+1))
fi
;;
http_filelist|http_filelist_script)
SUBDIRS_FILE="$LST_FILE_DIR/$UTIL"
if [ ! -f "$SUBDIRS_FILE" ]; then
echo "$SUBDIRS_FILE no found" 1>&2
ERR_COUNT=$((ERR_COUNT+1))
fi
grep -v '^#' "$SUBDIRS_FILE" | while read -r ARTF; do
if [ "$ARTF" == "" ]; then
continue
fi
dl_file_from_url "$URL/$ARTF"
if [ $? -ne 0 ]; then
echo "Error: Failed to download artifact '$ARTF' from list '$SUBDIRS_FILE' while processing '$TARBALL_NAME'"
ERR_COUNT=$((ERR_COUNT+1))
break
fi
done
;;
git|git_script)
BRANCH="$UTIL"
dl_git_from_url "$URL" "$BRANCH" ""
if [ $? -ne 0 ]; then
echo "Error: Failed to download '$URL' while processing '$TARBALL_NAME'"
ERR_COUNT=$((ERR_COUNT+1))
fi
;;
*)
echo "Error: Unknown method '$METHOD' while processing '$TARBALL_NAME'"
ERR_COUNT=$((ERR_COUNT+1))
;;
esac
else
dl_file_from_url "$URL"
if [ $? -ne 0 ]; then
echo "Error: Failed to download '$URL' while processing '$TARBALL_NAME'"
ERR_COUNT=$((ERR_COUNT+1))
fi
fi
done
return $ERR_COUNT
}
stx_tool_clone_or_update () {
local CMD
CMD="mkdir -p '$STX_TOOLS_DL_DIR'"
echo "$CMD"
eval "$CMD"
if [ $? -ne 0 ]; then
echo "Error: $CMD"
return 1
fi
dl_git_from_url "$STX_TOOLS_GIT_URL" "$STX_TOOLS_BRANCH" "$STX_TOOLS_DL_DIR"
if [ $? -ne 0 ]; then
echo "Error: Failed to download '$STX_TOOLS_GIT_URL'"
return 1;
fi
return 0
}
if [ -f $LOGFILE ]; then
rm -f $LOGFILE
fi
(
ERR_COUNT=0
stx_tool_clone_or_update
if [ $? -ne 0 ]; then
echo "Error: Failed to update stx_tools. Can't continue."
exit 1
fi
# At time of writing, only expect rpms_3rdparties.lst
RPM_LST_FILES=$(grep -l '://' $LST_FILE_DIR/rpms*.lst)
# At time of writing, only expect tarball-dl.lst
NON_RPM_FILES=$(grep -l '://' $LST_FILE_DIR/*lst | grep -v '[/]rpms[^/]*$')
for RPM_LST_FILE in $RPM_LST_FILES; do
raw_dl_from_rpm_lst "$RPM_LST_FILE"
ERR_COUNT=$((ERR_COUNT+$?))
done
for NON_RPM_FILE in $NON_RPM_FILES; do
raw_dl_from_non_rpm_lst "$NON_RPM_FILE"
ERR_COUNT=$((ERR_COUNT+$?))
done
if [ $ERR_COUNT -ne 0 ]; then
echo "Error: Failed to download $ERR_COUNT files"
exit 1
fi
exit 0
) | tee $LOGFILE
exit ${PIPESTATUS[0]}