align Rancher/k8s with 1.6.22 post RC1
[logging-analytics.git] / deploy / rancher / oom_rancher_setup.sh
1 #!/bin/bash
2 #############################################################################
3 #
4 # Copyright © 2018 Amdocs.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #        http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #############################################################################
18 #
19 # This installation is for a rancher managed install of kubernetes
20 # after this run the standard oom install
21 # this installation can be run on amy ubuntu 16.04 VM or physical host
22 # https://wiki.onap.org/display/DW/Cloud+Native+Deployment
23 # source from https://jira.onap.org/browse/LOG-320
24 # Michael O'Brien
25 # amsterdam
26 #     Rancher 1.6.10, Kubernetes 1.7.7, Kubectl 1.7.7, Helm 2.3.0, Docker 1.12
27 # beijing
28 #     Rancher 1.6.14, Kubernetes 1.8.10, Kubectl 1.8.10, Helm 2.8.2, Docker 17.03
29 # master/casablanca (until RC1)
30 #     Rancher 1.6.18, Kubernetes 1.10.3, Kubectl 1.10.3, Helm 2.9.2, Docker 17.03
31 # master/casablanca (post RC2) - integration change alignment for INT-586 - 29th Oct via LOG-806
32 #     Rancher 1.6.22, Kubernetes 1.11.2, kubectl 1.11.2, Helm 2.9.2, Docker 17.03
33
34 usage() {
35 cat <<EOF
36 Usage: $0 [PARAMs]
37 example
38 sudo ./oom_rancher_setup.sh -b master -s cd.onap.cloud -e onap -c false -a 104.209.168.116 -v true
39 -u                  : Display usage
40 -b [branch]         : branch = master or beijing or amsterdam (required)
41 -s [server]         : server = IP or DNS name (required)
42 -e [environment]    : use the default (onap)
43 -c [true/false]     : use computed client address (default true)
44 -a [IP address]     : client address ip - no FQDN
45 -v [true/false]     : validate
46 EOF
47 }
48
49 install_onap() {
50   #constants
51   USERNAME=ubuntu
52   PORT=8880
53
54   if [ "$BRANCH" == "amsterdam" ]; then
55     RANCHER_VERSION=1.6.10
56     KUBECTL_VERSION=1.7.7
57     HELM_VERSION=2.3.0
58     DOCKER_VERSION=1.12
59     AGENT_VERSION=1.2.6
60   elif [ "$BRANCH" == "beijing" ]; then
61     RANCHER_VERSION=1.6.14
62     KUBECTL_VERSION=1.8.10
63     HELM_VERSION=2.8.2
64     DOCKER_VERSION=17.03
65     AGENT_VERSION=1.2.9
66   elif [ "$BRANCH" == "casablanca" ]; then
67     RANCHER_VERSION=1.6.22
68     KUBECTL_VERSION=1.11.2
69     HELM_VERSION=2.9.1
70     DOCKER_VERSION=17.03
71     AGENT_VERSION=1.2.11
72   else
73     RANCHER_VERSION=1.6.22
74     KUBECTL_VERSION=1.11.2
75     HELM_VERSION=2.9.1
76     DOCKER_VERSION=17.03
77     AGENT_VERSION=1.2.11
78   fi
79
80   echo "Installing on ${SERVER} for ${BRANCH}: Rancher: ${RANCHER_VERSION} Kubectl: ${KUBECTL_VERSION} Helm: ${HELM_VERSION} Docker: ${DOCKER_VERSION}"
81   sudo echo "127.0.0.1 ${SERVER}" >> /etc/hosts
82
83   echo "If you must install as non-root - comment out the docker install below - run it separately, run the user mod, logout/login and continue this script"
84   curl https://releases.rancher.com/install-docker/$DOCKER_VERSION.sh | sh
85   sudo usermod -aG docker $USERNAME
86
87   echo "install make - required for beijing+"
88   sudo apt-get install make -y
89
90   sudo docker run -d --restart=unless-stopped -p $PORT:8080 --name rancher_server rancher/server:v$RANCHER_VERSION
91   sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/v$KUBECTL_VERSION/bin/linux/amd64/kubectl
92   sudo chmod +x ./kubectl
93   sudo mv ./kubectl /usr/local/bin/kubectl
94   sudo mkdir ~/.kube
95   wget http://storage.googleapis.com/kubernetes-helm/helm-v${HELM_VERSION}-linux-amd64.tar.gz
96   sudo tar -zxvf helm-v${HELM_VERSION}-linux-amd64.tar.gz
97   sudo mv linux-amd64/helm /usr/local/bin/helm
98
99   # create kubernetes environment on rancher using cli
100   RANCHER_CLI_VER=0.6.7
101   KUBE_ENV_NAME=$ENVIRON
102   wget https://releases.rancher.com/cli/v${RANCHER_CLI_VER}/rancher-linux-amd64-v${RANCHER_CLI_VER}.tar.gz
103   sudo tar -zxvf rancher-linux-amd64-v${RANCHER_CLI_VER}.tar.gz
104   sudo cp rancher-v${RANCHER_CLI_VER}/rancher .
105   sudo chmod +x ./rancher
106
107   echo "install jq"
108   apt install jq -y
109   echo "wait for rancher server container to finish - 3 min"
110   sleep 60
111   echo "2 more min"
112   sleep 60
113   echo "1 min left"
114   sleep 60
115
116   echo "get public and private tokens back to the rancher server so we can register the client later"
117   API_RESPONSE=`curl -s 'http://127.0.0.1:8880/v2-beta/apikey' -d '{"type":"apikey","accountId":"1a1","name":"autoinstall","description":"autoinstall","created":null,"kind":null,"removeTime":null,"removed":null,"uuid":null}'`
118   # Extract and store token
119   echo "API_RESPONSE: $API_RESPONSE"
120   KEY_PUBLIC=`echo $API_RESPONSE | jq -r .publicValue`
121   KEY_SECRET=`echo $API_RESPONSE | jq -r .secretValue`
122   echo "publicValue: $KEY_PUBLIC secretValue: $KEY_SECRET"
123
124   export RANCHER_URL=http://${SERVER}:$PORT
125   export RANCHER_ACCESS_KEY=$KEY_PUBLIC
126   export RANCHER_SECRET_KEY=$KEY_SECRET
127   ./rancher env ls
128   echo "wait 60 sec for rancher environments can settle before we create the onap kubernetes one"
129   sleep 60
130
131   echo "Creating kubernetes environment named ${KUBE_ENV_NAME}"
132   ./rancher env create -t kubernetes $KUBE_ENV_NAME > kube_env_id.json
133   PROJECT_ID=$(<kube_env_id.json)
134   echo "env id: $PROJECT_ID"
135   export RANCHER_HOST_URL=http://${SERVER}:$PORT/v1/projects/$PROJECT_ID
136   echo "you should see an additional kubernetes environment usually with id 1a7"
137   ./rancher env ls
138   # optionally disable cattle env
139
140   # add host registration url
141   # https://github.com/rancher/rancher/issues/2599
142   # wait for REGISTERING to ACTIVE
143   echo "sleep 90 to wait for REG to ACTIVE"
144   ./rancher env ls
145   sleep 30
146   echo "check on environments again before registering the URL response"
147   ./rancher env ls
148   sleep 30
149   ./rancher env ls
150   echo "60 more sec"
151   sleep 60
152
153   REG_URL_RESPONSE=`curl -X POST -u $KEY_PUBLIC:$KEY_SECRET -H 'Accept: application/json' -H 'ContentType: application/json' -d '{"name":"$SERVER"}' "http://$SERVER:8880/v1/projects/$PROJECT_ID/registrationtokens"`
154   echo "REG_URL_RESPONSE: $REG_URL_RESPONSE"
155   echo "wait for server to finish url configuration - 5 min"
156   sleep 240
157   echo "60 more sec"
158   sleep 60
159   # see registrationUrl in
160   REGISTRATION_TOKENS=`curl http://127.0.0.1:$PORT/v2-beta/registrationtokens`
161   echo "REGISTRATION_TOKENS: $REGISTRATION_TOKENS"
162   REGISTRATION_URL=`echo $REGISTRATION_TOKENS | jq -r .data[0].registrationUrl`
163   REGISTRATION_DOCKER=`echo $REGISTRATION_TOKENS | jq -r .data[0].image`
164   REGISTRATION_TOKEN=`echo $REGISTRATION_TOKENS | jq -r .data[0].token`
165   echo "Registering host for image: $REGISTRATION_DOCKER url: $REGISTRATION_URL registrationToken: $REGISTRATION_TOKEN"
166   HOST_REG_COMMAND=`echo $REGISTRATION_TOKENS | jq -r .data[0].command`
167   echo "Running agent docker..."
168   if [[ "$COMPUTEADDRESS" != false ]]; then
169       echo "sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher $REGISTRATION_DOCKER $RANCHER_URL/v1/scripts/$REGISTRATION_TOKEN"
170       sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher $REGISTRATION_DOCKER $RANCHER_URL/v1/scripts/$REGISTRATION_TOKEN
171   else
172       echo "sudo docker run -e CATTLE_AGENT_IP=\"$ADDRESS\" --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v$AGENT_VERSION http://$SERVER:$PORT/v1/scripts/$TOKEN"
173       sudo docker run -e CATTLE_AGENT_IP="$ADDRESS" --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v$AGENT_VERSION http://$SERVER:$PORT/v1/scripts/$REGISTRATION_TOKEN
174   fi
175   echo "waiting 8 min for host registration to finish"
176   sleep 420
177   echo "1 more min"
178   sleep 60
179   #read -p "wait for host registration to complete before generating the client token....."
180
181   # base64 encode the kubectl token from the auth pair
182   # generate this after the host is registered
183   KUBECTL_TOKEN=$(echo -n 'Basic '$(echo -n "$RANCHER_ACCESS_KEY:$RANCHER_SECRET_KEY" | base64 -w 0) | base64 -w 0)
184   echo "KUBECTL_TOKEN base64 encoded: ${KUBECTL_TOKEN}"
185   # add kubectl config - NOTE: the following spacing has to be "exact" or kubectl will not connect - with a localhost:8080 error
186   cat > ~/.kube/config <<EOF
187 apiVersion: v1
188 kind: Config
189 clusters:
190 - cluster:
191     api-version: v1
192     insecure-skip-tls-verify: true
193     server: "https://$SERVER:$PORT/r/projects/$PROJECT_ID/kubernetes:6443"
194   name: "${ENVIRON}"
195 contexts:
196 - context:
197     cluster: "${ENVIRON}"
198     user: "${ENVIRON}"
199   name: "${ENVIRON}"
200 current-context: "${ENVIRON}"
201 users:
202 - name: "${ENVIRON}"
203   user:
204     token: "$KUBECTL_TOKEN"
205
206 EOF
207
208   echo "run the following if you installed a higher kubectl version than the server"
209   echo "helm init --upgrade"
210   echo "Verify all pods up on the kubernetes system - will return localhost:8080 until a host is added"
211   echo "kubectl get pods --all-namespaces"
212   kubectl get pods --all-namespaces
213   echo "upgrade server side of helm in kubernetes"
214   sudo helm version
215   echo "sleep 90"
216   sleep 90
217   sudo helm init --upgrade
218   echo "sleep 90"
219   sleep 90
220   echo "verify both versions are the same below"
221   sudo helm version
222   echo "start helm server"
223   sudo helm serve &
224   echo "sleep 30"
225   sleep 30
226   echo "add local helm repo"
227   sudo helm repo add local http://127.0.0.1:8879
228   sudo helm repo list
229   echo "To enable grafana dashboard - do this after running cd.sh which brings up onap - or you may get a 302xx port conflict"
230   echo "kubectl expose -n kube-system deployment monitoring-grafana --type=LoadBalancer --name monitoring-grafana-client"
231   echo "to get the nodeport for a specific VM running grafana"
232   echo "kubectl get services --all-namespaces | grep graf"
233   kubectl get pods --all-namespaces
234   echo "finished!"
235 }
236
237 BRANCH=
238 SERVER=
239 ENVIRON=
240 COMPUTEADDRESS=true
241 ADDRESS=
242 VALIDATE=false
243
244 while getopts ":b:s:e:u:c:a:v" PARAM; do
245   case $PARAM in
246     u)
247       usage
248       exit 1
249       ;;
250     b)
251       BRANCH=${OPTARG}
252       ;;
253     e)
254       ENVIRON=${OPTARG}
255       ;;
256     s)
257       SERVER=${OPTARG}
258       ;;
259     c)
260       COMPUTEADDRESS=${OPTARG}
261       ;;
262     a)
263       ADDRESS=${OPTARG}
264       ;;
265     v)
266       VALIDATE=${OPTARG}
267       ;;
268     ?)
269       usage
270       exit
271       ;;
272     esac
273 done
274
275 if [[ -z $BRANCH ]]; then
276   usage
277   exit 1
278 fi
279
280 install_onap $BRANCH $SERVER $ENVIRON $COMPUTEADDRESS $ADDRESS $VALIDATE
281