reinstall pkgs when last install faild(mavis)

This commit is contained in:
shenyafeng 2022-07-12 15:25:48 +08:00
parent 012df34124
commit 9ef9ced280
2 changed files with 39 additions and 4 deletions

View File

@ -134,7 +134,7 @@ LOCK_FILE = "/var/run/kylin-unattended-upgrade.lock"
NOTIFICATION_PIPE = '/tmp/notification.pipe'
TIME_STAMP = "/var/lib/unattended-upgrades/unattended-upgrades-timestamp"
UNATTENDED_UPGRADE_PKG_LIST_FILE_PATH="/var/lib/kylin-system-updater/json/auto-upgrade-list.json"
OTA_PKGS_TO_INSTALL_LIST="/var/lib/unattended-upgrades/ota_pkgs_to_install_list"
# 禁止关机锁文件路径
FILELOCK_PATH = "/tmp/lock/"
SHUTDOWN_BLOCK_FILELOCK = "kylin-update.lock"
@ -158,7 +158,8 @@ def reload_options_config():
apt_pkg.config.clear("APT::Install-Recommends")
if apt_pkg.config.find_b("APT::Install-Suggests",False) == True:
apt_pkg.config.clear("APT::Install-Suggests")
if apt_pkg.config.find("Dir::Etc::sourceparts","")!="":
apt_pkg.config["Dir::Etc::sourceparts"]="":
apt_pkg.init_system()
def get_default_version():
@ -253,6 +254,25 @@ def unLockedEnableShutdown():
pidfile = None
return False
def is_dpkg_journal_dirty():
# type: () -> bool
"""
Return True if the dpkg journal is dirty
(similar to debSystem::CheckUpdates)
"""
logging.debug("checking whether dpkg journal is dirty")
d = os.path.join("/var/lib/dpkg/",
#os.path.dirname(apt_pkg.config.find_file("Dir::State::status")),
"updates")
for f in os.listdir(d):
if re.match("[0-9]+", f) or re.match("tmp.i",f):
return True
return False
def get_abnormally_installed_pkg_count():
output = subprocess.check_output('dpkg -l|grep ^i[^i]|wc -l',shell=True)
return output.decode().strip()
def get_white_list_with_version(srclist,list,namelist):
for name_with_version in srclist:
nvlist = name_with_version.strip().split('=',1)
@ -2943,6 +2963,13 @@ def run(options, # type: Options
except SystemError as e:
logging.error(_("GetArchives() failed: %s"), e)
if get_abnormally_installed_pkg_count() == '0' and not is_dpkg_journal_dirty():
local_pkgs_to_install = []
for item in fetcher.items:
local_pkgs_to_install.append(item.destfile)
with open(OTA_PKGS_TO_INSTALL_LIST,'w+') as f:
f.write(" ".join(local_pkgs_to_install))
fetcher_statistics = AcquireStatistics(fetcher=fetcher)
fetcher_statistics.GetAquireStatisticsOfPkgs()
logging.debug("%d local,%d remote"%(fetcher_statistics.local_pkg_amount,fetcher_statistics.remote_pkg_amount))

View File

@ -70,6 +70,7 @@ PID_FILE = "/var/run/unattended-upgrades.pid"
LOCK_FILE = "/var/run/kylin-unattended-upgrades.lock"
PKGS_TO_INSTALL_FLAG_FILE="/var/lib/unattended-upgrades/OTA_PKGS_TO_INSTALL"
TIME_STAMP = "/var/lib/unattended-upgrades/unattended-upgrades-timestamp"
OTA_PKGS_TO_INSTALL_LIST="/var/lib/unattended-upgrades/ota_pkgs_to_install_list"
## analytic unattended-upgrades-policy.conf start
POLICY_CONF_SECTION_AUTO_UPGRADE_POLICY = "autoUpgradePolicy"
@ -127,7 +128,8 @@ def reload_options_config():
apt_pkg.config.clear("APT::Install-Recommends")
if apt_pkg.config.find_b("APT::Install-Suggests",False) == True:
apt_pkg.config.clear("APT::Install-Suggests")
if apt_pkg.config.find("Dir::Etc::sourceparts","")!="":
apt_pkg.config["Dir::Etc::sourceparts"]="":
apt_pkg.init_system()
def is_dpkg_journal_dirty():
@ -1074,7 +1076,13 @@ if __name__ == "__main__":
abnormal_pkg_count = get_abnormally_installed_pkg_count()
logging.info("abnormal pkg count:%s,dpkg dirty:%s"%(abnormal_pkg_count,dpkg_journal_dirty))
if dpkg_journal_dirty:
dpkg_fix = subprocess.run("dpkg --configure -a",shell=True,stdout=open(logfile,'a+'),stderr=open(logfile,'a+'))
try:
with open(OTA_PKGS_TO_INSTALL_LIST,'r') as f:
pkgs = f.read()
ret = subprocess.run(["dpkg -i %s"%pkgs],shell=True,stdout=open(logfile,'a+'),stderr=open(logfile,'a+'))
except Exception as e:
logging.error(e)
# dpkg_fix = subprocess.run("dpkg --configure -a",shell=True,stdout=open(logfile,'a+'),stderr=open(logfile,'a+'))
if abnormal_pkg_count != '0':
apt_fix = subprocess.run("echo y|apt install -f",shell=True,stdout=open(logfile,'a+'),stderr=open(logfile,'a+'))
kylin_system_updater = KylinSystemUpdater()