-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_cluster.sh
executable file
·97 lines (77 loc) · 3.19 KB
/
setup_cluster.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
#!/bin/bash -e
set -x
export WORKSPACE=$(pwd)
export OPENSHIFT_REPO=${OPENSHIFT_REPO:-"${WORKSPACE}/tf-openshift"}
AI_CLUSTER_NAME=${AI_CLUSTER_NAME:-"test9"}
sso_url="https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token"
api_base="https://api.openshift.com/api/assisted-install/v1/"
cluster_name="test9"
if [[ -z "${OFFLINE_TOKEN}" ]]; then
echo "ERROR: Set offline token before call openshift API"
exit 1
fi
function get_access_token(){
local res=$(curl -s -X POST -d "client_id=cloud-services&grant_type=refresh_token&refresh_token=${OFFLINE_TOKEN}" $sso_url | jq -r '.access_token')
echo "$res"
}
function get_clusters(){
local token=$1
local res=$(curl -s -H 'Accept: application/json' -H "Authorization: Bearer ${token}" ${api_base}/clusters)
echo $res
}
function post_manifests_folder_to_ai(){
local clister_id=$1
local token=$2
local folder=$3
# Itarate over all manifests
for file in $(ls $OPENSHIFT_REPO/deploy/${folder}); do
local content=$(cat $OPENSHIFT_REPO/deploy/${folder}/${file} | base64)
local url="${api_base}clusters/${cluster_id}/manifests"
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -H "Authorization: Bearer ${token}" -d "{\"folder\":\"${folder}\",\"file_name\":\"${file}\",\"content\":\"${content}\"}" ${url}
done
}
function post_manifests_to_ai(){
local clister_id=$1
local token=$2
post_manifests_folder_to_ai ${cluster_id} ${token} "openshift"
post_manifests_folder_to_ai ${cluster_id} ${token} "manifests"
}
function post_generate_iso(){
local cluster_id=$1
local token=$2
local url="${api_base}clusters/${cluster_id}/downloads/image"
res=$(curl -s -H "Accept: application/json" -H "Content-type: application/json" -X POST -H "Authorization: Bearer ${token}" -d "{\"ssh_public_key\":\"$(cat ~/.ssh/id_rsa.pub)\",\"image_type\":\"full-iso\"}" ${url} | jq -r '.image_info.download_url')
echo "$res"
}
# Prepare openshift manifests
if [[ ! -d $OPENSHIFT_REPO ]]; then
git clone https://github.com/tungstenfabric/tf-openshift.git $OPENSHIFT_REPO
fi
# Get auth token
access_token=$(get_access_token)
clusters=$(get_clusters "${access_token}")
# Find cluster
clusters_len=$(echo "${clusters}" | jq length)
for i in $(seq 1 $clusters_len); do
echo No $i
cn=$(echo "${clusters}" | jq -r ".[$((i-1))].name")
if [[ "$cn" == "$cluster_name" ]]; then
echo "INFO: We have found cluster $cn"
cluster_id=$(echo "${clusters}" | jq -r ".[$((i-1))].id")
fi
done
if [[ -z ${cluster_id} ]]; then
echo "ERROR: unable to find cluster"
exit 1
fi
# Upload tf-openshift manifests to assisted installer
post_manifests_to_ai $cluster_id $access_token
# Generate and download ISO from assisted installer
download_url=$(post_generate_iso $cluster_id $access_token)
curl --output cluster.iso ${download_url}
# Run machines
#for i in $(seq 0 2); do
# sudo virt-install --name oc-master${i} --network network=default,model=virtio --memory 16384 --vcpus 4 --disk size=120 --cdrom /home/ubuntu/assisted_installer/discovery_image_test9.iso --os-variant rhel7 &
#done
# TODO Wait for machines up
# Setup hostnames for all machines