Merge "Accept maven configuration in Python tox sonar"
[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 "---> build-cost.sh"
12
13 set -euf -o pipefail
14
15 # shellcheck disable=SC1090
16 source ~/lf-env.sh
17
18 lf-activate-venv python-openstackclient
19
20 if [[ -z ${JOB_NAME:-} ]]; then
21     lf-echo-error "Required Env Variable Unset/Empty: JOB_NAME"
22     exit 1
23 fi
24
25 # Get the cost of the Openstack agents. The 'stack-cost' file is created when
26 # the 'lftools openstack stack cost' command is called from
27 # 'openstack-stack-delete.sh' script. The 'stack-cost' file will only be created
28 # if this is an openstack job.
29 if [[ -f stack-cost ]]; then
30     echo "DEBUG: $(cat stack-cost)"
31     echo "INFO: Retrieving Stack Cost..."
32     if ! stack_cost=$(grep -F "total: " stack-cost | awk '{print $2}'); then
33         echo "ERROR: Unable to retrieve Stack Cost, continuing anyway"
34         stack_cost=0
35     fi
36 else
37     echo "INFO: No Stack..."
38     stack_cost=0
39 fi
40
41 # Retrieve the current uptime (in seconds)
42 uptime=$(awk '{print $1}' /proc/uptime)
43 # Convert to integer by truncating fractional part' and round up by one
44 ((uptime=${uptime%\.*}+1))
45
46 instance_type=$(curl -s http://169.254.169.254/latest/meta-data/instance-type)
47
48 echo "INFO: Retrieving Pricing Info for: $instance_type"
49 url="https://pricing.vexxhost.net/v1/pricing/$instance_type/cost?seconds=$uptime"
50 json_block=$(curl -s "$url")
51
52 cost=$(jq .cost <<< "$json_block")
53 resource=$(jq .resource <<< "$json_block" | tr -d '"')
54
55 # Archive the cost date
56 mkdir -p "$WORKSPACE/archives/cost"
57
58 echo "INFO: Archiving Costs"
59
60 # Set the timestamp in GMT
61 # This format is readable by spreadsheet and is easily sortable
62 date=$(TZ=GMT date +'%Y-%m-%d %H:%M:%S')
63
64 cat << EOF > "$WORKSPACE/archives/cost.csv"
65 $JOB_NAME,$BUILD_NUMBER,$date,$resource,$uptime,$cost,$stack_cost
66 EOF
67