summaryrefslogtreecommitdiff
path: root/mason/tests/artifact_upload.py
blob: 21d109318f86b1a2c788f655d6d1801e0d13d19e (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
# Copyright 2014 Codethink Ltd

import cliapp
import json
import logging
import os
import urlparse

from turbo_hipster.lib import common
from turbo_hipster.lib import models


#TODO: Less different instances of this would be nice
class MorphologyHelper(object):

    def __init__(self, path):
        self.defs_repo = morphlib.gitdir.GitDirectory(path)
        self.loader = morphlib.morphloader.MorphologyLoader()
        self.finder = morphlib.morphologyfinder.MorphologyFinder(self.defs_repo)

    def load_morphology(self, path):
        text = self.finder.read_morphology(path)
        return self.loader.load_from_string(text)

    @classmethod
    def iterate_systems(cls, systems_list):
        for system in systems_list:
            yield morphlib.util.sanitise_morphology_path(system['morph'])
            if 'subsystems' in system:
                for subsystem in cls.iterate_systems(system['subsystems']):
                    yield subsystem

    def iterate_cluster_deployments(cls, cluster_morph):
        for system in cluster_morph['systems']:
            path = morphlib.util.sanitise_morphology_path(system['morph'])
            defaults = system.get('deploy-defaults', {})
            for name, options in system['deploy'].iteritems():
                config = dict(defaults)
                config.update(options)
                yield path, name, config

    def load_cluster_systems(self, cluster_morph):
        for system_path in set(self.iterate_systems(cluster_morph['systems'])):
            system_morph = self.load_morphology(system_path)
            yield system_path, system_morph


#TODO: Deployment


class Runner(models.Task):

    """This thread handles running the build-deploy-build test,
    which is used to ensure that Baserock can build Baserock."""

    log = logging.getLogger("task_plugins.build_deploy_test.task.Runner")

    def __init__(self, worker_server, plugin_config, job_name):
        super(Runner, self).__init__(worker_server, plugin_config, job_name)

        self.total_steps = 5
        print self.job_arguments

    def do_job_steps(self):
        self.log.info('Step 1: Creating a workspace')
        self._create_workspace()

        self.log.info('Step 2: Deploy and test the systems')
        self._deploy_and_test_systems()

        self.log.info('Step 3: Clean up')
        self._clean_up()

    def _do_git_config(self):
        cliapp.runcmd(['git', 'config', 'user.name', 'Mason Test Runner'])
        cliapp.runcmd(['git', 'config', 'user.email', 'mason@test.runner'])

    @common.task_step
    def _create_workspace(self):
        self.commit = self.job_arguments['ZUUL_COMMIT']
        self.project = self.job_arguments['ZUUL_PROJECT']
        self.ref = self.job_arguments['ZUUL_REF']
        self.workspace = '/root/mason-workspace'
        self.zuul_url = self.job_arguments['ZUUL_URL']

        url = urlparse.urlparse(self.zuul_url)
        self.defs_checkout = os.path.join(self.workspace,
                                          self.commit,
                                          url.hostname,
                                          '8080',
                                          self.project)

        self._do_git_config()
        cliapp.runcmd(['morph', 'init', self.workspace])

        repo = 'http://%s:8080/%s' % (url.hostname, self.project)
        cliapp.runcmd(['morph', 'checkout', repo, self.commit], cwd=self.workspace)

    @common.task_step
    def _test_systems(self):
        infrastructure = \
            self.plugin_config['config']['test-infrastructure-type']
        cmd = ['scripts/release-test']
        args = ['--deployment-host',
                self.plugin_config['config']['deployment-host'],
                '--trove-host', self.plugin_config['config']['trove-host'],
                '--trove-id', self.plugin_config['config']['trove-id'],
                '--test-ref', self.commit
        ]
        if infrastructure == 'openstack':
            cmd = ['/usr/lib/mason/mason-test-os']
            args += ['--net-id',
                    self.plugin_config['config']['openstack-network-id']]
        args += [self.plugin_config['config']['cluster-morphology']]

    @common.task_step
    def _clean_up(self):
        cliapp.runcmd(['rm', '-rf', self.workspace])