You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
16KB

  1. # -*- coding: utf-8 -*-
  2. # Importing the required dependencies
  3. import os
  4. import sys
  5. import time
  6. import json
  7. from os.path import exists as file_exists
  8. class color:
  9. PURPLE = '\033[95m'
  10. CYAN = '\033[96m'
  11. DARKCYAN = '\033[36m'
  12. BLUE = '\033[94m'
  13. GREEN = '\033[92m'
  14. YELLOW = '\033[93m'
  15. RED = '\033[91m'
  16. BOLD = '\033[1m'
  17. UNDERLINE = '\033[4m'
  18. END = '\033[0m'
  19. # 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.
  20. if os.getuid() != 0:
  21. print(color.BOLD + color.RED + "Operation not permitted!")
  22. print("Use SuperUser to run the script!" + color.END)
  23. sys.exit(-1)
  24. else:
  25. print(color.BOLD + color.GREEN + "SuperUser access granted, script access granted." + color.END)
  26. print("\n\n")
  27. time.sleep(3)
  28. # Creating a directory for future use to store all the logs.
  29. if(os.path.isdir("/osUpgradeEl7/")):
  30. pass
  31. else:
  32. os.makedirs("/osUpgradeEl7/")
  33. print(color.BOLD + color.GREEN + "Creating an os-upgrade directory" + color.END)
  34. print("\n\n")
  35. time.sleep(3)
  36. os.chdir("/osUpgradeEl7/")
  37. # Statefile for idempotence of the upgrade script so that all the occured operations need not to be repeated again.
  38. if (file_exists("/osUpgradeEl7/stateFile")):
  39. # If statefile exists, open the stateFile and read the existing state so that the previous steps are not happening again.
  40. stateFile = open("/osUpgradeEl7/stateFile", "r")
  41. state = stateFile.readline()
  42. print(color.BOLD + color.GREEN + "Current state is : " + state + color.END)
  43. stateFile.close()
  44. else:
  45. # If no statefile exists, then create a new stateFile and start the upgrade process.
  46. stateFile = open("/osUpgradeEl7/stateFile", "w")
  47. print(color.BOLD + color.GREEN + "Statefile doesn't exists, creating a new one." + color.END)
  48. state = "0"
  49. print(color.BOLD + color.GREEN + "Current state is : " + state + color.END)
  50. stateFile.write(state)
  51. stateFile.close()
  52. print("\n\n")
  53. time.sleep(3)
  54. operatingEnvironment = ""
  55. # Checking if the OS to be upgraded is supported by the script or not.
  56. if (file_exists("/etc/centos-release")):
  57. # Checking for the CentOS or AmeyOS distribution.
  58. releaseFile = open("/etc/centos-release", "r")
  59. release = releaseFile.readline()
  60. releaseFile.close()
  61. if (" 7." in release):
  62. # Checking if the release is EL7, if yes then continue.
  63. print(color.BOLD + color.GREEN + "Starting the upgrade process, the OS is supported" + color.END)
  64. else:
  65. print(color.BOLD + color.RED + "ERROR...! This version of CentOS / AmeyoOS is not supported." + color.END)
  66. sys.exit(-1)
  67. else:
  68. print(color.BOLD + color.RED + "OS files have been tampered with, couldn't proceed further with the upgrade" + color.END)
  69. sys.exit(-1)
  70. print("\n")
  71. time.sleep(3)
  72. if (int(state) <= 0):
  73. # Updates the grub configuration file
  74. print(color.BOLD + color.GREEN + "Updating the grub configuration file." + color.END)
  75. os.system("rm -rvf /etc/grub2.cfg")
  76. os.system("grub2-mkconfig -o /boot/grub2/grub.cfg")
  77. os.system("ln -s /boot/grub2/grub.cfg /etc/grub2.cfg")
  78. print(color.BOLD + color.GREEN + "Grub menu updated successfully." + color.END)
  79. # Removing the old repos and installing the new repos
  80. print(color.BOLD + color.GREEN + "STEP 1 : Removing the yum repositories and restoring the required one." + color.END)
  81. os.system("rm -rvf /etc/yum.repos.d/*")
  82. os.chdir("/etc/yum.repos.d/")
  83. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Base.repo") != 0):
  84. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  85. sys.exit(-1)
  86. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-CR.repo") != 0):
  87. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  88. sys.exit(-1)
  89. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Debuginfo.repo") != 0):
  90. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  91. sys.exit(-1)
  92. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Media.repo") != 0):
  93. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  94. sys.exit(-1)
  95. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Sources.repo") != 0):
  96. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  97. sys.exit(-1)
  98. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-Vault.repo") != 0):
  99. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  100. sys.exit(-1)
  101. if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/centos-repos/CentOS-fasttrack.repo") != 0):
  102. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  103. sys.exit(-1)
  104. 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):
  105. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  106. sys.exit(-1)
  107. os.chdir("/osUpgradeEl7/")
  108. # Updating the state in the stateFile.
  109. state = "1"
  110. print(color.BOLD + color.GREEN + "Step-1 completed, the modified state is : " + state + color.END)
  111. stateFile = open("/osUpgradeEl7/stateFile", "w")
  112. stateFile.write(state)
  113. stateFile.close()
  114. print("\n\n")
  115. time.sleep(3)
  116. if (int(state) <= 1):
  117. # Cleaning the metadata and updating the packages
  118. print(color.BOLD + color.GREEN + "STEP 2 : Cleaning the metadata and upgrading the required packages" + color.END)
  119. if(os.system("sudo yum clean all") != 0):
  120. print(color.BOLD + color.RED + "Couldn't perform the specified task, please re-run the script" + color.END)
  121. sys.exit(-1)
  122. if(os.system("sudo yum clean metadata") != 0):
  123. print(color.BOLD + color.RED + "Couldn't perform the specified task, please re-run the script" + color.END)
  124. sys.exit(-1)
  125. if(os.system("sudo yum install epel-release -y") != 0):
  126. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  127. sys.exit(-1)
  128. if(os.system("sudo yum update -y") != 0):
  129. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  130. sys.exit(-1)
  131. # Updating the state in the stateFile.
  132. state = "2"
  133. print(color.BOLD + color.GREEN + "Step-2 completed, the modified state is : " + state + color.END)
  134. stateFile = open("/osUpgradeEl7/stateFile", "w")
  135. stateFile.write(state)
  136. stateFile.close()
  137. print("\n\n")
  138. time.sleep(3)
  139. if (int(state) <= 2):
  140. # Rebooting our system.
  141. state = "3"
  142. print(color.BOLD + color.GREEN + "Step-3, the system will REBOOT now." + state + color.END)
  143. stateFile = open("/osUpgradeEl7/stateFile", "w")
  144. stateFile.write(state)
  145. stateFile.close()
  146. print("\n\n")
  147. time.sleep(3)
  148. os.system("reboot")
  149. if (int(state) <= 3):
  150. # Downloading and installing the epel & yum-utils packages
  151. print(color.BOLD + color.GREEN + "STEP 4 : Installing the epel-release and yum-utils" + color.END)
  152. if(os.system("yum -y install epel-release yum-utils") != 0):
  153. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  154. sys.exit(-1)
  155. # Updating the state in the stateFile.
  156. state = "4"
  157. print(color.BOLD + color.GREEN + "Step-4 completed, the modified state is : " + state + color.END)
  158. stateFile = open("/osUpgradeEl7/stateFile", "w")
  159. stateFile.write(state)
  160. stateFile.close()
  161. print("\n\n")
  162. time.sleep(3)
  163. if (int(state) <= 4):
  164. # Installing the elevate repos and packages for upgrade.
  165. print(color.BOLD + color.GREEN + "STEP 5 : Installing the elevate repos and packages for upgrade" + color.END)
  166. if(os.system("sudo yum install -y http://repo.almalinux.org/elevate/elevate-release-latest-el7.noarch.rpm") != 0):
  167. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  168. sys.exit(-1)
  169. if(os.system("sudo yum install -y leapp-upgrade leapp-data-rocky") != 0):
  170. print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  171. sys.exit(-1)
  172. # Updating the state in the stateFile.
  173. state = "5"
  174. print(color.BOLD + color.GREEN + "Step-5 completed, the modified state is : " + state + color.END)
  175. stateFile = open("/osUpgradeEl7/stateFile", "w")
  176. stateFile.write(state)
  177. stateFile.close()
  178. print("\n\n")
  179. time.sleep(3)
  180. if (int(state) <= 5):
  181. # Running the preupgrade tool to generate the report.
  182. print(color.BOLD + color.GREEN + "STEP 6 : Running the preupgrade tool to generate report and fetch errors" + color.END)
  183. os.system("sudo leapp preupgrade")
  184. # Updating the state in the stateFile.
  185. state = "6"
  186. print(color.BOLD + color.GREEN + "Step-6 completed, the modified state is : " + state + color.END)
  187. stateFile = open("/osUpgradeEl7/stateFile", "w")
  188. stateFile.write(state)
  189. stateFile.close()
  190. print("\n\n")
  191. time.sleep(3)
  192. if (int(state) <= 6):
  193. # Running commands from the json report generated by the preupgrade tool.
  194. print(color.BOLD + color.GREEN + "STEP 7 : Running commands from the json report generated by the preupgrade tool" + color.END)
  195. jsonFile = open('/var/log/leapp/leapp-report.json')
  196. jsonData = json.load(jsonFile)
  197. for entry in jsonData['entries']:
  198. for details in entry:
  199. if "detail" in details:
  200. if not (entry['detail'].get('remediations') is None):
  201. data = entry['detail']['remediations']
  202. for feild in data:
  203. if feild['type'] == "command":
  204. command = ""
  205. for partCommand in feild['context']:
  206. if partCommand == "sh" or partCommand == "-c":
  207. pass
  208. else:
  209. command += partCommand + " "
  210. os.system(command)
  211. # Updating the state in the stateFile.
  212. state = "7"
  213. print(color.BOLD + color.GREEN + "Step-7 completed, the modified state is : " + state + color.END)
  214. stateFile = open("/osUpgradeEl7/stateFile", "w")
  215. stateFile.write(state)
  216. stateFile.close()
  217. print("\n\n")
  218. time.sleep(3)
  219. if (int(state) <= 7):
  220. # Performing some additional steps for the smooth upgrade.
  221. print(color.BOLD + color.GREEN + "STEP 8 : Performing some additional steps for the smooth upgrade." + color.END)
  222. os.system("echo PermitRootLogin yes | sudo tee -a /etc/ssh/sshd_config")
  223. os.system("sudo rmmod floppy")
  224. os.system("sudo rmmod pata_acpi")
  225. os.system("sudo rm -rvf /var/lib/pacemaker/cib/cib.xml")
  226. os.system("sudo rm -rvf /etc/corosync/corosync.conf")
  227. os.system("sudo sed -i 's/,hmac-ripemd160/ /g' /etc/ssh/sshd_config")
  228. os.system("sudo yum remove -y libwebp7 gd-last libzip5 python-psycopg2")
  229. # Updating the state in the stateFile.
  230. state = "8"
  231. print(color.BOLD + color.GREEN + "Step-8 completed, the modified state is : " + state + color.END)
  232. stateFile = open("/osUpgradeEl7/stateFile", "w")
  233. stateFile.write(state)
  234. stateFile.close()
  235. print("\n\n")
  236. time.sleep(3)
  237. if (int(state) <= 8):
  238. # Performing the upgrade task.
  239. print(color.BOLD + color.GREEN + "STEP 9 : Performing the upgrade task." + color.END)
  240. os.system("sudo leapp upgrade")
  241. # Updating the state in the stateFile.
  242. state = "9"
  243. print(color.BOLD + color.GREEN + "Step-9 completed, the modified state is : " + state + color.END)
  244. stateFile = open("/osUpgradeEl7/stateFile", "w")
  245. stateFile.write(state)
  246. stateFile.close()
  247. print("\n\n")
  248. time.sleep(3)
  249. if (int(state) <= 9):
  250. # Updating the grub menu and restarting the system to boot into upgrade menu.
  251. print(color.BOLD + color.GREEN + "Updating the /etc/issue file." + color.END)
  252. with open("/etc/issue") as issueFile:
  253. lines = issueFile.readlines()
  254. lines[0] = "Ameyo OS 8 - Rocky"
  255. with open("/etc/issue", 'w') as issueFile:
  256. issueFile.writelines(lines)
  257. print(color.BOLD + color.RED + "STEP 10 : Restart the system for upgrade." + color.END)
  258. while(True):
  259. try:
  260. input = raw_input
  261. except NameError:
  262. pass
  263. rebootTask=input("Do you want to reboot to go to the upgrade menu (y/n) : ")
  264. rebootTask=rebootTask.lower()
  265. if((rebootTask == "y") or (rebootTask == "yes")):
  266. os.system("reboot")
  267. elif((rebootTask == "n") or (rebootTask == "no")):
  268. print("You need to manually reboot the system for the upgrade.")
  269. break
  270. else:
  271. pass
  272. # Updating the state in the stateFile.
  273. state = "10"
  274. print(color.BOLD + color.GREEN + "Step-10 completed, the modified state is : " + state + color.END)
  275. stateFile = open("/osUpgradeEl7/stateFile", "w")
  276. stateFile.write(state)
  277. stateFile.close()
  278. print("\n\n")
  279. time.sleep(3)
  280. # if (int(state) <= 10):
  281. # print(color.BOLD + color.GREEN + "STEP 11 : Performing the post-upgrade tasks." + color.END)
  282. # os.system("mkdir -p /etc/yum.repos.d.bkp/")
  283. # os.system("mv /etc/yum.repos.d/* /etc/yum.repos.d.bkp/")
  284. # os.chdir("/etc/yum.repos.d/")
  285. # if(os.system("curl -O https://dms-git.ameyo.net:8265/EL7toEL8/upgrade-el7-el8/raw/branch/master/alma-repo/ameyoosalma8base.repo") != 0):
  286. # print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  287. # sys.exit(-1)
  288. # os.chdir("/osUpgradeEl7/")
  289. # if(os.system("dnf install -y yum-utils python2") !=0):
  290. # print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  291. # sys.exit(-1)
  292. # os.system("alternatives --set python /usr/bin/python2")
  293. # if(os.system("sudo yum update -y") !=0):
  294. # print(color.BOLD + color.RED + "Couldn't connect to the internet, please check your internet connection and re-run the script" + color.END)
  295. # sys.exit(-1)
  296. # os.system("sudo grub2-mkconfig -o /boot/grub2/grub.cfg")
  297. # # Updating the state in the stateFile.
  298. # state = "11"
  299. # print(color.BOLD + color.GREEN + "Step-11 completed, the modified state is : " + state + color.END)
  300. # stateFile = open("/osUpgradeEl7/stateFile", "w")
  301. # stateFile.write(state)
  302. # stateFile.close()
  303. # print("\n\n")
  304. # time.sleep(3)
  305. # os.system("reboot")
  306. # print(color.BOLD + color.GREEN + "In-place upgrade is completed, you may use the system." + color.END)