Merge "stx-init-env: download builder images"

This commit is contained in:
Zuul 2021-11-04 15:00:33 +00:00 committed by Gerrit Code Review
commit b4163b676f
3 changed files with 177 additions and 57 deletions

2
import-stx Executable file → Normal file
View File

@ -1,4 +1,4 @@
#!/bin/bash # bash
if [ -z "$MINIKUBE_HOME" ];then if [ -z "$MINIKUBE_HOME" ];then
MINIKUBE_HOME=$HOME MINIKUBE_HOME=$HOME

View File

@ -1,78 +1,200 @@
#!/bin/bash #!/bin/bash
# Starlingx Debian Build Enviroment Setup Script if [[ "$0" != "${BASH_SOURCE[0]}" ]] ; then
echo >&2 "Don't source this file, execute it instead, eg: ${BASH_SOURCE[0]} --help"
# Normally this is called as 'source stx-init-env <sharedvolumedir>'
# Also we can execute the command 'source stx-init-env' as default
source import-stx
if [ $? -ne 0 ]; then
return 1 return 1
fi fi
usage() {
cat <<END
Usage: $0 OPTIONS
Initialize StarlingX build environment & (re-)start builder pods
--nuke delete minikube cluster and exit
-R,--restart-minikube
restart minikube cluster before starting pods
--rebuild build pod images instead of downloading them
END
}
notice() {
local tty_on tty_off
if [[ -t 2 ]] ; then
tty_on=$'\033[1;36m'
tty_off=$'\033[0m'
fi
echo >&2 "${tty_on}$*${tty_off}"
}
PROGNAME=$(basename "$0")
MINIKUBE=minikube MINIKUBE=minikube
HELM=helm HELM=helm
DOCKER=docker DOCKER=docker
PYTHON3=python3
DOCKER_PREFIX="starlingx/"
DOCKER_IMAGES="stx-builder stx-pkgbuilder stx-lat-tool stx-aptly"
DOCKER_TAG="master-debian-latest"
DOCKER_TAG_LOCAL="v0.1.0"
BUILD_DOCKER=0
DELETE_MINIKUBE=0
RESTART_MINIKUBE=0
minikube_started() {
docker ps | grep kicbase | grep -q $MINIKUBENAME
}
minikube_exists() {
local script=$(cat <<'END'
import json,sys
data = json.load (sys.stdin)
for x in data['valid']+data['invalid']:
if x['Name'] == sys.argv[1]:
sys.exit(0)
sys.exit(1)
END
)
$MINIKUBE profile list -l -o json | $PYTHON3 -c "$script" "$MINIKUBENAME"
}
cmdline_error() {
if [[ -n "$1" ]] ; then
echo "error: $1" >&2
fi
echo "Type \`$0 --help' for more info." >&2
exit 1
}
# process command line
temp=$(getopt -o hR --long help,restart-minikube,rebuild,nuke -n "$PROGNAME" -- "$@") || cmdline_error
eval set -- "$temp"
while true ; do
case "$1" in
-h|--help)
usage
exit 0
;;
-R|--restart-minikube)
RESTART_MINIKUBE=1
shift
;;
--rebuild)
BUILD_DOCKER=1
shift
;;
--nuke)
DELETE_MINIKUBE=1
shift
;;
--)
shift
break
;;
-?*)
cmdline_error
;;
*)
break
;;
esac
done
[[ "$#" -le 0 ]] || cmdline_error "too many arguments"
# import environment
source import-stx || return 1
# make sure required programs are installed
if ! command -v $MINIKUBE &> /dev/null; then if ! command -v $MINIKUBE &> /dev/null; then
echo "Command $MINIKUBE could not be found." echo >&2 "Command $MINIKUBE could not be found."
echo "Please install it as https://minikube.sigs.k8s.io/docs/start/" echo >&2 "Please install it as https://minikube.sigs.k8s.io/docs/start/"
echo "" echo ""
exit 1
fi fi
if ! command -v $HELM &> /dev/null; then if ! command -v $HELM &> /dev/null; then
echo "Command $HELM could not be found." echo >&2 "Command $HELM could not be found."
echo "Please install it as https://helm.sh/" echo >&2 "Please install it as https://helm.sh/"
echo "" echo ""
exit 1
fi fi
if ! command -v $DOCKER &> /dev/null; then if ! command -v $DOCKER &> /dev/null; then
echo "Command $DOCKER could not be found. Please install it." echo >&2 "Command $DOCKER could not be found. Please install it."
echo "" echo >&2 ""
exit 1
fi fi
MINIKUBE_EXIST=$(docker ps | grep kicbase | grep $MINIKUBENAME) # Make sure $STX_BUILD_HOME exists
if [ x"$MINIKUBE_EXIST" == x"" ]; then if [[ ! -d "$STX_BUILD_HOME" ]] ; then
echo "Start the new minikube $MINIKUBENAME" echo >&2 "The directory $STX_BUILD_HOME doesn't exist, please create it with the command:"
else echo >&2 ""
echo "The minikube $MINIKUBENAME exists, we will try to stop it..." echo >&2 " mkdir -p $STX_BUILD_HOME"
echo "Then restart it..." echo >&2 ""
echo "" echo >&2 "Then execute this script again!"
$MINIKUBE stop -p $MINIKUBENAME >> /dev/null exit 1
ret=$(docker ps | grep kicbase | grep $MINIKUBENAME)
if [ x"$ret" != x"" ]; then
echo "minikube container $MINIKUBENAME exist!"
echo "And the command 'minikube -p $MINIKUBENAME stop' failed. The reason may be"
echo "the current MINIKUBE_HOME/HOME is not the same as the $MINIKUBENAME"
echo "Please change the MINIKUBE_HOME/HOME directory to the previous value"
echo "then re-execute this script"
unset MINIKUBE_HOME
return 1
fi
fi fi
if [ ! -d "$STX_BUILD_HOME" ]; then # --nuke: just delete the cluster and exit
echo "Warning: The directory $STX_BUILD_HOME doesn't exist, please create it with the command:" if [[ $DELETE_MINIKUBE -eq 1 ]] ; then
echo "" if minikube_exists ; then
echo " mkdir -p $STX_BUILD_HOME" notice "Deleting minikube cluster \`$MINIKUBENAME'"
echo "" $MINIKUBE delete -p "$MINIKUBENAME" || exit 1
echo "Then execute this script again!" fi
return 1 exit 0
fi fi
$MINIKUBE start --driver=docker -p $MINIKUBENAME --mount=true --mount-string="$STX_BUILD_HOME:/workspace" # Stop minikube if necessary
if [ $? -ne 0 ]; then WANT_START_MINIKUBE=0
return 1 if [[ $RESTART_MINIKUBE -eq 1 ]] ; then
if minikube_started ; then
notice "Stopping minikube cluster \`$MINIKUBENAME'"
$MINIKUBE stop -p $MINIKUBENAME
if minikube_started ; then
echo >&2 "minikube container $MINIKUBENAME exist!"
echo >&2 "And the command 'minikube -p $MINIKUBENAME stop' failed. The reason may be"
echo >&2 "the current MINIKUBE_HOME/HOME is not the same as the $MINIKUBENAME"
echo >&2 "Please change the MINIKUBE_HOME/HOME directory to the previous value"
echo >&2 "then re-execute this script"
exit 1
fi
fi
WANT_START_MINIKUBE=1
elif ! minikube_started ; then
WANT_START_MINIKUBE=1
fi fi
# Build the container images from the dockerfiles # Start minikube
DOCKERNAMES="stx-builder stx-pkgbuilder stx-lat-tool stx-aptly" if [[ $WANT_START_MINIKUBE -eq 1 ]] ; then
notice "Starting minikube cluster \`$MINIKUBENAME'"
$MINIKUBE start --driver=docker -p $MINIKUBENAME \
--mount=true \
--mount-string="$STX_BUILD_HOME:/workspace" \
|| exit 1
fi
# Import minikube's docker environment
eval $(minikube -p $MINIKUBENAME docker-env) eval $(minikube -p $MINIKUBENAME docker-env)
for i in $DOCKERNAMES; do # Build container images
docker build -t $i:v0.1.0 -f stx/dockerfiles/$i.Dockerfile . if [[ $BUILD_DOCKER -eq 1 ]] ; then
if [ $? -ne 0 ]; then notice "Building docker images"
return 1 for img in $DOCKER_IMAGES; do
fi docker build --no-cache -t $img:$DOCKER_TAG_LOCAL -f stx/dockerfiles/$img.Dockerfile . || exit 1
done done
# else: download and retag
else
notice "Pulling docker images"
for img in $DOCKER_IMAGES; do
docker pull ${DOCKER_PREFIX}${img}:${DOCKER_TAG} || exit 1
docker tag ${DOCKER_PREFIX}${img}:${DOCKER_TAG} ${img}:${DOCKER_TAG_LOCAL} || exit 1
done
fi
# Restart pods
notice "Restarting pods"
stx control stop || exit 1
stx control start || exit 1
eval $(minikube -p $MINIKUBENAME docker-env -u)

View File

@ -180,9 +180,8 @@ stx-pkgbuilder/configmap/')
cmd = 'helm install ' + projectname + ' ' + helmchartdir cmd = 'helm install ' + projectname + ' ' + helmchartdir
self.logger.debug('Execute the helm start command: %s', cmd) self.logger.debug('Execute the helm start command: %s', cmd)
if helmstatus: if helmstatus:
self.logger.error('The helm release %s already exists.', self.logger.warning('The helm release %s already exists - nothing to do',
projectname) projectname)
sys.exit(1)
else: else:
repomgr_type = self.finish_configure() repomgr_type = self.finish_configure()
subprocess.check_call(cmd, shell=True, cwd=os.environ['PRJDIR']) subprocess.check_call(cmd, shell=True, cwd=os.environ['PRJDIR'])
@ -195,9 +194,8 @@ stx-pkgbuilder/configmap/')
self.logger.debug('Execute the helm stop command: %s', cmd) self.logger.debug('Execute the helm stop command: %s', cmd)
subprocess.check_call(cmd, shell=True) subprocess.check_call(cmd, shell=True)
else: else:
self.logger.error('The helm release %s does not exist.', self.logger.warning('The helm release %s does not exist - nothing to do',
projectname) projectname)
sys.exit(1)
def handleUpgradeTask(self, helmstatus, projectname): def handleUpgradeTask(self, helmstatus, projectname):
command.check_prjdir_env() command.check_prjdir_env()