aboutsummaryrefslogtreecommitdiffstats
path: root/prototypes/sfc_tacker/poc.tacker-up.sh
blob: e22eeac8bf907e8c427b377170d3fa171656b2ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash

#
# POC Script to build/install/deploy/orchestrate Tacker on an OPNFV Brhamaputra Fuel cluster
#         Script assuming it runs on the openstack primary controller (where is opendaylight
#         present) and there is a fuel master on 10.20.0.2 and can be reached with default
#         credentials.
#
# author: Ferenc Cserepkei <ferenc.cserepkei@ericsson.com>
#
#         (c) 2016 Telefonaktiebolaget L. M. ERICSSON
#
#         All rights reserved. This program and the accompanying materials are made available
#         under the terms of the Apache License, Version 2.0 which accompanies this distribution,
#         and is available at http://www.apache.org/licenses/LICENSE-2.0
#


SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error)

MYDIR=$(dirname $(readlink -f "$0"))
MYREPO="tacker-server"
CLIREPO="tacker-client"
DEPREPO="jsonrpclib"

CLIENT=$(echo python-python-tackerclient_*_all.deb)
JSONRPC=$(echo python-jsonrpclib_*_all.deb)
SERVER=$(echo python-tacker_*_all.deb)

#fuel admin user name
fadm="fadm"

# Function checks whether crudini is available, if not - installs
function chkCrudini () {
    if [[ ! -f '/usr/bin/crudini' ]]; then
        wget -N http://mirrors.kernel.org/ubuntu/pool/universe/p/python-iniparse/python-iniparse_0.4-2.1build1_all.deb
        wget -N http://archive.ubuntu.com/ubuntu/pool/universe/c/crudini/crudini_0.3-1_amd64.deb
        dpkg -i python-iniparse_0.4-2.1build1_all.deb crudini_0.3-1_amd64.deb
    fi
}

# Function checks whether a python egg is available, if not, installs
function chkPPkg () {
    PKG="$1"
    IPPACK=$(python - <<'____EOF'
import pip
from os.path import join
for package in pip.get_installed_distributions():
    print(package.location)
    print(join(package.location, *package._get_metadata("top_level.txt")))
____EOF
)
    echo "$IPPACK" | grep -q "$PKG"
    if [ $? -ne 0 ];then
        pip install "$PKG"
    fi
}

# Function setting up the build/deploy environment
function envSetup () {
    apt-get update
    apt-get install -y git python-pip python-all debhelper
    chkPPkg stdeb
    chkCrudini
}

# Function installs jsonrpclib from github
function deployJsonrpclib () {
    if [[ -e "${MYDIR}/${JSONRPC}" ]]; then
        echo "$JSONRPC exists."
        return 1
    fi
    cd $MYDIR
    rm -rf $DEPREPO
    git clone https://github.com/joshmarshall/jsonrpclib.git $DEPREPO
    cd $DEPREPO
    dpkg --purge python-jsonrpclib
    python setup.py --command-packages=stdeb.command bdist_deb
    cd "deb_dist"
    JSONRPC=$(echo python-jsonrpclib_*_all.deb)
    cp $JSONRPC $MYDIR
    dpkg -i $JSONRPC
}

# Function builds Tacker server from github
function buildTackerServer () {
    if [[ -e "${MYDIR}/${SERVER}" ]]; then
        echo "$SERVER exists."
        return 1
    fi
    cd $MYDIR
    rm -rf $MYREPO
    git clone  -b 'SFC_colorado' https://github.com/trozet/tacker.git $MYREPO
    cd $MYREPO
    patch -p  1 <<EOFSCP
diff -ruN a/setup.cfg b/setup.cfg
--- a/setup.cfg	2016-02-08 10:54:37.416525934 +0100
+++ b/setup.cfg	2016-02-08 10:55:29.293428896 +0100
@@ -22,14 +22,14 @@
 packages =
     tacker
 data_files =
-    etc/tacker =
+    /etc/tacker =
         etc/tacker/api-paste.ini
         etc/tacker/policy.json
         etc/tacker/tacker.conf
         etc/tacker/rootwrap.conf
-    etc/rootwrap.d =
+    /etc/rootwrap.d =
         etc/tacker/rootwrap.d/servicevm.filters
-    etc/init.d = etc/init.d/tacker-server
+    /etc/init.d = etc/init.d/tacker-server

 [global]
 setup-hooks =
EOFSCP
    dpkg --purge python-tacker
    python setup.py --command-packages=stdeb.command bdist_deb
}

# Function corrects and installs the Tacker-server debian package
function blessPackage () {
    pushd "${MYDIR}/${MYREPO}/deb_dist"
    SERVER=$(echo python-tacker_*_all.deb)
    popd
    DEBFILE="${MYDIR}/${MYREPO}/deb_dist/${SERVER}"
    TMPDIR=$(mktemp -d /tmp/deb.XXXXXX) || exit 1
    OUTPUT=$(basename "$DEBFILE")
    if [[ -e "${MYDIR}/${OUTPUT}" ]]; then
	echo "$OUTPUT exists."
	rm -r "$TMPDIR"
	return 1
    fi
    dpkg-deb -x "$DEBFILE" "$TMPDIR"
    dpkg-deb --control "$DEBFILE" "${TMPDIR}/DEBIAN"
    cd "$TMPDIR"
    patch -p 1 <<EOFDC
diff -ruN a/DEBIAN/control b/DEBIAN/control
--- a/DEBIAN/control	2016-02-08 10:06:18.000000000 +0000
+++ b/DEBIAN/control	2016-02-08 10:45:09.501373675 +0000
@@ -4,7 +4,7 @@
 Architecture: all
 Maintainer: OpenStack <openstack-dev@lists.openstack.org>
 Installed-Size: 1575
-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
+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
 Section: python
 Priority: optional
 Description: OpenStack servicevm/device manager
EOFDC
    cd "$MYDIR"
    echo "Patching  deb..."
    dpkg -b "$TMPDIR" "${MYDIR}/${SERVER}"
    rm -r "$TMPDIR"
    dpkg -i "${MYDIR}/${SERVER}"
}

# Function deploys Tacker-server (installs missing mandatory files: upstart, default)
function deployTackerServer () {
    rm -rf /etc/default/tacker-server
    cat > /etc/default/tacker-server <<EOFTD
ENABLED=true
PIDFILE=/var/run/tacker/tacker-server.pid
LOGFILE=/var/log/tacker/tacker-server.log
PATH="\${PATH:+\$PATH:}/usr/sbin:/sbin"
TMPDIR=/var/lib/tacker/tmp
EOFTD
    rm -rf /etc/init/tacker.conf
    cat > /etc/init/tacker.conf <<EOFSC
# tacker-server - Provides the Tacker servicevm/device manager service
description      "Openstack Tacker Server"
author           "Ferenc Cserepkei <ferenc.cserepkei@ericsson.com>"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
respawn limit 20 5
limit nofile 65535 65535

chdir /var/run

pre-start script
  # stop job from continuing if no config file found for daemon
  [ ! -f /etc/default/tacker-server ] && { stop; exit 0; }
  [ ! -f /etc/tacker/tacker.conf ]  && { stop; exit 0; }

  # source the config file
  . /etc/default/tacker-server

  # stop job from continuing if admin has not enabled service in
  # config file.
  [ -z "\$ENABLED" ] && { stop; exit 0; }

  mkdir -p /var/run/tacker
  mkdir -p /var/log/tacker
  echo "Starting tacker server"
end script

pre-stop script
  echo "Stopping tacker server"
end script

exec /usr/bin/python /usr/bin/tacker-server --log-file=/var/log/tacker/tacker-server.log -v -d --config-file=/etc/tacker/tacker.conf
EOFSC
}

# Function installs python-tackerclient from github
function deployTackerClient() {
    if [[ -e "${MYDIR}/${CLIENT}" ]]; then
        echo "$CLIENT exists."
        return 1
    fi
    cd $MYDIR
    rm -rf $CLIREPO
    dpkg --purge python-tackerclient
    git clone -b 'SFC_refactor' https://github.com/trozet/python-tackerclient.git $CLIREPO
    cd $CLIREPO
    python setup.py --command-packages=stdeb.command bdist_deb
    cd "deb_dist"
    CLIENT=$(echo python-python-tackerclient_*_all.deb)
    cp $CLIENT $MYDIR
    dpkg -i "${MYDIR}/${CLIENT}"
}

# Function removes the cloned git repositories
function remove_repo () {
    if [[ -d "${MYDIR}/${1}" ]]; then
        rm -r "$1"
    fi
}

# Funcion copies and installs built artifact on all remaining cluster nodes
function populate_client() {
    wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
    dpkg -i deb &&\
    rm deb

    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}" )
    myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
    for anode in $clusternodes ; do
        if [ "$anode" != "$myaddr" ] ; then
            echo "Installing $CLIENT on $anode"
            scp ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" ${CLIENT} ${fadm}@${anode}:${CLIENT}
            ssh ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" ${fadm}@${anode} sudo dpkg -i ${CLIENT}
            ssh ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" ${fadm}@${anode} rm ${CLIENT}

        fi
    done
}

# Function orchestrate the Tacker service
function orchestarte () {
    rm -rf /etc/puppet/modules/tacker
    pushd /etc/puppet/modules
    git clone https://github.com/trozet/puppet-tacker.git tacker
    rm -rf /etc/puppet/modules/tacker/.git
    popd

    ### Facts ###

    # Port(s)   Protocol ServiceDetails Source
    # 8805-8872 tcp,udp  Unassigned     IANA
    bind_port='8808'

    auth_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'auth_uri')
    identity_uri=$(crudini --get '/etc/heat/heat.conf' 'keystone_authtoken' 'identity_uri')
    int_addr=$(ifconfig br-mesh | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
    mgmt_addr=$(ifconfig br-mgmt | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
    pub_addr=$(ifconfig br-ex-lnx | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
    rabbit_host=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_hosts'| cut -d ':' -f 1)
    rabbit_password=$(crudini --get '/etc/heat/heat.conf' 'oslo_messaging_rabbit' 'rabbit_password')
    sql_host=$(hiera database_vip)
    database_connection="mysql://tacker:tacker@${sql_host}/tacker"
    internal_url="http://${int_addr}:${bind_port}"
    admin_url="http://${mgmt_addr}:${bind_port}"
    public_url="http://${pub_addr}:${bind_port}"
    heat_api_vip=$(crudini --get '/etc/heat/heat.conf' 'heat_api' 'bind_host')
    allowed_hosts="[ '${sql_host}', '${HOSTNAME%%.domain.tld}', 'localhost', '127.0.0.1', '%' ]"
    heat_uri="http://${heat_api_vip}:8004/v1"
    odl_port='8282'
    service_tenant='services'
    myRegion='RegionOne'
    myPassword='tacker'

    cat > configure_tacker.pp << EOF
   class mysql::config {}
   include mysql::config
   class mysql::server {}
   include mysql::server

   class { 'tacker':
     package_ensure        => 'absent',
     client_package_ensure => 'absent',
     bind_port             => '${bind_port}',
     keystone_password     => '${myPassword}',
     keystone_tenant       => '${service_tenant}',
     auth_uri              => '${auth_uri}',
     identity_uri          => '${identity_uri}',
     database_connection   => '${database_connection}',
     rabbit_host           => '${rabbit_host}',
     rabbit_password       => '${rabbit_password}',
     heat_uri              => '${heat_uri}',
     opendaylight_host     => '${mgmt_addr}',
     opendaylight_port     => '${odl_port}',
   }

   class { 'tacker::db::mysql':
       password      => '${myPassword}',
       allowed_hosts => ${allowed_hosts},
   }

   class { 'tacker::keystone::auth':
     password            => '${myPassword}',
     tenant              => '${service_tenant}',
     admin_url           => '${admin_url}',
     internal_url        => '${internal_url}',
     public_url          => '${public_url}',
     region              => '${myRegion}',
   }
EOF

    apt-get install -y mysql-client-5.5
    cat > /usr/bin/tacker-manage <<EOFAKEMANAGE
#!/bin/bash
EOFAKEMANAGE
    chmod +x /usr/bin/tacker-manage
    puppet apply configure_tacker.pp
    rm -f tackerc
    cat > tackerc <<EOFRC
#!/bin/sh
export LC_ALL=C
export OS_NO_CACHE='true'
export OS_TENANT_NAME='${service_tenant}'
export OS_PROJECT_NAME='${service_tenant}'
export OS_USERNAME='tacker'
export OS_PASSWORD='${myPassword}'
export OS_AUTH_URL='${auth_uri}'
export OS_DEFAULT_DOMAIN='default'
export OS_AUTH_STRATEGY='keystone'
export OS_REGION_NAME='${myRegion}'
export TACKER_ENDPOINT_TYPE='internalURL'
EOFRC
    chmod +x tackerc
}

# Funcion copies and installs built environment settings on all remaining cluster nodes
function populate_rc() {
    wget -O deb http://archive.ubuntu.com/ubuntu/pool/universe/s/sshpass/sshpass_1.05-1_amd64.deb &&\
    dpkg -i deb &&\
    rm deb

    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}" )
    myaddr=$(ifconfig br-fw-admin | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p')
    for anode in $clusternodes ; do
        if [ "$anode" != "$myaddr" ] ; then
            echo "Populating settings to $anode"
            scp ${SSH_OPTIONS[@]} -i "${MYDIR}/.ssh/id_rsa" tackerc ${fadm}@${anode}:tackerc
        fi
    done
}

envSetup
deployTackerClient
deployJsonrpclib
buildTackerServer
blessPackage
deployTackerServer
populate_client
orchestarte
populate_rc

remove_repo "$MYREPO"
remove_repo "$DEPREPO"
remove_repo "$CLIREPO"

echo "Built: ${MYDIR}/${OUTPUT}"
echo "Built: ${MYDIR}/${CLIENT}"
echo "Built: ${MYDIR}/${JSONRPC}"
echo "tackerc - mandatory environmental parameters file created"