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

"""
Synnefo ci utils module
"""

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

17
from kamaki.cli import config as kamaki_config
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
18
19
20
from kamaki.clients.astakos import AstakosClient
from kamaki.clients.cyclades import CycladesClient
from kamaki.clients.image import ImageClient
21
from kamaki.clients.compute import ComputeClient
22
import filelocker
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
23

24
DEFAULT_CONFIG_FILE = "new_config"
25
26
27
28
# 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)
29
]
30

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
31
32
33
34
35
36
37

def _run(cmd, verbose):
    """Run fabric with verbose level"""
    if verbose:
        args = ('running',)
    else:
        args = ('running', 'stdout',)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
38
    with fabric.hide(*args):  # Used * or ** magic. pylint: disable-msg=W0142
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
39
40
41
        return fabric.run(cmd)


42
43
44
45
46
47
def _put(local, remote):
    """Run fabric put command without output"""
    with fabric.quiet():
        fabric.put(local, remote)


Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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"""
68
    def wrapper(self, *args, **kwargs):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
69
70
71
        """wrapper function"""
        if not self.fabric_installed:
            self.setup_fabric()
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
72
            self.fabric_installed = True
73
        return fun(self, *args, **kwargs)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
74
75
76
77
78
    return wrapper


def _check_kamaki(fun):
    """Check if kamaki has been initialized"""
79
    def wrapper(self, *args, **kwargs):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
80
81
82
        """wrapper function"""
        if not self.kamaki_installed:
            self.setup_kamaki()
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
83
            self.kamaki_installed = True
84
        return fun(self, *args, **kwargs)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    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


105
106
107
108
109
110
111
112
113
# 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
114
115
116
class SynnefoCI(object):
    """SynnefoCI python class"""

117
    def __init__(self, config_file=None, build_id=None, cloud=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
118
119
120
121
122
123
124
        """ Initialize SynnefoCI python class

        Setup logger, local_dir, config and kamaki
        """
        # Setup logger
        self.logger = logging.getLogger('synnefo-ci')
        self.logger.setLevel(logging.DEBUG)
125
126
127
128
129
130
131
132
133
134
135

        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
136
137
138
139
140
141

        # 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
142
143
144
145
        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)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
146
147
        self.config = ConfigParser()
        self.config.optionxform = str
148
        self.config.read(config_file)
149
150

        # Read temporary_config file
151
152
        self.temp_config_file = \
            os.path.expanduser(self.config.get('Global', 'temporary_config'))
153
154
        self.temp_config = ConfigParser()
        self.temp_config.optionxform = str
155
        self.temp_config.read(self.temp_config_file)
156
157
        self.build_id = build_id
        self.logger.info("Will use \"%s\" as build id" % _green(self.build_id))
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
158

159
160
161
162
163
164
165
166
167
168
        # 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
169
170
171
172
        # Initialize variables
        self.fabric_installed = False
        self.kamaki_installed = False
        self.cyclades_client = None
173
        self.compute_client = None
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
174
175
176
177
178
        self.image_client = None

    def setup_kamaki(self):
        """Initialize kamaki

179
        Setup cyclades_client, image_client and compute_client
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
180
        """
181
182
183
184
185
186
187
188

        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
189
        self.logger.debug("Authentication URL is %s" % _green(auth_url))
190
        token = config.get_cloud(self.kamaki_cloud, "token")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
191
192
        #self.logger.debug("Token is %s" % _green(token))

193
        self.astakos_client = AstakosClient(auth_url, token)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
194
195

        cyclades_url = \
196
            self.astakos_client.get_service_endpoints('compute')['publicURL']
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
197
198
199
200
201
        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 = \
202
            self.astakos_client.get_service_endpoints('image')['publicURL']
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
203
204
205
206
        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

207
        compute_url = \
208
            self.astakos_client.get_service_endpoints('compute')['publicURL']
209
210
211
212
        self.logger.debug("Compute API url is %s" % _green(compute_url))
        self.compute_client = ComputeClient(compute_url, token)
        self.compute_client.CONNECTION_RETRY_LIMIT = 2

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
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
    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"""
240
        server_id = int(self.read_temp_config('server_id'))
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
241
242
243
244
245
246
        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
247
    def create_server(self, image=None, flavor=None, ssh_keys=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
248
249
        """Create slave server"""
        self.logger.info("Create a new server..")
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

        # Find a build_id to use
        if self.build_id is None:
            # If build_id is given use this, else ..
            # Find a uniq build_id to use
            ids = self.temp_config.sections()
            if ids:
                max_id = int(max(self.temp_config.sections(), key=int))
                self.build_id = max_id + 1
            else:
                self.build_id = 1
        self.logger.debug("New build id \"%s\" was created"
                          % _green(self.build_id))

        # Find an image to use
265
        image_id = self._find_image(image)
266
        # Find a flavor to use
267
268
269
        flavor_id = self._find_flavor(flavor)

        # Create Server
270
        server_name = self.config.get("Deployment", "server_name")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
271
        server = self.cyclades_client.create_server(
272
            "%s(BID: %s)" % (server_name, self.build_id),
273
274
            flavor_id,
            image_id)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
275
        server_id = server['id']
276
        self.write_temp_config('server_id', server_id)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
277
278
        self.logger.debug("Server got id %s" % _green(server_id))
        server_user = server['metadata']['users']
279
        self.write_temp_config('server_user', server_user)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
280
281
        self.logger.debug("Server's admin user is %s" % _green(server_user))
        server_passwd = server['adminPass']
282
        self.write_temp_config('server_passwd', server_passwd)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
283
284
285

        server = self._wait_transition(server_id, "BUILD", "ACTIVE")
        self._get_server_ip_and_port(server)
286
        self._copy_ssh_keys(ssh_keys)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
287

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
288
        # Setup Firewall
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
289
290
        self.setup_fabric()
        self.logger.info("Setup firewall")
291
        accept_ssh_from = self.config.get('Global', 'accept_ssh_from')
292
293
294
295
296
297
298
299
300
301
302
        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
303

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
304
305
306
307
308
        # Setup apt, download packages
        self.logger.debug("Setup apt. Install x2goserver and firefox")
        cmd = """
        echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf
        apt-get update
309
        apt-get install curl --yes --force-yes
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
310
311
312
313
314
        echo -e "\n\n{0}" >> /etc/apt/sources.list
        # Synnefo repo's key
        curl https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -
        # X2GO Key
        apt-key adv --recv-keys --keyserver keys.gnupg.net E1F958385BFE2B6E
315
        apt-get install x2go-keyring --yes --force-yes
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
316
        apt-get update
317
318
        apt-get install x2goserver x2goserver-xsession \
                iceweasel --yes --force-yes
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
319
320
321
        """.format(self.config.get('Global', 'apt_repo'))
        _run(cmd, False)

322
323
324
325
326
327
328
329
    def _find_flavor(self, flavor=None):
        """Find a suitable flavor to use

        Search by name (reg expression) or by id
        """
        # Get a list of flavors from config file
        flavors = self.config.get('Deployment', 'flavors').split(",")
        if flavor is not None:
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
330
            # If we have a flavor_name to use, add it to our list
331
332
333
334
            flavors.insert(0, flavor)

        list_flavors = self.compute_client.list_flavors()
        for flv in flavors:
335
336
            flv_type, flv_value = parse_typed_option(option="flavor",
                                                     value=flv)
337
338
339
340
341
342
            if flv_type == "name":
                # Filter flavors by name
                self.logger.debug(
                    "Trying to find a flavor with name \"%s\"" % flv_value)
                list_flvs = \
                    [f for f in list_flavors
343
344
                     if re.search(flv_value, f['name'], flags=re.I)
                     is not None]
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
            elif flv_type == "id":
                # Filter flavors by id
                self.logger.debug(
                    "Trying to find a flavor with id \"%s\"" % flv_value)
                list_flvs = \
                    [f for f in list_flavors
                     if f['id'].lower() == flv_value.lower()]
            else:
                self.logger.error("Unrecognized flavor type %s" % flv_type)

            # Check if we found one
            if list_flvs:
                self.logger.debug("Will use \"%s\" with id \"%s\""
                                  % (list_flvs[0]['name'], list_flvs[0]['id']))
                return list_flvs[0]['id']
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
360
361
362

        self.logger.error("No matching flavor found.. aborting")
        sys.exit(1)
363

364
    def _find_image(self, image=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
365
366
        """Find a suitable image to use

367
368
369
        In case of search by name, the image has to belong to one
        of the `DEFAULT_SYSTEM_IMAGES_UUID' users.
        In case of search by id it only has to exist.
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
370
        """
371
372
373
374
375
376
        # Get a list of images from config file
        images = self.config.get('Deployment', 'images').split(",")
        if image is not None:
            # If we have an image from command line, add it to our list
            images.insert(0, image)

377
378
        auth = self.astakos_client.authenticate()
        user_uuid = auth["access"]["token"]["tenant"]["id"]
379
380
        list_images = self.image_client.list_public(detail=True)['images']
        for img in images:
381
            img_type, img_value = parse_typed_option(option="image", value=img)
382
383
384
385
            if img_type == "name":
                # Filter images by name
                self.logger.debug(
                    "Trying to find an image with name \"%s\"" % img_value)
386
                accepted_uuids = DEFAULT_SYSTEM_IMAGES_UUID + [user_uuid]
387
                list_imgs = \
388
389
390
                    [i for i in list_images if i['user_id'] in accepted_uuids
                     and
                     re.search(img_value, i['name'], flags=re.I) is not None]
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
            elif img_type == "id":
                # Filter images by id
                self.logger.debug(
                    "Trying to find an image with id \"%s\"" % img_value)
                list_imgs = \
                    [i for i in list_images
                     if i['id'].lower() == img_value.lower()]
            else:
                self.logger.error("Unrecognized image type %s" % img_type)
                sys.exit(1)

            # Check if we found one
            if list_imgs:
                self.logger.debug("Will use \"%s\" with id \"%s\""
                                  % (list_imgs[0]['name'], list_imgs[0]['id']))
                return list_imgs[0]['id']

        # We didn't found one
        self.logger.error("No matching image found.. aborting")
        sys.exit(1)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
411
412
413
414
415

    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']
416
417
        if (".okeanos.io" in self.cyclades_client.base_url or
           ".demo.synnefo.org" in self.cyclades_client.base_url):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
418
419
420
421
422
423
            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
424
        self.write_temp_config('server_ip', server_ip)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
425
        self.logger.debug("Server's IPv4 is %s" % _green(server_ip))
426
        self.write_temp_config('server_port', server_port)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
427
        self.logger.debug("Server's ssh port is %s" % _green(server_port))
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
428
        self.logger.debug("Access server using \"ssh -X -p %s %s@%s\"" %
429
430
                          (server_port, server['metadata']['users'],
                           server_ip))
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
431

Christos Stavrakakis's avatar
Christos Stavrakakis committed
432
    @_check_fabric
433
    def _copy_ssh_keys(self, ssh_keys):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
434
        """Upload/Install ssh keys to server"""
435
        self.logger.debug("Check for authentication keys to use")
436
437
438
        if ssh_keys is None:
            ssh_keys = self.config.get("Deployment", "ssh_keys")

439
        if ssh_keys != "":
440
            ssh_keys = os.path.expanduser(ssh_keys)
441
442
            self.logger.debug("Will use %s authentication keys file" %
                              ssh_keys)
Christos Stavrakakis's avatar
Christos Stavrakakis committed
443
444
            keyfile = '/tmp/%s.pub' % fabric.env.user
            _run('mkdir -p ~/.ssh && chmod 700 ~/.ssh', False)
445
446
447
448
449
            if ssh_keys.startswith("http://") or \
                    ssh_keys.startswith("https://") or \
                    ssh_keys.startswith("ftp://"):
                cmd = """
                apt-get update
450
                apt-get install wget --yes --force-yes
451
452
453
454
455
456
457
                wget {0} -O {1} --no-check-certificate
                """.format(ssh_keys, keyfile)
                _run(cmd, False)
            elif os.path.exists(ssh_keys):
                _put(ssh_keys, keyfile)
            else:
                self.logger.debug("No ssh keys found")
458
                return
Christos Stavrakakis's avatar
Christos Stavrakakis committed
459
460
461
462
463
464
            _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")

465
    def write_temp_config(self, option, value):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
466
        """Write changes back to config file"""
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
        # Acquire the lock to write to temp_config_file
        with filelocker.lock("%s.lock" % self.temp_config_file,
                             filelocker.LOCK_EX):

            # Read temp_config again to get any new entries
            self.temp_config.read(self.temp_config_file)

            # If build_id section doesn't exist create a new one
            try:
                self.temp_config.add_section(str(self.build_id))
                creation_time = \
                    time.strftime("%a, %d %b %Y %X", time.localtime())
                self.temp_config.set(str(self.build_id),
                                     "created", str(creation_time))
            except DuplicateSectionError:
                pass
            self.temp_config.set(str(self.build_id), option, str(value))
            curr_time = time.strftime("%a, %d %b %Y %X", time.localtime())
            self.temp_config.set(str(self.build_id), "modified", curr_time)
            with open(self.temp_config_file, 'wb') as tcf:
                self.temp_config.write(tcf)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
488

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    def read_temp_config(self, option):
        """Read from temporary_config file"""
        # If build_id is None use the latest one
        if self.build_id is None:
            ids = self.temp_config.sections()
            if ids:
                self.build_id = int(ids[-1])
            else:
                self.logger.error("No sections in temporary config file")
                sys.exit(1)
            self.logger.debug("Will use \"%s\" as build id"
                              % _green(self.build_id))
        # Read specified option
        return self.temp_config.get(str(self.build_id), option)

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
504
505
506
    def setup_fabric(self):
        """Setup fabric environment"""
        self.logger.info("Setup fabric parameters..")
507
508
509
510
        fabric.env.user = self.read_temp_config('server_user')
        fabric.env.host_string = self.read_temp_config('server_ip')
        fabric.env.port = int(self.read_temp_config('server_port'))
        fabric.env.password = self.read_temp_config('server_passwd')
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
        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
530
    def clone_repo(self, local_repo=False):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
531
532
        """Clone Synnefo repo from slave server"""
        self.logger.info("Configure repositories on remote server..")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
533
        self.logger.debug("Install/Setup git")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
534
        cmd = """
535
        apt-get install git --yes --force-yes
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
536
537
538
        git config --global user.name {0}
        git config --global user.email {1}
        """.format(self.config.get('Global', 'git_config_name'),
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
539
540
541
                   self.config.get('Global', 'git_config_mail'))
        _run(cmd, False)

542
        # Find synnefo_repo and synnefo_branch to use
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
543
        synnefo_repo = self.config.get('Global', 'synnefo_repo')
544
545
        synnefo_branch = self.config.get("Global", "synnefo_branch")
        if synnefo_branch == "":
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
546
547
548
            synnefo_branch = \
                subprocess.Popen(
                    ["git", "rev-parse", "--abbrev-ref", "HEAD"],
549
550
551
                    stdout=subprocess.PIPE).communicate()[0].strip()
            if synnefo_branch == "HEAD":
                synnefo_branch = \
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
552
553
                    subprocess.Popen(
                        ["git", "rev-parse", "--short", "HEAD"],
554
555
                        stdout=subprocess.PIPE).communicate()[0].strip()
        self.logger.info("Will use branch %s" % synnefo_branch)
556
557
558
559
560
561
562
563
564
565
566

        if local_repo or synnefo_branch == "":
            # Use local_repo
            self.logger.debug("Push local repo to server")
            # Firstly create the remote repo
            _run("git init synnefo", False)
            # Then push our local repo over ssh
            # We have to pass some arguments to ssh command
            # namely to disable host checking.
            (temp_ssh_file_handle, temp_ssh_file) = tempfile.mkstemp()
            os.close(temp_ssh_file_handle)
567
            # XXX: git push doesn't read the password
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
            cmd = """
            echo 'exec ssh -o "StrictHostKeyChecking no" \
                           -o "UserKnownHostsFile /dev/null" \
                           -q "$@"' > {4}
            chmod u+x {4}
            export GIT_SSH="{4}"
            echo "{0}" | git push --mirror ssh://{1}@{2}:{3}/~/synnefo
            rm -f {4}
            """.format(fabric.env.password,
                       fabric.env.user,
                       fabric.env.host_string,
                       fabric.env.port,
                       temp_ssh_file)
            os.system(cmd)
        else:
            # Clone Synnefo from remote repo
            # Currently clonning synnefo can fail unexpectedly
            cloned = False
            for i in range(10):
                self.logger.debug("Clone synnefo from %s" % synnefo_repo)
                try:
                    _run("git clone %s synnefo" % synnefo_repo, False)
                    cloned = True
                    break
                except BaseException:
                    self.logger.warning(
                        "Clonning synnefo failed.. retrying %s" % i)
            if not cloned:
                self.logger.error("Can not clone Synnefo repo.")
                sys.exit(-1)

        # Checkout the desired synnefo_branch
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
600
        self.logger.debug("Checkout \"%s\" branch/commit" % synnefo_branch)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
601
        cmd = """
602
        cd synnefo
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
603
604
        for branch in `git branch -a | grep remotes | \
                       grep -v HEAD | grep -v master`; do
605
606
607
608
609
610
            git branch --track ${branch##*/} $branch
        done
        git checkout %s
        """ % (synnefo_branch)
        _run(cmd, False)

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
611
612
613
614
615
616
617
618
    @_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 \
619
                python-dev python-all python-pip --yes --force-yes
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
620
621
622
623
        pip install devflow
        """
        _run(cmd, False)

Christos Stavrakakis's avatar
Christos Stavrakakis committed
624
        if self.config.get('Global', 'patch_pydist') == "True":
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
625
626
627
628
629
630
631
            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)

632
633
        # Build synnefo packages
        self.logger.debug("Build synnefo packages")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
634
        cmd = """
635
        devflow-autopkg snapshot -b ~/synnefo_build-area --no-sign
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
636
        """
637
        with fabric.cd("synnefo"):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
638
639
            _run(cmd, True)

640
        # Install snf-deploy package
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
641
642
643
        self.logger.debug("Install snf-deploy package")
        cmd = """
        dpkg -i snf-deploy*.deb
644
        apt-get -f install --yes --force-yes
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
645
        """
646
        with fabric.cd("synnefo_build-area"):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
647
648
649
            with fabric.settings(warn_only=True):
                _run(cmd, True)

650
        # Setup synnefo packages for snf-deploy
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
651
652
653
654
655
656
        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)

657
658
    @_check_fabric
    def build_documentation(self):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
659
        """Build Synnefo documentation"""
660
661
662
        self.logger.info("Build Synnefo documentation..")
        _run("pip install -U Sphinx", False)
        with fabric.cd("synnefo"):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
663
664
            _run("devflow-update-version; "
                 "./ci/make_docs.sh synnefo_documentation", False)
665
666

    def fetch_documentation(self, dest=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
667
668
        """Fetch Synnefo documentation"""
        self.logger.info("Fetch Synnefo documentation..")
669
670
671
672
673
674
675
676
677
        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
678
    @_check_fabric
679
    def deploy_synnefo(self, schema=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
680
681
        """Deploy Synnefo using snf-deploy"""
        self.logger.info("Deploy Synnefo..")
682
683
        if schema is None:
            schema = self.config.get('Global', 'schema')
684
        self.logger.debug("Will use \"%s\" schema" % schema)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
685

686
687
688
689
        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
690
        self.logger.debug("Upload schema files to server")
691
        _put(os.path.join(schema_dir, "*"), "/etc/snf-deploy/")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
692
693
694
695
696
697
698
699
700

        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 = """
701
        snf-deploy --disable-colors --autoconf all
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
        """
        _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)

718
719
        self.logger.debug("Upload tests.sh file")
        unit_tests_file = os.path.join(self.ci_dir, "tests.sh")
720
        _put(unit_tests_file, ".")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
721
722
723

        self.logger.debug("Run unit tests")
        cmd = """
724
        bash tests.sh {0}
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
        """.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}
741
        BurninExitStatus=$?
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
742
743
744
745
746
747
        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
748
        exit $BurninExitStatus
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
749
750
751
752
        """.format(self.config.get('Burnin', 'cmd_options'))
        _run(cmd, True)

    @_check_fabric
753
    def fetch_compressed(self, src, dest=None):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
754
        """Create a tarball and fetch it locally"""
755
756
757
758
        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
759
        _run(cmd, False)
760
761
        if not os.path.exists(dest):
            os.makedirs(dest)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
762

763
764
        tmp_dir = tempfile.mkdtemp()
        fabric.get(tar_file, tmp_dir)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
765

766
767
768
        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)
769
770
        cmd = """
        cd %s
771
772
773
774
        tar xzf %s
        cp -r %s/* %s
        rm -r %s
        """ % (tmp_dir, tar_file, src, dest, tmp_dir)
775
        os.system(cmd)
776
777
778
779
780
        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
781
        """Fetch Synnefo packages"""
782
783
        if dest is None:
            dest = self.config.get('Global', 'pkgs_dir')
784
        dest = os.path.abspath(os.path.expanduser(dest))
785
786
787
        if not os.path.exists(dest):
            os.makedirs(dest)
        self.fetch_compressed("synnefo_build-area", dest)
788
        self.logger.info("Downloaded debian packages to %s" %
789
                         _green(dest))
790
791
792
793
794
795
796
797
798
799
800


def parse_typed_option(option, value):
    try:
        [type_, val] = value.strip().split(':')
        if type_ not in ["id", "name"]:
            raise ValueError
        return type_, val
    except ValueError:
        msg = "Invalid %s format. Must be [id|name]:.+" % option
        raise ValueError(msg)