Browse Source

partial upgrade script

master
kshitizsaini 2 years ago
parent
commit
3d5dd1d530
1 changed files with 317 additions and 0 deletions
  1. +317
    -0
      upgradeScript.py

+ 317
- 0
upgradeScript.py View File

@@ -0,0 +1,317 @@
# -*- coding: utf-8 -*-
# Importing the required dependencies
import os
import sys
import time
from os.path import exists as file_exists


# Checking that all the operations are being run by root user, in case of any other user, quit the script and ask the user to use superuser for the same.
if os.getuid() != 0:
print("Operation not permitted!")
print("Use SuperUser to run the script!")
sys.exit(-1)
else:
print("SuperUser access granted, script access granted.")
print("\n")
time.sleep(3)

# Creating a directory for future use to store all the logs.
if(os.path.isdir("/osUpgradeEl7/")):
pass
else:
os.makedirs("/osUpgradeEl7/")
os.chdir("/osUpgradeEl7/")

# Statefile for idempotence of the upgrade script so that all the occured operations need not to be repeated again.
if (file_exists("/osUpgradeEl7/stateFile")):
# If statefile exists, open the stateFile and read the existing state so that the previous steps are not happening again.
stateFile = open("/osUpgradeEl7/stateFile", "r")
state = stateFile.readline()
stateFile.close()
else:
# If no statefile exists, then create a new stateFile and start the upgrade process.
stateFile = open("/osUpgradeEl7/stateFile", "w")
state = "0"
stateFile.write(state)
stateFile.close()
os.system("echo \"python /osUpgradeEl7/upgradeScript.py\" >> /etc/rc.d/rc.local")
time.sleep(3)

# Checking if the OS to be upgraded is supported by the script or not.
if (file_exists("/etc/centos-release")):
# Checking for the CentOS or AmeyOS distribution.
releaseFile = open("/etc/centos-release", "r")
release = releaseFile.readline()
releaseFile.close()
if (" 7." in release):
# Checking if the release is EL7, if yes then continue.
print("Starting the upgrade process, the OS is supported")
basePackageListOS = ["", "lvm2", "grub2-common", "grub2", "setup", "openssh-server", "basesystem", "lshw", "ncurses-base", "audit", "tzdata", "kernel", "glibc-common", "irqbalance", "nspr", "rsyslog", "libstdc++", "biosdevname", "bash", "dracut-config-rescue", "pcre", "sg3_utils", "zlib", "man-db", "xz-libs", "sudo", "libuuid", "xfsprogs", "bzip2-libs", "kernel-tools", "chkconfig", "iwl2030-firmware", "readline", "iwl7260-firmware", "gawk", "iwl4965-firmware", "libattr", "iwl135-firmware", "libcap", "ivtv-firmware", "audit-libs", "iwl6000-firmware", "libffi", "iwl6000g2b-firmware", "libxml2", "iwl6000g2a-firmware", "expat", "iwl1000-firmware", "lua", "which", "libnl3", "file", "libaio", "nss-softokn", "libassuan", "e2fsprogs-libs", "libgomp", "libidn", "jansson", "sysvinit-tools", "tcp_wrappers-libs", "gdbm", "ethtool", "pciutils-libs", "newt", "iptables", "less", "device-mapper-persistent-data", "ipset", "vim-minimal", "bc", "pinentry", "kmod-libs", "libpng", "libselinux-utils", "ncurses", "gmp", "kernel-tools-libs", "libverto", "libsemanage", "p11-kit-trust", "coreutils", "openssl-libs", "python-libs", "shadow-utils", "cracklib", "libmount", "shared-mime-info", "cracklib-dicts", "pam", "gettext-libs", "pkgconfig", "gobject-introspection", "libutempter", "pyliblzma", "python-schedutils", "python-configobj", "libselinux-python", "pyxattr", "mariadb-libs", "openssl", "fipscheck", "nss-pem", "nss-sysinit", "libcurl", "rpm-libs", "openldap", "python-pycurl", "binutils", "alsa-lib", "hardlink", "libdaemon", "dmidecode", "libestr", "libseccomp", "json-c", "libsmartcols", "device-mapper", "util-linux", "cryptsetup-libs", "kmod", "systemd-libs", "systemd", "elfutils-default-yama-scope", "polkit-pkla-compat", "initscripts", "device-mapper-event-libs", "policycoreutils", "os-prober", "cronie-anacron", "crontabs", "NetworkManager-libnm", "dhcp-common", "dracut-network", "grub2-pc", "libdrm", "device-mapper-event", "wpa_supplicant", "ebtables", "alsa-firmware", "teamd", "dbus-python", "python-firewall", "plymouth-scripts", "python-pyudev", "pth", "rpm-build-libs", "gpgme", "yum-plugin-fastestmirror", "kbd-misc", "firewalld", "tuned", "NetworkManager-tui", "libgcc", "selinux-policy-targeted", "centos-release", "kexec-tools", "filesystem", "openssh-clients", "grub2-pc-modules", "authconfig", "kbd-legacy", "postfix", "nss-softokn-freebl", "chrony", "glibc", "qemu-guest-agent", "nss-util", "microcode_ctl", "ncurses-libs", "aic94xx-firmware", "libsepol", "parted", "libselinux", "iprutils", "info", "passwd", "libcom_err", "btrfs-progs", "sed", "e2fsprogs", "popt", "libsysfs", "libdb", "iwl105-firmware", "grep", "rootfiles", "elfutils-libelf", "iwl5000-firmware", "libacl", "iwl6050-firmware", "libcap-ng", "iwl3945-firmware", "libgpg-error", "iwl3160-firmware", "libgcrypt", "iwl100-firmware", "cpio", "iwl5150-firmware", "sqlite", "iwl2000-firmware", "findutils", "diffutils", "file-libs", "libmnl", "libnl3-cli", "p11-kit", "groff-base", "xz", "libunistring", "libedit", "libnfnetlink", "hostname", "lzo", "lz4", "keyutils-libs", "slang", "libnetfilter_conntrack", "iproute", "libteam", "ipset-libs", "acl", "tar", "libdb-utils", "libss", "make", "freetype", "linux-firmware", "mozjs17", "snappy", "libndp", "ustr", "libtasn1", "ca-certificates", "krb5-libs", "libblkid", "python", "gzip", "python-decorator", "glib2", "libcroco", "libpwquality", "cyrus-sasl-lib", "gettext", "yum-metadata-parser", "python-gobject-base", "python-iniparse", "newt-python", "python-perf", "python-linux-procfs", "python-slip", "grubby", "bind-export-libs", "libssh2", "fipscheck-lib", "nss", "nss-tools", "curl", "rpm", "libuser", "python-urlgrabber", "centos-logos", "logrotate", "libfastjson", "qrencode-libs", "libpipeline", "lsscsi", "numactl-libs", "sg3_utils-libs", "procps-ng", "kpartx", "device-mapper-libs", "dracut", "elfutils-libs", "dbus-libs", "dbus", "polkit", "iputils", "systemd-sysv", "grub2-tools-minimal", "hwdata", "grub2-tools", "cronie", "dhcp-libs", "openssh", "dhclient", "grub2-tools-extra", "libpciaccess", "selinux-policy", "lvm2-libs", "NetworkManager", "fxload", "alsa-tools-firmware", "dbus-glib", "python-slip-dbus", "plymouth-core-libs", "plymouth", "virt-what", "gnupg2", "rpm-python", "pygpgme", "yum", "firewalld-filesystem", "kbd", "NetworkManager-team"]
operatingEnvironment = "centos"
time.sleep(3)
else:
# If the release is not EL7, the exit the process.
print("ERROR...! This version of CentOS / AmeyoOS is not supported.")
sys.exit(-1)
elif (file_exists("/etc/oracle-release")):
# Checking for the Oracle Linux distribution.
releaseFile = open("/etc/oracle-release", "r")
release = releaseFile.readline()
releaseFile.close()
if(" 7." in release):
# Checking if the release is EL7, if yes then continue.
print("Starting the upgrade process, the OS is supported")
basePackageListOS = ["", "usermode", "redhat-release-server", "gnupg2", "setup", "rpm-python", "basesystem", "pygpgme", "kbd-legacy", "yum-utils", "tzdata", "rhn-client-tools", "glibc-common", "rhnsd", "nspr", "yum-plugin-ulninfo", "libstdc++", "firewalld-filesystem", "bash", "kbd", "pcre", "tuned", "zlib", "NetworkManager-tui", "xz-libs", "selinux-policy-targeted", "sed", "kexec-tools", "bzip2-libs", "openssh-server", "chkconfig", "kernel", "readline", "postfix", "libxml2", "pciutils", "elfutils-libelf", "microcode_ctl", "libattr", "chrony", "libacl", "aic94xx-firmware", "libcap-ng", "biosdevname", "libgcrypt", "parted", "expat", "sudo", "lua", "btrfs-progs", "findutils", "kernel-tools", "libnl3", "iprutils", "file", "uname26", "libaio", "iwl135-firmware", "libnl3-cli", "iwl2000-firmware", "libassuan", "NetworkManager-config-server", "groff-base", "iwl6000-firmware", "xz", "iwl6000g2b-firmware", "libunistring", "iwl4965-firmware", "oraclelinux-release", "iwl1000-firmware", "sysvinit-tools", "iwl2030-firmware", "libnfnetlink", "rootfiles", "newt", "lz4", "ethtool", "gdbm", "numactl-libs", "iptables", "less", "device-mapper-persistent-data", "ipset", "vim-minimal", "bc", "pinentry", "make", "freetype", "ncurses", "gmp", "lsscsi", "kernel-tools-libs", "libdaemon", "ustr", "libtasn1", "ca-certificates", "coreutils", "python-libs", "libblkid", "gzip", "libmount", "shared-mime-info", "newt-python", "cracklib-dicts", "pam", "python-iniparse", "cyrus-sasl-lib", "oracle-logos", "python-dmidecode", "gettext", "pygobject2", "gobject-introspection", "libutempter", "python-kitchen", "python-configobj", "python-ethtool", "rhnlib", "python-schedutils", "python-slip", "python-perf", "openssl", "nss", "nss-tools", "alsa-lib", "mariadb-libs", "fipscheck-lib", "libcurl", "rpm-libs", "openldap", "passwd", "python-urlgrabber", "libndp", "dmidecode", "libestr", "libseccomp", "json-c", "device-mapper", "util-linux", "cryptsetup-libs", "kmod", "systemd-libs", "systemd", "elfutils-default-yama-scope", "polkit", "systemd-sysv", "initscripts", "device-mapper-event-libs", "os-prober", "cronie-anacron", "crontabs", "dhcp-libs", "openssh", "dhclient", "grub2-tools-extra", "selinux-policy", "lvm2-libs", "NetworkManager", "libdrm", "ebtables", "alsa-firmware", "teamd", "dbus-python", "python-firewall", "plymouth-scripts", "python-pyudev", "python-gudev", "libgcc", "pth", "grub2-common", "rpm-build-libs", "filesystem", "gpgme", "grub2-pc-modules", "yum", "ncurses-base", "oraclelinux-release-el7", "nss-softokn-freebl", "rhn-setup", "glibc", "rhn-check", "nss-util", "yum-rhn-plugin", "ncurses-libs", "kbd-misc", "libsepol", "firewalld", "libselinux", "NetworkManager-team", "info", "lvm2", "libcom_err", "grub2", "libuuid", "openssh-clients", "popt", "authconfig", "libdb", "kernel-uek", "grep", "audit", "gawk", "lshw", "libffi", "qemu-guest-agent", "libcap", "irqbalance", "libgpg-error", "rsyslog", "audit-libs", "dracut-config-rescue", "cpio", "man-db", "sqlite", "e2fsprogs", "which", "xfsprogs", "diffutils", "sg3_utils", "file-libs", "libsysfs", "libmnl", "ivtv-firmware", "pciutils-libs", "iwl5150-firmware", "nss-softokn", "iwl3160-firmware", "p11-kit", "iwl7260-firmware", "e2fsprogs-libs", "iwl6050-firmware", "libgomp", "iwl105-firmware", "libidn", "iwl6000g2a-firmware", "libedit", "iwl3945-firmware", "jansson", "iwl5000-firmware", "slang", "iwl100-firmware", "tcp_wrappers-libs", "lzo", "hostname", "keyutils-libs", "libnetfilter_conntrack", "iproute", "libteam", "ipset-libs", "acl", "tar", "libdb-utils", "libss", "libpng", "libselinux-utils", "mozjs17", "snappy", "sg3_utils-libs", "libnl", "libverto", "libsemanage", "p11-kit-trust", "openssl-libs", "krb5-libs", "python", "shadow-utils", "cracklib", "glib2", "python-decorator", "libcroco", "libpwquality", "grubby", "libxml2-python", "linux-firmware", "kmod-libs", "gettext-libs", "pkgconfig", "yum-metadata-parser", "python-gobject-base", "python-chardet", "m2crypto", "pyliblzma", "pyOpenSSL", "python-linux-procfs", "libselinux-python", "pyxattr", "bind-export-libs", "nss-pem", "nss-sysinit", "binutils", "logrotate", "fipscheck", "libssh2", "curl", "rpm", "libuser", "python-pycurl", "hardlink", "qrencode-libs", "libpipeline", "libfastjson", "libsmartcols", "procps-ng", "kpartx", "device-mapper-libs", "dracut", "elfutils-libs", "dbus-libs", "dbus", "hwdata", "polkit-pkla-compat", "iputils", "grub2-tools-minimal", "policycoreutils", "grub2-tools", "cronie", "NetworkManager-libnm", "virt-what", "dhcp-common", "dracut-network", "grub2-pc", "device-mapper-event", "wpa_supplicant", "libpciaccess", "python-hwdata", "fxload", "alsa-tools-firmware", "dbus-glib", "python-slip-dbus", "plymouth-core-libs", "plymouth", "libgudev1", "yum"]
operatingEnvironment = "oracle"
time.sleep(3)
else:
# If the release is not EL7, the exit the process.
print("ERROR...! This version of Oracle Linux is not supported.")
sys.exit(-1)
else:
# Checking for any other Linux distribution.
print("ERROR...! This version of Linux is not supported.")
sys.exit(-1)

if (int(state) <= 0):
# Fetching the list of installed packages on the system and storing them in the list.
os.system("rpm -qa --qf '%{NAME},' > /tmp/pkgFile")
packageFile = open("/tmp/pkgFile", "r")
packagesInstalled = packageFile.read()
packagesInstalledList = packagesInstalled.split(",")
# Updating the state in the stateFile.
state = "1"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 1):
# Fetching the list of packages that are not present in the baseOS Packages and parsing the command to remove it.
remove = False
command = "yum -y remove"
for package in packagesInstalledList:
if package not in basePackageListOS:
remove = True
command = command + " " + package
# Updating the state in the stateFile.
state = "2"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 2):
# Removes the extra packages from the system to make it revert to the minimal install.
if (remove):
os.system(command)
else:
print("No package removal is required")
# Updating the state in the stateFile.
state = "3"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 3):
# Restoring the yum repositories for future use.
repoDir = os.getcwd()
# Deleting the existing yum repositories.
os.system("rm -rvf /etc/yum.repos.d/*")
os.chdir("/etc/yum.repos.d/")
if (operatingEnvironment == "centos"):
# Restoring the centos repositories
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Base.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-CR.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Debuginfo.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Media.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Sources.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Vault.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-fasttrack.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-x86_64-kernel.repo")
elif (operatingEnvironment == "oracle"):
# Restoring the oracle repositories
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/oracle-repos/oracle-linux-ol7.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/oracle-repos/uek-ol7.repo")
os.system("curl -O https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/oracle-repos/virt-ol7.repo")
os.chdir(repoDir)
# Updating the state in the stateFile.
state = "4"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (operatingEnvironment == "centos"):
if (int(state) <= 4):
# Downloading and installing the epel & yum-utils packages
os.system("yum -y install epel-release yum-utils")
# Updating the state in the stateFile.
state = "5"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 5):
# Downloading and working on the rpmconf package.
os.system("yum -y install rpmconf")
os.system("rpmconf -a")
# Updating the state in the stateFile.
state = "6"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 6):
# Removing the conflicting packages that were marked as orphans or leaves.
os.system("yum -y remove libsysfs-2.1.0-16.el7.x86_64")
# Updating the state in the stateFile.
state = "7"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 7):
# Running the package-cleanup to list any extra conflicting packages.
os.system("package-cleanup --leaves")
os.system("package-cleanup --orphans")
# Updating the state in the stateFile.
state = "8"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)
if (int(state) <= 8):
# Installs the dnf package manager for package management.
os.system("yum -y install dnf")
# Updating the state in the stateFile.
state = "9"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 9):
# Removing the yum package manager and yum configuration files.
os.system("dnf -y remove yum yum-metadata-parser")
os.system("rm -Rvf /etc/yum")
# Updating the state in the stateFile.
state = "10"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 10):
# Updating all the packages to latest for a smoother upgrade.
os.system("dnf makecache")
os.system("dnf -y upgrade")
# Updating the state in the stateFile.
state = "11"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 11):
# Installing the centos, epel repos for el8 upgrade.
os.system("dnf -y install https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/{centos-linux-repos-8-2.el8.noarch.rpm,centos-linux-release-8.4-1.2105.el8.noarch.rpm,centos-gpg-keys-8-2.el8.noarch.rpm}")
os.system("dnf -y upgrade https://dms-git.ameyo.net:8265/kshitizsaini113/upgrade-el7-el8/raw/branch/master/epel-release-latest-8.noarch.rpm")
# Updating the state in the stateFile.
state = "12"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 12):
# Removing the existing kernel and sysvinit-tools packages as tey will conflict with the upgraded packages.
os.system("rpm -ev `rpm -q kernel`")
os.system("rpm -ev --nodeps sysvinit-tools")
# Updating the state in the stateFile.
state = "13"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 13):
# Removing the packages conflicting for the release-server upgrade sync
os.system("dnf -y remove iprutils-2.4.17.1-3.el7_7.x86_64 initscripts-9.49.53-1.el7_9.1.x86_64 python36-rpmconf-1.0.22-1.el7.noarch")
# Updating the state in the stateFile.
state = "14"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)
if (int(state) <= 14):
# Upgrading the release-server to 8 for osupgrade.
os.system("dnf -y --releasever=8 --allowerasing --setopt=deltarpm=false distro-sync")
# Updating the state in the stateFile.
state = "15"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 15):
# Installing the kernel-core packages for el8 distribution.
os.system("dnf -y install kernel-core")
# Updating the state in the stateFile.
state = "16"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)

if (int(state) <= 16):
# Updating the minimal installation group and the core group for el8 distribution.
os.system("rm -rvf /etc/yum/protected.d")
os.system("rm -rvf /etc/yum/vars")
os.system("dnf -y groupupdate \"Core\" \"Minimal Install\"")
# Updating the state in the stateFile.
state = "100"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)
os.system("reboot")

elif (operatingEnvironment == "oracle"):
print("Oracle")

if (int(state) <= 100):
# Running the alma linux deploy script.
os.system("curl -O https://raw.githubusercontent.com/AlmaLinux/almalinux-deploy/master/almalinux-deploy.sh")
os.system("bash almalinux-deploy.sh")
time.sleep(3)
# Updating the state in the stateFile.
state = "101"
stateFile = open("/osUpgradeEl7/stateFile", "w")
stateFile.write(state)
stateFile.close()
time.sleep(3)
os.system("reboot")

if (int(state) <= 101):
os.system("dnf remove openssh*")
os.system("dnf install openssh*")
os.system("systemctl enable --now sshd")

if (int(state) <= 102):
os.system("dnf config-manager --enable ha powertools")
os.system("dnf install -y yum-utils")
os.system("yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo")
os.system("dnf -qy module disable postgresql")
os.system("dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm")
os.system("rpm -ivh http://repo.okay.com.mx/centos/8/x86_64/release/okay-release-1-5.el8.noarch.rpm")
os.system("dnf -y install http://rpms.remirepo.net/enterprise/8/remi/x86_64/remi-release-8.4-1.el8.remi.noarch.rpm")
os.system("dnf -y install https://forensics.cert.org/cert-forensics-tools-release-el8.rpm")
os.system("dnf config-manager --enable remi")

Loading…
Cancel
Save