aboutsummaryrefslogtreecommitdiff
blob: 13eafa2da728002fda06f9e0311afabf06bb6dc4 (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
# Copyright 2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import re
import os

import xml.etree.ElementTree as etree

from twisted.internet import defer
from twisted.python import log

from buildbot.process.buildstep import BuildStep
from buildbot.process.results import SUCCESS
from buildbot.process.results import FAILURE
from buildbot.process import remotecommand
from buildbot.plugins import steps

class SetupPropertys(BuildStep):
    name = 'Setup propertys for CPV check'
    description = 'Running'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        # set this in config
        super().__init__(**kwargs)

    #@defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        if self.gentooci.config.project['worker_config']['basedir'] is None:
            worker_basedir = self.getProperty('builddir').split('/builds')[0]
        else:
            worker_basedir = self.gentooci.config.project['worker_config']['basedir']
        self.setProperty('worker_basedir', worker_basedir, 'worker_basedir')
        self.setProperty('rootworkdir', False, 'rootworkdir')
        portage_repos_path = os.path.join(worker_basedir, self.gentooci.config.project['worker_config']['repository_basedir'])
        self.setProperty('portage_repos_path', portage_repos_path, 'portage_repos_path')
        return SUCCESS

class AddPackage(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'AddPackage'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.package_data = {}
        self.package_data['name'] = self.getProperty("package")
        self.package_data['repository_uuid'] = self.getProperty("repository_data")['uuid']
        self.package_data['category_uuid'] = self.getProperty("category_data")['uuid']
        self.package_data['uuid'] = yield self.gentooci.db.packages.addPackage(
                                            self.package_data['name'],
                                            self.package_data['repository_uuid'],
                                            self.package_data['category_uuid']
                                            )
        print(self.package_data)
        self.setProperty("package_data", self.package_data, 'package_data')
        return SUCCESS

class UpdateEmails(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'UpdateEmails'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        packages_emails = yield self.gentooci.db.packages.getEmailsIdsByPackageUuid(self.getProperty("package_data")['uuid'])
        if packages_emails != []:
            yield self.gentooci.db.packages.delPackageEmail(self.getProperty("package_data")['uuid'])
        # update email
        for maintainer in self.getProperty("maintainers"):
            print(maintainer)
            email_data = yield self.gentooci.db.packages.getEmailIdByEmail(maintainer['email'])
            print(email_data)
            if email_data is None:
                email_id = yield self.gentooci.db.packages.addEmail(maintainer['email'])
                print(email_id)
            else:
                email_id = email_data['id']
            if maintainer['proxied'] is None:
                proxied = 'no'
            else:
                proxied = maintainer['proxied']
            res = yield self.gentooci.db.packages.addPackageEmail(
                                                                email_id,
                                                                self.getProperty("package_data")['uuid'],
                                                                maintainer['type'],
                                                                proxied
                                                                )
            print(res)
        return SUCCESS

class ReadMetadataXML(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'ReadMetadataXML'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        self._xml_tree = etree.fromstring(self.getProperty("metadata_xml"))
        # get maintainer
        self.maintainers = []
        for maintainer in self._xml_tree.findall('maintainer'):
            self.maintainers.append(dict(
                                        type=maintainer.get('type'),
                                        proxied=maintainer.get('proxied'),
                                        email=maintainer.find('email').text,
                                        ))
        print(self.maintainers)
        self.setProperty("maintainers", self.maintainers, 'maintainers')
        yield self.build.addStepsAfterCurrentStep([UpdateEmails()])
        #   update email
        return SUCCESS

class CheckSha256Metadata(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'CheckSha256Metadata'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        # if checksum diff update
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        sha256sum_metadata = self.getProperty("sha256sum_metadata").split()[0]
        print(sha256sum_metadata)
        package_metadata_data_updated = False
        self.package_metadata_data = yield self.gentooci.db.packages.getPackageMetadataByPackageUuid(self.getProperty("package_data")['uuid'])
        print(self.package_metadata_data)
        if self.package_metadata_data is None:
            res = yield self.gentooci.db.packages.addMetadata(
                                                            self.getProperty("package_data")['uuid'],
                                                            sha256sum_metadata
                                                            )
            print(res)
            package_metadata_data_updated = True
        elif self.package_metadata_data['sha256'] != sha256sum_metadata:
            # update sha256
            package_metadata_data_updated = True
        if package_metadata_data_updated:
            yield self.build.addStepsAfterCurrentStep([
            steps.SetPropertyFromCommand(
                                        name = 'CatMetadataFile',
                                        haltOnFailure = True,
                                        flunkOnFailure = True,
                                        command=['cat', self.getProperty("metadata_file")],
                                        strip=False,
                                        property='metadata_xml'
                                        ),
            ReadMetadataXML(),
            ])
        return SUCCESS

class CheckMetadataPackagePath(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'CheckMetadataPackagePath'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        # set metadata.xml path
        self.repository_basedir = yield os.path.join(self.getProperty("worker_basedir"), self.getProperty('portage_repos_path'))
        print(self.repository_basedir)
        self.repository_path = yield os.path.join(self.repository_basedir, self.getProperty("repository_data")['name'])
        print(self.repository_path)
        self.cp_path = yield os.path.join(self.repository_path, self.getProperty("change_data")['cp'])
        print(self.cp_path)
        self.metadata_file = yield '/'.join([self.cp_path, 'metadata.xml'])
        print(self.metadata_file)
        # check file exist
        cmd = remotecommand.RemoteCommand('stat', {'file': self.metadata_file})
        yield self.runCommand(cmd)
        if cmd.didFail():
            return FAILURE
        self.setProperty("metadata_file", self.metadata_file, 'metadata_file')
        # checksum on file
        yield self.build.addStepsAfterCurrentStep([
            steps.SetPropertyFromCommand(
                                        name = 'ChecksumMetadataPackage',
                                        haltOnFailure = True,
                                        flunkOnFailure = True,
                                        command=['sha256sum', self.metadata_file],
                                        workdir=self.repository_path,
                                        strip=False,
                                        property='sha256sum_metadata'
                                        ),
            CheckSha256Metadata(),
            ])
        return SUCCESS

class CheckP(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'CheckP'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.package = self.getProperty("change_data")['cp'].split('/')[1]
        print(self.package)
        self.package_data = yield self.gentooci.db.packages.getPackageByName(self.package,
                                                                            self.getProperty("category_data")['uuid'],
                                                                            self.getProperty("repository_data")['uuid'])
        print(self.package_data)
        if self.package_data is None:
            self.setProperty("package", self.package, 'package')
            yield self.build.addStepsAfterCurrentStep([AddPackage()])
        else:
            self.setProperty("package_data", self.package_data, 'package_data')
        return SUCCESS

class TriggerCheckForV(BuildStep):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    name = 'TriggerCheckForV'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    @defer.inlineCallbacks
    def run(self):
        addStepUpdateVData = []
        for cpv in self.getProperty("change_data")['cpvs']:
            print(cpv)
            addStepUpdateVData.append(
                steps.Trigger(
                    schedulerNames=['update_v_data'],
                    waitForFinish=False,
                    updateSourceStamp=False,
                    set_properties={
                        'cpv' : cpv,
                        'package_data' : self.getProperty("package_data"),
                        'repository_data' : self.getProperty("repository_data"),
                        'category_data' : self.getProperty("category_data"),
                        'change_data' : self.getProperty("change_data"),
                        'project_data' : self.getProperty("project_data"),
                        'cp_worker' : self.getProperty('workername'),
                    }
                )
            )
        yield self.build.addStepsAfterCurrentStep(addStepUpdateVData)
        return SUCCESS