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

Update zksync-env.sh #600

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
74 changes: 32 additions & 42 deletions .github/scripts/zksync-env.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
## This script updates server-env-custom (merges the etc/env/xxx file with configmap)
# This script updates server-env-custom (merges the etc/env/xxx file with configmap)
# loadtest (without parameters) - combined env
# --repo [loadtest] - env from file
# --kube [loadtest] - env from configmap
Expand All @@ -11,35 +11,32 @@
set -e

serverEnv="server-env-custom"
repoRoot=`cd $( dirname "${BASH_SOURCE[0]}" )/../.. >/dev/null 2>&1 && pwd`

repoRoot=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
cmd="$1"
opts=""

if ( echo "--update-from --diff-from" | grep -qw "\\$cmd" ); then
# Determine options and namespace
if [[ "$cmd" == "--update-from" || "$cmd" == "--diff-from" ]]; then
fromfile="$2"
namespace="${ZKSYNC_ENV:-$3}"
else
namespace="${ZKSYNC_ENV:-$2}"
fi

[ -z $namespace ] || opts="$opts -n $namespace"
[ -z "$namespace" ] || opts="$opts -n $namespace"

kube_env() {
if ( kubectl $opts get cm $serverEnv &> /dev/null ); then
kubectl $opts get cm $serverEnv -o go-template --template='{{ range $k,$v := .data}}{{ printf "%s=%s" $k $v }}{{"\n"}}{{end}}'
fi
kubectl $opts get cm "$serverEnv" -o go-template --template='{{ range $k,$v := .data}}{{ printf "%s=%s" $k $v }}{{"\n"}}{{end}}' || true
}

##
repo_env() {
. $repoRoot/etc/env/$namespace.env
export $(cut -d= -f1 etc/env/$namespace.env | sed -r '/^\s*#/d')
source "$repoRoot/etc/env/$namespace.env"
export $(cut -d= -f1 "$repoRoot/etc/env/$namespace.env" | sed -r '/^\s*#/d')
env | sed "/^\($1\)/d"
}

cmDiff() {
kubectl $opts create cm $serverEnv --from-env-file="$tmpfile" --dry-run -o yaml | kubectl diff -f - || :
kubectl $opts create cm "$serverEnv" --from-env-file="$tmpfile" --dry-run -o yaml | kubectl diff -f - || true
}

cleanup() {
Expand All @@ -50,60 +47,53 @@ cleanup() {

trap cleanup EXIT

## We call ourself - carefull!
case $cmd in
--kube)
kube_env
;;
--repo)
# current env to sanitize
sanitize=$(env | cut -d= -f1 | sed ':a;N;$!ba;s/\n/\\\|/g')
repo_env "$sanitize"
;;
--diff)
tmpfile=$(mktemp -u)
bash $0 $2 > "$tmpfile"
cmDiff
tmpfile=$(mktemp -u)
bash "$0" "$2" > "$tmpfile"
cmDiff
;;
--merge)
tmpfile=$(mktemp -u)
bash $0 $2 > "$tmpfile"
bash "$0" "$2" > "$tmpfile"

# overwrites configmap!
outp=$(cmDiff)
if ( echo "$outp" | grep -Fq '+++' ); then
kubectl $opts create cm $serverEnv --from-env-file="$tmpfile" --dry-run -o yaml | \
kubectl apply -f -
elif [ -n "$outp" ]; then
# write a error (since no diff)
echo $outp
exit 2
# Overwrite configmap if there's a diff
if outp=$(cmDiff); then
if echo "$outp" | grep -Fq '+++'; then
kubectl $opts create cm "$serverEnv" --from-env-file="$tmpfile" --dry-run -o yaml | kubectl apply -f -
elif [ -n "$outp" ]; then
echo "$outp"
exit 2
fi
fi
;;
--update-from)
tmpfile=$(mktemp -u)
{ cat $fromfile; bash $0 --kube $namespace; } | sort -u -t '=' -k 1,1 > "$tmpfile"
{ cat "$fromfile"; bash "$0" --kube "$namespace"; } | sort -u -t '=' -k 1,1 > "$tmpfile"

# overwrites configmap!
outp=$(cmDiff)
if ( echo "$outp" | grep -Fq '+++' ); then
kubectl $opts create cm $serverEnv --from-env-file="$tmpfile" --dry-run -o yaml | \
kubectl apply -f -
elif [ -n "$outp" ]; then
# write a error (since no diff)
echo $outp
exit 2
# Overwrite configmap if there's a diff
if outp=$(cmDiff); then
if echo "$outp" | grep -Fq '+++'; then
kubectl $opts create cm "$serverEnv" --from-env-file="$tmpfile" --dry-run -o yaml | kubectl apply -f -
elif [ -n "$outp" ]; then
echo "$outp"
exit 2
fi
fi
;;
--diff-from)
tmpfile=$(mktemp -u)
{ cat $fromfile; bash $0 --kube $namespace; } | sort -u -t '=' -k 1,1 > "$tmpfile"
{ cat "$fromfile"; bash "$0" --kube "$namespace"; } | sort -u -t '=' -k 1,1 > "$tmpfile"
cmDiff
;;
*)
# Combine two outputs.
# We favour output from kube!
#
{ bash $0 --kube $1; bash $0 --repo $1; } | sort -u -t '=' -k 1,1
{ bash "$0" --kube "$1"; bash "$0" --repo "$1"; } | sort -u -t '=' -k 1,1
;;
esac