#!/usr/bin/env python
import os
import re
import subprocess
def pick_versions(yum_list):
current = None
versions = {}
for line in yum_list.splitlines():
if line == 'Installed Packages':
is_current = True
continue
elif line == 'Available Packages':
is_current = False
continue
_, full_version, _ = line.split()
matched = re.match(r'(\d+)\.(\d+)\.(\d+).*', full_version)
numeric_version = tuple(int(matched.group(i)) for i in range(1, 4))
if is_current:
current = numeric_version
else:
versions[numeric_version] = full_version
return current, versions
def get_target(current, versions):
newer_versions = set(x[:2] for x in versions.keys() if current[:2] < x[:2])
if len(newer_versions) > 1:
target_version = sorted(newer_versions)[0] # avoid jump
else:
target_version = current[:2]
target_updates = [x for x in versions.keys() if x[:2] == target_version]
return max(target_updates) if target_updates else None
package_name = 'gitlab-ee'
os.putenv('LANG', 'C')
subprocess.call(['yum', 'check-update'], stdout=subprocess.PIPE)
yum_list = subprocess.check_output(['yum', '--showduplicates', '--quiet', 'list', package_name])
current, versions = pick_versions(yum_list)
target = get_target(current, versions)
if target is not None:
subprocess.call(['yum', 'update', '-y', package_name + '-' + versions[target]])