Merge "Change from using chef to local config files"
[integration.git] / bootstrap / vagrant-onap / lib / oom
1 #!/bin/bash
2
3 source /var/onap/functions
4
5 RANCHER_PORT=8880
6 oom_delay=30
7 export RANCHER_URL=http://localhost:$RANCHER_PORT
8 export RANCHER_ACCESS_KEY='access_key'
9 export RANCHER_SECRET_KEY='secret_key'
10
11 # _install_docker() - Function that installs Docker version for Rancher
12 function _install_docker {
13     if ! $(docker version &>/dev/null); then
14         curl https://releases.rancher.com/install-docker/1.12.sh | sh
15         _configure_docker_settings 15
16     fi
17 }
18
19 # _pull_rancher_images() - Function that retrieves Rancher images required for k8s
20 function _pull_rancher_images {
21     for image in "net:v0.13.5" "k8s:v1.8.5-rancher3" \
22 "lb-service-rancher:v0.7.17" "network-manager:v0.7.18" "metadata:v0.9.5" \
23 "kubectld:v0.8.5" "kubernetes-agent:v0.6.6" "dns:v0.15.3" \
24 "kubernetes-auth:v0.0.8" "healthcheck:v0.3.3" "etcd:v2.3.7-13" \
25 "etc-host-updater:v0.0.3" "net:holder"; do
26         pull_docker_image rancher/$image &
27     done
28 }
29
30 # _pull_k8s_images() - Function that retrieves Google k8s images
31 function _pull_k8s_images {
32     for image in "kubernetes-dashboard-amd64:v1.7.1" \
33 "k8s-dns-sidecar-amd64:1.14.5" "k8s-dns-kube-dns-amd64:1.14.5" \
34 "k8s-dns-dnsmasq-nanny-amd64:1.14.5" "heapster-influxdb-amd64:v1.3.3" \
35 "heapster-grafana-amd64:v4.4.3" "heapster-amd64:v1.4.0" "pause-amd64:3.0"; do
36         pull_docker_image gcr.io/google_containers/$image &
37     done
38 }
39
40 # _install_rancher() - Function that installs Rancher CLI and container
41 function _install_rancher {
42     local rancher_version=v0.6.5
43     local rancher_server_version=v1.6.10
44     local rancher_server=rancher/server:$rancher_server_version
45
46     if [ ! -d /opt/rancher/current ]; then
47         mkdir -p /opt/rancher/current
48         wget https://github.com/rancher/cli/releases/download/$rancher_version/rancher-linux-amd64-$rancher_version.tar.gz
49         tar -xzf rancher-linux-amd64-$rancher_version.tar.gz -C /tmp
50         mv /tmp/rancher-$rancher_version/rancher /opt/rancher/current/
51     fi
52
53     _install_docker
54     pull_docker_image $rancher_server
55     run_docker_image -d --restart=unless-stopped -p $RANCHER_PORT:8080 $rancher_server
56     while true; do
57         if curl --fail -X GET $RANCHER_URL; then
58             break
59         fi
60         echo "waiting for racher"
61         sleep $oom_delay
62     done
63 }
64
65 # _install_kubernetes() - Function that deploys kubernetes via RancherOS host registration
66 function _install_kubernetes {
67     local rancher_agent_version=v1.2.7
68     local rancher_agent=rancher/agent:$rancher_agent_version
69
70     _install_rancher
71
72     _pull_rancher_images
73     _pull_k8s_images
74     pull_docker_image $rancher_agent
75     wait_docker_pull
76
77     pushd /opt/rancher/current/
78     export RANCHER_ENVIRONMENT=`./rancher env create -t kubernetes onap_on_kubernetes`
79     popd
80
81     install_python_package rancher-agent-registration
82     export no_proxy=$no_proxy,$IP_ADDRESS
83     rancher-agent-registration --host-ip $IP_ADDRESS --url http://$IP_ADDRESS:$RANCHER_PORT --environment $RANCHER_ENVIRONMENT --key $RANCHER_ACCESS_KEY --secret $RANCHER_SECRET_KEY
84 }
85
86 # _install_kubectl() - Function that installs kubectl as client for kubernetes
87 function _install_kubectl {
88     if ! $(kubectl version &>/dev/null); then
89         rm -rf ~/.kube
90         curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
91         chmod +x ./kubectl
92         mv ./kubectl /usr/local/bin/kubectl
93         mkdir ~/.kube
94         pushd ~/.kube
95         python /var/onap/files/kubectl_config_generator.py
96         popd
97     fi
98 }
99
100 # _install_helm() - Function that install Kubernetes Package Manager
101 function _install_helm {
102     local helm_version=v2.3.0
103
104     if ! $(helm version &>/dev/null); then
105         wget http://storage.googleapis.com/kubernetes-helm/helm-${helm_version}-linux-amd64.tar.gz
106         tar -zxvf helm-${helm_version}-linux-amd64.tar.gz -C /tmp
107         mv /tmp/linux-amd64/helm /usr/local/bin/helm
108         helm init
109     fi
110 }
111
112 # _pull_images_from_yaml() - Function that parses a yaml file and pull their images
113 function _pull_images_from_yaml_file {
114     local values_file=$1
115     local prefix=$2
116     local s='[[:space:]]*'
117     local w='[a-zA-Z0-9_]*'
118     fs=`echo @|tr @ '\034'`
119
120     for line in $(sed -ne "s|^\($s\):|\1|" \
121 -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
122 -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $values_file |
123 awk -F$fs '{
124 indent = length($1)/2;
125 vname[indent] = $2;
126 for (i in vname) {
127     if (i > indent) {
128         delete vname[i]}
129     }
130     if (length($3) > 0) {
131         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])(".")}
132         printf("%s%s%s=%s\n", "'$prefix'",vn, $2, $3);
133     }
134 }' | grep image); do
135         echo $line
136         if echo $line | grep -q Version ; then
137             pull_docker_image "$image_name:$(echo $line | awk -F "=" '{print $2}')" &
138         else
139             image_name=`echo ${line#*=}`
140             if [[ ${image_name#*${nexus_docker_repo:-nexus3.onap.org:10001}} == *:* ]]; then
141                 pull_docker_image $image_name &
142             else
143                 pull_docker_image $image_name:latest
144             fi
145         fi
146     done
147 }
148
149 # get_oom_images() - Function that retrieves ONAP images from official hub
150 function get_oom_images {
151     if [[ "$build_image" == "True" ]]; then
152         # TODO(electrocucaracha): Create a function for calling the build docker function of every ONAP project
153         echo "Not Implemented"
154     else
155         if [[ "$clone_repo" != "True" ]]; then
156             clone_repos "oom"
157         fi
158
159         docker_openecomp_login
160         for values_file in `find ${src_folders[oom]}/kubernetes -name values.yaml -type f`; do
161             _pull_images_from_yaml_file $values_file
162         done
163         docker logout
164         wait_docker_pull
165     fi
166 }
167
168 # _install_oom() - Function that clones OOM and deploys ONAP
169 function install_oom {
170     if [[ "$clone_repo" != "True" ]]; then
171         clone_repos "oom"
172     fi
173     pushd ${src_folders[oom]}/kubernetes/oneclick
174     source setenv.bash
175
176     pushd ${src_folders[oom]}/kubernetes/config
177     cp onap-parameters-sample.yaml onap-parameters.yaml
178     ./createConfig.sh -n onap
179     popd
180
181     for app in consul msb mso message-router sdnc vid robot portal policy appc aai sdc dcaegen2 log cli multicloud clamp vnfsdk uui aaf vfc kube2msb; do
182         ./createAll.bash -n onap -a $app
183     done
184     popd
185 }
186
187 # init_oom() - Function that deploys ONAP using OOM
188 function init_oom {
189     mount_external_partition sda /var/lib/docker/
190     _install_kubernetes
191     _install_kubectl
192     _install_helm
193     if [[ "$clone_repo" == "True" ]]; then
194         clone_repos "oom"
195     fi
196
197     if [[ "$skip_get_images" == "False" ]]; then
198         get_oom_images
199         if [[ "$skip_install" == "False" ]]; then
200             until kubectl cluster-info; do
201                 echo "waiting for kubernetes host"
202                 sleep $oom_delay
203             done
204             install_oom
205         fi
206     fi
207 }