POC script updates for Colorado
[fuel.git] / prototypes / sfc_tacker / poc.tacker-up.sh
1 #!/bin/bash
2
3 #
4 # POC Script to build/install/deploy/orchestrate Tacker on an OPNFV Brhamaputra Fuel cluster
5 #         Script assuming it runs on the openstack primary controller (where is opendaylight
6 #         present) and there is a fuel master on 10.20.0.2 and can be reached with default
7 #         credentials.
8 #
9 # author: Ferenc Cserepkei <ferenc.cserepkei@ericsson.com>
10 #
11 #         (c) 2016 Telefonaktiebolaget L. M. ERICSSON
12 #
13 #         All rights reserved. This program and the accompanying materials are made available
14 #         under the terms of the Apache License, Version 2.0 which accompanies this distribution,
15 #         and is available at http://www.apache.org/licenses/LICENSE-2.0
16 #
17
18
19 SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error)
20
21 MYDIR=$(dirname $(readlink -f "$0"))
22 MYREPO="tacker-server"
23 CLIREPO="tacker-client"
24 DEPREPO="jsonrpclib"
25
26 CLIENT=$(echo python-python-tackerclient_*_all.deb)
27 JSONRPC=$(echo python-jsonrpclib_*_all.deb)
28 SERVER=$(echo python-tacker_*_all.deb)
29
30 #fuel admin user name
31 fadm="fadm"
32
33 # Function checks whether crudini is available, if not - installs
34 function chkCrudini () {
35     if [[ ! -f '/usr/bin/crudini' ]]; then
36         wget -N http://mirrors.kernel.org/ubuntu/pool/universe/p/python-iniparse/python-iniparse_0.4-2.1build1_all.deb
37         wget -N http://archive.ubuntu.com/ubuntu/pool/universe/c/crudini/crudini_0.3-1_amd64.deb
38         dpkg -i python-iniparse_0.4-2.1build1_all.deb crudini_0.3-1_amd64.deb
39     fi
40 }
41
42 # Function checks whether a python egg is available, if not, installs
43 function chkPPkg () {
44     PKG="$1"
45     IPPACK=$(python - <<'____EOF'
46 import pip
47 from os.path import join
48 for package in pip.get_installed_distributions():
49     print(package.location)
50     print(join(package.location, *package._get_metadata("top_level.txt")))
51 ____EOF
52 )
53     echo "$IPPACK" | grep -q "$PKG"
54     if [ $? -ne 0 ];then
55         pip install "$PKG"
56     fi
57 }
58
59 # Function setting up the build/deploy environment
60 function envSetup () {
61     apt-get update
62     apt-get install -y git python-pip python-all debhelper
63     chkPPkg stdeb
64     chkCrudini
65 }
66
67 # Function installs jsonrpclib from github
68 function deployJsonrpclib () {
69     if [[ -e "${MYDIR}/${JSONRPC}" ]]; then
70         echo "$JSONRPC exists."
71         return 1
72     fi
73     cd $MYDIR
74     rm -rf $DEPREPO
75     git clone https://github.com/joshmarshall/jsonrpclib.git $DEPREPO
76     cd $DEPREPO
77     dpkg --purge python-jsonrpclib
78     python setup.py --command-packages=stdeb.command bdist_deb
79     cd "deb_dist"
80     JSONRPC=$(echo python-jsonrpclib_*_all.deb)
81     cp $JSONRPC $MYDIR
82     dpkg -i $JSONRPC
83 }
84
85 # Function builds Tacker server from github
86 function buildTackerServer () {
87     if [[ -e "${MYDIR}/${SERVER}" ]]; then
88         echo "$SERVER exists."
89         return 1
90     fi
91     cd $MYDIR
92     rm -rf $MYREPO
93     git clone  -b 'SFC_colorado' https://github.com/trozet/tacker.git $MYREPO
94     cd $MYREPO
95     patch -p  1 <<EOFSCP
96 diff -ruN a/setup.cfg b/setup.cfg
97 --- a/setup.cfg 2016-02-08 10:54:37.416525934 +0100
98 +++ b/setup.cfg 2016-02-08 10:55:29.293428896 +0100
99 @@ -22,14 +22,14 @@
100  packages =
101      tacker
102  data_files =
103 -    etc/tacker =
104 +    /etc/tacker =
105          etc/tacker/api-paste.ini
106          etc/tacker/policy.json
107          etc/tacker/tacker.conf
108          etc/tacker/rootwrap.conf
109 -    etc/rootwrap.d =
110 +    /etc/rootwrap.d =
111          etc/tacker/rootwrap.d/servicevm.filters
112 -    etc/init.d = etc/init.d/tacker-server
113 +    /etc/init.d = etc/init.d/tacker-server
114
115  [global]
116  setup-hooks =
117 EOFSCP
118     dpkg --purge python-tacker
119     python setup.py --command-packages=stdeb.command bdist_deb
120 }
121
122 # Function corrects and installs the Tacker-server debian package
123 function blessPackage () {
124     pushd "${MYDIR}/${MYREPO}/deb_dist"
125     SERVER=$(echo python-tacker_*_all.deb)
126     popd
127     DEBFILE="${MYDIR}/${MYREPO}/deb_dist/${SERVER}"
128     TMPDIR=$(mktemp -d /tmp/deb.XXXXXX) || exit 1
129     OUTPUT=$(basename "$DEBFILE")
130     if [[ -e "${MYDIR}/${OUTPUT}" ]]; then
131         echo "$OUTPUT exists."
132         rm -r "$TMPDIR"
133         return 1
134     fi
135     dpkg-deb -x "$DEBFILE" "$TMPDIR"
136     dpkg-deb --control "$DEBFILE" "${TMPDIR}/DEBIAN"
137     cd "$TMPDIR"
138     patch -p 1 <<EOFDC
139 diff -ruN a/DEBIAN/control b/DEBIAN/control
140 --- a/DEBIAN/control    2016-02-08 10:06:18.000000000 +0000
141 +++ b/DEBIAN/control    2016-02-08 10:45:09.501373675 +0000
142 @@ -4,7 +4,7 @@
143  Architecture: all
144  Maintainer: OpenStack <openstack-dev@lists.openstack.org>
145  Installed-Size: 1575
146 -Depends: python (>= 2.7), python (<< 2.8), python:any (>= 2.7.1-0ubuntu2), python-pbr, python-paste, python-pastedeploy, python-routes, python-anyjson, python-babel, python-eventlet, python-greenlet, python-httplib2, python-requests, python-iso8601, python-jsonrpclib, python-jinja2, python-kombu, python-netaddr, python-sqlalchemy (>= 1.0~), python-sqlalchemy (<< 1.1), python-webob, python-heatclient, python-keystoneclient, alembic, python-six, python-stevedore, python-oslo.config, python-oslo.messaging-, python-oslo.rootwrap, python-novaclient
147 +Depends: python (>= 2.7), python (<< 2.8), python:any (>= 2.7.1-0ubuntu2), python-pbr, python-paste, python-pastedeploy, python-routes, python-anyjson, python-babel, python-eventlet, python-greenlet, python-httplib2, python-requests, python-iso8601, python-jsonrpclib, python-jinja2, python-kombu, python-netaddr, python-sqlalchemy (>= 1.0~), python-sqlalchemy (<< 1.1), python-webob, python-heatclient, python-keystoneclient, alembic, python-six, python-stevedore, python-oslo.config, python-oslo.messaging, python-oslo.rootwrap, python-novaclient
148  Section: python
149  Priority: optional
150  Description: OpenStack servicevm/device manager
151 EOFDC
152     cd "$MYDIR"
153     echo "Patching  deb..."
154     dpkg -b "$TMPDIR" "${MYDIR}/${SERVER}"
155     rm -r "$TMPDIR"
156     dpkg -i "${MYDIR}/${SERVER}"
157 }
158
159 # Function deploys Tacker-server (installs missing mandatory files: upstart, default)
160 function deployTackerServer () {
161     rm -rf /etc/default/tacker-server
162     cat > /etc/default/tacker-server <<EOFTD
163 ENABLED=true
164 PIDFILE=/var/run/tacker/tacker-server.pid
165 LOGFILE=/var/log/tacker/tacker-server.log
166 PATH="\${PATH:+\$PATH:}/usr/sbin:/sbin"
167 TMPDIR=/var/lib/tacker/tmp
168 EOFTD
169     rm -rf /etc/init/tacker.conf
170     cat > /etc/init/tacker.conf <<EOFSC
171 # tacker-server - Provides the Tacker servicevm/device manager service
172 description      "Openstack Tacker Server"
173 author           "Ferenc Cserepkei <ferenc.cserepkei@ericsson.com>"
174
175 start on runlevel [2345]
176 stop on runlevel [!2345]
177
178 respawn
179 respawn limit 20 5
180 limit nofile 65535 65535
181
182 chdir /var/run
183
184 pre-start script
185   # stop job from continuing if no config file found for daemon
186   [ ! -f /etc/default/tacker-server ] && { stop; exit 0; }
187   [ ! -f /etc/tacker/tacker.conf ]  && { stop; exit 0; }
188
189   # source the config file
190   . /etc/default/tacker-server
191
192   # stop job from continuing if admin has not enabled service in
193   # config file.
194   [ -z "\$ENABLED" ] && { stop; exit 0; }
195
196   mkdir -p /var/run/tacker
197   mkdir -p /var/log/tacker
198   echo "Starting tacker server"
199 end script
200
201 pre-stop script
202   echo "Stopping tacker server"
203 end script
204
205 exec /usr/bin/python /usr/bin/tacker-server --log-file=/var/log/tacker/tacker-server.log -v -d --config-file=/etc/tacker/tacker.conf
206 EOFSC
207 }
208
209 # Function installs python-tackerclient from github
210 function deployTackerClient() {
211     if [[ -e "${MYDIR}/${CLIENT}" ]]; then
212         echo "$CLIENT exists."
213         return 1
214     fi
215     cd $MYDIR
216     rm -rf $CLIREPO
217     dpkg --purge python-tackerclient
218     git clone -b 'SFC_refactor' https://github.com/trozet/python-tackerclient.git $CLIREPO
219     cd $CLIREPO
220     python setup.py --command-packages=stdeb.command bdist_deb
221     cd "deb_dist"
222     CLIENT=$(echo python-python-tackerclient_*_all.deb)
223     cp $CLIENT $MYDIR
224     dpkg -i "${MYDIR}/${CLIENT}"
225 }
226
227 # Function removes the cloned git repositories
228 function remove_repo () {
229     if [[ -d "${MYDIR}/${1}" ]]; then
230         rm -r "$1"
231     fi
232 }
233
234 # Funcion copies and installs built artifact on all remaining cluster nodes
235 function populate_client() {
236     wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
237     dpkg -i deb &&\
238     rm deb
239
240     clusternodes=$(sshpass -p "r00tme" ssh ${SSH_OPTIONS[@]} root@10.20.0.2 fuel node | cut -d '|' -f 5 | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" )
241     myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
242     for anode in $clusternodes ; do
243         if [ "$anode" != "$myaddr" ] ; then
244             echo "Installing $CLIENT on $anode"
245             scp ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" ${CLIENT} ${fadm}@${anode}:${CLIENT}
246             ssh ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" ${fadm}@${anode} sudo dpkg -i ${CLIENT}
247             ssh ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" ${fadm}@${anode} rm ${CLIENT}
248
249         fi
250     done
251 }
252
253 # Function orchestrate the Tacker service
254 function orchestarte () {
255     rm -rf /etc/puppet/modules/tacker
256     pushd /etc/puppet/modules
257     git clone https://github.com/trozet/puppet-tacker.git tacker
258     rm -rf /etc/puppet/modules/tacker/.git
259     popd
260
261     ### Facts ###
262
263     # Port(s)   Protocol ServiceDetails Source
264     # 8805-8872 tcp,udp  Unassigned     IANA
265     bind_port='8808'
266
267     auth_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'auth_uri')
268     identity_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'identity_uri')
269     int_addr=$(ifconfig br-mesh | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
270     mgmt_addr=$(ifconfig br-mgmt | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
271     pub_addr=$(ifconfig br-ex-lnx | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
272     rabbit_host=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_hosts'| cut -d ':' -f 1)
273     rabbit_password=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_password')
274     sql_host=$(hiera database_vip)
275     database_connection="mysql://tacker:tacker@${sql_host}/tacker"
276     internal_url="http://${int_addr}:${bind_port}"
277     admin_url="http://${mgmt_addr}:${bind_port}"
278     public_url="http://${pub_addr}:${bind_port}"
279     heat_api_vip=$(crudini --get '/etc/heat/heat.conf' 'heat_api' 'bind_host')
280     allowed_hosts="[ '${sql_host}', '${HOSTNAME%%.domain.tld}', 'localhost', '127.0.0.1', '%' ]"
281     heat_uri="http://${heat_api_vip}:8004/v1"
282     odl_port='8282'
283     service_tenant='services'
284     myRegion='RegionOne'
285     myPassword='tacker'
286
287     cat > configure_tacker.pp << EOF
288    class mysql::config {}
289    include mysql::config
290    class mysql::server {}
291    include mysql::server
292
293    class { 'tacker':
294      package_ensure        => 'absent',
295      client_package_ensure => 'absent',
296      bind_port             => '${bind_port}',
297      keystone_password     => '${myPassword}',
298      keystone_tenant       => '${service_tenant}',
299      auth_uri              => '${auth_uri}',
300      identity_uri          => '${identity_uri}',
301      database_connection   => '${database_connection}',
302      rabbit_host           => '${rabbit_host}',
303      rabbit_password       => '${rabbit_password}',
304      heat_uri              => '${heat_uri}',
305      opendaylight_host     => '${mgmt_addr}',
306      opendaylight_port     => '${odl_port}',
307    }
308
309    class { 'tacker::db::mysql':
310        password      => '${myPassword}',
311        allowed_hosts => ${allowed_hosts},
312    }
313
314    class { 'tacker::keystone::auth':
315      password            => '${myPassword}',
316      tenant              => '${service_tenant}',
317      admin_url           => '${admin_url}',
318      internal_url        => '${internal_url}',
319      public_url          => '${public_url}',
320      region              => '${myRegion}',
321    }
322 EOF
323
324     apt-get install -y mysql-client-5.5
325     cat > /usr/bin/tacker-manage <<EOFAKEMANAGE
326 #!/bin/bash
327 EOFAKEMANAGE
328     chmod +x /usr/bin/tacker-manage
329     puppet apply configure_tacker.pp
330     rm -f tackerc
331     cat > tackerc <<EOFRC
332 #!/bin/sh
333 export LC_ALL=C
334 export OS_NO_CACHE='true'
335 export OS_TENANT_NAME='${service_tenant}'
336 export OS_PROJECT_NAME='${service_tenant}'
337 export OS_USERNAME='tacker'
338 export OS_PASSWORD='${myPassword}'
339 export OS_AUTH_URL='${auth_uri}'
340 export OS_DEFAULT_DOMAIN='default'
341 export OS_AUTH_STRATEGY='keystone'
342 export OS_REGION_NAME='${myRegion}'
343 export TACKER_ENDPOINT_TYPE='internalURL'
344 EOFRC
345     chmod +x tackerc
346 }
347
348 # Funcion copies and installs built environment settings on all remaining cluster nodes
349 function populate_rc() {
350     wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
351     dpkg -i deb &&\
352     rm deb
353
354     clusternodes=$(sshpass -p "r00tme" ssh ${SSH_OPTIONS[@]} root@10.20.0.2 fuel node | cut -d '|' -f 5 | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" )
355     myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
356     for anode in $clusternodes ; do
357         if [ "$anode" != "$myaddr" ] ; then
358             echo "Populating settings to $anode"
359             scp ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" tackerc ${fadm}@${anode}:tackerc
360         fi
361     done
362 }
363
364 envSetup
365 deployTackerClient
366 deployJsonrpclib
367 buildTackerServer
368 blessPackage
369 deployTackerServer
370 populate_client
371 orchestarte
372 populate_rc
373
374 remove_repo "$MYREPO"
375 remove_repo "$DEPREPO"
376 remove_repo "$CLIREPO"
377
378 echo "Built: ${MYDIR}/${OUTPUT}"
379 echo "Built: ${MYDIR}/${CLIENT}"
380 echo "Built: ${MYDIR}/${JSONRPC}"
381 echo "tackerc - mandatory environmental parameters file created"