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