Merge "Revert "Pass host cpu flags to compute guests""
[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-08-09 05:13:13.000000000 -0400
141 +++ b/DEBIAN/control    2016-08-09 05:32:56.761035046 -0400
142 @@ -4,7 +4,7 @@
143  Architecture: all
144  Maintainer: OpenStack <openstack-dev@lists.openstack.org>
145  Installed-Size: 1565
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         fi
249     done
250 }
251
252 # Function orchestrate the Tacker service
253 function orchestarte () {
254     rm -rf /etc/puppet/modules/tacker
255     pushd /etc/puppet/modules
256     git clone https://github.com/trozet/puppet-tacker.git tacker
257     rm -rf /etc/puppet/modules/tacker/.git
258     popd
259
260     ### Facts ###
261
262     # Port(s)   Protocol ServiceDetails Source
263     # 8805-8872 tcp,udp  Unassigned     IANA
264     bind_port='8808'
265
266     auth_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'auth_uri')
267     identity_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'identity_uri')
268     int_addr=$(ifconfig br-mesh | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
269     mgmt_addr=$(ifconfig br-mgmt | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
270     pub_addr=$(ifconfig br-ex-lnx | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
271     rabbit_host=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_hosts'| cut -d ':' -f 1)
272     rabbit_password=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_password')
273     sql_host=$(hiera database_vip)
274     database_connection="mysql://tacker:tacker@${sql_host}/tacker"
275     internal_url="http://${int_addr}:${bind_port}"
276     admin_url="http://${mgmt_addr}:${bind_port}"
277     public_url="http://${pub_addr}:${bind_port}"
278     heat_api_vip=$(crudini --get '/etc/heat/heat.conf' 'heat_api' 'bind_host')
279     allowed_hosts="[ '${sql_host}', '${HOSTNAME%%.domain.tld}', 'localhost', '127.0.0.1', '%' ]"
280     heat_uri="http://${heat_api_vip}:8004/v1"
281     odl_port='8282'
282     service_tenant='services'
283     myRegion='RegionOne'
284     myPassword='tacker'
285
286     cat > configure_tacker.pp << EOF
287    class mysql::config {}
288    include mysql::config
289    class mysql::server {}
290    include mysql::server
291
292    class { 'tacker':
293      package_ensure        => 'absent',
294      client_package_ensure => 'absent',
295      bind_port             => '${bind_port}',
296      keystone_password     => '${myPassword}',
297      keystone_tenant       => '${service_tenant}',
298      auth_uri              => '${auth_uri}',
299      identity_uri          => '${identity_uri}',
300      database_connection   => '${database_connection}',
301      rabbit_host           => '${rabbit_host}',
302      rabbit_password       => '${rabbit_password}',
303      heat_uri              => '${heat_uri}',
304      opendaylight_host     => '${mgmt_addr}',
305      opendaylight_port     => '${odl_port}',
306    }
307
308    class { 'tacker::db::mysql':
309        password      => '${myPassword}',
310        allowed_hosts => ${allowed_hosts},
311    }
312
313    class { 'tacker::keystone::auth':
314      password            => '${myPassword}',
315      tenant              => '${service_tenant}',
316      admin_url           => '${admin_url}',
317      internal_url        => '${internal_url}',
318      public_url          => '${public_url}',
319      region              => '${myRegion}',
320    }
321 EOF
322
323     apt-get install -y mysql-client-5.5
324     cat > /usr/bin/tacker-manage <<EOFAKEMANAGE
325 #!/bin/bash
326 EOFAKEMANAGE
327     chmod +x /usr/bin/tacker-manage
328     puppet apply configure_tacker.pp
329     rm -f tackerc
330     cat > tackerc <<EOFRC
331 #!/bin/sh
332 export LC_ALL=C
333 export OS_NO_CACHE='true'
334 export OS_TENANT_NAME='${service_tenant}'
335 export OS_PROJECT_NAME='${service_tenant}'
336 export OS_USERNAME='tacker'
337 export OS_PASSWORD='${myPassword}'
338 export OS_AUTH_URL='${auth_uri}'
339 export OS_DEFAULT_DOMAIN='default'
340 export OS_AUTH_STRATEGY='keystone'
341 export OS_REGION_NAME='${myRegion}'
342 export TACKER_ENDPOINT_TYPE='internalURL'
343 EOFRC
344     chmod +x tackerc
345 }
346
347 # Funcion copies and installs built environment settings on all remaining cluster nodes
348 function populate_rc() {
349     wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
350     dpkg -i deb &&\
351     rm deb
352
353     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}" )
354     myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
355     for anode in $clusternodes ; do
356         if [ "$anode" != "$myaddr" ] ; then
357             echo "Populating settings to $anode"
358             scp ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" tackerc ${fadm}@${anode}:tackerc
359         fi
360     done
361 }
362
363 envSetup
364 deployTackerClient
365 deployJsonrpclib
366 buildTackerServer
367 blessPackage
368 deployTackerServer
369 populate_client
370 orchestarte
371 populate_rc
372
373 remove_repo "$MYREPO"
374 remove_repo "$DEPREPO"
375 remove_repo "$CLIREPO"
376
377 echo "Built: ${MYDIR}/${OUTPUT}"
378 echo "Built: ${MYDIR}/${CLIENT}"
379 echo "Built: ${MYDIR}/${JSONRPC}"
380 echo "tackerc - mandatory environmental parameters file created"