summaryrefslogtreecommitdiff
path: root/firehose/plugin/firehose_plugin.py
blob: fdd5b20dfadcf6acf2e3a84f61ec3c38e2a4d5ef (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright (C) 2014 - 2015  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
from contextlib import closing, contextmanager
from fs.tempfs import TempFS
import os
import re
import logging

from firehose.config import FirehoseConfig
import morphlib

from debian.debian_support import Version

@contextmanager
def firehose_git(app):
    try:
        username = (app.runcmd_unchecked(["git", "config", "--global", "user.name"]))[1].strip()
        email = (app.runcmd_unchecked(["git", "config", "--global", "user.email"]))[1].strip()
        app.runcmd(["git", "config", "--global", "user.name", "Firehose merge bot"])
        app.runcmd(["git", "config", "--global", "user.email", "firehose@merge.bot"])
        yield ()
    finally:
        app.runcmd(["git", "config", "--global", "user.name", username])
        app.runcmd(["git", "config", "--global", "user.email", email])

class FirehosePlugin(cliapp.Plugin):
    sha_filename=""

    def enable(self):
        logging.basicConfig(filename='/var/log/firehose.log',level=logging.INFO)
        logging.info("Firehose plugin enabled")
        self.app.add_subcommand('firehose', self.firehose_cmd,
                                arg_synopsis='some-firehose.yaml...')
        self.app.settings.string(['gerrit-username'],
                                 'Username for gerrit',
                                 default=None)
        self.app.settings.string(['gerrit-url'],
                                 'URL for gerrit',
                                 default='gerrit.baserock.org')
        self.app.settings.string(['git-username'],
                                 'git username',
                                 default=None)
        self.app.settings.string(['git-email'],
                                 'git email address',
                                 default=None)

    def disable(self):
        logging.info("Firehose plugin disabled")
        pass

    def firehose_cmd(self, args):
        confs = []
        if len(args) == 0:
            raise cliapp.AppException("Expected list of firehoses on command line")
        for fname in args:
            with open(fname, "r") as fh:
                confs.append(FirehoseConfig(fname, fh))

        # Ensure all incoming configurations are based on, and landing in, the
        # same repository.  This is because we're only supporting an aggregated
        # integration mode for now.

        if len(set(c.landing_repo for c in confs)) > 1:
            raise cliapp.AppException("Not all firehoses have the same landing repo")
        if len(set(c.landing_baseref for c in confs)) > 1:
            raise cliapp.AppException("Not all firehoses have the same landing baseref")
        if len(set(c.landing_destref for c in confs)) > 1:
            raise cliapp.AppException("Not all firehoses have the same landing destref")


        # Ensure that all incoming configurations have unique things they are
        # integrating into.  Note: this allows for the same upstream to be
        # tracked in multiple configs, providing they are all targetting a
        # different part of the system.  (e.g. linux kernels for multiple BSPs)

        if len(set("%s:%s" % (c.landing_stratum,
                              c.landing_chunk) for c in confs)) != len(confs):
            raise cliapp.AppException("Not all firehoses have unique landing locations")

        with closing(TempFS(temp_dir=self.app.settings['tempdir'])) as tfs, \
                firehose_git(self.app):
            self.base_path = tfs.getsyspath("/")
            self.make_workspace()
            self.setup_git()
            self.make_branch(confs[0].landing)
            self.log_sha(os.path.basename(fname.replace('.yaml','')))
            self.insert_githook()
            self.reset_to_tracking(confs[0].landing)
            self.load_morphologies()
            for c in confs:
                self.update_for_conf(c)
            if self.updated_morphologies():
                self.commit_and_push(os.path.basename(fname.replace(".yaml", "")))

    def make_path(self, *subpath):
        logging.info("make_path: " + self.base_path)
        return os.path.join(self.base_path, *subpath)

    def make_workspace(self):
        logging.info("make_workspace: " + self.make_path("ws"))
        self.app.subcommands['init']([self.make_path("ws")])

    def setup_git(self):
        git_username = self.app.settings['git-username']
        git_email = self.app.settings['git-email']
        if (len(git_username) > 1) and (len(git_email) > 1):
            logging.info("setup_git: " + git_username + " " + git_email)
            self.app.runcmd(['git', 'config', '--global', 'user.name', git_username])
            self.app.runcmd(['git', 'config', '--global', 'user.email', git_email])

    def make_branch(self, root):
        os.chdir(self.make_path("ws"))
        try:
            logging.info("make_branch: " + \
                         root['repo'] + " " + root['destref'] + " " + root['baseref'])
            self.app.subcommands['branch']([root['repo'], root['destref'], root['baseref']])
        except cliapp.AppException, ae:
            if "already exists in" in str(ae):
                self.app.subcommands['checkout']([root['repo'], root['destref']])
            else:
                raise
        repopath = root['repo'].replace(':', '/')
        self.gitpath = self.make_path("ws", root['destref'], repopath)
        logging.info("gitpath: " + self.gitpath)

    def log_sha(self, name):
        sha = self.app.runcmd(['git', 'rev-parse', 'HEAD'], cwd=self.gitpath).strip()
        self.sha_filename = '/var/lib/firehose/'+name+'_log.json'
        if (os.path.exists(os.path.abspath(self.sha_filename))):
            with open(self.sha_filename) as f:
                log = f.read()
                if sha in log:
                    logging.info('Everything up-to-date')
                    print 'Everything up-to-date'
                    exit(0)
        # Save the hash to a file
        with open(self.sha_filename, 'w') as log:
            log.write('{\"sha\":\"'+sha+'\"}')
        logging.info('log_sha: wrote {\"sha\":\"'+sha+'\"}')

    def insert_githook(self):
        gerrit_username = self.app.settings['gerrit-username']
        gerrit_url = self.app.settings['gerrit-url']
        status, output, error = \
            self.app.runcmd_unchecked(['scp', '-p', '-P', '29418',
                                       gerrit_username+'@'+
                                       gerrit_url+':hooks/commit-msg',
                                       self.gitpath+
                                       '/.git/hooks/commit-msg'],
                                      cwd=self.gitpath)
        if status != 0:
            # Remove the previously recorded hash
            os.remove(self.sha_filename)
            if "publickey" in error:
                raise cliapp.AppException(
                    "Cannot insert githook. "+
                    "Check your ssh public key is registered in gerrit")
            else:
                self.app.output.write(error)
                raise cliapp.AppException(
                    "Woah! unable to insert githook")

    def reset_to_tracking(self, root):
        logging.info('reset_to_tracking')
        branch_head_sha = self.app.runcmd(['git', 'rev-parse', 'HEAD'],
                                          cwd=self.gitpath).strip()
        self.app.runcmd(['git', 'reset', '--hard', 'origin/%s' % root['baseref']],
                        cwd=self.gitpath)
        self.app.runcmd(['git', 'reset', '--soft', branch_head_sha],
                        cwd=self.gitpath)

    def load_morphologies(self):
        logging.info('load_morphologies')
        ws = morphlib.workspace.open(self.gitpath)
        sb = morphlib.sysbranchdir.open_from_within(self.gitpath)
        loader = morphlib.morphloader.MorphologyLoader()
        morphs = morphlib.morphset.MorphologySet()
        for morph in sb.load_all_morphologies(loader):
            morphs.add_morphology(morph)

        self.ws = ws
        self.sb = sb
        self.loader = loader
        self.morphs = morphs
        self.lrc, self.rrc = morphlib.util.new_repo_caches(self.app)

    def find_cached_repo(self, stratum, chunk):
        logging.info('find_cached_repo')
        urls = []
        def wanted_spec(m, kind, spec):
            if not m['kind'] == 'stratum' and kind == 'chunks':
                return False
            if m.get('name') == stratum and spec.get('name') == chunk:
                return True
        def process_spec(m, kind, spec):
            urls.append(spec['repo'])
            return False
        self.morphs.traverse_specs(process_spec, wanted_spec)
        if len(urls) != 1:
            # Remove the previously recorded hash
            os.remove(self.sha_filename)
            raise cliapp.AppException(
                "Woah! expected 1 chunk matching %s:%s (got %d)" % (
                    stratum, chunk, len(urls)))
        return self.lrc.get_updated_repo(urls[0])

    def git_in_repo_unchecked(self, repo, *args):
        args = list(args)
        args.insert(0, "git")
        return self.app.runcmd_unchecked(args, cwd=repo.path)
    def git_in_repo(self, repo, *args):
        args = list(args)
        args.insert(0, "git")
        return self.app.runcmd(args, cwd=repo.path)

    def all_shas_for_refs(self, repo):
        logging.info('all_shas_for_refs')
        all_lines = self.git_in_repo(repo, "for-each-ref").strip().split("\n")
        for refline in all_lines:
            (sha, objtype, name) = refline.split(None, 2)
            if objtype == 'tag':
                sha = self.git_in_repo(repo, "rev-list", "-1", sha).strip()
            yield name, sha

    def interested_in_ref(self, conf, refname):
        logging.info('interested_in_ref')
        if conf.tracking_mode == 'follow-tip':
            return refname == conf.tracking_ref
        elif conf.tracking_mode == 'refs':
            return any(re.match(filterstr, refname)
                       for filterstr in conf.tracking_filters)
        else:
            raise FirehoseConfigError(conf, "Unknown value: %s" %
                                      conf.tracking_mode, ["tracking", "mode"])

    def rewrite_ref(self, conf, ref):
        logging.info('rewrite_ref')
        if conf.tracking_mode == 'refs':
            for transform in conf.tracking_transforms:
                ref = re.sub(transform['match'], transform['replacement'], ref)
        return ref

    def compare_refs(self, ref1, ref2):
        logging.info('compare_refs: ' + str(ref1) + " " + str(ref2))
        if ref1[0] == ref2[0]:
            return 0
        v1 = Version(ref1[0].replace("/", "-"))
        v2 = Version(ref2[0].replace("/", "-"))
        if v1 < v2:
            return -1
        return 1

    def sanitise_refname(self, refname):
        if refname.startswith("refs/"):
            logging.info('sanitise_refname: ' + \
                         refname + " -> " + "/".join((refname.split("/"))[2:]))
            return "/".join((refname.split("/"))[2:])
        else:
            return refname

    def update_refs(self, stratum, chunk, sha, refname):
        logging.info('update_refs')
        def wanted_spec(m, kind, spec):
            if not m['kind'] == 'stratum' and kind == 'chunks':
                return False
            if m.get('name') == stratum and spec.get('name') == chunk:
                return True
        def process_spec(m, kind, spec):
            spec['ref'] = sha
            spec['unpetrify-ref'] = refname
            return True
        self.morphs.traverse_specs(process_spec, wanted_spec)

    def update_for_conf(self, conf):
        stratum = conf.landing_stratum
        chunk = conf.landing_chunk
        logging.info('update_for_conf: ' + stratum + " " + chunk)
        crc = self.find_cached_repo(stratum, chunk)
        interesting_refs = [
            (self.rewrite_ref(conf, name), name, sha)
            for (name, sha) in self.all_shas_for_refs(crc)
            if self.interested_in_ref(conf, name)]
        interesting_refs.sort(self.compare_refs)
        (_, refname, sha) = interesting_refs.pop()
        refname = self.sanitise_refname(refname)
        self.update_refs(stratum, chunk, sha, refname)

    def updated_morphologies(self):
        logging.info('updated_morphologies')
        if not any(m.dirty for m in self.morphs.morphologies):
            return False
        for morph in self.morphs.morphologies:
            if morph.dirty:
                self.loader.unset_defaults(morph)
                self.loader.save_to_file(
                    self.sb.get_filename(morph.repo_url, morph.filename), morph)
                morph.dirty = False

        return True

    def commit_and_push(self, name):
        gerrit_username = self.app.settings['gerrit-username']
        gerrit_url = self.app.settings['gerrit-url']
        commit_msg = 'Update to '+name+' ref in definitions.git'
        branch_name = 'HEAD:refs/for/master/firehose/'+name
        (code, out, err) = self.app.runcmd_unchecked(
            ['git', 'commit', '-a', '-m', commit_msg],
            cwd=self.gitpath)
        logging.info('commit_and_push: ' + str(code))
        if code == 0:
            status, output, error = \
                self.app.runcmd_unchecked(['git', 'push',
                                           'ssh://'+gerrit_username+
                                           '@'+gerrit_url+
                                           ':29418/baserock/'+
                                           'baserock/definitions',
                                           branch_name], cwd=self.gitpath)
            if status != 0:
                # Remove the previously recorded hash
                os.remove(self.sha_filename)
                if "publickey" in error:
                    raise cliapp.AppException(
                        "Could not push to gerrit. "+
                        "Check your ssh public key is "+
                        "registered in gerrit")
                else:
                    self.app.output.write(error)
                    raise cliapp.AppException(
                        "Woah! unable to push changes to gerrit")