-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine shell scripts to make shellcheck happy
Signed-off-by: Pavel Macík <[email protected]>
- Loading branch information
Showing
5 changed files
with
108 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,132 @@ | ||
#!/bin/bash | ||
|
||
export TMP_DIR=$(readlink -m .tmp) | ||
mkdir -p $TMP_DIR | ||
export WORKDIR=$(readlink -m .) | ||
export TMP_DIR WORKDIR | ||
|
||
function keycloak_url() { | ||
f=$TMP_DIR/keycloak.url | ||
if [ ! -f $f ]; then | ||
echo -n "https://$(oc get routes keycloak -n ${RHDH_NAMESPACE} -o jsonpath='{.spec.host}')" >$f | ||
TMP_DIR=$(readlink -m .tmp) | ||
mkdir -p "$TMP_DIR" | ||
WORKDIR=$(readlink -m .) | ||
|
||
keycloak_url() { | ||
f="$TMP_DIR/keycloak.url" | ||
if [ ! -f "$f" ]; then | ||
echo -n "https://$(oc get routes keycloak -n "${RHDH_NAMESPACE}" -o jsonpath='{.spec.host}')" >"$f" | ||
fi | ||
cat $f | ||
cat "$f" | ||
} | ||
|
||
function backstage_url() { | ||
f=$TMP_DIR/backstage.url | ||
if [ ! -f $f ]; then | ||
echo -n "https://$(oc get routes ${RHDH_HELM_RELEASE_NAME}-developer-hub -n ${RHDH_NAMESPACE} -o jsonpath='{.spec.host}')" >$f | ||
backstage_url() { | ||
f="$TMP_DIR/backstage.url" | ||
if [ ! -f "$f" ]; then | ||
echo -n "https://$(oc get routes "${RHDH_HELM_RELEASE_NAME}-developer-hub" -n "${RHDH_NAMESPACE}" -o jsonpath='{.spec.host}')" >"$f" | ||
fi | ||
cat $f | ||
cat "$f" | ||
} | ||
|
||
export keycloak_url backstage_url | ||
|
||
function create_per_grp() { | ||
create_per_grp() { | ||
varname=$2 | ||
obj_count=${!varname} | ||
if [[ -z ${!varname} ]]; then | ||
echo "$varname is not set: Skipping $1 " | ||
exit 1 | ||
fi | ||
local iter_count=$(echo "(${obj_count}/${GROUP_COUNT})" | bc) | ||
local mod=$(echo "(${obj_count}%${GROUP_COUNT})" | bc) | ||
local iter_count mod | ||
iter_count=$(echo "(${obj_count}/${GROUP_COUNT})" | bc) | ||
mod=$(echo "(${obj_count}%${GROUP_COUNT})" | bc) | ||
|
||
if [[ ! ${mod} -eq 0 ]]; then | ||
iter_count=$(echo "${iter_count}+1" | bc) | ||
fi | ||
indx=0 | ||
for i in $(seq 1 $((${iter_count}))); do | ||
for j in $(seq 1 $((${GROUP_COUNT}))); do | ||
for _ in $(seq 1 "${iter_count}"); do | ||
for g in $(seq 1 "${GROUP_COUNT}"); do | ||
indx=$((1 + indx)) | ||
[[ ${obj_count} -lt $indx ]] && break | ||
local out=$(${1} ${j} ${indx}) | ||
$1 "$g" "$indx" | ||
done | ||
done | ||
} | ||
|
||
function clone_and_upload() { | ||
clone_and_upload() { | ||
git_str="${GITHUB_USER}:${GITHUB_TOKEN}@github.com" | ||
base_name=$(basename $GITHUB_REPO) | ||
base_name=$(basename "$GITHUB_REPO") | ||
git_dir=$TMP_DIR/${base_name} | ||
git_repo=$(echo $GITHUB_REPO | sed -e "s/github.com/${git_str}/g") | ||
[[ -d ${git_dir} ]] && rm -rf ${git_dir} | ||
git clone $git_repo $git_dir | ||
cd $git_dir | ||
git_repo=${GITHUB_REPO//github.com/${git_str}} | ||
[[ -d "${git_dir}" ]] && rm -rf "${git_dir}" | ||
git clone "$git_repo" "$git_dir" | ||
cd "$git_dir" || return | ||
git config user.name "rhdh-performance-bot" | ||
git config user.email [email protected] | ||
tmp_branch=$(mktemp -u XXXXXXXXXX) | ||
git checkout -b $tmp_branch | ||
mv -vf ${1} . | ||
filename=$(basename ${1}) | ||
git add $filename | ||
git checkout -b "$tmp_branch" | ||
mv -vf "$1" . | ||
filename=$(basename "$1") | ||
git add "$filename" | ||
git commit -a -m "commit objects" | ||
git push -f --set-upstream origin $tmp_branch | ||
git push -f --set-upstream origin "$tmp_branch" | ||
cd .. | ||
sleep 5 | ||
upload_url=${GITHUB_REPO%.*}/blob/${tmp_branch}/${filename} | ||
upload_url="${GITHUB_REPO%.*}/blob/${tmp_branch}/${filename}" | ||
curl -k "$(backstage_url)/api/catalog/locations" -X POST -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' --data-raw '{"type":"url","target":"'"${upload_url}"'"}' | ||
} | ||
|
||
function create_api() { | ||
# shellcheck disable=SC2016 | ||
create_api() { | ||
export grp_indx=$1 | ||
export api_indx=$2 | ||
cat $WORKDIR/template/component/api.template | envsubst '${grp_indx} ${api_indx}' >>$TMP_DIR/api.yaml | ||
envsubst '${grp_indx} ${api_indx}' <"$WORKDIR/template/component/api.template" >>"$TMP_DIR/api.yaml" | ||
} | ||
|
||
function create_cmp() { | ||
# shellcheck disable=SC2016 | ||
create_cmp() { | ||
export grp_indx=$1 | ||
export cmp_indx=$2 | ||
cat $WORKDIR/template/component/component.template | envsubst '${grp_indx} ${cmp_indx}' >>$TMP_DIR/component.yaml | ||
envsubst '${grp_indx} ${cmp_indx}' <"$WORKDIR/template/component/component.template" >>"$TMP_DIR/component.yaml" | ||
} | ||
|
||
function create_group() { | ||
create_group() { | ||
token=$(get_token) | ||
curl -s -k --location --request POST "$(keycloak_url)/auth/admin/realms/backstage/groups" \ | ||
-H 'Content-Type: application/json' \ | ||
-H 'Authorization: Bearer '$token \ | ||
-H 'Authorization: Bearer '"$token" \ | ||
--data-raw '{"name": "group'"${0}"'"}' | ||
} | ||
|
||
function create_groups() { | ||
create_groups() { | ||
echo "Creating Groups in Keycloak" | ||
refresh_pid=$! | ||
sleep 5 | ||
export -f create_group | ||
seq 1 ${GROUP_COUNT} | xargs -n1 -P10 bash -c 'create_group' | ||
seq 1 "${GROUP_COUNT}" | xargs -n1 -P10 bash -c 'create_group' | ||
kill $refresh_pid | ||
} | ||
|
||
function create_user() { | ||
create_user() { | ||
token=$(get_token) | ||
grp=$(echo "${0}%${GROUP_COUNT}" | bc) | ||
[[ $grp -eq 0 ]] && grp=${GROUP_COUNT} | ||
curl -s -k --location --request POST "$(keycloak_url)/auth/admin/realms/backstage/users" \ | ||
-H 'Content-Type: application/json' \ | ||
-H 'Authorization: Bearer '$token \ | ||
-H 'Authorization: Bearer '"$token" \ | ||
--data-raw '{"firstName":"test'"${0}"'","lastName":"tester", "email":"test'"${0}"'@test.com", "enabled":"true", "username":"test'"${0}"'","groups":["/group'"${grp}"'"]}' | ||
} | ||
|
||
function create_users() { | ||
create_users() { | ||
echo "Creating Users in Keycloak" | ||
export GROUP_COUNT | ||
refresh_pid=$! | ||
sleep 5 | ||
export -f create_user | ||
seq 1 ${BACKSTAGE_USER_COUNT} | xargs -n1 -P10 bash -c 'create_user' | ||
seq 1 "${BACKSTAGE_USER_COUNT}" | xargs -n1 -P10 bash -c 'create_user' | ||
kill $refresh_pid | ||
} | ||
|
||
function get_token() { | ||
get_token() { | ||
token_file=$TMP_DIR/token.json | ||
if [ ! -f $token_file ] || [ $(date +%s) -gt $(jq -rc '.expires_in_timestamp' $token_file) ]; then | ||
keycloak_pass=$(oc -n ${RHDH_NAMESPACE} get secret credential-example-sso -o template --template='{{.data.ADMIN_PASSWORD}}' | base64 -d) | ||
curl -s -k $(keycloak_url)/auth/realms/master/protocol/openid-connect/token -d username=admin -d password=${keycloak_pass} -d 'grant_type=password' -d 'client_id=admin-cli' | jq -r ".expires_in_timestamp = $(date -d '30 seconds' +%s)" >$token_file | ||
if [ ! -f "$token_file" ] || [ "$(date +%s)" -gt "$(jq -rc '.expires_in_timestamp' "$token_file")" ]; then | ||
keycloak_pass=$(oc -n "${RHDH_NAMESPACE}" get secret credential-example-sso -o template --template='{{.data.ADMIN_PASSWORD}}' | base64 -d) | ||
curl -s -k "$(keycloak_url)/auth/realms/master/protocol/openid-connect/token" -d username=admin -d "password=${keycloak_pass}" -d 'grant_type=password' -d 'client_id=admin-cli' | jq -r ".expires_in_timestamp = $(date -d '30 seconds' +%s)" >"$token_file" | ||
fi | ||
jq -rc '.access_token' $token_file | ||
jq -rc '.access_token' "$token_file" | ||
} | ||
|
||
export -f keycloak_url backstage_url get_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters