Merge "Add templates Tox + SonarQube Jenkins plugin"
[releng/global-jjb.git] / shell / job-cost.sh
index 6493a6e..a7dceea 100644 (file)
@@ -8,14 +8,20 @@
 # which accompanies this distribution, and is available at
 # http://www.eclipse.org/legal/epl-v10.html
 ##############################################################################
-echo "---> build-cost.sh"
+echo "---> job-cost.sh"
 
 set -euf -o pipefail
 
 # shellcheck disable=SC1090
 source ~/lf-env.sh
 
-lf-activate-venv python-openstackclient
+# AWS job cost not supported, exit
+if grep -qi amazon /sys/devices/virtual/dmi/id/bios_vendor ; then
+  echo "INFO: Not able to calculate job cost on AWS"
+  exit 0
+fi
+
+lf-activate-venv zipp==1.1.0 python-openstackclient
 
 if [[ -z ${JOB_NAME:-} ]]; then
     lf-echo-error "Required Env Variable Unset/Empty: JOB_NAME"
@@ -43,14 +49,24 @@ uptime=$(awk '{print $1}' /proc/uptime)
 # Convert to integer by truncating fractional part' and round up by one
 ((uptime=${uptime%\.*}+1))
 
+# EC2 and OpenStack have simiar instace metadata APIs at this IP
+# AWS docs: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
+# Nova docs: https://docs.openstack.org/nova/latest/user/metadata.html
 instance_type=$(curl -s http://169.254.169.254/latest/meta-data/instance-type)
 
 echo "INFO: Retrieving Pricing Info for: $instance_type"
 url="https://pricing.vexxhost.net/v1/pricing/$instance_type/cost?seconds=$uptime"
-jason_block=$(curl -s "$url")
+json_block=$(curl -s "$url")
 
-cost=$(jq .cost <<< "$jason_block")
-resource=$(jq .resource <<< "$jason_block" | tr -d '"')
+# check if JSON returned and can be parsed
+if jq <<< "$json_block" > /dev/null 2>&1; then
+    cost=$(jq .cost <<< "$json_block")
+    resource=$(jq .resource <<< "$json_block" | tr -d '"')
+else
+    echo "ERROR: Pricing API returned invalid json"
+    cost=0
+    resource='unknown'
+fi
 
 # Archive the cost date
 mkdir -p "$WORKSPACE/archives/cost"
@@ -61,7 +77,6 @@ echo "INFO: Archiving Costs"
 # This format is readable by spreadsheet and is easily sortable
 date=$(TZ=GMT date +'%Y-%m-%d %H:%M:%S')
 
-cat << EOF > "$WORKSPACE/archives/cost.csv"
-$JOB_NAME,$BUILD_NUMBER,$date,$resource,$uptime,$cost,$stack_cost
-EOF
-
+# Format the uptime, cost & stack_cost fields
+printf "%s,%s,%s,%s,%d,%.2f,%.2f\n" "$JOB_NAME" "$BUILD_NUMBER" "$date" \
+       "$resource" "$uptime" "$cost" "$stack_cost" > "$WORKSPACE/archives/cost.csv"