|
- # -*- coding: utf-8 -*-
- # Importing the required dependencies
- import os
- import sys
- import time
- import json
- from os.path import exists as file_exists
-
- class color:
- PURPLE = '\033[95m'
- CYAN = '\033[96m'
- DARKCYAN = '\033[36m'
- BLUE = '\033[94m'
- GREEN = '\033[92m'
- YELLOW = '\033[93m'
- RED = '\033[91m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- END = '\033[0m'
-
- # 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(color.BOLD + color.RED + "Operation not permitted!")
- print("Use SuperUser to run the script!" + color.END)
- sys.exit(-1)
- else:
- print(color.BOLD + color.GREEN + "SuperUser access granted, script access granted." + color.END)
-
- print("\n\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/")
- print(color.BOLD + color.GREEN + "Creating an os-upgrade directory" + color.END)
-
- print("\n\n")
- time.sleep(3)
-
- 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()
- print(color.BOLD + color.GREEN + "Current state is : " + state + color.END)
- stateFile.close()
- else:
- # If no statefile exists, then create a new stateFile and start the upgrade process.
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- print(color.BOLD + color.GREEN + "Statefile doesn't exists, creating a new one." + color.END)
- state = "0"
- print(color.BOLD + color.GREEN + "Current state is : " + state + color.END)
- stateFile.write(state)
- stateFile.close()
-
- print("\n\n")
- time.sleep(3)
-
- operatingEnvironment = ""
-
- # 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(color.BOLD + color.GREEN + "Starting the upgrade process, the OS is supported" + color.END)
- else:
- print(color.BOLD + color.RED + "ERROR...! This version of CentOS / AmeyoOS is not supported." + color.END)
- sys.exit(-1)
- else:
- print(color.BOLD + color.RED + "OS files have been tampered with, couldn't proceed further with the upgrade" + color.END)
- sys.exit(-1)
-
- print("\n")
- time.sleep(3)
-
- if (int(state) <= 0):
- # Updates the grub configuration file
- print(color.BOLD + color.GREEN + "Updating the grub configuration file." + color.END)
- os.system("rm -rvf /etc/grub2.cfg")
- os.system("grub2-mkconfig -o /boot/grub2/grub.cfg")
- os.system("ln -s /boot/grub2/grub.cfg /etc/grub2.cfg")
- print(color.BOLD + color.GREEN + "Grub menu updated successfully." + color.END)
- # Removing the old repos and installing the new repos
- print(color.BOLD + color.GREEN + "STEP 1 : Removing the yum repositories and restoring the required one." + color.END)
- os.system("rm -rvf /etc/yum.repos.d/*")
- os.chdir("/etc/yum.repos.d/")
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Base.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-CR.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Debuginfo.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Media.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Sources.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Vault.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-fasttrack.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-x86_64-kernel.repo") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- os.chdir("/osUpgradeEl7/")
- # Updating the state in the stateFile.
- state = "1"
- print(color.BOLD + color.GREEN + "Step-1 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 1):
- # Cleaning the metadata and updating the packages
- print(color.BOLD + color.GREEN + "STEP 2 : Cleaning the metadata and upgrading the required packages" + color.END)
- if(os.system("sudo yum clean all") != 0):
- print(color.BOLD + color.RED + "Couldn't perform the specified task, please re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("sudo yum clean metadata") != 0):
- print(color.BOLD + color.RED + "Couldn't perform the specified task, please re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("sudo yum install epel-release -y") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("sudo yum update -y") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- # Updating the state in the stateFile.
- state = "2"
- print(color.BOLD + color.GREEN + "Step-2 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 2):
- # Rebooting our system.
- state = "3"
- print(color.BOLD + color.GREEN + "Step-3, the system will REBOOT now." + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
- os.system("reboot")
-
- if (int(state) <= 3):
- # Downloading and installing the epel & yum-utils packages
- print(color.BOLD + color.GREEN + "STEP 4 : Installing the epel-release and yum-utils" + color.END)
- if(os.system("yum -y install epel-release yum-utils") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- # Updating the state in the stateFile.
- state = "4"
- print(color.BOLD + color.GREEN + "Step-4 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 4):
- # Installing the elevate repos and packages for upgrade.
- print(color.BOLD + color.GREEN + "STEP 5 : Installing the elevate repos and packages for upgrade" + color.END)
- if(os.system("sudo yum install -y http://repo.almalinux.org/elevate/elevate-release-latest-el7.noarch.rpm") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- if(os.system("sudo yum install -y leapp-upgrade leapp-data-rocky") != 0):
- print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- sys.exit(-1)
- # Updating the state in the stateFile.
- state = "5"
- print(color.BOLD + color.GREEN + "Step-5 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 5):
- # Running the preupgrade tool to generate the report.
- print(color.BOLD + color.GREEN + "STEP 6 : Running the preupgrade tool to generate report and fetch errors" + color.END)
- os.system("sudo leapp preupgrade")
- # Updating the state in the stateFile.
- state = "6"
- print(color.BOLD + color.GREEN + "Step-6 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 6):
- # Running commands from the json report generated by the preupgrade tool.
- print(color.BOLD + color.GREEN + "STEP 7 : Running commands from the json report generated by the preupgrade tool" + color.END)
- jsonFile = open('/var/log/leapp/leapp-report.json')
- jsonData = json.load(jsonFile)
- for entry in jsonData['entries']:
- for details in entry:
- if "detail" in details:
- if not (entry['detail'].get('remediations') is None):
- data = entry['detail']['remediations']
- for feild in data:
- if feild['type'] == "command":
- command = ""
- for partCommand in feild['context']:
- if partCommand == "sh" or partCommand == "-c":
- pass
- else:
- command += partCommand + " "
- os.system(command)
- # Updating the state in the stateFile.
- state = "7"
- print(color.BOLD + color.GREEN + "Step-7 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 7):
- # Performing some additional steps for the smooth upgrade.
- print(color.BOLD + color.GREEN + "STEP 8 : Performing some additional steps for the smooth upgrade." + color.END)
- os.system("echo PermitRootLogin yes | sudo tee -a /etc/ssh/sshd_config")
- os.system("sudo rmmod floppy")
- os.system("sudo rmmod pata_acpi")
- os.system("sudo rm -rvf /var/lib/pacemaker/cib/cib.xml")
- os.system("sudo rm -rvf /etc/corosync/corosync.conf")
- os.system("sudo sed -i 's/,hmac-ripemd160/ /g' /etc/ssh/sshd_config")
- os.system("sudo yum remove -y libwebp7 gd-last libzip5 python-psycopg2")
- # Updating the state in the stateFile.
- state = "8"
- print(color.BOLD + color.GREEN + "Step-8 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 8):
- # Performing the upgrade task.
- print(color.BOLD + color.GREEN + "STEP 9 : Performing the upgrade task." + color.END)
- os.system("sudo leapp upgrade")
- # Updating the state in the stateFile.
- state = "9"
- print(color.BOLD + color.GREEN + "Step-9 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- if (int(state) <= 9):
- # Updating the grub menu and restarting the system to boot into upgrade menu.
- print(color.BOLD + color.GREEN + "Updating the /etc/issue file." + color.END)
- with open("/etc/issue") as issueFile:
- lines = issueFile.readlines()
- lines[0] = "Ameyo OS 8 - Rocky"
- with open("/etc/issue", 'w') as issueFile:
- issueFile.writelines(lines)
- print(color.BOLD + color.RED + "STEP 10 : Restart the system for upgrade." + color.END)
- while(True):
- try:
- input = raw_input
- except NameError:
- pass
- rebootTask=input("Do you want to reboot to go to the upgrade menu (y/n) : ")
- rebootTask=rebootTask.lower()
- if((rebootTask == "y") or (rebootTask == "yes")):
- os.system("reboot")
- elif((rebootTask == "n") or (rebootTask == "no")):
- print("You need to manually reboot the system for the upgrade.")
- break
- else:
- pass
- # Updating the state in the stateFile.
- state = "10"
- print(color.BOLD + color.GREEN + "Step-10 completed, the modified state is : " + state + color.END)
- stateFile = open("/osUpgradeEl7/stateFile", "w")
- stateFile.write(state)
- stateFile.close()
- print("\n\n")
- time.sleep(3)
-
- # if (int(state) <= 10):
- # print(color.BOLD + color.GREEN + "STEP 11 : Performing the post-upgrade tasks." + color.END)
- # os.system("mkdir -p /etc/yum.repos.d.bkp/")
- # os.system("mv /etc/yum.repos.d/* /etc/yum.repos.d.bkp/")
- # os.chdir("/etc/yum.repos.d/")
- # if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/alma-repo/ameyoosalma8base.repo") != 0):
- # print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- # sys.exit(-1)
- # os.chdir("/osUpgradeEl7/")
- # if(os.system("dnf install -y yum-utils python2") !=0):
- # print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- # sys.exit(-1)
- # os.system("alternatives --set python /usr/bin/python2")
- # if(os.system("sudo yum update -y") !=0):
- # print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
- # sys.exit(-1)
- # os.system("sudo grub2-mkconfig -o /boot/grub2/grub.cfg")
- # # Updating the state in the stateFile.
- # state = "11"
- # print(color.BOLD + color.GREEN + "Step-11 completed, the modified state is : " + state + color.END)
- # stateFile = open("/osUpgradeEl7/stateFile", "w")
- # stateFile.write(state)
- # stateFile.close()
- # print("\n\n")
- # time.sleep(3)
- # os.system("reboot")
-
- # print(color.BOLD + color.GREEN + "In-place upgrade is completed, you may use the system." + color.END)
|