Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / capture-instance-metadata.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2020 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
12 echo "---> capture-instance-metadata.sh"
13
14 # shellcheck disable=SC1090
15 source ~/lf-env.sh
16
17 if [[ "${PYTHON:-}" -ne "" ]]; then
18     lf-activate-venv --python "$PYTHON" lftools
19 elif python3; then
20     lf-activate-venv --python python3 lftools
21 else
22     lf-activate-venv lftools
23 fi
24
25 # detect if we're in EC2
26 if [ -n "${NOMAD_DC}" ]; then
27     echo "INFO: Running in Nomad, no metadata"
28     exit 0
29 fi
30
31 if [[ ! -f /run/cloud-init/result.json ]]; then
32     echo "INFO: Running in unsupported cloud, no metadata"
33     exit 0
34 fi
35
36 # AWS not supported, exit
37 cloudtype="$(jq -r .v1.datasource /run/cloud-init/result.json)"
38 if [[ $cloudtype == "DataSourceEc2Local" ]]; then
39     echo "INFO: Running in AWS, not capturing instance metadata"
40     exit 0
41 fi
42
43 # Retrive OpenStack instace metadata APIs at this IP.
44 # The instance id and other metadata is useful for debugging VM.
45 echo "INFO: Running in OpenStack, capturing instance metadata"
46 curl -s http://169.254.169.254/openstack/latest/meta_data.json \
47     | python -mjson.tool > "$WORKSPACE/archives/instance_meta_data.json"