-
Notifications
You must be signed in to change notification settings - Fork 193
/
push.py
161 lines (139 loc) · 6.28 KB
/
push.py
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
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
The tool for triggering updates pushes.
"""
import click
from datetime import datetime
import json
import glob
from collections import defaultdict
from pyramid.paster import get_appsettings
from sqlalchemy import engine_from_config
from sqlalchemy.sql import or_
import bodhi.server.notifications
from bodhi.server.util import transactional_session_maker
from bodhi.server.models import Update, Base, UpdateRequest, Build, Release
@click.command()
@click.option('--releases', help='Push updates for specific releases')
#@click.option('--type', default=None, help='Push a specific type of update',
# type=click.Choice(['security', 'bugfix', 'enhancement', 'newpackage']))
@click.option('--request', default='testing,stable',
help='Push updates with a specific request (default: testing,stable)')
@click.option('--builds', help='Push updates for specific builds')
@click.option('--username', envvar='USERNAME', prompt=True)
@click.option('--cert-prefix', default="shell",
help="The prefix of a fedmsg cert used to sign the message.")
@click.option('--config', help='Configuration file to use for database credentials',
default='/etc/bodhi/production.ini')
@click.option('--staging', help='Use the staging bodhi instance',
is_flag=True, default=False)
@click.option('--resume', help='Resume one or more previously failed pushes',
is_flag=True, default=False)
def push(username, cert_prefix, config, **kwargs):
staging = kwargs.pop('staging')
resume = kwargs.pop('resume')
lockfiles = defaultdict(list)
locked_updates = []
if staging:
locks = '/var/cache/bodhi/mashing/MASHING-*'
else:
locks = '/mnt/koji/mash/updates/MASHING-*'
for lockfile in glob.glob(locks):
with file(lockfile) as lock:
state = json.load(lock)
for update in state['updates']:
lockfiles[lockfile].append(update)
locked_updates.append(update)
settings = get_appsettings(config)
engine = engine_from_config(settings, 'sqlalchemy.')
Base.metadata.create_all(engine)
db_factory = transactional_session_maker(engine)
update_titles = None
with db_factory() as session:
updates = []
# If we're resuming a push
if resume:
for lockfile in lockfiles:
doit = raw_input('Resume %s? (y/n) ' % lockfile).strip().lower()
if doit == 'n':
continue
for update in lockfiles[lockfile]:
update = session.query(Update).filter(Update.title==update).first()
updates.append(update)
click.echo(update)
else:
# Accept both comma and space separated request list
requests = kwargs['request'].replace(',', ' ').split(' ')
requests = [UpdateRequest.from_string(val) for val in requests]
query = session.query(Update).filter(Update.request.in_(requests))
if kwargs.get('builds'):
query = query.join(Update.builds)
query = query.filter(or_(*[Build.nvr==build for build in kwargs['builds'].split(',')]))
if kwargs.get('releases'):
releases = []
for r in kwargs['releases'].split(','):
release = session.query(Release).filter(
or_(Release.name==r,
Release.name==r.upper(),
Release.version==r)).first()
if not release:
click.echo('Unknown release: %s' % r)
else:
releases.append(release)
query = query.filter(or_(*[Update.release==r for r in releases]))
for update in query.all():
# Skip locked updates that are in a current push
if update.locked:
if update.title in locked_updates:
continue
# Warn about locked updates that aren't a part of a push and
# push them again.
else:
click.echo('Warning: %s is locked but not in a push' %
update.title)
# Skip unsigned updates (this checks that all builds in the update are signed)
if not update.signed:
click.echo('Warning: %s has unsigned builds and has been skipped' %
update.title)
continue
updates.append(update)
for update in updates:
print update.title
doit = raw_input('Push these %d updates? (y/n) ' % len(updates)).lower().strip()
if doit == 'y':
click.echo('\nLocking updates...')
for update in updates:
update.locked = True
update.date_locked = datetime.utcnow()
update_titles = list([update.title for update in updates])
else:
click.echo('\nAborting push')
raise Exception('Aborting push')
if update_titles:
click.echo('\nSending masher.start fedmsg')
# Because we're a script, we want to send to the fedmsg-relay,
# that's why we say active=True
bodhi.server.notifications.init(active=True, cert_prefix=cert_prefix)
bodhi.server.notifications.publish(
topic='masher.start',
msg=dict(
updates=update_titles,
resume=resume,
agent=username,
),
force=True,
)
if __name__ == '__main__':
push()