upgrade rancher/k8s to 1.6.25
[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 # casablanca (until RC1)
30 #     Rancher 1.6.18, Kubernetes 1.10.3, Kubectl 1.10.3, Helm 2.9.2, Docker 17.03
31 # casablanca - 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 # master/dublin - LOG-895
34 #     Rancher 1.6.25, Kubernetes 1.11.3, kubectl 1.11.3, Helm 2.9.2, Docker 17.03
35
36 usage() {
37 cat <<EOF
38 Usage: $0 [PARAMs]
39 example
40 sudo ./oom_rancher_setup.sh -b master -s cd.onap.cloud -e onap -c false -a 104.209.168.116 -v true
41 -u                  : Display usage
42 -b [branch]         : branch = master or beijing or amsterdam (required)
43 -s [server]         : server = IP or DNS name (required)
44 -e [environment]    : use the default (onap)
45 -c [true/false]     : use computed client address (default true)
46 -a [IP address]     : client address ip - no FQDN
47 -v [true/false]     : validate
48 EOF
49 }
50
51 install_onap() {
52   #constants
53   USERNAME=ubuntu
54   PORT=8880
55
56   if [ "$BRANCH" == "amsterdam" ]; then
57     RANCHER_VERSION=1.6.10
58     KUBECTL_VERSION=1.7.7
59     HELM_VERSION=2.3.0
60     DOCKER_VERSION=1.12
61     AGENT_VERSION=1.2.6
62   elif [ "$BRANCH" == "beijing" ]; then
63     RANCHER_VERSION=1.6.14
64     KUBECTL_VERSION=1.8.10
65     HELM_VERSION=2.8.2
66     DOCKER_VERSION=17.03
67     AGENT_VERSION=1.2.9
68   elif [ "$BRANCH" == "casablanca" ]; then
69     RANCHER_VERSION=1.6.22
70     KUBECTL_VERSION=1.11.2
71     HELM_VERSION=2.9.1
72     DOCKER_VERSION=17.03
73     AGENT_VERSION=1.2.11
74   else
75     RANCHER_VERSION=1.6.25
76     KUBECTL_VERSION=1.11.3
77     HELM_VERSION=2.9.1
78     DOCKER_VERSION=17.03
79     AGENT_VERSION=1.2.11
80   fi
81
82   echo "Installing on ${SERVER} for ${BRANCH}: Rancher: ${RANCHER_VERSION} Kubectl: ${KUBECTL_VERSION} Helm: ${HELM_VERSION} Docker: ${DOCKER_VERSION}"
83   sudo echo "127.0.0.1 ${SERVER}" >> /etc/hosts
84
85   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"
86   curl https://releases.rancher.com/install-docker/$DOCKER_VERSION.sh | sh
87   sudo usermod -aG docker $USERNAME
88
89   echo "install make - required for beijing+"
90   sudo apt-get install make -y
91
92   sudo docker run -d --restart=unless-stopped -p $PORT:8080 --name rancher_server rancher/server:v$RANCHER_VERSION
93   sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/v$KUBECTL_VERSION/bin/linux/amd64/kubectl
94   sudo chmod +x ./kubectl
95   sudo mv ./kubectl /usr/local/bin/kubectl
96   sudo mkdir ~/.kube
97   wget http://storage.googleapis.com/kubernetes-helm/helm-v${HELM_VERSION}-linux-amd64.tar.gz
98   sudo tar -zxvf helm-v${HELM_VERSION}-linux-amd64.tar.gz
99   sudo mv linux-amd64/helm /usr/local/bin/helm
100
101   # create kubernetes environment on rancher using cli
102   RANCHER_CLI_VER=0.6.7
103   KUBE_ENV_NAME=$ENVIRON
104   wget https://releases.rancher.com/cli/v${RANCHER_CLI_VER}/rancher-linux-amd64-v${RANCHER_CLI_VER}.tar.gz
105   sudo tar -zxvf rancher-linux-amd64-v${RANCHER_CLI_VER}.tar.gz
106   sudo cp rancher-v${RANCHER_CLI_VER}/rancher .
107   sudo chmod +x ./rancher
108
109   echo "install jq"
110   apt install jq -y
111   echo "wait for rancher server container to finish - 3 min"
112   sleep 60
113   echo "2 more min"
114   sleep 60
115   echo "1 min left"
116   sleep 60
117
118   echo "get public and private tokens back to the rancher server so we can register the client later"
119   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}'`
120   # Extract and store token
121   echo "API_RESPONSE: $API_RESPONSE"
122   KEY_PUBLIC=`echo $API_RESPONSE | jq -r .publicValue`
123   KEY_SECRET=`echo $API_RESPONSE | jq -r .secretValue`
124   echo "publicValue: $KEY_PUBLIC secretValue: $KEY_SECRET"
125
126   export RANCHER_URL=http://${SERVER}:$PORT
127   export RANCHER_ACCESS_KEY=$KEY_PUBLIC
128   export RANCHER_SECRET_KEY=$KEY_SECRET
129   ./rancher env ls
130   echo "wait 60 sec for rancher environments can settle before we create the onap kubernetes one"
131   sleep 60
132
133   echo "Creating kubernetes environment named ${KUBE_ENV_NAME}"
134   ./rancher env create -t kubernetes $KUBE_ENV_NAME > kube_env_id.json
135   PROJECT_ID=$(<kube_env_id.json)
136   echo "env id: $PROJECT_ID"
137   export RANCHER_HOST_URL=http://${SERVER}:$PORT/v1/projects/$PROJECT_ID
138   echo "you should see an additional kubernetes environment usually with id 1a7"
139   ./rancher env ls
140   # optionally disable cattle env
141
142   # add host registration url
143   # https://github.com/rancher/rancher/issues/2599
144   # wait for REGISTERING to ACTIVE
145   echo "sleep 90 to wait for REG to ACTIVE"
146   ./rancher env ls
147   sleep 30
148   echo "check on environments again before registering the URL response"
149   ./rancher env ls
150   sleep 30
151   ./rancher env ls
152   echo "60 more sec"
153   sleep 60
154
155   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"`
156   echo "REG_URL_RESPONSE: $REG_URL_RESPONSE"
157   echo "wait for server to finish url configuration - 5 min"
158   sleep 240
159   echo "60 more sec"
160   sleep 60
161   # see registrationUrl in
162   REGISTRATION_TOKENS=`curl http://127.0.0.1:$PORT/v2-beta/registrationtokens`
163   echo "REGISTRATION_TOKENS: $REGISTRATION_TOKENS"
164   REGISTRATION_URL=`echo $REGISTRATION_TOKENS | jq -r .data[0].registrationUrl`
165   REGISTRATION_DOCKER=`echo $REGISTRATION_TOKENS | jq -r .data[0].image`
166   REGISTRATION_TOKEN=`echo $REGISTRATION_TOKENS | jq -r .data[0].token`
167   echo "Registering host for image: $REGISTRATION_DOCKER url: $REGISTRATION_URL registrationToken: $REGISTRATION_TOKEN"
168   HOST_REG_COMMAND=`echo $REGISTRATION_TOKENS | jq -r .data[0].command`
169   echo "Running agent docker..."
170   if [[ "$COMPUTEADDRESS" != false ]]; then
171       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"
172       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
173   else
174       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"
175       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
176   fi
177   echo "waiting 8 min for host registration to finish"
178   sleep 420
179   echo "1 more min"
180   sleep 60
181   #read -p "wait for host registration to complete before generating the client token....."
182
183   # base64 encode the kubectl token from the auth pair
184   # generate this after the host is registered
185   KUBECTL_TOKEN=$(echo -n 'Basic '$(echo -n "$RANCHER_ACCESS_KEY:$RANCHER_SECRET_KEY" | base64 -w 0) | base64 -w 0)
186   echo "KUBECTL_TOKEN base64 encoded: ${KUBECTL_TOKEN}"
187   # add kubectl config - NOTE: the following spacing has to be "exact" or kubectl will not connect - with a localhost:8080 error
188   cat > ~/.kube/config <<EOF
189 apiVersion: v1
190 kind: Config
191 clusters:
192 - cluster:
193     api-version: v1
194     insecure-skip-tls-verify: true
195     server: "https://$SERVER:$PORT/r/projects/$PROJECT_ID/kubernetes:6443"
196   name: "${ENVIRON}"
197 contexts:
198 - context:
199     cluster: "${ENVIRON}"
200     user: "${ENVIRON}"
201   name: "${ENVIRON}"
202 current-context: "${ENVIRON}"
203 users:
204 - name: "${ENVIRON}"
205   user:
206     token: "$KUBECTL_TOKEN"
207
208 EOF
209
210   echo "run the following if you installed a higher kubectl version than the server"
211   echo "helm init --upgrade"
212   echo "Verify all pods up on the kubernetes system - will return localhost:8080 until a host is added"
213   echo "kubectl get pods --all-namespaces"
214   kubectl get pods --all-namespaces
215   echo "upgrade server side of helm in kubernetes"
216   sudo helm version
217   echo "sleep 90"
218   sleep 90
219   sudo helm init --upgrade
220   echo "sleep 90"
221   sleep 90
222   echo "verify both versions are the same below"
223   sudo helm version
224   echo "start helm server"
225   sudo helm serve &
226   echo "sleep 30"
227   sleep 30
228   echo "add local helm repo"
229   sudo helm repo add local http://127.0.0.1:8879
230   sudo helm repo list
231   echo "To enable grafana dashboard - do this after running cd.sh which brings up onap - or you may get a 302xx port conflict"
232   echo "kubectl expose -n kube-system deployment monitoring-grafana --type=LoadBalancer --name monitoring-grafana-client"
233   echo "to get the nodeport for a specific VM running grafana"
234   echo "kubectl get services --all-namespaces | grep graf"
235   kubectl get pods --all-namespaces
236   echo "finished!"
237 }
238
239 BRANCH=
240 SERVER=
241 ENVIRON=
242 COMPUTEADDRESS=true
243 ADDRESS=
244 VALIDATE=false
245
246 while getopts ":b:s:e:u:c:a:v" PARAM; do
247   case $PARAM in
248     u)
249       usage
250       exit 1
251       ;;
252     b)
253       BRANCH=${OPTARG}
254       ;;
255     e)
256       ENVIRON=${OPTARG}
257       ;;
258     s)
259       SERVER=${OPTARG}
260       ;;
261     c)
262       COMPUTEADDRESS=${OPTARG}
263       ;;
264     a)
265       ADDRESS=${OPTARG}
266       ;;
267     v)
268       VALIDATE=${OPTARG}
269       ;;
270     ?)
271       usage
272       exit
273       ;;
274     esac
275 done
276
277 if [[ -z $BRANCH ]]; then
278   usage
279   exit 1
280 fi
281
282 install_onap $BRANCH $SERVER $ENVIRON $COMPUTEADDRESS $ADDRESS $VALIDATE
283