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

Calculate storage improvements #956

Merged
merged 2 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
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
39 changes: 2 additions & 37 deletions services/storage-calculator/.lagoon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ objects:
- name: SERVICE_NAME
value: ${SERVICE_NAME}
- name: CRONJOBS
value: ${CRONJOBS}
value: "5 */12 * * * /lagoon/cronjob.sh /calculate-storage.sh"
volumeMounts:
- mountPath: /var/run/secrets/lagoon/sshkey/
name: lagoon-sshkey
Expand All @@ -99,39 +99,4 @@ objects:
memory: 10Mi
test: false
triggers:
- type: ConfigChange
- apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: cronjob-storage-calculator
spec:
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
schedule: "5 */12 * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
annotations:
alpha.image.policy.openshift.io/resolve-names: "*"
labels:
cronjob: storage-calculator
parent: cronjob-storage-calculator
spec:
containers:
- name: storage-calculator
image: ${REGISTRY}/${OPENSHIFT_PROJECT}/${SERVICE_NAME}:latest
command:
- /lagoon/cronjob.sh
- "/calculate-storage.sh"
envFrom:
- configMapRef:
name: lagoon-env
env:
- name: JWTSECRET
valueFrom:
secretKeyRef:
name: jwtsecret
key: JWTSECRET
restartPolicy: OnFailure
- type: ConfigChange
125 changes: 73 additions & 52 deletions services/storage-calculator/calculate-storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,47 @@ echo "$ALL_ENVIRONMENTS" | jq -c '.data.environments[] | select((.environments|l
do
PROJECT_NAME=$(echo "$project" | jq -r '.name')
OPENSHIFT_URL=$(echo "$project" | jq -r '.openshift.consoleUrl')
STORAGE_CALC=$(echo "$project" | jq -r '.storageCalc')
echo "$OPENSHIFT_URL: Handling project $PROJECT_NAME"
OPENSHIFT_TOKEN=$(echo "$project" | jq -r '.openshift.token')
# loop through each environment of the current project
echo "$project" | jq -c '.environments[]' | while read environment
do
ENVIRONMENT_OPENSHIFT_PROJECTNAME=$(echo "$environment" | jq -r '.openshiftProjectName')
ENVIRONMENT_NAME=$(echo "$environment" | jq -r '.name')
ENVIRONMENT_ID=$(echo "$environment" | jq -r '.id')
# Match the Project name to the Project Regex
if [[ $PROJECT_NAME =~ $PROJECT_REGEX ]]; then
STORAGE_CALC=$(echo "$project" | jq -r '.storageCalc')
echo "$OPENSHIFT_URL: Handling project $PROJECT_NAME"
OPENSHIFT_TOKEN=$(echo "$project" | jq -r '.openshift.token')
# loop through each environment of the current project
echo "$project" | jq -c '.environments[]' | while read environment
do
ENVIRONMENT_OPENSHIFT_PROJECTNAME=$(echo "$environment" | jq -r '.openshiftProjectName')
ENVIRONMENT_NAME=$(echo "$environment" | jq -r '.name')
ENVIRONMENT_ID=$(echo "$environment" | jq -r '.id')

echo "$OPENSHIFT_URL - $PROJECT_NAME: handling development environment $ENVIRONMENT_NAME"

if [[ $STORAGE_CALC != "1" ]]; then
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: storage calculation disabled, skipping"

echo "$OPENSHIFT_URL - $PROJECT_NAME: handling development environment $ENVIRONMENT_NAME"

if [[ $STORAGE_CALC != "1" ]]; then
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: storage calculation disabled, skipping"

MUTATION="mutation addOrUpdateEnvironmentStorage {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"storage-calc-disabled\", bytesUsed:0}) {
id
}
}"

# Convert GraphQL file into single line (but with still \n existing), turn \n into \\n, esapee the Quotes
query=$(echo $MUTATION | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | awk -F'\n' '{if(NR == 1) {printf $0} else {printf "\\n"$0}}')
curl -s -XPOST -H 'Content-Type: application/json' -H "$BEARER" api:3000/graphql -d "{\"query\": \"$query\"}"

continue

fi

OC="oc --insecure-skip-tls-verify --token=$OPENSHIFT_TOKEN --server=$OPENSHIFT_URL -n $ENVIRONMENT_OPENSHIFT_PROJECTNAME"

PVCS=($(${OC} get pvc -o name | sed 's/persistentvolumeclaims\///'))
MUTATION="mutation addOrUpdateEnvironmentStorage {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"storage-calc-disabled\", bytesUsed:0}) {
id
}
}"

if [[ ! ${#PVCS[@]} -gt 0 ]]; then
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: no PVCs found writing API with 0 bytes"
# Convert GraphQL file into single line (but with still \n existing), turn \n into \\n, esapee the Quotes
query=$(echo $MUTATION | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | awk -F'\n' '{if(NR == 1) {printf $0} else {printf "\\n"$0}}')
curl -s -XPOST -H 'Content-Type: application/json' -H "$BEARER" api:3000/graphql -d "{\"query\": \"$query\"}"

MUTATION="mutation addOrUpdateEnvironmentStorage {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"none\", bytesUsed:0}) {
id
}
}"
continue

# Convert GraphQL file into single line (but with still \n existing), turn \n into \\n, esapee the Quotes
query=$(echo $MUTATION | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | awk -F'\n' '{if(NR == 1) {printf $0} else {printf "\\n"$0}}')
curl -s -XPOST -H 'Content-Type: application/json' -H "$BEARER" api:3000/graphql -d "{\"query\": \"$query\"}"
fi

else
OC="oc --insecure-skip-tls-verify --token=$OPENSHIFT_TOKEN --server=$OPENSHIFT_URL -n $ENVIRONMENT_OPENSHIFT_PROJECTNAME"
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: creating storage-calc pod"

${OC} run --image alpine storage-calc -- sh -c "while sleep 3600; do :; done"
${OC} run --image amazeeio/alpine-mysql-client storage-calc -- sh -c "while sleep 3600; do :; done"
${OC} rollout pause deploymentconfig/storage-calc

${OC} env --from=configmap/lagoon-env deploymentconfig/storage-calc

PVCS=($(${OC} get pvc -o name | sed 's/persistentvolumeclaims\///'))

for PVC in "${PVCS[@]}"
do
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: mounting ${PVC} into storage-calc"
Expand All @@ -100,14 +89,11 @@ do

echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: loading storage information"

for PVC in "${PVCS[@]}"
do
STORAGE_BYTES=$(${OC} exec ${POD} -- sh -c "du -s /storage/${PVC} | cut -f1")
# STORAGE_BYTES=$(echo "${DF}" | grep /storage/${PVC} | awk '{ print $4 }')
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: ${PVC} uses ${STORAGE_BYTES} bytes"
if [[ ! ${#PVCS[@]} -gt 0 ]]; then
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: no PVCs found writing API with 0 bytes"

MUTATION="mutation addOrUpdateEnvironmentStorage {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"${PVC}\", bytesUsed:${STORAGE_BYTES}}) {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"none\", bytesUsed:0}) {
id
}
}"
Expand All @@ -116,10 +102,45 @@ do
query=$(echo $MUTATION | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | awk -F'\n' '{if(NR == 1) {printf $0} else {printf "\\n"$0}}')
curl -s -XPOST -H 'Content-Type: application/json' -H "$BEARER" api:3000/graphql -d "{\"query\": \"$query\"}"

done
else
for PVC in "${PVCS[@]}"
do
STORAGE_BYTES=$(${OC} exec ${POD} -- sh -c "du -s /storage/${PVC} | cut -f1")
# STORAGE_BYTES=$(echo "${DF}" | grep /storage/${PVC} | awk '{ print $4 }')
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: ${PVC} uses ${STORAGE_BYTES} kilobytes"

MUTATION="mutation addOrUpdateEnvironmentStorage {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"${PVC}\", bytesUsed:${STORAGE_BYTES}}) {
id
}
}"

# Convert GraphQL file into single line (but with still \n existing), turn \n into \\n, esapee the Quotes
query=$(echo $MUTATION | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | awk -F'\n' '{if(NR == 1) {printf $0} else {printf "\\n"$0}}')
curl -s -XPOST -H 'Content-Type: application/json' -H "$BEARER" api:3000/graphql -d "{\"query\": \"$query\"}"

done
fi

if mariadb_size=$(${OC} exec ${POD} -- sh -c "if [ \"\$MARIADB_HOST\" ]; then mysql -N -s -h \$MARIADB_HOST -u\$MARIADB_USERNAME -p\$MARIADB_PASSWORD -P\$MARIADB_PORT -e 'SELECT ROUND(SUM(data_length + index_length) / 1024, 0) FROM information_schema.tables'; else exit 1; fi") && [ "$mariadb_size" ]; then
echo "$OPENSHIFT_URL - $PROJECT_NAME - $ENVIRONMENT_NAME: Database uses ${mariadb_size} kilobytes"

MUTATION="mutation addOrUpdateEnvironmentStorage {
addOrUpdateEnvironmentStorage(input:{environment:${ENVIRONMENT_ID}, persistentStorageClaim:\"mariadb\", bytesUsed:${mariadb_size}}) {
id
}
}"

# Convert GraphQL file into single line (but with still \n existing), turn \n into \\n, esapee the Quotes
query=$(echo $MUTATION | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | awk -F'\n' '{if(NR == 1) {printf $0} else {printf "\\n"$0}}')
curl -s -XPOST -H 'Content-Type: application/json' -H "$BEARER" api:3000/graphql -d "{\"query\": \"$query\"}"
fi

${OC} delete deploymentconfig/storage-calc
fi
done

done
else
echo "$OPENSHIFT_URL - $PROJECT_NAME: SKIP, does not match Regex: $PROJECT_REGEX"
fi
done