Chore: Upgrade Jenkins-job-builder to 6.3.0
[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 lf-activate-venv --python python3 "lftools[openstack]" \
20     kubernetes \
21     niet \
22     python-heatclient \
23     python-openstackclient \
24     python-magnumclient \
25     yq
26
27 os_cloud="${OS_CLOUD:-vex}"
28 fixed_network="${FIXED_NETWORK}"
29 fixed_subnet="${FIXED_SUBNET}"
30 cluster_template_name="${CLUSTER_TEMPLATE_NAME}"
31 cluster_name="${CLUSTER_NAME}"
32 base_image="${BASE_IMAGE}"
33 keypair="${KEYPAIR}"
34 master_flavor="${MASTER_FLAVOR}"
35 node_flavor="${NODE_FLAVOR}"
36 master_count="${MASTER_COUNT:-1}"
37 node_count="${NODE_COUNT:-2}"
38 boot_volume_size="${BOOT_VOLUME_SIZE}"
39 docker_volume_size="${DOCKER_VOLUME_SIZE}"
40 k8s_version="${KUBERNETES_VERSION}"
41 cluster_settle_time="${CLUSTER_SETTLE_TIME:-1m}"
42
43
44 # Create the template for the cluster first. Returns the cluster ID as $template_uuid
45 template_uuid=$(openstack coe cluster template create "$cluster_template_name" \
46     --os-cloud "$os_cloud" \
47     --image "$base_image" \
48     --keypair "$keypair" \
49     --external-network public \
50     --fixed-network "$fixed_network" \
51     --fixed-subnet "$fixed_subnet" \
52     --floating-ip-disabled \
53     --master-flavor "$master_flavor" \
54     --flavor "$node_flavor" \
55     --docker-volume-size "$docker_volume_size" \
56     --network-driver flannel \
57     --master-lb-enabled \
58     --volume-driver cinder \
59     --labels \
60 boot_volume_type=ssd,boot_volume_size="${boot_volume_size}",\
61 kube_version="${k8s_version}",kube_tag="${k8s_version}" \
62     --coe kubernetes \
63     -f value -c uuid | tail -1)
64
65 # Create the kubernetes cluster
66 cluster_uuid=$(openstack coe cluster create "$cluster_name" \
67     --os-cloud "$os_cloud" \
68     --master-count "$master_count" \
69     --node-count "$node_count" \
70     --cluster-template "$template_uuid" | awk -F ' ' '{print $5}')
71
72 # Sleep for a little, because sometimes OpenStack has to catch up with itself
73 sleep 15
74
75 while \
76 [ "$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" \
77 -c status -f value)" == "CREATE_IN_PROGRESS" ]; do
78     # echo "sleeping $(date)"
79     sleep 2m
80 done
81
82 if [ "$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" \
83 -c status -f value)" == "CREATE_FAILED" ]; then
84     echo "Failed to create cluster: $cluster_uuid $(date)"
85     openstack --os-cloud "$os_cloud" coe cluster delete "$cluster_uuid"
86     sleep 5m
87     openstack --os-cloud "$os_cloud" coe cluster template delete "$template_uuid"
88     exit 1
89 fi
90
91 if [ "$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" \
92 -c status -f value)" == "CREATE_COMPLETE" ]; then
93     echo "Successfully created cluster: $cluster_uuid."
94 fi