Skip to content
libre 3.38 KiB
Newer Older
Pierre Ozoux's avatar
Pierre Ozoux committed
#!/bin/bash
export NS=`pwd | rev | cut -d'/' -f 2 | rev | sed 's/\./-/'`
export DOMAIN=`pwd | rev | cut -d'/' -f 2 | rev`
export SUBDOMAIN=`pwd | rev | cut -d'/' -f 1 | rev`
export FQDN=${SUBDOMAIN}-${NS}
export FQDN_DOTS=${SUBDOMAIN}.`echo $DOMAIN | sed 's/\(.*\)-/\1./'`
export PG_DOMAIN=$NS
export EPOCH=`date "+%s"`
export NUAGE_URL=`kubectl -n $NS get ing -o custom-columns=HOST:spec.rules[*].host --selector=app.kubernetes.io/name=nextcloud --no-headers=true`
export BUCKET=${FQDN}
export S3_ENDPOINT=hot-objects.liiib.re
Pierre Ozoux's avatar
Pierre Ozoux committed
export S3_ENDPOINT_COLD=https://cold-objects.liiib.re
Pierre Ozoux's avatar
Pierre Ozoux committed
export S3_SECRET=${NS}-dumps

for key in `kubectl -n ${NS} get secrets ${S3_SECRET} -o json | jq -r '.data | keys | .[]'`; do
  #echo "loading $key into env..."
  export $key=`kubectl -n ${NS} get secrets ${S3_SECRET} -o json | jq -r ".data.$key" | base64 -d`
  #echo "done."
done


function show_usage {
  echo "Usage:"
  echo " - apply"
  echo " - diff"
  echo " - update"
  echo " - changens"
  exit 1
}

if [ $# -eq 0 ]; then
  show_usage
fi

export VERSION=stable
source ./.env
Pierre Ozoux's avatar
Pierre Ozoux committed
#cat ./version.php | grep 'array(' | cut -d\( -f2 | cut -d\) -f1 | sed 's/,/\./g'
Pierre Ozoux's avatar
Pierre Ozoux committed
if [ "${APP}" = "nextcloud" ]; then
  export REPLICAS="${REPLICAS:-2}"
Timothee Gosselin's avatar
Timothee Gosselin committed
  export NC_VERSION=26.0.8.2
  export NC_IMAGE_TAG=26.0.8-2023-10-30T10-37-45Z 
  export NC_WEB_IMAGE_TAG=web-26.0.8-2023-10-30T10-37-45Z 
elif [ "${APP}" = "rocketchat" ]; then
  export REPLICAS="${REPLICAS:-2}"
Pierre Ozoux's avatar
Pierre Ozoux committed
fi

create_update_job_nc () {
  if [ "${APP}" = "nextcloud" ]; then
    kubectl -n $NS delete job update-nc
    libre apply ../../common/nextcloud/other-manifests/update-job.yml
  fi
}

case "$1" in
  apply|diff)
    if [ -z "$2" ]; then
      manifest="../../common/$APP/manifests/app.yml"
      if [ -f "$manifest" ]; then
        cat "${manifest}" | envsubst '$BUCKET $NC_VERSION $NC_IMAGE_TAG $NC_WEB_IMAGE_TAG $ALLOWED_HOSTS $NS $EPOCH $NUAGE_URL $PG_DOMAIN $DOMAIN $SUBDOMAIN $FQDN $FQDN_DOTS $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY $S3_ENDPOINT_COLD $S3_ENDPOINT $REPLICAS $LOOL_ADMIN_PASS'| /home/deploy/libre-cli -t - | kubectl -n ${NS} $1 -f -
Pierre Ozoux's avatar
Pierre Ozoux committed
      else
        echo "Manifest $manifest not found"
        exit
      fi
    else
Timothee Gosselin's avatar
Timothee Gosselin committed
      cat "${@:2}" | envsubst '$BUCKET $NC_VERSION $NC_IMAGE_TAG $NC_WEB_IMAGE_TAG $ALLOWED_HOSTS $NS $EPOCH $NUAGE_URL $PG_DOMAIN $DOMAIN $SUBDOMAIN $FQDN $FQDN_DOTS $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY $S3_ENDPOINT_COLD $S3_ENDPOINT $REPLICAS $LOOL_ADMIN_PASS'| /home/deploy/libre-cli -t - | kubectl -n ${NS} $1 -f -
Pierre Ozoux's avatar
Pierre Ozoux committed
    fi;;
  update)
Timothee Gosselin's avatar
Timothee Gosselin committed
    echo $FQDN_DOTS
Pierre Ozoux's avatar
Pierre Ozoux committed
    libre diff > /tmp/diff
    retVal=$? # returns 1 if diff
    if [ $retVal -ne 0 ]; then # if diff
      if [ "$2" = "--batch" ]; then
Pierre Ozoux's avatar
Pierre Ozoux committed
        if [ "$3" = "--scaledown" ]; then
          kubectl -n ${NS} scale --replicas=0 deployment/${FQDN}-app
        fi
Pierre Ozoux's avatar
Pierre Ozoux committed
        cat /tmp/diff | grep -v "value:\|image\|generation\|+++\|---\|@@\|diff" | grep "^+\|^-"
        retVal=$? # returns 1 if grep
        if [ $retVal -ne 1 ]; then
          read -p "Ctrl-c to not apply the diff"
Timothee Gosselin's avatar
Timothee Gosselin committed
        fi
      elif [ "$2" = "--force" ]; then
        cat /tmp/diff | grep -v "value:\|image\|generation\|+++\|---\|@@\|diff" | grep "^+\|^-"
        echo "Forcing update in 2s..."
        sleep 2
Pierre Ozoux's avatar
Pierre Ozoux committed
      else
        cat /tmp/diff
        read -p "Ctrl-c to not apply the diff"
      fi
      create_update_job_nc
      libre apply
Pierre Ozoux's avatar
Pierre Ozoux committed
    else
      echo "Nothing to update, good job!"
    fi;;
  changens)
    kubens $NS;;
  *)
    show_usage
esac