summaryrefslogtreecommitdiff
path: root/lorry-controller
blob: a3efa2c2162a464c8ffc0e94bea59e5d4845438f (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
#
# Copyright (C) 2013  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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import cliapp
import logging
import os
import time
import re
import urllib2


from lorrycontroller.confparser import LorryControllerConfig
from lorrycontroller.workingstate import WorkingStateManager
from lorrycontroller.htmlstatus import HTMLStatusManager


defaults = {
    'work-area': '/home/lorry/controller-area',
    'config-name': 'lorry-controller.conf',
    'lorry': 'lorry',
}


token_finder = re.compile("([0-9a-f]{40})")


class LorryController(cliapp.Application):

    def add_settings(self):
        self.settings.string(['work-area'],
                             'path to the area for  the controller to work in',
                             metavar='PATH',
                             default=defaults['work-area'])
        self.settings.boolean(['dry-run'],
                              "do a dry-run and don't actually do anything "
                              "beyond updating the git tree",
                              default=False)
        self.settings.string(['lorry'],
                             'path to the lorry binary to use',
                             metavar='LORRY',
                             default=defaults['lorry'])
        self.settings.string(['config-name'],
                             'configuration leafname.  Defaults to '
                             'lorry-controller.conf',
                             metavar='CONFNAME',
                             default=defaults['config-name'])
        self.settings.boolean(['lorry-verbose'],
                              'Whether to pass --verbose to lorry',
                              default=False)
        self.settings.string(['lorry-log'],
                             'Log file name for lorry if wanted',
                             metavar='LORRYLOG',
                             default=None)
        self.settings.string(['html-file'],
                             'HTML filename for lorry controller status',
                             metavar='HTMLFILE',
                             default=None)

    def process_args(self, args):
        logging.info("Starting to control lorry")
        try:
            os.chdir(self.settings['work-area'])
        except OSError, e:
            logging.error("Unable to chdir() to %s" % 
                          self.settings['work-area'])
            raise SystemExit(2)
        if not os.path.isdir("git"):
            logging.error("Unable to find git checkout")
            raise SystemExit(3)
        if not os.path.isdir("work"):
            os.mkdir("work")

        logging.info("Updating configuration checkout")
        self.rungit(['remote', 'update', 'origin'])
        self.rungit(['reset', '--hard', 'origin/master'])
        self.rungit(['clean', '-fdx'])

        self.lorrycmd=[self.settings['lorry']]
        if self.settings['lorry-verbose']:
            self.lorrycmd += ["--verbose"]
        if self.settings['lorry-log'] is not None:
            self.lorrycmd += ["--log", self.settings['lorry-log']]

        if not os.path.exists(os.path.join('git',
                                           self.settings['config-name'])):
            logging.error("Unable to find lorry-controller.conf in git")
            raise SystemExit(4)

        self.conf = LorryControllerConfig(self, 'git/lorry-controller.conf')
        self.html = HTMLStatusManager(self)
        if self.settings['dry-run']:
            self.html.series = 0
        self.html.write_out_status()
        self.conf.parse_config()

        with WorkingStateManager(self) as mgr:
            # Update any troves
            self.html.set_mgr(mgr)
            self.html.bump_state()
            self.conf.update_troves(mgr)
            prev_lorries = set(mgr.lorry_state.keys())
            cur_lorries = set(self.conf.lorries.keys())
            logging.info("Starting processing.  Previously %d lorries "
                         "were handled.  We currently have %d defined." % (
                    len(prev_lorries), len(cur_lorries)))

            # 1. Handle deletes for any old lorries we no longer want
            self.html.bump_state()
            logging.info("Delete any old lorries...")
            for dead_lorry in prev_lorries - cur_lorries:
                self.html.set_processing(dead_lorry)
                logging.info("Dead lorry: %s" % dead_lorry)
                conf_uuid = mgr.lorry_state[dead_lorry]['conf']
                if conf_uuid in self.conf.configs:
                    should_delete = self.conf.configs[conf_uuid]['destroy']
                else:
                    # Could not find UUID in config, switch to 'never'
                    should_delete = "never"
                want_destroy = (should_delete == "always")
                if should_delete == "unchanged":
                    exit, out, err = self.maybe_runcmd(
                        ['git', 'ls-remote', 'ssh://git@localhost/%s.git' %
                         dead_lorry], dry=True)
                    if exit != 0:
                        logging.error("Unable to ls-remote to decide if "
                                      "unchanged.  Assuming it is changed.")
                    else:
                        logging.debug("TODO: Should decide if unchanged!")

                if want_destroy:
                    exit, out, err = self.maybe_runcmd(['ssh', 'git@localhost',
                                                        'destroy', dead_lorry],
                                                       dry=True)
                    if exit != 0:
                        logging.error("Unable to destroy %s" % dead_lorry)
                    else:
                        token = token_finder.match(out).group(1)
                        exit, out, err = self.maybe_runcmd(
                            ['ssh', 'git@localhost', 'destroy', dead_lorry,
                             token])
                        if exit != 0:
                            logging.error("Unable to destroy %s despite having"
                                          " the token %s" %
                                          (dead_lorry, token))
                        else:
                            logging.debug("Destroyed")
                del mgr.lorry_state[dead_lorry]

            # 2. Handle creates for any new lorries we now want
            self.html.bump_state()
            logging.info("Create any new lorries...")
            for new_lorry in cur_lorries - prev_lorries:
                self.html.set_processing(new_lorry)
                logging.info("New lorry: %s" % new_lorry)
                lorry = self.conf.lorries[new_lorry]
                conf_uuid = lorry['controller-uuid']
                conf = self.conf.configs[conf_uuid]
                nextdue = self.conf.duetimes[new_lorry]
                # Make new lorries overdue.
                nextdue -= conf['interval-parsed']
                should_create = conf['create'] == "always"
                store_state = True
                if should_create:
                    exit, out, err = self.maybe_runcmd(["ssh", "git@localhost",
                                                        "create", new_lorry])
                    if exit != 0:
                        if ' already exists' in err:
                            logging.warn("Repository %s already exists" %
                                         new_lorry)
                        else:
                            logging.error("Unable to create repository %s" %
                                          new_lorry)
                            logging.error(err)
                            store_state = False
                if store_state:
                    self.maybe_runcmd(["ssh", "git@localhost", "set-head",
                                       new_lorry, lorry['source-HEAD']])
                    mgr.lorry_state[new_lorry] = {
                        'destroy': conf['destroy'],
                        'conf': conf_uuid,
                        'lorry': lorry,
                        'next-due': nextdue,
                        }
                else:
                    # Remove this from cur_lorries so we don't run it
                    cur_lorries.remove(new_lorry)

            # 3. For every lorry we have, update the settings if necessary.
            #    and reset the next-due as appropriate.
            self.html.bump_state()
            logging.info("Update active lorry configurations...")
            updated_count = 0
            for upd_lorry in cur_lorries:
                if mgr.lorry_state[upd_lorry]['lorry'] != \
                        self.conf.lorries[upd_lorry]:
                    lorry = self.conf.lorries[upd_lorry]
                    old_lorry = mgr.lorry_state[upd_lorry]["lorry"]
                    if lorry["source-HEAD"] != \
                            old_lorry.get("source-HEAD", "refs/heads/master"):
                        self.maybe_runcmd(['ssh', 'git@localhost', 'set-head',
                                           upd_lorry, lorry["source-HEAD"]])
                    conf_uuid = lorry['controller-uuid']
                    conf = self.conf.configs[conf_uuid]
                    nextdue = self.conf.duetimes[upd_lorry]
                    mgr.lorry_state[upd_lorry] = {
                        'destroy': conf['destroy'],
                        'conf': conf_uuid,
                        'lorry': lorry,
                        'next-due': nextdue,
                    }
                    updated_count += 1
            logging.info("Result: %d/%d lorries needed updating" % (
                    updated_count, len(cur_lorries)))

            # 3. Iterate all active lorries and see if they're due
            logging.info("Iterate active lorries looking for work...")
            now = time.time()
            lorried = 0
            earliest_due = None
            what_early_due = ""
            lorries_to_run = []
            for lorry in cur_lorries:
                state = mgr.lorry_state[lorry]
                conf_uuid = state['conf']
                conf = self.conf.configs[conf_uuid]
                due = state['next-due']
                if now >= due:
                    lorries_to_run.append(lorry)
            lorries_to_run.sort()
            for lorry in lorries_to_run:
                state = mgr.lorry_state[lorry]
                conf_uuid = state['conf']
                conf = self.conf.configs[conf_uuid]
                due = state['next-due']
                lorried += 1
                logging.info("Running %d/%d. Lorrying: %s" % (
                        lorried, len(lorries_to_run),lorry))
                self.html.set_processing(lorry)
                with mgr.runner(lorry) as runner:
                    runner.run_lorry(*self.lorrycmd)
                while state['next-due'] <= now:
                    state['next-due'] += conf['interval-parsed']

            for lorry in cur_lorries:
                state = mgr.lorry_state[lorry]
                due = state['next-due']
                if earliest_due is None or due < earliest_due:
                    earliest_due = due
                    what_early_due = lorry

            if earliest_due is None:
                logging.info("Lorried %d.  No idea what's next." % lorried)
            else:
                logging.info("Lorried %d.  %s due in %d seconds" % (
                        lorried, what_early_due, int(earliest_due - now)))
            logging.info("All done.")
        self.html.bump_state()

    def rungit(self, args):
        self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'],
                                                   'git'))

    def maybe_http_request(self, url, dry=False):
        if (not self.settings['dry-run']) or dry:
            request = urllib2.Request(url, None, {})
            response = urllib2.urlopen(request)
            code = response.getcode()
            if code == 200:
                return 0, response.read(), ''
            else:
                return code, response.read(), str(code)
        else:
            logging.debug('DRY-RUN: Not sending a request to %s' % url)
            return 0, 'DRY-RUN', 'DRY-RUN'

    def maybe_runcmd(self, cmdline, dry=False, *args, **kwargs):
        if (not self.settings['dry-run']) or dry:
            return self.runcmd_unchecked(cmdline, *args, **kwargs)
        else:
            logging.debug("DRY-RUN: Not running %r" % cmdline)
            return 0, 'DRY-RUN', 'DRY-RUN'

if __name__ == '__main__':
    LorryController(version='1').run()