Patch Jenkins and delivery agent Java runtimes
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
"""Targeted The-Ocean Java update; run as root after reviewing this file.
|
||||
|
||||
prepare builds and smoke-tests images without restarting services. apply
|
||||
requires those prepared images and refuses active/queued Jenkins work.
|
||||
Rollback copies and selected-field evidence remain outside the repository.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
ROOT = Path('/root/ssw590-java-patch-20261123')
|
||||
JAVA_IMAGE = ('eclipse-temurin:21.0.13.1_2-jdk-jammy@'
|
||||
'sha256:4b3d0998ee65462bd86ff060c87119b3575294cb3d0927372afa6f3708b0c8db')
|
||||
SERVICES = [
|
||||
dict(name='gitea-jenkins-1', service='jenkins',
|
||||
directory='/root/gitea', compose='docker-compose.yml',
|
||||
image='ssw590-jenkins:2.580.1-java21.0.13.1-2'),
|
||||
dict(name='ssw590-delivery-agent-1', service='agent',
|
||||
directory='/opt/ssw590-delivery-agent', compose='compose.droplet.yaml',
|
||||
image='ssw590-delivery-agent:20261114-java21.0.13.1-2'),
|
||||
]
|
||||
|
||||
|
||||
def run(*args, capture=True, **kwargs):
|
||||
result = subprocess.run(args, check=True, text=True,
|
||||
stdout=subprocess.PIPE if capture else None,
|
||||
stderr=subprocess.STDOUT, **kwargs)
|
||||
return result.stdout.strip() if capture else ''
|
||||
|
||||
|
||||
def inspect(name):
|
||||
# Never serialize full inspect output: it can contain secret environment values.
|
||||
obj = json.loads(run('docker', 'inspect', name))[0]
|
||||
return dict(name=name, image=obj['Image'], reference=obj['Config']['Image'],
|
||||
running=obj['State']['Running'], oom=obj['State']['OOMKilled'],
|
||||
restarts=obj['RestartCount'],
|
||||
mounts=[dict(type=m['Type'], source=m['Source'],
|
||||
destination=m['Destination'], rw=m['RW'])
|
||||
for m in obj['Mounts']])
|
||||
|
||||
|
||||
def assert_idle():
|
||||
home = Path(run('docker', 'volume', 'inspect', '-f', '{{.Mountpoint}}',
|
||||
'gitea_jenkins-home'))
|
||||
active = []
|
||||
for path in (home / 'jobs').rglob('build.xml'):
|
||||
tree = ET.parse(path).getroot()
|
||||
if tree.find('result') is None:
|
||||
active.append(str(path.relative_to(home)))
|
||||
assert not active, 'Refusing restart: unfinished Jenkins builds: ' + repr(active)
|
||||
queue = home / 'queue.xml'
|
||||
if queue.exists():
|
||||
tree = ET.parse(queue).getroot()
|
||||
assert not list(tree.iter('item')), 'Refusing restart: Jenkins queue is nonempty'
|
||||
for items in tree.iter('items'):
|
||||
assert not list(items), 'Refusing restart: Jenkins queue contains work'
|
||||
|
||||
|
||||
def smoke(image):
|
||||
result = run('docker', 'run', '--rm', '--pull', 'never', '--network', 'none',
|
||||
'--read-only', '--cap-drop', 'ALL', '--security-opt',
|
||||
'no-new-privileges', '--entrypoint', 'java', image, '-version')
|
||||
assert '21.0.13.1' in result and '+2' in result, result
|
||||
return result
|
||||
|
||||
|
||||
def compose(spec, *args):
|
||||
return run('docker', 'compose', '-f', spec['compose'], *args,
|
||||
cwd=spec['directory'])
|
||||
|
||||
|
||||
def prepare():
|
||||
ROOT.mkdir(mode=0o700, exist_ok=True)
|
||||
os.chmod(ROOT, 0o700)
|
||||
before_path = ROOT / 'before.json'
|
||||
if before_path.exists():
|
||||
before = json.loads(before_path.read_text())
|
||||
assert [inspect(s['name'])['image'] for s in SERVICES] == [
|
||||
s['image'] for s in before], 'Current images differ from original checkpoint'
|
||||
else:
|
||||
before = [inspect(s['name']) for s in SERVICES]
|
||||
before_path.write_text(json.dumps(before, indent=2) + '\n')
|
||||
for spec in SERVICES:
|
||||
shutil.copy2(Path(spec['directory']) / spec['compose'],
|
||||
ROOT / (spec['service'] + '-compose.before'))
|
||||
assert_idle()
|
||||
run('docker', 'pull', JAVA_IMAGE, capture=False)
|
||||
smoke(JAVA_IMAGE)
|
||||
env = dict(os.environ, DOCKER_BUILDKIT='0')
|
||||
# No application state, Compose backup or evidence enters the build context.
|
||||
context = ROOT / 'empty-context'
|
||||
context.mkdir(exist_ok=True)
|
||||
built = []
|
||||
for spec, old in zip(SERVICES, before):
|
||||
rollback = 'ssw590-java-rollback:' + spec['service'] + '-20261123'
|
||||
run('docker', 'tag', old['image'], rollback)
|
||||
run('docker', 'build', '--pull=false', '--memory', '512m',
|
||||
'--cpu-quota', '50000', '--build-arg', 'BASE_IMAGE=' + rollback,
|
||||
'-t', spec['image'], '-f', str(ROOT / 'Dockerfile'), str(context),
|
||||
env=env, capture=False)
|
||||
built.append(dict(name=spec['name'], reference=spec['image'],
|
||||
image=run('docker', 'image', 'inspect', '-f', '{{.Id}}', spec['image']),
|
||||
java=smoke(spec['image']), rollback=rollback))
|
||||
(ROOT / 'prepared.json').write_text(json.dumps(built, indent=2) + '\n')
|
||||
print(json.dumps(built, indent=2))
|
||||
|
||||
|
||||
def apply():
|
||||
before = json.loads((ROOT / 'before.json').read_text())
|
||||
prepared = json.loads((ROOT / 'prepared.json').read_text())
|
||||
assert_idle()
|
||||
for spec, old, built in zip(SERVICES, before, prepared):
|
||||
assert inspect(spec['name'])['image'] == old['image'], 'Unexpected current image'
|
||||
assert run('docker', 'image', 'inspect', '-f', '{{.Id}}', spec['image']) == built['image']
|
||||
path = Path(spec['directory']) / spec['compose']
|
||||
assert path.read_bytes() == (ROOT / (spec['service'] + '-compose.before')).read_bytes(), 'Compose changed after preparation'
|
||||
source = path.read_text()
|
||||
needle = 'image: ' + old['reference']
|
||||
assert source.count(needle) == 1, 'Expected exactly one original image selector'
|
||||
path.write_text(source.replace(needle, 'image: ' + spec['image']))
|
||||
compose(spec, 'config', '--quiet')
|
||||
# Stop the idle agent first so no delivery job can start during maintenance.
|
||||
compose(SERVICES[1], 'stop', '-t', '30', 'agent')
|
||||
compose(SERVICES[0], 'up', '-d', '--no-deps', '--pull', 'never', 'jenkins')
|
||||
for _ in range(60):
|
||||
try:
|
||||
code = run('docker', 'exec', SERVICES[0]['name'], 'curl', '-sS',
|
||||
'--max-time', '5', '-o', '/dev/null', '-w', '%{http_code}',
|
||||
'http://localhost:8080/login') if inspect(SERVICES[0]['name'])['running'] else '000'
|
||||
except subprocess.CalledProcessError:
|
||||
code = '000'
|
||||
if code == '200':
|
||||
break
|
||||
time.sleep(3)
|
||||
else:
|
||||
raise RuntimeError('Controller did not return HTTP 200; retain checkpoint and investigate')
|
||||
compose(SERVICES[1], 'up', '-d', '--no-deps', '--pull', 'never', 'agent')
|
||||
after = []
|
||||
for spec, old in zip(SERVICES, before):
|
||||
now = inspect(spec['name'])
|
||||
assert now['running'] and not now['oom']
|
||||
assert now['mounts'] == old['mounts'], 'Persistent mounts changed'
|
||||
now['java'] = run('docker', 'exec', spec['name'], 'java', '-version')
|
||||
assert '21.0.13.1' in now['java'] and '+2' in now['java']
|
||||
after.append(now)
|
||||
(ROOT / 'after.json').write_text(json.dumps(after, indent=2) + '\n')
|
||||
print(json.dumps(after, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=['prepare', 'apply'])
|
||||
args = parser.parse_args()
|
||||
assert os.geteuid() == 0, 'Run as root on the authorized host'
|
||||
{'prepare': prepare, 'apply': apply}[args.action]()
|
||||
Reference in New Issue
Block a user