Skip to content

Commit

Permalink
Patch creates users, groups, api and components
Browse files Browse the repository at this point in the history
This patch introduces two new environment variables, that need to be set for deploy.sh script to run, GITHUB_USER and GITHUB_REPO. They will be used to host the API and Components for backstage.
Example:
GITHUB_USER=yogananth-subramanian
GITHUB_REPO=https://github.com/yogananth-subramanian/test1.git

It also introduces 5 more variables to modify deploy.sh behavior and create backstage objects.

- PRE_LOAD_DB, default set to true, if set, users and groups created before backstage_install or else they would be created after.
- BACKSTAGE_USER_COUNT, number of users in keycloak to create 
- GROUP_COUNT, number of groups in keycloak to create 
- API_COUNT, number of api object to create
- COMPONENT_COUNT, number of components to create
  • Loading branch information
yogananth-subramanian authored and pmacik committed Nov 20, 2023
1 parent a7e8521 commit baf5ef2
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
109 changes: 109 additions & 0 deletions ci-scripts/rhdh-setup/create_resource.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#/bin/bash

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`

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
indx=$((1+indx))
[[ ${obj_count} -lt $indx ]] && break
local out=$(${1} ${j} ${indx})
done
done
}

clone_and_upload() {
export backstage_url="https://$(oc get routes ${RHDH_HELM_RELEASE_NAME}-developer-hub -n ${RHDH_NAMESPACE} -o jsonpath='{.spec.host}')"
git_str="${GITHUB_USER}:${GITHUB_TOKEN}@github.com"
base_name=`basename $GITHUB_REPO`
git_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
cd $git_dir
tmp_branch=`mktemp -u XXXXXXXXXX`
git checkout -b $tmp_branch
mv ../${1} .
git add ${1}
git commit -a -m "commit objects"
git push -f --set-upstream origin $tmp_branch
cd ..
sleep 5
upload_url=${GITHUB_REPO%.*}/blob/${tmp_branch}/${1}
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}"'"}'
}

create_api() {
export grp_indx=$1
export api_indx=$2
cat template/component/api.template | envsubst '${grp_indx} ${api_indx}'>> api.yaml
}

create_cmp() {
export grp_indx=$1
export cmp_indx=$2
cat template/component/component.template | envsubst '${grp_indx} ${cmp_indx}'>> component.yaml
}

create_group() {
token=`cat /tmp/token`
curl -s -k --location --request POST ${keycloak_url}'/auth/admin/realms/backstage/groups' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$token \
--data-raw '{"name": "group'"${0}"'"}'
}

create_groups() {
echo "Creating Groups in Keycloak"
export keycloak_url="https://$(oc get routes keycloak -n ${RHDH_NAMESPACE} -o jsonpath='{.spec.host}')"
export keycloak_pass=$(oc -n ${RHDH_NAMESPACE} get secret credential-example-sso -o template --template='{{.data.ADMIN_PASSWORD}}'|base64 -d)
export -f get_token
nohup bash -c 'get_token' &
refresh_pid=$!
sleep 5
export -f create_group
seq 1 ${GROUP_COUNT}| xargs -n1 -P10 bash -c 'create_group'
kill $refresh_pid
}

create_user() {
token=`cat /tmp/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 \
--data-raw '{"firstName":"test'"${0}"'","lastName":"tester", "email":"test'"${0}"'@test.com", "enabled":"true", "username":"test'"${0}"'","groups":["/group'"${grp}"'"]}'
}

create_users() {
echo "Creating Users in Keycloak"
export keycloak_url="https://$(oc get routes keycloak -n ${RHDH_NAMESPACE} -o jsonpath='{.spec.host}')"
export keycloak_pass=$(oc -n ${RHDH_NAMESPACE} get secret credential-example-sso -o template --template='{{.data.ADMIN_PASSWORD}}'|base64 -d)
export -f get_token
export GROUP_COUNT
nohup bash -c 'get_token' &
refresh_pid=$!
sleep 5
export -f create_user
seq 1 ${BACKSTAGE_USER_COUNT}| xargs -n1 -P10 bash -c 'create_user'
kill $refresh_pid
}

get_token() {
while true; do
echo -n $(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 .access_token)>/tmp/token
sleep 30
done
}
36 changes: 36 additions & 0 deletions ci-scripts/rhdh-setup/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/bash
set -uo pipefail

source ./create_resource.sh

[ -z ${QUAY_TOKEN} ]
[ -z ${GITHUB_TOKEN} ]
[ -z ${GITHUB_USER} ]
[ -z ${GITHUB_REPO} ]

export RHDH_NAMESPACE=${RHDH_NAMESPACE:-rhdh-performance}
export RHDH_HELM_RELEASE_NAME=${RHDH_HELM_RELEASE_NAME:-rhdh}
Expand All @@ -20,6 +25,12 @@ export RHDH_IMAGE_TAG=${RHDH_IMAGE_TAG:-1.0-162}
export RHDH_HELM_REPO=${RHDH_HELM_REPO:-https://gist.githubusercontent.com/nickboldt/a8483eb244f9c4286798e85accaa70af/raw} #v1.0-162
export RHDH_HELM_CHART=${RHDH_HELM_CHART:-developer-hub}

export PRE_LOAD_DB="${PRE_LOAD_DB:-true}"
export BACKSTAGE_USER_COUNT="${BACKSTAGE_USER_COUNT:-1}"
export GROUP_COUNT="${GROUP_COUNT:-1}"
export API_COUNT="${API_COUNT:-1}"
export COMPONENT_COUNT="${COMPONENT_COUNT:-1}"

delete() {
for cr in keycloakusers keycloakclients keycloakrealms keycloaks; do
for res in $($clin get $cr.keycloak.org -o name); do
Expand Down Expand Up @@ -137,10 +148,34 @@ spec:
EOF
}

create_objs() {
if ! $PRE_LOAD_DB; then
create_groups
create_users
fi

if [[ ${GITHUB_USER} ]] && [[ ${GITHUB_REPO} ]] ; then
create_per_grp create_cmp COMPONENT_COUNT
[[ $? -eq 0 ]] && clone_and_upload component.yaml

create_per_grp create_api API_COUNT
[[ $? -eq 0 ]] && clone_and_upload api.yaml
else
echo "skipping component creating. GITHUB_REPO and GITHUB_USER not set"
exit 1
fi
}

install() {
appurl=$(oc whoami --show-console)
export OPENSHIFT_APP_DOMAIN=${appurl#*.}
keycloak_install

if $PRE_LOAD_DB; then
create_groups
create_users
fi

backstage_install
setup_monitoring
}
Expand All @@ -161,3 +196,4 @@ while getopts "rd" flag; do
done

install
create_objs
12 changes: 12 additions & 0 deletions ci-scripts/rhdh-setup/template/component/api.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: wayback-archive-${api_indx}
description: Archive API for the wayback copy machine
spec:
type: openapi
lifecycle: production
owner: group${grp_indx}
definition:
$text: https://github.com/APIs-guru/openapi-directory/blob/main/APIs/archive.org/wayback/1.0.0/openapi.yaml
11 changes: 11 additions & 0 deletions ci-scripts/rhdh-setup/template/component/component.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: playback-sdk-${cmp_indx}
description: Audio and video playback SDK
spec:
type: library
lifecycle: experimental
owner: group${grp_indx}
system: audio-playback

0 comments on commit baf5ef2

Please sign in to comment.