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