Skip to content

Instantly share code, notes, and snippets.

@mikegreen
Created May 19, 2020 22:23
Show Gist options
  • Save mikegreen/8e31a6f54057d46dc26508f10bc95cf2 to your computer and use it in GitHub Desktop.
Save mikegreen/8e31a6f54057d46dc26508f10bc95cf2 to your computer and use it in GitHub Desktop.
TFE captre
#!/bin/bash
#Count TFE Metrics
#Requires TFE_TOKEN environment variable to be set to site admin api token
#Check for TFE_TOKEN
if [ -n "$TFE_TOKEN" ]; then
echo "----------------------------------"
echo "TFE Token detected!"
echo "----------------------------------"
else
echo "TFE Token not detected. Ensure TF_TOKEN environment variable is set with site admin api token"
exit 1
fi
#Determine TFE Hostname from Replicated config
TFE_HOSTNAME="$(replicatedctl app-config export | jq -r '.hostname.value | select(. != null)')"
#Set counts, array, and request headers
TOTAL_WORKSPACE=0
TOTAL_ORGS=0
TOTAL_TEAMS=0
vcs_provider=()
AUTH_HEADER="Authorization: Bearer $TFE_TOKEN"
CONTENT_TYPE_HEADER="Content-Type: application/vnd.api+json"
#Count number of Terraform Organizations, Workspaces in each Organization, and total number of Teams registered in those Workspaces
for org in $(curl -s "https://$TFE_HOSTNAME/api/v2/organizations?fields%5Borganizations%5D=id" -H "$AUTH_HEADER" -H "$CONTENT_TYPE_HEADER" | jq -r ".data[].id");
do
TOTAL_ORGS=$((TOTAL_ORGS + 1))
workspace=$(curl -s "https://$TFE_HOSTNAME/api/v2/organizations/$org/workspaces?fields%5Bworkspaces%5D=id" -H "$AUTH_HEADER" -H "$CONTENT_TYPE_HEADER" | jq -r ".data[].id" | wc -l)
TOTAL_WORKSPACE=$((TOTAL_WORKSPACE + $workspace))
teams=$(curl -s "https://$TFE_HOSTNAME/api/v2/organizations/$org/teams?fields%5Bteams%5D=id" -H "$AUTH_HEADER" -H "$CONTENT_TYPE_HEADER" | jq -r ".data[].id" | wc -l)
TOTAL_TEAMS=$((TOTAL_TEAMS + $teams))
vcs=$(curl -s "https://$TFE_HOSTNAME/api/v2/organizations/$org/oauth-clients" -H "$AUTH_HEADER" -H "$CONTENT_TYPE_HEADER" | jq -r '.data[].attributes["service-provider"]')
vcs_provider+=($vcs)
done
#Determine total number of users registered with Terraform Enterprise
TOTAL_USERS=$(curl -s "https://$TFE_HOSTNAME/api/v2/admin/users" -H "$AUTH_HEADER" -H "$CONTENT_TYPE_HEADER" | jq -r ".meta.pagination[\"total-count\"]")
#Detect OS Type and Version
OS_VERSION=$(awk -F'=' '/PRETTY_NAME/ {print $2}' /etc/*-release | awk -F'"' '{print $2}')
#Detect installation type and worker image
INSTALLATION_TYPE="$(replicatedctl app-config export | jq -r '.installation_type.value | select(. != null)')"
WORKER_IMAGE="$(replicatedctl app-config export | jq -r '.custom_image_tag.value | select(. != null)')"
if [ -n "$INSTALLATION_TYPE" ]; then
INSTALLATION_TYPE_MSG="$INSTALLATION_TYPE"
if [ "$WORKER_IMAGE" == "hashicorp/build-worker:now" ]; then
WORKER_IMAGE_MSG="Default worker image in use ($WORKER_IMAGE)"
elif [ "$WORKER_IMAGE" != "hashicorp/build-worker:now" ]; then
WORKER_IMAGE_MSG="Custom image in use ($WORKER_IMAGE)"
else
WORKER_IMAGE_MSG="Unable to detect worker image. Validate worker image configuration at https://$TFE_HOSTNAME:8800/settings"
fi
else
INSTALLATION_TYPE_MSG="Installation type not set. Complete server configuration at: https://$TFE_HOSTNAME:8800/settings"
fi
#Outputs
echo "TFE Address: https://$TFE_HOSTNAME"
echo "----------------------------------"
echo "Total Orgs: $TOTAL_ORGS"
echo "----------------------------------"
echo "Total Workspaces: $TOTAL_WORKSPACE"
echo "----------------------------------"
echo "Total Teams: $TOTAL_TEAMS"
echo "----------------------------------"
echo "Total Users: $TOTAL_USERS"
echo "----------------------------------"
echo "VCS Providers: ${vcs_provider[*]}"
echo "----------------------------------"
echo "Operating System Type/Version: $OS_VERSION"
echo "----------------------------------"
echo "Installation Type: $INSTALLATION_TYPE_MSG"
echo "----------------------------------"
echo "Worker Image: $WORKER_IMAGE_MSG"
echo "----------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment