Run factory announcer on gocd

Ported the announcer to python3 and made the config parsing a little
easier. The current state is no longer stored as filename, but in a
config file
This commit is contained in:
Stephan Kulow
2019-05-31 12:06:25 +02:00
parent c354f8c83e
commit cdd6dc4f06
7 changed files with 174 additions and 103 deletions

View File

@@ -1,8 +1,8 @@
#!/usr/bin/python
#!/usr/bin/python3
import httplib
import http.client
import re
from urlparse import urlparse, urljoin
from urllib.parse import urlparse, urljoin
import smtplib
from email.mime.text import MIMEText
import os
@@ -21,32 +21,16 @@ config_defaults = {
'sender': 'noreply@opensuse.org',
'to': 'opensuse-factory@opensuse.org',
'relay': 'relay.suse.de',
'url' : "http://download.opensuse.org/tumbleweed/iso/",
'iso' : "openSUSE-Tumbleweed-DVD-x86_64-Current.iso",
'name' : 'factory-announcer',
'subject' : 'New Tumbleweed snapshot {version} released!',
'changesfile' : "Changes.{version}.txt",
'bodytemplate' : """
Please note that this mail was generated by a script.
The described changes are computed based on the x86_64 DVD.
The full online repo contains too many changes to be listed here.
Please check the known defects of this snapshot before upgrading:
https://openqa.opensuse.org/tests/overview?distri=opensuse&groupid=1&version=Tumbleweed&build={version}
Please do not reply to this email to report issues, rather file a bug
on bugzilla.opensuse.org. For more information on filing bugs please
see https://en.opensuse.org/openSUSE:Submitting_bug_reports
{text}
""",
}
def _load_config(handle = None):
d = config_defaults
y = yaml.safe_load(handle) if handle is not None else {}
return namedtuple('Config', sorted(d.keys()))(*[ y.get(p, d[p]) for p in sorted(d.keys()) ])
keys = set(d.keys()) | set(y.keys())
for key in keys:
y[key] = y.get(key, d.get(key, None))
return y
parser = argparse.ArgumentParser(description="Announce new snapshots")
parser.add_argument("--dry", action="store_true", help="dry run")
@@ -58,7 +42,7 @@ parser.add_argument("--relay", metavar="RELAY", help="SMTP relay server address"
parser.add_argument("--version", metavar="VERSION", help="announce specific version")
parser.add_argument("--config", metavar="FILE", type=argparse.FileType(), help="YAML config file to override defaults")
parser.add_argument("--dump-config", action="store_true", help="dump built in YAML config")
parser.add_argument("--state-file", metavar="STATE_FILE", help="Yaml config of previously announced", required=True)
options = parser.parse_args()
# Set logging configuration
@@ -66,30 +50,35 @@ logging.basicConfig(level=logging.DEBUG if options.debug
else logging.INFO,
format='%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s')
if options.dump_config:
print(yaml.dump(config_defaults, default_flow_style=False))
sys.exit(0)
state = {}
try:
with open(options.state_file, 'r') as file:
state = yaml.safe_load(file)
if state is None:
state = {}
except IOError:
pass
config = _load_config(options.config)
if not options.sender:
options.sender = config.sender
if not options.to:
options.to = config.to
if not options.relay:
options.relay = config.relay
if options.sender:
config['sender'] = options.sender
if options.to:
config['to'] = options.to
if options.relay:
config['relay'] = options.relay
if not options.sender or not options.to or not options.relay:
if options.dump_config:
print(yaml.dump(config, default_flow_style=False))
sys.exit(0)
if not config['sender'] or not config['to'] or not config['relay']:
logger.error("need to specify --from and --to and --relay")
sys.exit(1)
datadir = save_data_path('opensuse.org', config.name)
current_fn = os.path.join(datadir, "announcer-current-version")
if not options.version:
u = urlparse(urljoin(config.url, config.iso))
conn = httplib.HTTPConnection(u.hostname, 80)
u = urlparse(urljoin(config['url'], config['iso']))
conn = http.client.HTTPConnection(u.hostname, 80)
conn.request('HEAD', u.path)
res = conn.getresponse()
if res.status != 302:
@@ -108,39 +97,39 @@ if not options.version:
else:
version = options.version
if os.path.lexists(current_fn):
prev = os.readlink(current_fn)
if prev == version:
logger.debug("version unchanged, exit")
sys.exit(0)
if state.get(config['name'], None) == version:
logger.info("version unchanged, exit")
sys.exit(0)
u = urlparse(urljoin(config.url, config.changesfile.format(version=version)))
conn = httplib.HTTPConnection(u.hostname, 80)
url = urljoin(config['url'], config['changesfile'].format(version=version))
# take the safer route
url = url.replace('download.opensuse.org', 'downloadcontent.opensuse.org')
u = urlparse(url)
conn = http.client.HTTPConnection(u.hostname, 80)
conn.request('HEAD', u.path)
res = conn.getresponse()
if res.status == 302:
loc = res.getheader('location')
if loc is None:
raise Exception("empty location!")
u = urlparse(loc)
conn = httplib.HTTPConnection(u.hostname, 80)
conn = http.client.HTTPConnection(u.hostname, 80)
conn.request('GET', u.path)
res = conn.getresponse()
if res.status != 200:
raise Exception("http fail: %s %s" % (res.status, res.reason))
raise Exception("http %s fail: %s %s" % (u, res.status, res.reason))
txt = res.read()
if "====" not in txt:
txt = res.read().decode('latin1')
if '====' not in txt:
logger.error("no changes or file corrupt? not sending anything")
sys.exit(1)
msg = MIMEText(config.bodytemplate.format(version=version, text=txt))
msg['Subject'] = config.subject.format(version=version)
msg['From'] = options.sender
msg['To'] = options.to
msg['Mail-Followup-To'] = options.to
msg = MIMEText(config['bodytemplate'].format(version=version, text=txt))
msg['Subject'] = config['subject'].format(version=version)
msg['From'] = config['sender']
msg['To'] = config['to']
msg['Mail-Followup-To'] = config['to']
msg['Date'] = email.utils.formatdate(localtime=1)
msg['Message-ID'] = email.utils.make_msgid()
@@ -149,11 +138,10 @@ if options.dry:
print(msg.as_string())
else:
logger.info("announcing version {}".format(version))
s = smtplib.SMTP(options.relay)
s.sendmail(options.sender, [msg['To']], msg.as_string())
s = smtplib.SMTP(config['relay'])
s.send_message(msg)
s.quit()
tmpfn = os.path.join(datadir, ".announcer-current-version")
os.symlink(version, tmpfn)
os.rename(tmpfn, current_fn)
state[config['name']] = version
with open(options.state_file, 'w') as file:
yaml.dump(state, file)