5aef45d1f3c6d02db9c181e8829597049a1c4d3a
[releng/global-jjb.git] / shell / openstack-kubernetes-create.sh
1 #!/bin/bash -l
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 # shellcheck disable=SC2153,SC2034
12 echo "---> Creating kubernetes cluster"
13
14 set -eux -o pipefail
15
16 # shellcheck disable=SC1090
17 source ~/lf-env.sh
18
19 # Check if openstack venv was previously created
20 if [ -f "/tmp/.os_lf_venv" ]; then
21     os_lf_venv=$(cat "/tmp/.os_lf_venv")
22 fi
23
24 if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
25     echo "Re-use existing venv: ${os_lf_venv}"
26     PATH=$os_lf_venv/bin:$PATH
27 else
28     lf-activate-venv --python python3 "cryptography<3.4" \
29         "lftools[openstack]" \
30         kubernetes \
31         "niet~=1.4.2" \
32         python-heatclient \
33         python-openstackclient \
34         python-magnumclient \
35         setuptools \
36         "openstacksdk<0.99" \
37         yq
38 fi
39
40 os_cloud="${OS_CLOUD:-vex}"
41 fixed_network="${FIXED_NETWORK}"
42 fixed_subnet="${FIXED_SUBNET}"
43 cluster_template_name="${CLUSTER_TEMPLATE_NAME}"
44 cluster_name="${CLUSTER_NAME}"
45 base_image="${BASE_IMAGE}"
46 keypair="${KEYPAIR}"
47 master_flavor="${MASTER_FLAVOR}"
48 node_flavor="${NODE_FLAVOR}"
49 master_count="${MASTER_COUNT:-1}"
50 node_count="${NODE_COUNT:-2}"
51 boot_volume_size="${BOOT_VOLUME_SIZE}"
52 docker_volume_size="${DOCKER_VOLUME_SIZE}"
53 k8s_version="${KUBERNETES_VERSION}"
54 cluster_settle_time="${CLUSTER_SETTLE_TIME:-1m}"
55
56
57 # Create the template for the cluster first. Returns the cluster ID as $template_uuid
58 template_uuid=$(openstack coe cluster template create "$cluster_template_name" \
59     --os-cloud "$os_cloud" \
60     --image "$base_image" \
61     --keypair "$keypair" \
62     --external-network public \
63     --fixed-network "$fixed_network" \
64     --fixed-subnet "$fixed_subnet" \
65     --floating-ip-disabled \
66     --master-flavor "$master_flavor" \
67     --flavor "$node_flavor" \
68     --docker-volume-size "$docker_volume_size" \
69     --network-driver flannel \
70     --master-lb-enabled \
71     --volume-driver cinder \
72     --labels \
73 boot_volume_type=ssd,boot_volume_size="${boot_volume_size}",\
74 kube_version="${k8s_version}",kube_tag="${k8s_version}" \
75     --coe kubernetes \
76     -f value -c uuid | tail -1)
77
78 # Create the kubernetes cluster
79 cluster_uuid=$(openstack coe cluster create "$cluster_name" \
80     --os-cloud "$os_cloud" \
81     --master-count "$master_count" \
82     --node-count "$node_count" \
83     --cluster-template "$template_uuid" | awk -F ' ' '{print $5}')
84
85 # Sleep for a little, because sometimes OpenStack has to catch up with itself
86 sleep 15
87
88 while \
89 [ "$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" \
90 -c status -f value)" == "CREATE_IN_PROGRESS" ]; do
91     # echo "sleeping $(date)"
92     sleep 2m
93 done
94
95 if [ "$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" \
96 -c status -f value)" == "CREATE_FAILED" ]; then
97     echo "Failed to create cluster: $cluster_uuid $(date)"
98     openstack --os-cloud "$os_cloud" coe cluster delete "$cluster_uuid"
99     sleep 5m
100     openstack --os-cloud "$os_cloud" coe cluster template delete "$template_uuid"
101     exit 1
102 fi
103
104 if [ "$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" \
105 -c status -f value)" == "CREATE_COMPLETE" ]; then
106     echo "Successfully created cluster: $cluster_uuid."
107 fi