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