0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

1つ前のマイナーバージョンの最新パッチに更新する

Last updated at Posted at 2020-03-18

要件

  • major.minor.patch の semantic versioning に従っている
  • このスクリプトを適切な頻度で実行する

スクリプト

#!/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]])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?