Last active
January 1, 2020 16:28
-
-
Save stefancrain/d1dd0ce46b77e1bb2fb12b92d48509a5 to your computer and use it in GitHub Desktop.
Automatically upgrade pinned packages in a Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# A python script to update alpine requirements in docker | |
# | |
import glob | |
import os | |
import re | |
import sys | |
from urllib.request import Request, urlopen | |
from bs4 import BeautifulSoup | |
import massedit | |
filename = "Dockerfile" | |
if os.path.exists(filename): | |
print("--------------") | |
textfile = open(filename, "r") | |
DockerfileContent = textfile.read() | |
textfile.close() | |
# finding the alpine version | |
# currently compatible with these formats: | |
# - python:x.y.z-alpinex.y.z | |
# - alpine:x.y.z | |
# - golang:x.y.z-alpinex.y.z | |
alpine_search = re.search("FROM.*alpine(:|)(.*)", DockerfileContent) | |
alpine = alpine_search.group(2) | |
print("Alpine Version: ", alpine) | |
# find all locked apks installed | |
APKList = re.findall("(([a-z-]+)==[a-z0-9.-]+)", DockerfileContent) | |
for version, apk in APKList: | |
print("--------------") | |
print("Upgrading: ", apk) | |
page = (urlopen(Request(f"https://pkgs.alpinelinux.org/packages?name=" + apk + "&branch=v" + alpine + "&arch=x86_64")).read().decode("ascii", "ignore")) | |
soup = BeautifulSoup(page, "html5lib") | |
versions = soup.find("td", class_="version").find(text=True) | |
date = soup.find("td", class_="bdate").find(text=True) | |
print(apk, versions, date) | |
massedit.edit_files([filename],[ "re.sub(r'"+ apk+"=[a-z0-9.-:-+]+', '"+apk+"="+versions+"', line)"] ,dry_run=True,) | |
massedit.edit_files([filename],[ "re.sub(r'"+ apk+"=[a-z0-9.-:-+]+', '"+apk+"="+versions+"', line)"] ,dry_run=False,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# A python script to update debian requirements in docker | |
# | |
import glob | |
import os | |
import re | |
import sys | |
from urllib.request import Request, urlopen | |
from bs4 import BeautifulSoup | |
import massedit | |
filename = "Dockerfile" | |
os_version = "buster" | |
if os.path.exists(filename): | |
print("--------------") | |
textfile = open(filename, "r") | |
DockerfileContent = textfile.read() | |
textfile.close() | |
# maybe override os_version with detected os | |
# problem : stable / slim are not in web ui | |
os_search = re.search("FROM.*debian(:|)(.*)", DockerfileContent) | |
os = os_search.group(2) | |
print("os Version: ", os, os_version) | |
# find all locked pkgs installed | |
packageList = re.findall("(([a-z-]+)=[a-z0-9.-]+)", DockerfileContent) | |
for version, pkg in packageList: | |
print("--------------") | |
print("Upgrading: ", pkg) | |
page = ( urlopen(Request(f"https://packages.debian.org/" + os_version + "/" + pkg)).read().decode("ascii", "ignore")) | |
soup = BeautifulSoup(page, "html5lib") | |
# Get the list of versions | |
versions = soup.find("h1").find(text=True) | |
version = re.findall("\(([a-z0-9.-:-+]+).*\)", versions) | |
try: | |
print(pkg, version[0]) | |
massedit.edit_files([filename],[ "re.sub(r'"+ pkg+"=[a-z0-9.-:-+]+', '"+pkg+"="+version[0]+"', line)"] ,dry_run=True,) | |
massedit.edit_files([filename],[ "re.sub(r'"+ pkg+"=[a-z0-9.-:-+]+', '"+pkg+"="+version[0]+"', line)"] ,dry_run=False,) | |
except IndexError: | |
print ('unable to find version') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html5lib==1.0.1 | |
beautifulsoup4==4.8.2 | |
massedit==0.68.6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment