Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup default Functions & Tasks while uninstall+ refactor installer script #4

Merged
merged 9 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions chart/stash/templates/update-status-function.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: stash.appscode.com/v1beta1
kind: Function
metadata:
name: update-status
labels:
{{- include "stash.labels" . | nindent 4 }}
spec:
image: {{ .Values.operator.registry }}/{{ .Values.operator.repository }}:{{ .Values.operator.tag }}
args:
- update-status
- --namespace=${NAMESPACE:=default}
- --repository=${REPOSITORY_NAME:=}
- --backup-session=${BACKUP_SESSION:=}
- --restore-session=${RESTORE_SESSION:=}
- --output-dir=${outputDir:=}
- --enable-status-subresource=${ENABLE_STATUS_SUBRESOURCE:=false}
hossainemruz marked this conversation as resolved.
Show resolved Hide resolved
154 changes: 123 additions & 31 deletions deploy/stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,64 @@ kubectl config current-context || {
}
echo ""

OS=""
ARCH=""
DOWNLOAD_URL=""
DOWNLOAD_DIR=""
TEMP_DIRS=()
ONESSL=""

HELM=""
CHART_NAME="stash"
CHART_LOCATION="chart"

# http://redsymbol.net/articles/bash-exit-traps/
function cleanup() {
hossainemruz marked this conversation as resolved.
Show resolved Hide resolved
rm -rf $ONESSL ca.crt ca.key server.crt server.key
rm -rf ca.crt ca.key server.crt server.key
# remove temporary directories
for dir in "${TEMP_DIRS[@]}"; do
rm -rf "${dir}"
done
}

# detect operating system
function detectOS() {
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')

case "$OS" in
# Minimalist GNU for Windows
cygwin* | mingw* | msys*) OS='windows';;
esac
}

# detect machine architecture
function detectArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5";;
armv6*) ARCH="armv6";;
armv7*) ARCH="arm";;
aarch64) ARCH="arm64";;
x86) ARCH="386";;
x86_64) ARCH="amd64";;
i686) ARCH="386";;
i386) ARCH="386";;
esac
}

detectOS
detectArch

# download file pointed by DOWNLOAD_URL variable
# store download file to the directory pointed by DOWNLOAD_DIR variable
# you have to sent the output file name as argument. i.e. downloadFile myfile.tar.gz
function downloadFile() {
if curl --output /dev/null --silent --head --fail "$DOWNLOAD_URL"; then
curl -fsSL ${DOWNLOAD_URL} -o $DOWNLOAD_DIR/$1
else
echo "File does not exist"
exit 1
fi
}

export APPSCODE_ENV=${APPSCODE_ENV:-prod}
Expand All @@ -35,37 +90,26 @@ onessl_found() {
return 1
}

# download onessl if it does not exist
onessl_found || {
echo "Downloading onessl ..."
if [[ "$(uname -m)" == "aarch64" ]]; then
curl -fsSL -o onessl https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-linux-arm64
chmod +x onessl
export ONESSL=./onessl
else
# ref: https://stackoverflow.com/a/27776822/244009
case "$(uname -s)" in
Darwin)
curl -fsSL -o onessl https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-darwin-amd64
chmod +x onessl
export ONESSL=./onessl
;;

Linux)
curl -fsSL -o onessl https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-linux-amd64
chmod +x onessl
export ONESSL=./onessl
;;

CYGWIN* | MINGW* | MSYS*)
curl -fsSL -o onessl.exe https://github.com/kubepack/onessl/releases/download/0.10.0/onessl-windows-amd64.exe
chmod +x onessl.exe
export ONESSL=./onessl.exe
;;
*)
echo 'other OS'
;;
esac
fi

ARTIFACT="https://github.com/kubepack/onessl/releases/download/0.10.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If we need to change URL we won't need to change for every OS.
  2. Make os detection/download part reusable. If we need to download another binary in future, we would be able to reuse .
  3. Support more architecture. In future, we may need to support ARM.

ONESSL_BIN=onessl-${OS}-${ARCH}
case "$OS" in
cygwin* | mingw* | msys*)
ONESSL_BIN=${ONESSL_BIN}.exe
;;
esac

DOWNLOAD_URL=${ARTIFACT}/${ONESSL_BIN}
DOWNLOAD_DIR="$(mktemp -dt onessl-XXXXXX)"
TEMP_DIRS+=($DOWNLOAD_DIR) # store DOWNLOAD_DIR to cleanup later

downloadFile $ONESSL_BIN # downloaded file name will be saved as the value of ONESSL_BIN variable

export ONESSL=${DOWNLOAD_DIR}/${ONESSL_BIN}
chmod +x $ONESSL
}

# ref: https://stackoverflow.com/a/7069755/244009
Expand Down Expand Up @@ -311,7 +355,8 @@ if [ "$STASH_UNINSTALL" -eq 1 ]; then
kubectl delete secret stash-apiserver-cert --namespace $PROMETHEUS_NAMESPACE || true
# delete psp resources
kubectl delete psp stash-operator-psp stash-backup-job stash-backupsession-cron stash-restore-job || true

# delete update-status function
kubectl delete function update-status || true
echo "waiting for stash operator pod to stop running"
for (( ; ; )); do
pods=($(kubectl get pods --namespace $STASH_NAMESPACE -l app=stash -o jsonpath='{range .items[*]}{.metadata.name} {end}'))
Expand Down Expand Up @@ -472,6 +517,53 @@ for crd in "${non_namespaced_crds[@]}"; do
}
done

# Install "update-status" Function.
# we only need to resolve the variables of 'spec.image' part and keep others.
# we can't use onessl to resolve the variables as it will try to resolve all the variables.
# we will use helm to generate yaml from template then apply it.
if [ -x "$(command -v helm)" ]; then
export HELM=helm
else
echo "Helm is not installed!. Downloading Helm."
ARTIFACT="https://get.helm.sh"
HELM_VERSION="v2.14.1"
HELM_BIN=helm
HELM_DIST=${HELM_BIN}-${HELM_VERSION}-${OS}-${ARCH}.tar.gz

case "$OS" in
cygwin* | mingw* | msys*)
HELM_BIN=${HELM_BIN}.exe
;;
esac

DOWNLOAD_URL=${ARTIFACT}/${HELM_DIST}
DOWNLOAD_DIR="$(mktemp -dt helm-XXXXXX)"
TEMP_DIRS+=($DOWNLOAD_DIR)

downloadFile $HELM_DIST

tar xf ${DOWNLOAD_DIR}/${HELM_DIST} -C ${DOWNLOAD_DIR}
export HELM=${DOWNLOAD_DIR}/${OS}-${ARCH}/${HELM_BIN}
chmod +x $HELM
fi

if [[ "$APPSCODE_ENV" == "dev" ]]; then
CHART_LOCATION="chart"
else
# download chart from remove repository and extract into a temporary directory
CHART_LOCATION="$(mktemp -dt appscode-XXXXXX)"
TEMP_DIRS+=(${CHART_LOCATION})
TEMP_INSTALLER_REPO="${CHART_NAME}-installer"
$HELM repo add "${TEMP_INSTALLER_REPO}" "https://charts.appscode.com/stable"
$HELM fetch --untar --untardir ${CHART_LOCATION} "${TEMP_INSTALLER_REPO}/${CHART_NAME}"
$HELM repo remove "${TEMP_INSTALLER_REPO}"
fi

$HELM template ${CHART_LOCATION}/${CHART_NAME} -x templates/update-status-function.yaml \
hossainemruz marked this conversation as resolved.
Show resolved Hide resolved
--set operator.registry=${STASH_DOCKER_REGISTRY} \
--set operator.tag=${STASH_IMAGE_TAG} \
| kubectl apply -f -

if [ "$STASH_ENABLE_VALIDATING_WEBHOOK" = true ]; then
echo "checking whether admission webhook(s) are activated or not"
active=$($ONESSL wait-until-has annotation \
Expand Down