summaryrefslogtreecommitdiff
path: root/mason/tests/artifact_upload.py
blob: aa6c56fce5b78150e9d4f62a6fcf9a765d31354e (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
# Copyright 2014 Codethink Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import cliapp
import json
import logging
import os
import urlparse

import mason

class Runner(mason.runners.JobRunner):

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

    log = logging.getLogger("mason.tests.artifact_upload.Runner")

    def __init__(self, worker_server, plugin_config, job_name):
        super(Runner, self).__init__(worker_server, plugin_config, job_name)
        self.config = self.plugin_config['config']
        self._set_defaults()

        self.total_steps = 2
        if self.config['upload-build-artifacts']:
            self.total_steps += 1
        if self.config['upload-release-artifacts']:
            self.total_steps += 1

    def _set_defaults(self):
        self.config['public-trove-host'] = \
            self.config.get('public-trove-host') or 'git.baserock.org'
        self.config['public-trove-username'] = \
            self.config.get('public-trove-username') or 'root'
        self.config['public-trove-artifact-dir'] = \
            self.config.get('public-trove-artifact-dir') \
            or '/home/cache/artifacts'

        self.config['download-server-address'] = \
            self.config.get('download-server-address') \
            or 'download.baserock.org'
        self.config['download-server-username'] = \
            self.config.get('download-server-username') or 'root'
        self.config['download-server-private-dir'] = \
            self.config.get('download-server-private-dir') \
            or '/srv/download.baserock.org/baserock/.publish-temp'
        self.config['download-server-public-dir'] = \
            self.config.get('download-server-public-dir') \
            or '/srv/download.baserock.org/baserock'

        self.config['release-artifact-dir'] = \
            self.config.get('release-artifact-dir') or '.'
        self.config['local-build-artifacts-dir'] = \
            self.config.get('local-build-artifacts-dir') or 'build-artifacts'
        self.config['architecture'] = \
            self.config.get('architecture') or []

        if 'upload-release-artifacts' not in self.config:
            self.config['upload-release-artifacts'] = True
        if 'upload-build-artifacts' not in self.config:
            self.config['upload-build-artifacts'] = True

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

        if self.config['upload-build-artifacts']:
            self.log.info('Step 2: Publish the build artifacts')
            self._publish_build_artifacts()

        if self.config['upload-release-artifacts']:
            self.log.info('Step %d: Publish the release artifacts' %
                          (self.current_step + 1))
            self._publish_release_artifacts()

        self.log.info('Step %d: Clean up' % (self.current_step + 1))
        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'])

    @mason.util.job_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.morph_helper = mason.util.MorphologyHelper(self.defs_checkout)

        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)

    @mason.util.job_step
    def _publish_build_artifacts(self):
        publisher = mason.publishers.BuildArtifactPublisher(
            self.config, self.defs_repo)
        publisher.publish_build_artifacts()

    @mason.util.job_step
    def _publish_release_artifacts(self):
        publisher = mason.publishers.ReleaseArtifactPublisher(self.config)
        publisher.publish_release_artifacts()

    @mason.util.job_step
    def _clean_up(self):
        cliapp.runcmd(['rm', '-rf', self.workspace])