utils.py 22.1 KB
Newer Older
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python

"""
Synnefo ci utils module
"""

import os
import sys
import time
import logging
import fabric.api as fabric
12
import subprocess
13
import tempfile
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
14
15
from ConfigParser import ConfigParser, DuplicateSectionError

16
from kamaki.cli import config as kamaki_config
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
17
18
19
20
from kamaki.clients.astakos import AstakosClient
from kamaki.clients.cyclades import CycladesClient
from kamaki.clients.image import ImageClient

21
DEFAULT_CONFIG_FILE = "new_config"
22
23
24
25
26
# UUID of owner of system images
DEFAULT_SYSTEM_IMAGES_UUID = [
    "25ecced9-bf53-4145-91ee-cf47377e9fb2",  # production (okeanos.grnet.gr)
    "04cbe33f-29b7-4ef1-94fb-015929e5fc06",  # testing (okeanos.io)
    ]
27

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
28
29
30
31
32
33
34

def _run(cmd, verbose):
    """Run fabric with verbose level"""
    if verbose:
        args = ('running',)
    else:
        args = ('running', 'stdout',)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
35
    with fabric.hide(*args):  # Used * or ** magic. pylint: disable-msg=W0142
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        return fabric.run(cmd)


def _red(msg):
    """Red color"""
    #return "\x1b[31m" + str(msg) + "\x1b[0m"
    return str(msg)


def _yellow(msg):
    """Yellow color"""
    #return "\x1b[33m" + str(msg) + "\x1b[0m"
    return str(msg)


def _green(msg):
    """Green color"""
    #return "\x1b[32m" + str(msg) + "\x1b[0m"
    return str(msg)


def _check_fabric(fun):
    """Check if fabric env has been set"""
59
    def wrapper(self, *args, **kwargs):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
60
61
62
        """wrapper function"""
        if not self.fabric_installed:
            self.setup_fabric()
63
        return fun(self, *args, **kwargs)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
64
65
66
67
68
    return wrapper


def _check_kamaki(fun):
    """Check if kamaki has been initialized"""
69
    def wrapper(self, *args, **kwargs):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
70
71
72
        """wrapper function"""
        if not self.kamaki_installed:
            self.setup_kamaki()
73
        return fun(self, *args, **kwargs)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    return wrapper


class _MyFormatter(logging.Formatter):
    """Logging Formatter"""
    def format(self, record):
        format_orig = self._fmt
        if record.levelno == logging.DEBUG:
            self._fmt = "  %(msg)s"
        elif record.levelno == logging.INFO:
            self._fmt = "%(msg)s"
        elif record.levelno == logging.WARNING:
            self._fmt = _yellow("[W] %(msg)s")
        elif record.levelno == logging.ERROR:
            self._fmt = _red("[E] %(msg)s")
        result = logging.Formatter.format(self, record)
        self._fmt = format_orig
        return result


94
95
96
97
98
99
100
101
102
# Too few public methods. pylint: disable-msg=R0903
class _InfoFilter(logging.Filter):
    """Logging Filter that allows DEBUG and INFO messages only"""
    def filter(self, rec):
        """The filter"""
        return rec.levelno in (logging.DEBUG, logging.INFO)


# Too many instance attributes. pylint: disable-msg=R0902
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
103
104
105
class SynnefoCI(object):
    """SynnefoCI python class"""

106
    def __init__(self, config_file=None, cleanup_config=False, cloud=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
107
108
109
110
111
112
113
        """ Initialize SynnefoCI python class

        Setup logger, local_dir, config and kamaki
        """
        # Setup logger
        self.logger = logging.getLogger('synnefo-ci')
        self.logger.setLevel(logging.DEBUG)
114
115
116
117
118
119
120
121
122
123
124

        handler1 = logging.StreamHandler(sys.stdout)
        handler1.setLevel(logging.DEBUG)
        handler1.addFilter(_InfoFilter())
        handler1.setFormatter(_MyFormatter())
        handler2 = logging.StreamHandler(sys.stderr)
        handler2.setLevel(logging.WARNING)
        handler2.setFormatter(_MyFormatter())

        self.logger.addHandler(handler1)
        self.logger.addHandler(handler2)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
125
126
127
128
129
130

        # Get our local dir
        self.ci_dir = os.path.dirname(os.path.abspath(__file__))
        self.repo_dir = os.path.dirname(self.ci_dir)

        # Read config file
131
132
133
134
        if config_file is None:
            config_file = DEFAULT_CONFIG_FILE
        if not os.path.isabs(config_file):
            config_file = os.path.join(self.ci_dir, config_file)
Christos Stavrakakis's avatar
Christos Stavrakakis committed
135

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
136
137
        self.config = ConfigParser()
        self.config.optionxform = str
138
        self.config.read(config_file)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
139
140
141
142
        temp_config = self.config.get('Global', 'temporary_config')
        if cleanup_config:
            try:
                os.remove(temp_config)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
143
            except OSError:
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
144
145
146
147
                pass
        else:
            self.config.read(self.config.get('Global', 'temporary_config'))

148
149
150
151
152
153
154
155
156
157
        # Set kamaki cloud
        if cloud is not None:
            self.kamaki_cloud = cloud
        elif self.config.has_option("Deployment", "kamaki_cloud"):
            kamaki_cloud = self.config.get("Deployment", "kamaki_cloud")
            if kamaki_cloud == "":
                self.kamaki_cloud = None
        else:
            self.kamaki_cloud = None

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
158
159
160
161
162
163
164
165
166
167
168
        # Initialize variables
        self.fabric_installed = False
        self.kamaki_installed = False
        self.cyclades_client = None
        self.image_client = None

    def setup_kamaki(self):
        """Initialize kamaki

        Setup cyclades_client and image_client
        """
169
170
171
172
173
174
175
176

        config = kamaki_config.Config()
        if self.kamaki_cloud is None:
            self.kamaki_cloud = config.get_global("default_cloud")

        self.logger.info("Setup kamaki client, using cloud '%s'.." %
                         self.kamaki_cloud)
        auth_url = config.get_cloud(self.kamaki_cloud, "url")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
177
        self.logger.debug("Authentication URL is %s" % _green(auth_url))
178
        token = config.get_cloud(self.kamaki_cloud, "token")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
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
        #self.logger.debug("Token is %s" % _green(token))

        astakos_client = AstakosClient(auth_url, token)

        cyclades_url = \
            astakos_client.get_service_endpoints('compute')['publicURL']
        self.logger.debug("Cyclades API url is %s" % _green(cyclades_url))
        self.cyclades_client = CycladesClient(cyclades_url, token)
        self.cyclades_client.CONNECTION_RETRY_LIMIT = 2

        image_url = \
            astakos_client.get_service_endpoints('image')['publicURL']
        self.logger.debug("Images API url is %s" % _green(image_url))
        self.image_client = ImageClient(cyclades_url, token)
        self.image_client.CONNECTION_RETRY_LIMIT = 2

    def _wait_transition(self, server_id, current_status, new_status):
        """Wait for server to go from current_status to new_status"""
        self.logger.debug("Waiting for server to become %s" % new_status)
        timeout = self.config.getint('Global', 'build_timeout')
        sleep_time = 5
        while True:
            server = self.cyclades_client.get_server_details(server_id)
            if server['status'] == new_status:
                return server
            elif timeout < 0:
                self.logger.error(
                    "Waiting for server to become %s timed out" % new_status)
                self.destroy_server(False)
                sys.exit(-1)
            elif server['status'] == current_status:
                # Sleep for #n secs and continue
                timeout = timeout - sleep_time
                time.sleep(sleep_time)
            else:
                self.logger.error(
                    "Server failed with status %s" % server['status'])
                self.destroy_server(False)
                sys.exit(-1)

    @_check_kamaki
    def destroy_server(self, wait=True):
        """Destroy slave server"""
        server_id = self.config.getint('Temporary Options', 'server_id')
        self.logger.info("Destoying server with id %s " % server_id)
        self.cyclades_client.delete_server(server_id)
        if wait:
            self._wait_transition(server_id, "ACTIVE", "DELETED")

    @_check_kamaki
229
    def create_server(self, image_id=None, flavor_id=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
230
231
        """Create slave server"""
        self.logger.info("Create a new server..")
232
233
234
235
236
237
238
        if image_id is None:
            image = self._find_image()
            self.logger.debug("Will use image \"%s\"" % _green(image['name']))
            image_id = image["id"]
        self.logger.debug("Image has id %s" % _green(image_id))
        if flavor_id is None:
            flavor_id = self.config.getint("Deployment", "flavor_id")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
239
240
        server = self.cyclades_client.create_server(
            self.config.get('Deployment', 'server_name'),
241
242
            flavor_id,
            image_id)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
243
244
245
246
247
248
249
250
251
252
253
        server_id = server['id']
        self.write_config('server_id', server_id)
        self.logger.debug("Server got id %s" % _green(server_id))
        server_user = server['metadata']['users']
        self.write_config('server_user', server_user)
        self.logger.debug("Server's admin user is %s" % _green(server_user))
        server_passwd = server['adminPass']
        self.write_config('server_passwd', server_passwd)

        server = self._wait_transition(server_id, "BUILD", "ACTIVE")
        self._get_server_ip_and_port(server)
Christos Stavrakakis's avatar
Christos Stavrakakis committed
254
        self._copy_ssh_keys()
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
255
256
257
258

        self.setup_fabric()
        self.logger.info("Setup firewall")
        accept_ssh_from = self.config.get('Global', 'filter_access_network')
259
260
261
262
263
264
265
266
267
268
269
        if accept_ssh_from != "":
            self.logger.debug("Block ssh except from %s" % accept_ssh_from)
            cmd = """
            local_ip=$(/sbin/ifconfig eth0 | grep 'inet addr:' | \
                cut -d':' -f2 | cut -d' ' -f1)
            iptables -A INPUT -s localhost -j ACCEPT
            iptables -A INPUT -s $local_ip -j ACCEPT
            iptables -A INPUT -s {0} -p tcp --dport 22 -j ACCEPT
            iptables -A INPUT -p tcp --dport 22 -j DROP
            """.format(accept_ssh_from)
            _run(cmd, False)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
270
271
272
273

    def _find_image(self):
        """Find a suitable image to use

274
275
        It has to belong to one of the `DEFAULT_SYSTEM_IMAGES_UUID'
        users and contain the word given by `image_name' option.
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
276
277
278
279
        """
        image_name = self.config.get('Deployment', 'image_name').lower()
        images = self.image_client.list_public(detail=True)['images']
        # Select images by `system_uuid' user
280
281
        images = [x for x in images
                  if x['user_id'] in DEFAULT_SYSTEM_IMAGES_UUID]
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
282
        # Select images with `image_name' in their names
283
284
        images = [x for x in images
                  if x['name'].lower().find(image_name) != -1]
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
285
286
287
288
289
290
291
        # Let's select the first one
        return images[0]

    def _get_server_ip_and_port(self, server):
        """Compute server's IPv4 and ssh port number"""
        self.logger.info("Get server connection details..")
        server_ip = server['attachments'][0]['ipv4']
292
        if ".okeanos.io" in self.cyclades_client.base_url:
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
293
294
295
296
297
298
299
300
301
302
303
            tmp1 = int(server_ip.split(".")[2])
            tmp2 = int(server_ip.split(".")[3])
            server_ip = "gate.okeanos.io"
            server_port = 10000 + tmp1 * 256 + tmp2
        else:
            server_port = 22
        self.write_config('server_ip', server_ip)
        self.logger.debug("Server's IPv4 is %s" % _green(server_ip))
        self.write_config('server_port', server_port)
        self.logger.debug("Server's ssh port is %s" % _green(server_port))

Christos Stavrakakis's avatar
Christos Stavrakakis committed
304
305
    @_check_fabric
    def _copy_ssh_keys(self):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
306
        """Upload/Install ssh keys to server"""
307
308
        if not self.config.has_option("Deployment", "ssh_keys"):
            return
Christos Stavrakakis's avatar
Christos Stavrakakis committed
309
310
        authorized_keys = self.config.get("Deployment",
                                          "ssh_keys")
311
        if authorized_keys != "" and os.path.exists(authorized_keys):
Christos Stavrakakis's avatar
Christos Stavrakakis committed
312
313
314
315
316
317
318
319
320
            keyfile = '/tmp/%s.pub' % fabric.env.user
            _run('mkdir -p ~/.ssh && chmod 700 ~/.ssh', False)
            fabric.put(authorized_keys, keyfile)
            _run('cat %s >> ~/.ssh/authorized_keys' % keyfile, False)
            _run('rm %s' % keyfile, False)
            self.logger.debug("Uploaded ssh authorized keys")
        else:
            self.logger.debug("No ssh keys found")

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
    def write_config(self, option, value, section="Temporary Options"):
        """Write changes back to config file"""
        try:
            self.config.add_section(section)
        except DuplicateSectionError:
            pass
        self.config.set(section, option, str(value))
        temp_conf_file = self.config.get('Global', 'temporary_config')
        with open(temp_conf_file, 'wb') as tcf:
            self.config.write(tcf)

    def setup_fabric(self):
        """Setup fabric environment"""
        self.logger.info("Setup fabric parameters..")
        fabric.env.user = self.config.get('Temporary Options', 'server_user')
        fabric.env.host_string = \
            self.config.get('Temporary Options', 'server_ip')
Christos Stavrakakis's avatar
Christos Stavrakakis committed
338
339
340
341
        fabric.env.port = self.config.getint('Temporary Options',
                                             'server_port')
        fabric.env.password = self.config.get('Temporary Options',
                                              'server_passwd')
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
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
        fabric.env.connection_attempts = 10
        fabric.env.shell = "/bin/bash -c"
        fabric.env.disable_known_hosts = True
        fabric.env.output_prefix = None

    def _check_hash_sum(self, localfile, remotefile):
        """Check hash sums of two files"""
        self.logger.debug("Check hash sum for local file %s" % localfile)
        hash1 = os.popen("sha256sum %s" % localfile).read().split(' ')[0]
        self.logger.debug("Local file has sha256 hash %s" % hash1)
        self.logger.debug("Check hash sum for remote file %s" % remotefile)
        hash2 = _run("sha256sum %s" % remotefile, False)
        hash2 = hash2.split(' ')[0]
        self.logger.debug("Remote file has sha256 hash %s" % hash2)
        if hash1 != hash2:
            self.logger.error("Hashes differ.. aborting")
            sys.exit(-1)

    @_check_fabric
    def clone_repo(self):
        """Clone Synnefo repo from slave server"""
        self.logger.info("Configure repositories on remote server..")
        self.logger.debug("Setup apt, install curl and git")
        cmd = """
        echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf
        apt-get update
        apt-get install curl git --yes
        echo -e "\n\ndeb {0}" >> /etc/apt/sources.list
        curl https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -
        apt-get update
        git config --global user.name {1}
373
        git config --global user.email {2}
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
374
375
376
377
378
379
        """.format(self.config.get('Global', 'apt_repo'),
                   self.config.get('Global', 'git_config_name'),
                   self.config.get('Global', 'git_config_mail'))
        _run(cmd, False)

        synnefo_repo = self.config.get('Global', 'synnefo_repo')
380
381
        synnefo_branch = self.config.get("Global", "synnefo_branch")
        if synnefo_branch == "":
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
382
383
384
            synnefo_branch = \
                subprocess.Popen(
                    ["git", "rev-parse", "--abbrev-ref", "HEAD"],
385
386
387
                    stdout=subprocess.PIPE).communicate()[0].strip()
            if synnefo_branch == "HEAD":
                synnefo_branch = \
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
388
389
                    subprocess.Popen(
                        ["git", "rev-parse", "--short", "HEAD"],
390
391
                        stdout=subprocess.PIPE).communicate()[0].strip()
        self.logger.info("Will use branch %s" % synnefo_branch)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
392
        # Currently clonning synnefo can fail unexpectedly
Christos Stavrakakis's avatar
Christos Stavrakakis committed
393
        cloned = False
394
        for i in range(10):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
395
396
            self.logger.debug("Clone synnefo from %s" % synnefo_repo)
            try:
397
                _run("git clone %s synnefo" % synnefo_repo, False)
Christos Stavrakakis's avatar
Christos Stavrakakis committed
398
                cloned = True
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
399
                break
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
400
            except BaseException:
Christos Stavrakakis's avatar
Christos Stavrakakis committed
401
402
                self.logger.warning("Clonning synnefo failed.. retrying %s"
                                    % i)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
403
        cmd = """
404
        cd synnefo
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
405
406
        for branch in `git branch -a | grep remotes | \
                       grep -v HEAD | grep -v master`; do
407
408
409
410
411
412
            git branch --track ${branch##*/} $branch
        done
        git checkout %s
        """ % (synnefo_branch)
        _run(cmd, False)

Christos Stavrakakis's avatar
Christos Stavrakakis committed
413
414
415
        if not cloned:
            self.logger.error("Can not clone Synnefo repo.")
            sys.exit(-1)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
416
417
418

        deploy_repo = self.config.get('Global', 'deploy_repo')
        self.logger.debug("Clone snf-deploy from %s" % deploy_repo)
Christos Stavrakakis's avatar
Christos Stavrakakis committed
419
        _run("git clone --depth 1 %s" % deploy_repo, False)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433

    @_check_fabric
    def build_synnefo(self):
        """Build Synnefo packages"""
        self.logger.info("Build Synnefo packages..")
        self.logger.debug("Install development packages")
        cmd = """
        apt-get update
        apt-get install zlib1g-dev dpkg-dev debhelper git-buildpackage \
                python-dev python-all python-pip --yes
        pip install devflow
        """
        _run(cmd, False)

Christos Stavrakakis's avatar
Christos Stavrakakis committed
434
        if self.config.get('Global', 'patch_pydist') == "True":
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
            self.logger.debug("Patch pydist.py module")
            cmd = r"""
            sed -r -i 's/(\(\?P<name>\[A-Za-z\]\[A-Za-z0-9_\.)/\1\\\-/' \
                /usr/share/python/debpython/pydist.py
            """
            _run(cmd, False)

        self.logger.debug("Build snf-deploy package")
        cmd = """
        git checkout -t origin/debian
        git-buildpackage --git-upstream-branch=master \
                --git-debian-branch=debian \
                --git-export-dir=../snf-deploy_build-area \
                -uc -us
        """
        with fabric.cd("snf-deploy"):
            _run(cmd, True)

        self.logger.debug("Install snf-deploy package")
        cmd = """
        dpkg -i snf-deploy*.deb
        apt-get -f install --yes
        """
        with fabric.cd("snf-deploy_build-area"):
            with fabric.settings(warn_only=True):
                _run(cmd, True)

        self.logger.debug("Build synnefo packages")
        cmd = """
        devflow-autopkg snapshot -b ~/synnefo_build-area --no-sign
        """
        with fabric.cd("synnefo"):
            _run(cmd, True)

        self.logger.debug("Copy synnefo debs to snf-deploy packages dir")
        cmd = """
        cp ~/synnefo_build-area/*.deb /var/lib/snf-deploy/packages/
        """
        _run(cmd, False)

475
476
    @_check_fabric
    def build_documentation(self):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
477
        """Build Synnefo documentation"""
478
479
480
        self.logger.info("Build Synnefo documentation..")
        _run("pip install -U Sphinx", False)
        with fabric.cd("synnefo"):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
481
482
            _run("devflow-update-version; "
                 "./ci/make_docs.sh synnefo_documentation", False)
483
484

    def fetch_documentation(self, dest=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
485
486
        """Fetch Synnefo documentation"""
        self.logger.info("Fetch Synnefo documentation..")
487
488
489
490
491
492
493
494
495
        if dest is None:
            dest = "synnefo_documentation"
        dest = os.path.abspath(dest)
        if not os.path.exists(dest):
            os.makedirs(dest)
        self.fetch_compressed("synnefo/synnefo_documentation", dest)
        self.logger.info("Downloaded documentation to %s" %
                         _green(dest))

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
496
    @_check_fabric
497
    def deploy_synnefo(self, schema=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
498
499
        """Deploy Synnefo using snf-deploy"""
        self.logger.info("Deploy Synnefo..")
500
501
        if schema is None:
            schema = self.config.get('Global', 'schema')
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
502
503
        self.logger.debug("Will use %s schema" % schema)

504
505
506
507
        schema_dir = os.path.join(self.ci_dir, "schemas/%s" % schema)
        if not (os.path.exists(schema_dir) and os.path.isdir(schema_dir)):
            raise ValueError("Unknown schema: %s" % schema)

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
508
509
        self.logger.debug("Upload schema files to server")
        with fabric.quiet():
510
            fabric.put(os.path.join(schema_dir, "*"), "/etc/snf-deploy/")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536

        self.logger.debug("Change password in nodes.conf file")
        cmd = """
        sed -i 's/^password =.*/password = {0}/' /etc/snf-deploy/nodes.conf
        """.format(fabric.env.password)
        _run(cmd, False)

        self.logger.debug("Run snf-deploy")
        cmd = """
        snf-deploy all --autoconf
        """
        _run(cmd, True)

    @_check_fabric
    def unit_test(self):
        """Run Synnefo unit test suite"""
        self.logger.info("Run Synnefo unit test suite")
        component = self.config.get('Unit Tests', 'component')

        self.logger.debug("Install needed packages")
        cmd = """
        pip install mock
        pip install factory_boy
        """
        _run(cmd, False)

537
538
        self.logger.debug("Upload tests.sh file")
        unit_tests_file = os.path.join(self.ci_dir, "tests.sh")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
539
540
541
542
543
        with fabric.quiet():
            fabric.put(unit_tests_file, ".")

        self.logger.debug("Run unit tests")
        cmd = """
544
        bash tests.sh {0}
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        """.format(component)
        _run(cmd, True)

    @_check_fabric
    def run_burnin(self):
        """Run burnin functional test suite"""
        self.logger.info("Run Burnin functional test suite")
        cmd = """
        auth_url=$(grep -e '^url =' .kamakirc | cut -d' ' -f3)
        token=$(grep -e '^token =' .kamakirc | cut -d' ' -f3)
        images_user=$(kamaki image list -l | grep owner | \
                      cut -d':' -f2 | tr -d ' ')
        snf-burnin --auth-url=$auth_url --token=$token \
            --force-flavor=2 --image-id=all \
            --system-images-user=$images_user \
            {0}
        log_folder=$(ls -1d /var/log/burnin/* | tail -n1)
        for i in $(ls $log_folder/*/details*); do
            echo -e "\\n\\n"
            echo -e "***** $i\\n"
            cat $i
        done
        """.format(self.config.get('Burnin', 'cmd_options'))
        _run(cmd, True)

    @_check_fabric
571
    def fetch_compressed(self, src, dest=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
572
        """Create a tarball and fetch it locally"""
573
574
575
576
        self.logger.debug("Creating tarball of %s" % src)
        basename = os.path.basename(src)
        tar_file = basename + ".tgz"
        cmd = "tar czf %s %s" % (tar_file, src)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
577
        _run(cmd, False)
578
579
        if not os.path.exists(dest):
            os.makedirs(dest)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
580

581
582
        tmp_dir = tempfile.mkdtemp()
        fabric.get(tar_file, tmp_dir)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
583

584
585
586
        dest_file = os.path.join(tmp_dir, tar_file)
        self._check_hash_sum(dest_file, tar_file)
        self.logger.debug("Untar packages file %s" % dest_file)
587
588
        cmd = """
        cd %s
589
590
591
592
        tar xzf %s
        cp -r %s/* %s
        rm -r %s
        """ % (tmp_dir, tar_file, src, dest, tmp_dir)
593
        os.system(cmd)
594
595
596
597
598
        self.logger.info("Downloaded %s to %s" %
                         (src, _green(dest)))

    @_check_fabric
    def fetch_packages(self, dest=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
599
        """Fetch Synnefo packages"""
600
601
602
603
604
605
        if dest is None:
            dest = self.config.get('Global', 'pkgs_dir')
        dest = os.path.abspath(dest)
        if not os.path.exists(dest):
            os.makedirs(dest)
        self.fetch_compressed("synnefo_build-area", dest)
606
        self.logger.info("Downloaded debian packages to %s" %
607
                         _green(dest))