~fginther/ubuntu-ci-services-itself/bsb-source-detail

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
# Ubuntu CI Engine
# Copyright 2013 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json
import logging

from restish import http, resource

from ci_utils import amqp_utils, restish_utils

log = logging.getLogger(__name__)


def _status():
    return restish_utils.json_ok({
        'rabbit_configured': amqp_utils.get_config() is not None,
    })


def _build_source(source_packages, series, ppa, archive, progress_trigger):
    params = {
        'source_packages': source_packages,
        'series': series,
        'ppa': ppa,
        'archive': archive,
        'progress_trigger': progress_trigger,
    }

    r = amqp_utils.progress_waiting(progress_trigger, params)
    if r:
        log.error('Unable to notify progress trigger, aborting build_source')
        return http.service_unavailable(body=r)

    r = amqp_utils.send('bsbuilder', json.dumps(params))
    if r:
        # send only returns something if it an error message
        r = http.service_unavailable(body=r)
    return r


class API(resource.Resource):
    @resource.child()
    def status(self, request, segments):
        log.debug('[status]: %s %s', request.url, str(segments))
        return restish_utils.http_get_resource(_status)

    @resource.child()
    def build_source(self, request, segments):
        return restish_utils.http_post_resource(_build_source)