summaryrefslogtreecommitdiff
path: root/ybd/concourse.py
blob: 4d5a201650f5c9b71deff6d4bb6bfbbd06a84ca9 (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
# Copyright (C) 2016  Codethink Limited
#
# 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, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-2 =*=

import yaml
import app
from app import log, timer, defs

# Concourse data model:
# a 'resource' is an input line into a box
# a 'job' is a box on the diagram
# a 'job' has a 'plan' - a set of 'tasks' which operate on 'resources'


class Pipeline(object):

    def __init__(self, dn):

        self.resources = [{'name': dn['name'], 'type': 'foo'}]
        self.jobs = []
        self.config = {'run': {'path': 'ybd', 'args': []},
                       'platform': 'linux',
                       'image': 'docker:///devcurmudgeon/foo'}

        self.write_pipeline(dn)
        output = app.defs.get(dn)['name'] + '.yml'
        with open(output, 'w') as f:
            pipeline = {'resources': self.resources, 'jobs': self.jobs}
            f.write(yaml.dump(pipeline, default_flow_style=False))
        log('CONCOURSE', 'pipeline is at', output)

    def write_pipeline(self, dn):
        dn = app.defs.get(dn)
        self.add_resource(dn)
        aggregate = []
        for it in dn.get('build-depends', []) + dn.get('contents', []):
            component = app.defs.get(it)
            self.add_resource(component)
            if component.get('kind', 'chunk') == 'chunk':
                aggregate += [{'get': component['name']}]
            else:
                self.write_pipeline(component)
                aggregate += [{'get': component['name'],
                               'passed': [component['name']]}]

        self.add_job(dn, [{'aggregate': aggregate}, {'put': dn['name']}])

    def add_job(self, component, plan):
        found = False
        for job in self.jobs:
            if job['name'] == component['name']:
                found = True
                for i in plan:
                    if i not in job['plan']:
                        job['plan'] += i
        if not found:
            self.jobs += [{'name': component['name'], 'plan': plan}]

    def add_resource(self, component):
        found = False
        for resource in self.resources:
            if resource['name'] == component['name']:
                found = True
        if not found:
            if component.get('kind', 'chunk') == 'chunk':
                self.resources += [{'name': component['name'],
                                    'type': 'git',
                                    'source': {'uri': component.get('repo'),
                                               'branch': 'master'}}]
            else:
                self.resources += [{'name': component['name'], 'type': 'foo'}]