0998dd1af2beddb8bbc6db94d3ee4c1b7714d951
[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 # detect if we're in EC2
15 if [ -n "${NOMAD_DC}" ]; then
16     echo "INFO: Running in Nomad, no metadata"
17     exit 0
18 fi
19
20 if [[ ! -f /run/cloud-init/result.json ]]; then
21     echo "INFO: Running in unsupported cloud, no metadata"
22     exit 0
23 fi
24
25 # AWS not supported, exit
26 cloudtype="$(jq -r .v1.datasource /run/cloud-init/result.json)"
27 if [[ $cloudtype == "DataSourceEc2Local" ]]; then
28     echo "INFO: Running in AWS, not capturing instance metadata"
29     exit 0
30 fi
31
32 # Retrive OpenStack instace metadata APIs at this IP.
33 # The instance id and other metadata is useful for debugging VM.
34 echo "INFO: Running in OpenStack, capturing instance metadata"
35 curl -s http://169.254.169.254/openstack/latest/meta_data.json \
36     | python -mjson.tool > "$WORKSPACE/archives/instance_meta_data.json"