docker build: Update saltstack repo URL, locale
[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 # 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     JSONRPC=$(echo python-jsonrpclib_*_all.deb)
78     cp $JSONRPC $MYDIR
79     dpkg -i $JSONRPC
80 }
81
82 # Function builds Tacker server from github
83 function buildTackerServer () {
84     if [[ -e "${MYDIR}/${SERVER}" ]]; then
85         echo "$SERVER exists."
86         return 1
87     fi
88     cd $MYDIR
89     rm -rf $MYREPO
90     git clone  -b 'SFC_brahmaputra' https://github.com/trozet/tacker.git $MYREPO
91     cd $MYREPO
92     patch -p  1 <<EOFSCP
93 diff -ruN a/setup.cfg b/setup.cfg
94 --- a/setup.cfg 2016-02-08 10:54:37.416525934 +0100
95 +++ b/setup.cfg 2016-02-08 10:55:29.293428896 +0100
96 @@ -22,14 +22,14 @@
97  packages =
98      tacker
99  data_files =
100 -    etc/tacker =
101 +    /etc/tacker =
102          etc/tacker/api-paste.ini
103          etc/tacker/policy.json
104          etc/tacker/tacker.conf
105          etc/tacker/rootwrap.conf
106 -    etc/rootwrap.d =
107 +    /etc/rootwrap.d =
108          etc/tacker/rootwrap.d/servicevm.filters
109 -    etc/init.d = etc/init.d/tacker-server
110 +    /etc/init.d = etc/init.d/tacker-server
111
112  [global]
113  setup-hooks =
114 EOFSCP
115     dpkg --purge python-tacker
116     python setup.py --command-packages=stdeb.command bdist_deb
117 }
118
119 # Function corrects and installs the Tacker-server debian package
120 function blessPackage () {
121     pushd "${MYDIR}/${MYREPO}/deb_dist"
122     SERVER=$(echo python-tacker_*_all.deb)
123     popd
124     DEBFILE="${MYDIR}/${MYREPO}/deb_dist/${SERVER}"
125     TMPDIR=$(mktemp -d /tmp/deb.XXXXXX) || exit 1
126     OUTPUT=$(basename "$DEBFILE")
127     if [[ -e "${MYDIR}/${OUTPUT}" ]]; then
128         echo "$OUTPUT exists."
129         rm -r "$TMPDIR"
130         return 1
131     fi
132     dpkg-deb -x "$DEBFILE" "$TMPDIR"
133     dpkg-deb --control "$DEBFILE" "${TMPDIR}/DEBIAN"
134     cd "$TMPDIR"
135     patch -p 1 <<EOFDC
136 diff -ruN a/DEBIAN/control b/DEBIAN/control
137 --- a/DEBIAN/control    2016-02-08 10:06:18.000000000 +0000
138 +++ b/DEBIAN/control    2016-02-08 10:45:09.501373675 +0000
139 @@ -4,7 +4,7 @@
140  Architecture: all
141  Maintainer: OpenStack <openstack-dev@lists.openstack.org>
142  Installed-Size: 1575
143 -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
144 +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
145  Section: python
146  Priority: optional
147  Description: OpenStack servicevm/device manager
148 EOFDC
149     cd "$MYDIR"
150     echo "Patching  deb..."
151     dpkg -b "$TMPDIR" "${MYDIR}/${SERVER}"
152     rm -r "$TMPDIR"
153     dpkg -i "${MYDIR}/${SERVER}"
154 }
155
156 # Function deploys Tacker-server (installs missing mandatory files: upstart, default)
157 function deployTackerServer () {
158     rm -rf /etc/default/tacker-server
159     cat > /etc/default/tacker-server <<EOFTD
160 ENABLED=true
161 PIDFILE=/var/run/tacker/tacker-server.pid
162 LOGFILE=/var/log/tacker/tacker-server.log
163 PATH="\${PATH:+\$PATH:}/usr/sbin:/sbin"
164 TMPDIR=/var/lib/tacker/tmp
165 EOFTD
166     rm -rf /etc/init/tacker.conf
167     cat > /etc/init/tacker.conf <<EOFSC
168 # tacker-server - Provides the Tacker servicevm/device manager service
169 description      "Openstack Tacker Server"
170 author           "Ferenc Cserepkei <ferenc.cserepkei@ericsson.com>"
171
172 start on runlevel [2345]
173 stop on runlevel [!2345]
174
175 respawn
176 respawn limit 20 5
177 limit nofile 65535 65535
178
179 chdir /var/run
180
181 pre-start script
182   # stop job from continuing if no config file found for daemon
183   [ ! -f /etc/default/tacker-server ] && { stop; exit 0; }
184   [ ! -f /etc/tacker/tacker.conf ]  && { stop; exit 0; }
185
186   # source the config file
187   . /etc/default/tacker-server
188
189   # stop job from continuing if admin has not enabled service in
190   # config file.
191   [ -z "\$ENABLED" ] && { stop; exit 0; }
192
193   mkdir -p /var/run/tacker
194   mkdir -p /var/log/tacker
195   echo "Starting tacker server"
196 end script
197
198 pre-stop script
199   echo "Stopping tacker server"
200 end script
201
202 exec /usr/bin/python /usr/bin/tacker-server --log-file=/var/log/tacker/tacker-server.log -v -d --config-file=/etc/tacker/tacker.conf
203 EOFSC
204 }
205
206 # Function installs python-tackerclient from github
207 function deployTackerClient() {
208     if [[ -e "${MYDIR}/${CLIENT}" ]]; then
209         echo "$CLIENT exists."
210         return 1
211     fi
212     cd $MYDIR
213     rm -rf $CLIREPO
214     dpkg --purge python-tackerclient
215     git clone -b 'SFC_refactor' https://github.com/trozet/python-tackerclient.git $CLIREPO
216     cd $CLIREPO
217     python setup.py --command-packages=stdeb.command bdist_deb
218     cd "deb_dist"
219     CLIENT=$(echo python-python-tackerclient_*_all.deb)
220     cp $CLIENT $MYDIR
221     dpkg -i "${MYDIR}/${CLIENT}"
222 }
223
224 # Function removes the cloned git repositories
225 function remove_repo () {
226     if [[ -d "${MYDIR}/${1}" ]]; then
227         rm -r "$1"
228     fi
229 }
230
231 # Funcion copies and installs built artifact on all remaining cluster nodes
232 function populate_client() {
233     wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
234     dpkg -i deb &&\
235     rm deb
236
237     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}" )
238     myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
239     for anode in $clusternodes ; do
240         if [ "$anode" != "$myaddr" ] ; then
241             echo "Installing $CLIENT on $anode"
242             scp ${SSH_OPTIONS[@]} $CLIENT $anode:$CLIENT
243             ssh ${SSH_OPTIONS[@]} $anode dpkg -i $CLIENT
244             ssh ${SSH_OPTIONS[@]} $anode rm $CLIENT
245         fi
246     done
247 }
248
249 # Function orchestrate the Tacker service
250 function orchestarte () {
251     rm -rf /etc/puppet/modules/tacker
252     pushd /etc/puppet/modules
253     git clone https://github.com/trozet/puppet-tacker.git tacker
254     rm -rf /etc/puppet/modules/tacker/.git
255     popd
256
257     ### Facts ###
258
259     # Port(s)   Protocol ServiceDetails Source
260     # 8805-8872 tcp,udp  Unassigned     IANA
261     bind_port='8808'
262
263     auth_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'auth_uri')
264     identity_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'identity_uri')
265     mgmt_addr=$(ifconfig br-mgmt | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
266     pub_addr=$(ifconfig br-ex-lnx | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
267     rabbit_host=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_hosts'| cut -d ':' -f 1)
268     rabbit_password=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_password')
269     sql_host=$(hiera database_vip)
270     database_connection="mysql://tacker:tacker@${sql_host}/tacker"
271     admin_url="http://${mgmt_addr}:${bind_port}"
272     public_url="http://${pub_addr}:${bind_port}"
273     heat_api_vip=$(crudini --get '/etc/heat/heat.conf' 'heat_api' 'bind_host')
274     allowed_hosts="[ '${sql_host}', '${HOSTNAME%%.domain.tld}', 'localhost', '127.0.0.1', '%' ]"
275     heat_uri="http://${heat_api_vip}:8004/v1"
276     odl_port='8282'
277     service_tenant='services'
278     myRegion='RegionOne'
279     myPassword='tacker'
280
281     cat > configure_tacker.pp << EOF
282    class mysql::config {}
283    include mysql::config
284    class mysql::server {}
285    include mysql::server
286
287    class { 'tacker':
288      package_ensure        => 'absent',
289      client_package_ensure => 'absent',
290      bind_port             => '${bind_port}',
291      keystone_password     => '${myPassword}',
292      keystone_tenant       => '${service_tenant}',
293      auth_uri              => '${auth_uri}',
294      identity_uri          => '${identity_uri}',
295      database_connection   => '${database_connection}',
296      rabbit_host           => '${rabbit_host}',
297      rabbit_password       => '${rabbit_password}',
298      heat_uri              => '${heat_uri}',
299      opendaylight_host     => '${mgmt_addr}',
300      opendaylight_port     => '${odl_port}',
301    }
302
303    class { 'tacker::db::mysql':
304        password      => '${myPassword}',
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='${myPassword}'
328 export OS_AUTH_URL='${auth_uri}'
329 export OS_DEFAULT_DOMAIN='default'
330 export OS_AUTH_STRATEGY='keystone'
331 export OS_REGION_NAME='${myRegion}'
332 export TACKER_ENDPOINT_TYPE='internalURL'
333 EOFRC
334     chmod +x tackerc
335 }
336
337 # Funcion copies and installs built environment settings on all remaining cluster nodes
338 function populate_rc() {
339     wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
340     dpkg -i deb &&\
341     rm deb
342
343     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}" )
344     myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
345     for anode in $clusternodes ; do
346         if [ "$anode" != "$myaddr" ] ; then
347             echo "Populating seetings to $anode"
348             scp ${SSH_OPTIONS[@]} tackerc $anode:tackerc
349         fi
350     done
351 }
352
353 envSetup
354 deployTackerClient
355 deployJsonrpclib
356 buildTackerServer
357 blessPackage
358 deployTackerServer
359 populate_client
360 orchestarte
361 populate_rc
362
363 remove_repo "$MYREPO"
364 remove_repo "$DEPREPO"
365 remove_repo "$CLIREPO"
366
367 echo "Built: ${MYDIR}/${OUTPUT}"
368 echo "Built: ${MYDIR}/${CLIENT}"
369 echo "Built: ${MYDIR}/${JSONRPC}"
370 echo "tackerc - mandatory environmental parameters file created"