Skip to content

Commit

Permalink
(HP-572) create dynamodb table for gen3-license workspace (#2441)
Browse files Browse the repository at this point in the history
* (HP-572) create dynamodb table for gen3-license workspace

* (HP-572) get GSI from hatchery config

* (HP-572) remove extra quotes from 'GSI'

* (HP-572) move distribute-license for backwards compatibility

---------

Co-authored-by: Mingfei Shao <[email protected]>
  • Loading branch information
george42-ctds and mfshao authored Feb 29, 2024
1 parent 8103e82 commit b9ab8af
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions gen3/bin/kube-setup-hatchery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,60 @@ gen3 jupyter j-namespace setup
#
(g3k_kv_filter ${GEN3_HOME}/kube/services/hatchery/serviceaccount.yaml BINDING_ONE "name: hatchery-binding1-$namespace" BINDING_TWO "name: hatchery-binding2-$namespace" CURRENT_NAMESPACE "namespace: $namespace" | g3kubectl apply -f -) || true

function exists_or_create_gen3_license_table() {
# Create dynamodb table for gen3-license if it does not exist.
TARGET_TABLE="$1"
echo "Checking for dynamoDB table: ${TARGET_TABLE}"

# cron job to distribute licenses if using Stata workspaces
if [ "$(g3kubectl get configmaps/manifest-hatchery -o yaml | grep "\"image\": .*stata.*")" ];
then
gen3 job cron distribute-licenses '* * * * *'
FOUND_TABLE=`aws dynamodb list-tables | jq -r .TableNames | jq -c -r '.[]' | grep $TARGET_TABLE`
if [ -n "$FOUND_TABLE" ]; then
echo "Target table already exists in dynamoDB: $FOUND_TABLE"
else
echo "Creating table ${TARGET_TABLE}"
GSI=`g3kubectl get configmaps/manifest-hatchery -o json | jq -r '.data."license-user-maps-global-secondary-index"'`
if [[ -z "$GSI" || "$GSI" == "null" ]]; then
echo "Error: No global-secondary-index in configuration"
return 0
fi
aws dynamodb create-table \
--no-cli-pager \
--table-name "$TARGET_TABLE" \
--attribute-definitions AttributeName=itemId,AttributeType=S \
AttributeName=environment,AttributeType=S \
AttributeName=isActive,AttributeType=S \
--key-schema AttributeName=itemId,KeyType=HASH \
AttributeName=environment,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--global-secondary-indexes \
"[
{
\"IndexName\": \"$GSI\",
\"KeySchema\": [{\"AttributeName\":\"environment\",\"KeyType\":\"HASH\"},
{\"AttributeName\":\"isActive\",\"KeyType\":\"RANGE\"}],
\"Projection\":{
\"ProjectionType\":\"INCLUDE\",
\"NonKeyAttributes\":[\"itemId\",\"userId\",\"licenseId\",\"licenseType\"]
},
\"ProvisionedThroughput\": {
\"ReadCapacityUnits\": 5,
\"WriteCapacityUnits\": 3
}
}
]"
fi
}

TARGET_TABLE=`g3kubectl get configmaps/manifest-hatchery -o json | jq -r '.data."license-user-maps-dynamodb-table"'`
if [[ -z "$TARGET_TABLE" || "$TARGET_TABLE" == "null" ]]; then
echo "No gen3-license table in configuration"
# cron job to distribute licenses if using Stata workspaces but not using dynamoDB
if [ "$(g3kubectl get configmaps/manifest-hatchery -o yaml | grep "\"image\": .*stata.*")" ];
then
gen3 job cron distribute-licenses '* * * * *'
fi
else
echo "Found gen3-license table in configuration: $TARGET_TABLE"
exists_or_create_gen3_license_table "$TARGET_TABLE"
fi

policy=$( cat <<EOM
Expand Down

0 comments on commit b9ab8af

Please sign in to comment.