summaryrefslogtreecommitdiff
path: root/ybd/__main__.py
blob: 059749f1e7184d1c59f1e7fa4c11c0cb6d5371c5 (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
#!/usr/bin/env python
# Copyright (C) 2014-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 =*=

'''A module to build a definition.'''

import os
import sys
import fcntl
import app
from assembly import compose, RetryException
from deployment import deploy
from definitions import Definitions
import cache
import sandbox
import sandboxlib


print('')
if not os.path.exists('./VERSION'):
    if os.path.basename(os.getcwd()) != 'definitions':
        if os.path.isdir(os.path.join(os.getcwd(), 'definitions')):
            os.chdir(os.path.join(os.getcwd(), 'definitions'))
        else:
            if os.path.isdir(os.path.join(os.getcwd(), '..', 'definitions')):
                os.chdir(os.path.join(os.getcwd(), '..', 'definitions'))

app.setup(sys.argv)
app.cleanup(app.config['tmp'])
cache.cull(app.config['artifacts'])

with app.timer('TOTAL'):
    tmp_lock = open(os.path.join(app.config['tmp'], 'lock'), 'r')
    fcntl.flock(tmp_lock, fcntl.LOCK_SH | fcntl.LOCK_NB)

    target = os.path.join(app.config['defdir'], app.config['target'])
    app.log('TARGET', 'Target is %s' % target, app.config['arch'])
    with app.timer('DEFINITIONS', 'parsing %s' % app.config['def-version']):
        defs = Definitions()
    with app.timer('CACHE-KEYS', 'cache-key calculations'):
        cache.cache_key(defs, app.config['target'])

    target = defs.get(app.config['target'])
    if app.config['total'] == 0 or (app.config['total'] == 1 and
                                    target.get('kind') == 'cluster'):
        app.exit('ARCH', 'ERROR: no definitions found for', app.config['arch'])

    defs.save_trees()

    sandbox.executor = sandboxlib.executor_for_platform()
    app.log(app.config['target'], 'Sandbox using %s' % sandbox.executor)
    if sandboxlib.chroot == sandbox.executor:
        app.log(app.config['target'], 'WARNING: using chroot is less safe ' +
                'than using linux-user-chroot')

    if app.config.get('instances'):
        app.spawn()

    while True:
        try:
            compose(defs, target)
            break
        except KeyboardInterrupt:
            app.log(target, 'Interrupted by user')
            os._exit(1)
        except RetryException:
            pass
        except:
            import traceback
            traceback.print_exc()
            app.log(target, 'Exiting: uncaught exception')
            os._exit(1)

    if app.config.get('reproduce'):
        app.log('REPRODUCED',
                'Matched %s of' % len(app.config['reproduced']),
                app.config['tasks'])
        for match in app.config['reproduced']:
            print match[0], match[1]

    if target.get('kind') == 'cluster' and app.config.get('fork') is None:
        with app.timer(target, 'cluster deployment'):
            deploy(defs, target)