-
Notifications
You must be signed in to change notification settings - Fork 6
/
fetch-infra-env.sh
executable file
·107 lines (87 loc) · 3.16 KB
/
fetch-infra-env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# Helper script to fetch infra-env from assisted-installer
# usage: ./fetch-infra-env.sh
# usage: ./fetch-infor-env <cluster-name>
#
# based on https://github.com/openshift/assisted-service/tree/master/docs/user-guide/samples
if ! type "yq" > /dev/null; then
echo "Cannot find yq in the path, please install yq on the node first. ref: https://github.com/mikefarah/yq#install"
fi
if ! type "jinja2" > /dev/null; then
echo "Cannot find jinja2 in the path, will install it with pip3 install jinja2-cli and pip3 install jinja2-cli[yaml]"
pip3 install --user jinja2-cli
pip3 install --user jinja2-cli[yaml]
fi
usage(){
echo "Usage : $0"
echo "Usage : $0 <cluster_name>"
echo "Example : $0 sno130"
}
if [[ ( $@ == "--help") || $@ == "-h" ]]
then
usage
exit
fi
basedir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
cluster_name=$1; shift
if [ -z "$cluster_name" ]; then
cluster_name=$(ls -t $basedir/instances |head -1)
fi
cluster_workspace=$basedir/instances/$cluster_name
config_file=$cluster_workspace/config-resolved.yaml
if [ -f "$config_file" ]; then
echo "Will access cluster $cluster_name with config: $config_file"
else
"Config file $config_file not exist, please check."
exit -1
fi
domain_name=$(yq '.cluster.domain' $config_file)
api_fqdn="api."$cluster_name"."$domain_name
export KUBECONFIG=$cluster_workspace/auth/kubeconfig
bmc_noproxy=$(yq ".bmc.bypass_proxy" $config_file)
CURL=curl
if [[ "true"=="${bmc_noproxy}" ]]; then
CURL+=" --noproxy ${bmc_address}"
fi
echo "-------------------------------"
echo "Waiting for Assisted Installer..."
assisted_rest=http://$api_fqdn:8090
# first wait for 200 response code
REMOTE_CURL="ssh -q -oStrictHostKeyChecking=no core@$api_fqdn curl -s"
while [[ "$($REMOTE_CURL -o /dev/null -w ''%{http_code}'' $assisted_rest/api/assisted-install/v2/clusters)" != "200" ]]; do
echo -n "."
sleep 10;
done
# now wait till cluster_id become avaiable
while
cluster_href=$($REMOTE_CURL $assisted_rest/api/assisted-install/v2/clusters|jq -r '.[0].href')
[[ "$cluster_href" == "null" ]]
do
echo -n "."
sleep 10;
done
echo
cluster_id=$(echo ${cluster_href}|rev|cut -f 1 -d / |rev)
output="${cluster_workspace}/assisted-env-${cluster_id}"
echo "Output directory: ${output}"
mkdir -p ${output}
# TODO should we wait till it's ready to install?
echo "Saving ${output}/cluster.json"
$REMOTE_CURL $assisted_rest${cluster_href} > ${output}/cluster.json
echo "Saving ${output}/hosts.json"
$REMOTE_CURL "$assisted_rest${cluster_href}/hosts" > ${output}/hosts.json
while read -r host_href; do
id=$(echo ${host_href}|rev|cut -f 1 -d / | rev)
echo "Saving ${output}/infra_envs.${id}.json"
$REMOTE_CURL "$assisted_rest${host_href}" > ${output}/infra_envs.${id}.json
echo "==> Parsing to ${output}/infra_envs.${id}.parsed.json"
jq \
'def convert_json(key): key|=(.// empty|fromjson);
.| convert_json(.ntp_sources)
| convert_json(.domain_name_resolutions)
| convert_json(.disks_info)
| convert_json(.images_status)
| convert_json(.validations_info)
| convert_json(.inventory)' \
${output}/infra_envs.${id}.json > ${output}/infra_envs.${id}.parsed.json
done < <(jq -r '.[]|.href' ${output}/hosts.json)