From 0222b1986877d103a930bbd5726ad6aa155ada29 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Wed, 19 Mar 2025 17:04:05 +0500 Subject: [PATCH] detect and remove podnam --- dockerManager/dockerInstall.py | 96 ++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 32 deletions(-) diff --git a/dockerManager/dockerInstall.py b/dockerManager/dockerInstall.py index baa4b3145..4143db497 100755 --- a/dockerManager/dockerInstall.py +++ b/dockerManager/dockerInstall.py @@ -1,66 +1,98 @@ #!/usr/local/CyberCP/bin/python import os import sys +import time + sys.path.append('/usr/local/CyberCP') import plogical.CyberCPLogFileWriter as logging from serverStatus.serverStatusUtil import ServerStatusUtil from plogical.processUtilities import ProcessUtilities -import time - class DockerInstall: - @staticmethod - def submitInstallDocker(CommandCP = 0): + def submitInstallDocker(CommandCP=0): try: - statusFile = open(ServerStatusUtil.lswsInstallStatusPath, 'w') logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, "Starting Docker Installation..\n", 1) - if ProcessUtilities.decideDistro() == ProcessUtilities.cent8: + # Check if Podman is installed + if os.system("which podman > /dev/null 2>&1") == 0: # + logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, + "Podman detected. Removing Podman before installing Docker...\n", 1) # + # Stop and remove all Podman containers (NEW LINES) + ServerStatusUtil.executioner("podman stop -a", statusFile) # + ServerStatusUtil.executioner("podman rm -a", statusFile) # + + # Remove Podman completely + if ProcessUtilities.decideDistro() in [ProcessUtilities.cent8, ProcessUtilities.centos]: # + ServerStatusUtil.executioner("dnf remove -y podman", statusFile) # + else: # Debian-based + ServerStatusUtil.executioner("apt-get remove -y podman", statusFile) # + + # Remove leftover Podman directories (NEW LINES) + ServerStatusUtil.executioner("rm -rf /var/lib/containers", statusFile) # + + # Remove Podman socket if it exists (NEW LINES) + if os.path.exists("/run/podman/podman.sock"): # + os.remove("/run/podman/podman.sock") # + logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, + "Removed Podman socket.\n", 1) # + + logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, + "Podman completely removed.\n", 1) # + + # Check and unset DOCKER_HOST if set (NEW LINES) + if "DOCKER_HOST" in os.environ: # + ServerStatusUtil.executioner("unset DOCKER_HOST", statusFile) # + logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, + "Unset DOCKER_HOST to avoid conflicts.\n", 1) # + + # Remove systemd override if it forces Podman for Docker (NEW LINES) + if os.path.exists("/etc/systemd/system/docker.service.d/override.conf"): # + os.remove("/etc/systemd/system/docker.service.d/override.conf") # + logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, + "Removed systemd override forcing Podman.\n", 1) # + + # Install Docker based on OS version + if ProcessUtilities.decideDistro() == ProcessUtilities.cent8: if os.path.exists(ProcessUtilities.debugPath): logging.CyberCPLogFileWriter.writeToFile(f'Docker installation started for cent8/9') - command = 'sudo yum install -y yum-utils' - ServerStatusUtil.executioner(command, statusFile) - - command = 'yum install yum-utils -y' - ServerStatusUtil.executioner(command, statusFile) - - command = 'yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo' - ServerStatusUtil.executioner(command, statusFile) - - command = 'sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin --allowerasing -y' + commands = [ + 'sudo yum install -y yum-utils', + 'yum install yum-utils -y', + 'yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo', + 'sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin --allowerasing -y' + ] elif ProcessUtilities.decideDistro() == ProcessUtilities.centos: - command = 'sudo yum install -y docker' + commands = ['sudo yum install -y docker'] else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io docker-compose' + commands = ['DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io docker-compose'] - if CommandCP: - ProcessUtilities.executioner(command, 'root', True) - else: - if not ServerStatusUtil.executioner(command, statusFile): - logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, - "Failed to install Docker. [404]\n", 1) - return 0 + for command in commands: + if CommandCP: + ProcessUtilities.executioner(command, 'root', True) + else: + if not ServerStatusUtil.executioner(command, statusFile): + logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, + "Failed to install Docker. [404]\n", 1) + return 0 - command = 'sudo systemctl enable docker' - - ProcessUtilities.executioner(command, 'root', True) - - command = 'sudo systemctl start docker' - ProcessUtilities.executioner(command, 'root', True) + # Enable and start Docker service + ProcessUtilities.executioner('sudo systemctl enable docker', 'root', True) + ProcessUtilities.executioner('sudo systemctl start docker', 'root', True) logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, - "Docker successfully installed.[200]\n", 1) + "Docker successfully installed. [200]\n", 1) time.sleep(2) except BaseException as msg: logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath, str(msg) + ' [404].', 1) + def main(): DockerInstall.submitInstallDocker()