Revert "Pass the builder type to verify jobs"
[releng/global-jjb.git] / shell / job-cost.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> job-cost.sh"
12
13 set -euf -o pipefail
14
15 # shellcheck disable=SC1090
16 source ~/lf-env.sh
17
18 # AWS job cost not supported, exit
19 cloudtype="$(jq -r .v1.datasource /run/cloud-init/result.json)"
20 if [[ $cloudtype == "DataSourceEc2Local" ]]; then
21   echo "INFO: Not able to calculate job cost on AWS"
22   exit 0
23 fi
24
25 lf-activate-venv zipp==1.1.0 python-openstackclient
26
27 if [[ -z ${JOB_NAME:-} ]]; then
28     lf-echo-error "Required Env Variable Unset/Empty: JOB_NAME"
29     exit 1
30 fi
31
32 # Get the cost of the Openstack agents. The 'stack-cost' file is created when
33 # the 'lftools openstack stack cost' command is called from
34 # 'openstack-stack-delete.sh' script. The 'stack-cost' file will only be created
35 # if this is an openstack job.
36 if [[ -f stack-cost ]]; then
37     echo "DEBUG: $(cat stack-cost)"
38     echo "INFO: Retrieving Stack Cost..."
39     if ! stack_cost=$(grep -F "total: " stack-cost | awk '{print $2}'); then
40         echo "ERROR: Unable to retrieve Stack Cost, continuing anyway"
41         stack_cost=0
42     fi
43 else
44     echo "INFO: No Stack..."
45     stack_cost=0
46 fi
47
48 # Retrieve the current uptime (in seconds)
49 uptime=$(awk '{print $1}' /proc/uptime)
50 # Convert to integer by truncating fractional part' and round up by one
51 ((uptime=${uptime%\.*}+1))
52
53 # EC2 and OpenStack have simiar instace metadata APIs at this IP
54 # AWS docs: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
55 # Nova docs: https://docs.openstack.org/nova/latest/user/metadata.html
56 instance_type=$(curl -s http://169.254.169.254/latest/meta-data/instance-type)
57
58 echo "INFO: Retrieving Pricing Info for: $instance_type"
59 url="https://pricing.vexxhost.net/v1/pricing/$instance_type/cost?seconds=$uptime"
60 json_block=$(curl -s "$url")
61
62 # check if JSON returned and can be parsed
63 if jq . <<< "$json_block" > /dev/null 2>&1; then
64     cost=$(jq .cost <<< "$json_block")
65     resource=$(jq .resource <<< "$json_block" | tr -d '"')
66 else
67     echo "ERROR: Pricing API returned invalid json"
68     cost=0
69     resource='unknown'
70 fi
71
72 # Archive the cost date
73 mkdir -p "$WORKSPACE/archives/cost"
74
75 echo "INFO: Archiving Costs"
76
77 # Set the timestamp in GMT
78 # This format is readable by spreadsheet and is easily sortable
79 date=$(TZ=GMT date +'%Y-%m-%d %H:%M:%S')
80
81 # Format the uptime, cost & stack_cost fields
82 printf "%s,%s,%s,%s,%d,%.2f,%.2f\n" "$JOB_NAME" "$BUILD_NUMBER" "$date" \
83        "$resource" "$uptime" "$cost" "$stack_cost" > "$WORKSPACE/archives/cost.csv"