Skip to content

Commit

Permalink
Merge pull request #2922 from uselagoon/buildstep-times
Browse files Browse the repository at this point in the history
  • Loading branch information
tobybellwood authored Nov 10, 2021
2 parents 285cb61 + 3d3a6d1 commit f0337ef
Showing 1 changed file with 127 additions and 8 deletions.
135 changes: 127 additions & 8 deletions images/kubectl-build-deploy-dind/build-deploy-docker-compose.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# get the buildname from the pod, $HOSTNAME contains this in the running pod, so we can use this
# set it to something usable here
LAGOON_BUILD_NAME=$HOSTNAME

function cronScheduleMoreOftenThan30Minutes() {
#takes a unexpanded cron schedule, returns 0 if it's more often that 30 minutes
MINUTE=$(echo $1 | (read -a ARRAY; echo ${ARRAY[0]}) )
Expand Down Expand Up @@ -72,6 +76,45 @@ function featureFlag() {
echo "${!defaultFlagVar}"
}

function patchBuildStep() {
[ "$1" ] || return #total start time
[ "$2" ] || return #step start time
[ "$3" ] || return #previous step end time
[ "$4" ] || return #namespace
[ "$5" ] || return #buildstep
[ "$6" ] || return #buildstep
totalStartTime=$(date -d "${1}" +%s)
startTime=$(date -d "${2}" +%s)
endTime=$(date -d "${3}" +%s)
timeZone=$(date +"%Z")

diffSeconds="$(($endTime-$startTime))"
diffTime=$(date -d @${diffSeconds} +"%H:%M:%S" -u)

diffTotalSeconds="$(($endTime-$totalStartTime))"
diffTotalTime=$(date -d @${diffTotalSeconds} +"%H:%M:%S" -u)

echo "##############################################"
echo "STEP ${6}: Completed at ${3} (${timeZone}) Duration ${diffTime} Elapsed ${diffTotalTime}"
echo "##############################################"

# patch the buildpod with the buildstep
kubectl patch --insecure-skip-tls-verify -n ${4} pod ${LAGOON_BUILD_NAME} \
-p "{\"metadata\":{\"labels\":{\"lagoon.sh/buildStep\":\"${5}\"}}}"

# tiny sleep to allow patch to complete before logs roll again
sleep 0.5s
}

##############################################
### PREPARATION
##############################################

set +x
buildStartTime="$(date +"%Y-%m-%d %H:%M:%S")"
echo "STEP: Preparation started ${buildStartTime}"
set -x

##############################################
### PUSH the latest .lagoon.yml into lagoon-yaml configmap as a pre-deploy field
##############################################
Expand All @@ -96,12 +139,6 @@ if kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-yaml
fi
set -x

##############################################
### PREPARATION
##############################################

set +x # reduce noise in build logs

# validate .lagoon.yml
if ! lagoon-linter; then
echo "https://docs.lagoon.sh/lagoon/using-lagoon-the-basics/lagoon-yml#restrictions describes some possible reasons for this build failure."
Expand All @@ -111,6 +148,12 @@ else
echo "lagoon-linter found no issues with the .lagoon.yml file"
fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "initialSetup" "Initial Environment Setup"
previousStepEnd=${currentStepEnd}
set -x

# Load path of docker-compose that should be used
DOCKER_COMPOSE_YAML=($(cat .lagoon.yml | shyaml get-value docker-compose-yaml))

Expand Down Expand Up @@ -413,6 +456,12 @@ do

done

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "configureVars" "Configured Variables"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### BUILD IMAGES
##############################################
Expand Down Expand Up @@ -561,6 +610,12 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then

fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuildComplete" "Image Builds"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### RUN PRE-ROLLOUT tasks defined in .lagoon.yml
##############################################
Expand Down Expand Up @@ -590,7 +645,11 @@ else
echo "pre-rollout tasks are currently disabled LAGOON_PREROLLOUT_DISABLED is set to true"
fi


set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "preRolloutsCompleted" "Pre-Rollout Tasks"
previousStepEnd=${currentStepEnd}
set -x


##############################################
Expand Down Expand Up @@ -692,6 +751,12 @@ LAGOON_PR_NUMBER=${PR_NUMBER}\n\
" >> /kubectl-build-deploy/values.env
fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "serviceConfigurationComplete" "Service Configuration Phase 1"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### CUSTOM FASTLY API SECRETS .lagoon.yml
##############################################
Expand Down Expand Up @@ -853,7 +918,7 @@ do
MAIN_GENERATED_ROUTE=$SERVICE_NAME
fi


set +x
ROUTE_FASTLY_SERVICE_WATCH=false
# if the builddeploy controller is injecting a featureflag value, load it in
if [ -z $LAGOON_FASTLY_AUTOGENERATED_FEATURE_FLAG ]; then
Expand All @@ -869,6 +934,7 @@ do
LAGOON_FASTLY_AUTOGENERATED=$TEMP_LAGOON_FASTLY_AUTOGENERATED
fi
fi
set -x
# Create the fastly values required
FASTLY_ARGS=()
# if the feature is enabled, then do what is required to generated the labels/annotations etc
Expand Down Expand Up @@ -918,6 +984,12 @@ do
fi
done

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "serviceConfiguration2Complete" "Service Configuration Phase 2"
previousStepEnd=${currentStepEnd}
set -x

TEMPLATE_PARAMETERS=()

##############################################
Expand Down Expand Up @@ -1416,6 +1488,12 @@ else
done
fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "routeConfigurationComplete" "Route/Ingress Configuration"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### Backup Settings
##############################################
Expand Down Expand Up @@ -1578,6 +1656,12 @@ if [ "$(ls -A $YAML_FOLDER/)" ]; then
kubectl apply --insecure-skip-tls-verify -n ${NAMESPACE} -f $YAML_FOLDER/
fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "backupConfigurationComplete" "Backup Configuration"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### PROJECT WIDE ENV VARIABLES
##############################################
Expand Down Expand Up @@ -1774,6 +1858,12 @@ elif [ "$BUILD_TYPE" == "promote" ]; then

fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imagePushComplete" "Image Push to Registry"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### CREATE PVC, DEPLOYMENTS AND CRONJOBS
##############################################
Expand Down Expand Up @@ -1880,6 +1970,12 @@ do

done

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "deploymentTemplatingComplete" "Deployment Templating"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### APPLY RESOURCES
##############################################
Expand Down Expand Up @@ -1934,6 +2030,11 @@ do
fi
done

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "deploymentApplyComplete" "Applying Deployments"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### CLEANUP NATIVE CRONJOBS which have been removed from .lagoon.yml or modified to run more frequently than every 15 minutes
Expand All @@ -1956,6 +2057,12 @@ do
fi
done

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "cronjobCleanupComplete" "Cronjob Cleanup"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### RUN POST-ROLLOUT tasks defined in .lagoon.yml
##############################################
Expand Down Expand Up @@ -1986,6 +2093,12 @@ else
echo "post-rollout tasks are currently disabled LAGOON_POSTROLLOUT_DISABLED is set to true"
fi

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "postRolloutsCompleted" "Post-Rollout Tasks"
previousStepEnd=${currentStepEnd}
set -x

##############################################
### PUSH the latest .lagoon.yml into lagoon-yaml configmap
##############################################
Expand All @@ -1999,4 +2112,10 @@ if kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-yaml
# create it
kubectl --insecure-skip-tls-verify -n ${NAMESPACE} create configmap lagoon-yaml --from-file=post-deploy=.lagoon.yml
fi
set -x

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "deployCompleted" "Build and Deploy"
previousStepEnd=${currentStepEnd}
set -x

0 comments on commit f0337ef

Please sign in to comment.