-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdeploy-controlplane.sh
More file actions
executable file
·158 lines (135 loc) · 5.05 KB
/
deploy-controlplane.sh
File metadata and controls
executable file
·158 lines (135 loc) · 5.05 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $SCRIPTDIR/.bluemixrc
#################################################################################
# Pull Dockerhub images
#################################################################################
echo "Looking up Bluemix registry images"
BLUEMIX_IMAGES=$(cf ic images --format "{{.Repository}}:{{.Tag}}")
REQUIRED_IMAGES=(
${CONTROLLER_IMAGE}
${REGISTRY_IMAGE}
)
for image in ${REQUIRED_IMAGES[@]}; do
echo $BLUEMIX_IMAGES | grep $image > /dev/null
if [ $? -ne 0 ]; then
echo "Pulling ${DOCKERHUB_NAMESPACE}/$image from Dockerhub"
cf ic cpi ${DOCKERHUB_NAMESPACE}/$image ${BLUEMIX_REGISTRY_HOST}/${BLUEMIX_REGISTRY_NAMESPACE}/$image
fi
done
#################################################################################
# Start a local controller
#################################################################################
echo "Starting controller"
cf ic group create --name amalgam8_controller \
--publish 6379 --memory 256 --auto \
--min 1 --max 2 --desired 1 \
--env POLL_INTERVAL=5s \
--env LOG_LEVEL=debug \
${BLUEMIX_REGISTRY_HOST}/${BLUEMIX_REGISTRY_NAMESPACE}/${CONTROLLER_IMAGE}
echo "Waiting for controller to start..."
sleep 15s
echo "Mapping route to controller: $CONTROLLER_URL"
cf ic route map --hostname $CONTROLLER_HOSTNAME --domain $ROUTES_DOMAIN amalgam8_controller
#################################################################################
# Provision Service Discovery, or start a local registry
#################################################################################
if [ "$ENABLE_SERVICEDISCOVERY" = true ]; then
cf service sd &> /dev/null
if [ $? -ne 0 ]; then
echo "Creating a Service Discovery instance..."
cf create-service service_discovery free sd
else
echo "Found an existing Service Discovery instance"
fi
if [ $(cf service-key sd sdkey | grep -ic "No service key") -gt 0 ]; then
echo "Creating Service Discovery credentials"
cf create-service-key sd sdkey
else
echo "Found existing Service Discovery credentials"
fi
SDKEY=$(cf service-key sd sdkey | tail -n +3)
REGISTRY_URL=$(echo "$SDKEY" | jq -r '.url')
REGISTRY_TOKEN=$(echo "$SDKEY" | jq -r '.auth_token')
else
# TODO: Deploy registry containers
echo "Not not implemented"
exit 1
fi
#################################################################################
# Provision Message Hub, or fallback to polling
#################################################################################
if [ "$ENABLE_MESSAGEHUB" = true ]; then
cf service mh &> /dev/null
if [ $? -ne 0 ]; then
echo "Creating a Message Hub instance..."
cf create-service messagehub standard mh
else
echo "Found an existing Message Hub instance"
fi
if [ $(cf service-key mh mhkey | grep -ic "No service key") -gt 0 ]; then
echo "Creating Message Hub credentials"
cf create-service-key mh mhkey
else
echo "Found existing Message Hub credentials"
fi
MHKEY=$(cf service-key mh mhkey | tail -n +3)
KAFKA_API_KEY=$(echo "$MHKEY" | jq -r '.api_key')
KAFKA_ADMIN_URL=$(echo "$MHKEY" | jq -r '.kafka_admin_url')
KAFKA_REST_URL=$(echo "$MHKEY" | jq -r '.kafka_rest_url')
KAFKA_BROKERS=$(echo "$MHKEY" | jq -r '.kafka_brokers_sasl')
KAFKA_USER=$(echo "$MHKEY" | jq -r '.user')
KAFKA_PASSWORD=$(echo "$MHKEY" | jq -r '.password')
fi
#################################################################################
# Post the local tenant to controller
#################################################################################
# Wait for controller route to set up
echo "Waiting for controller route to set up"
attempt=0
while true; do
code=$(curl -w "%{http_code}" "${CONTROLLER_URL}/health" -o /dev/null)
if [ "$code" = "200" ]; then
echo "Controller route is ready"
break
fi
attempt=$((attempt + 1))
if [ "$attempt" -gt 10 ]; then
echo "Timeout waiting for controller route..."
echo "Deploying the controlplane has failed"
exit 1
fi
sleep 10s
done
echo "Setting up a new controller tenant named 'local'"
read -d '' tenant << EOF
{
"id": "${CONTROLLER_TENANT_ID}",
"req_tracking_header" : "X-Request-ID",
"credentials": {
"registry": {
"url": "${REGISTRY_URL}",
"token": "${REGISTRY_TOKEN}"
}
}
}
EOF
if [ "$ENABLE_MESSAGEHUB" = true ]; then
read -d '' kafka << EOF
{
"credentials": {
"kafka": {
"api_key": "${KAFKA_API_KEY}",
"admin_url": "${KAFKA_ADMIN_URL}",
"rest_url": "${KAFKA_REST_URL}",
"brokers": ${KAFKA_BROKERS},
"user": "${KAFKA_USER}",
"password": "${KAFKA_PASSWORD}",
"sasl": true
}
}
}
EOF
tenant=$(jq -s '.[0] * .[1]' <(echo $tenant) <(echo $kafka))
fi
echo $tenant | curl -H "Content-Type: application/json" -d @- "${CONTROLLER_URL}/v1/tenants"