summaryrefslogtreecommitdiff
path: root/ick2lib/app.py
blob: 1f7cb25f36beaa1ff1a4760d6154f8c40369ccb5 (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
# Copyright 2017  Lars Wirzenius
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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-3+ =*=


import logging
import os
import random


import cliapp
import yaml


import ick2lib


class ApiApp(cliapp.Application):

    def add_settings(self):
        self.settings.boolean(
            ['debug'],
            'Turn on debug mode (incl. for yarn tests)'
        )

        self.settings.string(
            ['pid-file'],
            'Store application PID in FILE',
            metavar='FILE'
        )

        self.settings.string(
            ['port-file'],
            'Store randomly chosen listening port (in debug mode) in FILE',
            metavar='FILE'
        )

        self.settings.string(
            ['projects'],
            'Read project list from YAML format FILE',
            metavar='FILE'
        )

    def write_pid_file(self):
        write_file(self.settings['pid-file'], str(os.getpid()))

    def pick_random_port(self):
        return random.randint(1025, 32767)

    def write_port_file(self, port):
        write_file(self.settings['port-file'], str(port))

    def load_projects(self, filename):
        with open(filename) as f:
            projects_config = yaml.safe_load(f)

        projects = {}
        for name, config in projects_config['projects'].items():
            p = ick2lib.Project(name)
            p.set_git(config['git'])
            projects[name] = p
            for shell in config['shell_steps']:
                p.add_build_step(shell)

        return projects

    def process_args(self, args):
        assert self.settings['debug']

        projects = {}
        if os.path.exists(self.settings['projects']):
            projects = self.load_projects(self.settings['projects'])

        self.write_pid_file()
        port = self.pick_random_port()
        self.write_port_file(port)

        apiapp = ick2lib.ApiService()
        apiapp.set_projects(projects)
        apiapp.run_debug(port)


def write_file(filename, content):
    logging.info('Writing file %s: %r', filename, content)
    with open(filename, 'w') as f:
        f.write('{}\n'.format(content))