From a6cecb9fdff6d8691bdb22852d3eaaa0fda3947b Mon Sep 17 00:00:00 2001 From: gau1991 Date: Wed, 6 May 2015 16:14:24 +0530 Subject: [PATCH] Improved logic for GPG key fix --- ee/cli/plugins/stack.py | 12 +------ ee/cli/plugins/stack_migrate.py | 11 +------ ee/cli/plugins/stack_upgrade.py | 10 +----- ee/core/aptget.py | 56 ++++++++++++++++++++++++++------- ee/core/gpgkeyfix.py | 8 ++++- 5 files changed, 54 insertions(+), 43 deletions(-) diff --git a/ee/cli/plugins/stack.py b/ee/cli/plugins/stack.py index 24854bbb..cd0d624f 100644 --- a/ee/cli/plugins/stack.py +++ b/ee/cli/plugins/stack.py @@ -16,7 +16,6 @@ from ee.core.checkfqdn import check_fqdn from pynginxconfig import NginxConfig from ee.core.services import EEService from ee.core.variables import EEVariables -from ee.core.gpgkeyfix import gpgkeyfix import random import string import configparser @@ -1584,16 +1583,7 @@ class EEStackController(CementBaseController): if len(apt_packages): EESwap.add(self) Log.info(self, "Updating apt-cache, please wait...") - - if not EEAptGet.update(self): - Log.info(self, "Fixing mixing GPG keys, please wait...") - gpgkeyfix(self) - if not EEAptGet.update(self): - Log.info(self, Log.FAIL + "Oops Something went " - "wrong!!") - Log.error(self, "Check logs for reason " - "`tail /var/log/ee/ee.log` & Try Again!!!") - + EEAptGet.update(self) Log.info(self, "Installing packages, please wait...") EEAptGet.install(self, apt_packages) if len(packages): diff --git a/ee/cli/plugins/stack_migrate.py b/ee/cli/plugins/stack_migrate.py index 7a864239..e200036b 100644 --- a/ee/cli/plugins/stack_migrate.py +++ b/ee/cli/plugins/stack_migrate.py @@ -7,7 +7,6 @@ from ee.core.aptget import EEAptGet from ee.core.shellexec import EEShellExec from ee.core.apt_repo import EERepo from ee.core.services import EEService -from ee.core.gpgkeyfix import gpgkeyfix import configparser import os @@ -89,15 +88,7 @@ class EEStackMigrateController(CementBaseController): "libclass-dbi-mysql-perl"] Log.info(self, "Updating apt-cache, please wait...") - - if not EEAptGet.update(self): - Log.info(self, "Fixing mixing GPG keys, please wait...") - gpgkeyfix(self) - if not EEAptGet.update(self): - Log.info(self, Log.FAIL + "Oops Something went wrong!!") - Log.error(self, "Check logs for reason " - "`tail /var/log/ee/ee.log` & Try Again!!!") - + EEAptGet.update(self) Log.info(self, "Installing MariaDB, please wait...") EEAptGet.remove(self, ["mysql-common", "libmysqlclient18"]) EEAptGet.auto_remove(self) diff --git a/ee/cli/plugins/stack_upgrade.py b/ee/cli/plugins/stack_upgrade.py index f1b17718..1bfe07e3 100644 --- a/ee/cli/plugins/stack_upgrade.py +++ b/ee/cli/plugins/stack_upgrade.py @@ -7,7 +7,6 @@ from ee.core.apt_repo import EERepo from ee.core.services import EEService from ee.core.fileutils import EEFileUtils from ee.core.shellexec import EEShellExec -from ee.core.gpgkeyfix import gpgkeyfix import configparser import os @@ -77,14 +76,7 @@ class EEStackUpgradeController(CementBaseController): "php55", "php56") Log.info(self, "Updating apt-cache, please wait...") - if not EEAptGet.update(self): - Log.info(self, "Fixing mixing GPG keys, please wait...") - gpgkeyfix(self) - if not EEAptGet.update(self): - Log.info(self, Log.FAIL + "Oops Something went wrong!!") - Log.error(self, "Check logs for reason " - "`tail /var/log/ee/ee.log` & Try Again!!!") - + EEAptGet.update(self) Log.info(self, "Installing packages, please wait ...") EEAptGet.install(self, EEVariables.ee_php) diff --git a/ee/core/aptget.py b/ee/core/aptget.py index 27c16382..a61e94de 100644 --- a/ee/core/aptget.py +++ b/ee/core/aptget.py @@ -4,6 +4,7 @@ import apt_pkg import sys import subprocess from ee.core.logging import Log +from ee.core.apt_repo import EERepo from sh import apt_get from sh import ErrorReturnCode @@ -19,18 +20,38 @@ class EEAptGet(): with open('/var/log/ee/ee.log', 'a') as f: proc = subprocess.Popen('apt-get update', shell=True, - stdin=None, stdout=f, stderr=f, + stdin=None, stdout=f, + stderr=subprocess.PIPE, executable="/bin/bash") proc.wait() + output, error_output = proc.communicate() - if proc.returncode == 0: - return True - else: - return False + # Check what is error in error_output + if "NO_PUBKEY" in str(error_output): + # Split the output + error_list = str(error_output).split("\\n") + + # Use a loop to add misising keys + for single_error in error_list: + if "NO_PUBKEY" in single_error: + key = single_error.rsplit(None, 1)[-1] + EERepo.add_key(self, key) + + proc = subprocess.Popen('apt-get update', + shell=True, + stdin=None, stdout=f, stderr=f, + executable="/bin/bash") + proc.wait() + + if proc.returncode == 0: + return True + else: + Log.info(self, Log.FAIL + "Oops Something went wrong!!") + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") except Exception as e: - Log.error(self, "Error while installing packages, " - "apt-get exited with error") + Log.error(self, "apt-get update exited with error") def check_upgrade(self): """ @@ -71,7 +92,10 @@ class EEAptGet(): if proc.returncode == 0: return True else: - Log.error(self, "Unable to run apt-get dist_upgrade") + Log.info(self, Log.FAIL + "Oops Something went " + "wrong!!") + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") except Exception as e: Log.error(self, "Error while installing packages, " "apt-get exited with error") @@ -94,11 +118,16 @@ class EEAptGet(): if proc.returncode == 0: return True else: - Log.error(self, "Unable to run apt-get install") + Log.info(self, Log.FAIL + "Oops Something went " + "wrong!!") + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") except Exception as e: - Log.error(self, "Error while installing packages, " - "apt-get exited with error") + Log.info(self, Log.FAIL + "Oops Something went " + "wrong!!") + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") def remove(self, packages, auto=False, purge=False): all_packages = ' '.join(packages) @@ -118,7 +147,10 @@ class EEAptGet(): if proc.returncode == 0: return True else: - Log.error(self, "Unable to run apt-get remove/purge") + Log.info(self, Log.FAIL + "Oops Something went " + "wrong!!") + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") except Exception as e: Log.error(self, "Error while installing packages, " diff --git a/ee/core/gpgkeyfix.py b/ee/core/gpgkeyfix.py index b020338d..2f2e1802 100644 --- a/ee/core/gpgkeyfix.py +++ b/ee/core/gpgkeyfix.py @@ -1,17 +1,23 @@ """EasyEngine apt-get update GPG Key fix module""" -import os +from ee.core.apt_repo import EERepo import subprocess def gpgkeyfix(self): try: + # Run apt-get update sub = subprocess.Popen('apt-get update', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) sub.wait() + output, error_output = sub.communicate() + # Check what is error in error_output if "NO_PUBKEY" in str(error_output): + # Split the output error_list = str(error_output).split("\\n") + + # Use a loop to add misising keys for single_error in error_list: if "NO_PUBKEY" in single_error: key = single_error.rsplit(None, 1)[-1]