upload upgrade result only once per run

This commit is contained in:
shenyafeng 2022-08-24 16:21:09 +08:00
parent 8b0c3370d5
commit 2ca3632b67
1 changed files with 191 additions and 73 deletions

View File

@ -145,6 +145,77 @@ pidfile = None
# set from the sigint signal handler
SIGNAL_STOP_REQUEST = False
class PackageComponent():
def MarkUpgrade(self):
pass
def MarkInstall(self):
pass
class PackageGroup(PackageComponent):
def __init__(self,name) -> None:
self.name = name
self.upgrade_list = []
self.install_list = []
def AddCache(self,cache):
for pkg in self.upgrade_list:
pkg.AddCache(cache)
for pkg in self.install_list:
pkg.AddCache(cache)
def FindPackage(self,name):
for pkg in self.upgrade_list:
if pkg.FindPackage(name):
return True
for pkg in self.install_list:
if pkg.FindPackage(name):
return True
return False
def AddPackageToUpgradeList(self,Package):
self.upgrade_list.append(Package)
def RemovePackageFromUpgradeList(self,Package):
if Package in self.upgrade_list:
self.upgrade_list.remove(Package)
def AddPackageToInstallList(self,Package):
self.install_list.append(Package)
def RemovePackageFromInstallList(self,Package):
if Package in self.install_list:
self.install_list.remove(Package)
def MarkUpgrade(self):
for package in self.upgrade_list:
package.MarkUpgrade()
def MarkInstall(self):
for package in self.install_list:
package.MarkInstall()
class Package(PackageComponent):
def __init__(self,name,version) -> None:
self.name = name
self.candidate_version = version
self.deps = []
def FindPackage(self,name):
if name in self.deps:
return True
return False
def AddCache(self,cache):
self.cache = cache
def MarkUpgrade(self):
return
def MarkInstall(self):
return
def reload_options_config():
#添加默认保留旧配置
apt_pkg.config["DPkg::Options::"] = "--force-confold"
@ -299,8 +370,8 @@ def WriteValueToFile(file,section,option,value):
config=configparser.ConfigParser(allow_no_value=True)
config.add_section(section)
config.set(section,option,value)
config.write(open(file,"w"))
config.write(open(file,"w"))
def signal_handler_int(signal,frame):
# type: (int, object) -> None
@ -499,9 +570,21 @@ class KylinSystemUpdater:
self.update_interface = dbus.Interface(self.update_proxy,dbus_interface='com.kylin.systemupgrade.interface')
self.success = False
self.whitelist_with_candidate_version = []
self.group_rec=[]
self.single_rec=[]
self.update_group = []
# self.group_rec=[]
# self.single_rec=[]
self.errdict = {}
def AddPackageInstallErrorRecord(self,pkg,errmsg):
self.errdict.update({pkg:errmsg})
def DumpInstallErrorRecord(self):
errlist = []
for key in self.errdict.keys():
errlist.append("%s,%s"%(key,self.errdict[key]))
return errlist
def DataBackendCollect(self,updateinfo,json_file):
self.data_collect_interface.DataBackendCollect(updateinfo,json_file)
@ -532,7 +615,31 @@ class KylinSystemUpdater:
def update_detect_finished_handler(success,updatelist,error_status,error_cause):
if success:
logging.info("update detect success,quiting main loop")
self.update_group = updatelist
try:
for update_group in self.update_group:
json_file_path = ("/var/lib/kylin-system-updater/json/%s.json"%(update_group))
if os.path.exists(json_file_path):
with open(json_file_path,'r') as f:
data = json.load(f)
# package_name = data['package']
# upgrade_list = []
# install_list = []
'''
gp = PackageGroup(package_name)
for key in data['upgrade_list'].keys():
PackageGroup.AddPackageToUpgradeList(Package(key,data['upgrade_list'][key]['new_version']))
# upgrade_list.append((key,data['upgrade_list'][key]['new_version']))
for key in data['install_list'].keys():
PackageGroup.AddPackageToInstallList(Package(key,data['install_list'][key]['new_version']))
# install_list.append((key,data['install_list'][key]['new_version']))
self.group_list.append(gp)
'''
for key in data['upgrade_list'].keys():
self.whitelist_with_candidate_version.append((key,data['upgrade_list'][key]['new_version']))
for key in data['install_list'].keys():
self.whitelist_with_candidate_version.append((key,data['install_list'][key]['new_version']))
'''
if os.path.exists(UNATTENDED_UPGRADE_PKG_LIST_FILE_PATH):
with open(UNATTENDED_UPGRADE_PKG_LIST_FILE_PATH, "r") as f:
row_data = json.load(f)
@ -543,7 +650,8 @@ class KylinSystemUpdater:
for key in row_data['group_json'].keys():
self.group_rec.append((key,row_data['group_json'][key]['new_version'],row_data['group_json'][key]['changelog']))
for key in row_data['single_json'].keys():
self.single_rec.append((key,row_data['single_json'][key]['new_version'],row_data['single_json'][key]['changelog']))
self.single_rec.append((key,row_data['single_json'][key]['new_version'],row_data['single_json'][key]['changelog']))
'''
except Exception as e:
logging.error(e)
self.loop.quit()
@ -1074,7 +1182,7 @@ class LogInstallProgress(apt.progress.base.InstallProgress):
per=str(int(float(percent)))
f.write("%s"%per)
#f.write(_("Progress: %s %% (%s)") % (percent, pkg))
'''
if re.search("Installed",status):
UpdateInfos = {}
UpdateInfos.update({"packageName":"kylin-unattended-upgrade"})
@ -1084,17 +1192,28 @@ class LogInstallProgress(apt.progress.base.InstallProgress):
json_file = json.dumps(UpdateInfos.copy())
UpdateInfos.clear()
kylin_system_updater.DataBackendCollect("UpdateInfos",json_file)
'''
logging.info("%s:%s:%s"%(pkg,percent,status))
def error(self,pkg, errormsg):
UpdateInfos = {}
UpdateInfos.update({"packageName":"kylin-unattended-upgrade"})
UpdateInfos.update({"appname":str(pkg)})
UpdateInfos.update({"source":"kylin unattended upgrade"})
UpdateInfos.update({"status":0})
UpdateInfos.update({"errorCode":str(errormsg)})
json_file = json.dumps(UpdateInfos.copy())
UpdateInfos.clear()
kylin_system_updater.DataBackendCollect("UpdateInfos",json_file)
'''
for key in package_deps.keys():
if str(pkg) in package_deps[key]:
group_name = kylin_system_updater.FindPackageGroup(key)
UpdateInfos = {}
UpdateInfos.update({"packageName":"kylin-unattended-upgrade"})
UpdateInfos.update({"appname":group_name})
UpdateInfos.update({"source":"kylin unattended upgrade"})
UpdateInfos.update({"status":0})
UpdateInfos.update({"errorCode":str(errormsg)})
json_file = json.dumps(UpdateInfos.copy())
UpdateInfos.clear()
kylin_system_updater.DataBackendCollect("UpdateInfos",json_file)
kylin_system_updater.AddPackageInstallErrorRecord(str(pkg),str(errormsg))
'''
logging.error("%s:%s"%(pkg,errormsg))
kylin_system_updater.AddPackageInstallErrorRecord(str(pkg),str(errormsg))
def _fixup_fds(self):
# () -> None
@ -2705,7 +2824,7 @@ def main(options, rootdir="/"):
try:
res = run(options, rootdir, logfile_dpkg)
logging.info("result:%s,%s"%(res.success,res.result_str))
release = ''
version = ''
original_version = ''
@ -2723,66 +2842,51 @@ def main(options, rootdir="/"):
release = original_version
release = original_version
'''
date = time.strftime("%Y-%m-%d %H:%M:%S")
#history record
history = {}
if res.success and len(res.pkgs) > 0 and res.result_str == "total_install":
logging.info("total install")
date = time.strftime("%Y-%m-%d %H:%M:%S")
history.update({"date":date})
history.update({"appname":"kylin-unattended-upgrade"})
history.update({"appname_cn":"自动更新"})
history.update({"version":""})
history.update({"description":"download and install security upgrades automatically"})
history.update({"keyword":"1"})
history.update({"changelog":""})
history.update({"status":"success"})
history.update({"errorcode":"cache commit error"})
history.update({"status_cn":"成功"})
#data collect info
UpdateInfos = {}
UpdateInfos.update({"packageName":"kylin-unattended-upgrade"})
UpdateInfos.update({"appname":"kylin-unattended-upgrade"})
UpdateInfos.update({"source":"kylin unattended upgrade"})
UpdateInfos.update({"status":1})
UpdateInfos.update({"errorCode":"cache commit error"})
if res.success and len(res.pkgs) > 0 :
if res.result_str == "total_install":
# with open(TIME_STAMP,'w') as f:
# f.write(time.time())
kylin_system_updater.SetConfigValue("SYSTEM","os_version",release)
kylin_system_updater.SetConfigValue("SYSTEM","update_version",original_version)
for rec in kylin_system_updater.group_rec:
history.update({"appname":rec[0]+"(自动更新)"})
history.update({"version":rec[1]})
history.update({"description":""})
history.update({"date":date})
history.update({"status":"success"})
history.update({"keyword":""})
history.update({"errorcode":""})
history.update({"appname_cn":""})
history.update({"status_cn":"成功"})
history.update({"changelog":rec[2]})
kylin_system_updater.SetConfigValue("SYSTEM","os_version",release)
kylin_system_updater.SetConfigValue("SYSTEM","update_version",original_version)
kylin_system_updater.InsertUpgradeHistory(history)
for rec in kylin_system_updater.single_rec:
history.update({"appname":rec[0]+"(自动更新)"})
history.update({"version":rec[1]})
history.update({"description":""})
history.update({"date":date})
history.update({"status":"success"})
history.update({"keyword":""})
history.update({"errorcode":""})
history.update({"appname_cn":""})
history.update({"status_cn":"成功"})
history.update({"changelog":rec[2]})
kylin_system_updater.InsertUpgradeHistory(history)
json_file = json.dumps(UpdateInfos.copy())
kylin_system_updater.DataBackendCollect("UpdateInfos",json_file)
elif not res.success:
for rec in kylin_system_updater.group_rec:
history.update({"appname":rec[0]+"(自动更新)"})
history.update({"version":rec[1]})
history.update({"description":""})
history.update({"date":date})
history.update({"status":"failed"})
history.update({"keyword":""})
history.update({"errorcode":""})
history.update({"appname_cn":""})
errorlist = kylin_system_updater.DumpInstallErrorRecord()
errorlist.append("cache commit error")
errcode = "\n".join(errorlist)
if options.install_only:
history.update({"status":"failed"})
history.update({"status_cn":"失败"})
history.update({"changelog":rec[2]})
kylin_system_updater.InsertUpgradeHistory(history)
for rec in kylin_system_updater.single_rec:
history.update({"appname":rec[0]+"(自动更新)"})
history.update({"version":rec[1]})
history.update({"description":""})
history.update({"date":date})
history.update({"status":"failed"})
history.update({"keyword":""})
history.update({"errorcode":""})
history.update({"appname_cn":""})
history.update({"status_cn":"失败"})
history.update({"changelog":rec[2]})
history.update({"errorcode":errcode})
kylin_system_updater.InsertUpgradeHistory(history)
UpdateInfos.update({"status":0})
UpdateInfos.update({"errorCode":errcode})
json_file = json.dumps(UpdateInfos.copy())
kylin_system_updater.DataBackendCollect("UpdateInfos",json_file)
else:
pass
logging.info("no pkgs to install")
if 'PROJECT_CODENAME' in os_release_info:
if os_release_info['PROJECT_CODENAME']=='V10SP1-edu':
@ -2846,8 +2950,9 @@ def main(options, rootdir="/"):
except Exception as e:
WriteValueToFile(UNATTENDED_UPGRADE_CONFIG_FILE_PATH,"UNATTENDED_UPGRADE","autoupdate_run_status","idle")
logger = logging.getLogger()
logger.exception(_("An error occurred: %s"), e)
logging.error(e)
# logger = logging.getLogger()
# logger.exception(_("An error occurred: %s"), e)
# log_content = get_dpkg_log_content(logfile_dpkg,
# install_start_time)
# if not options.dry_run:
@ -2891,7 +2996,17 @@ def adjust_candidate_with_version(cache,namelistwithversion):
pkg.candidate = v
# if str(v) in versionlist:
# pkg.candidate = v
'''
dep_list = []
dep = pkg.candidate.get_dependencies("PreDepends")
for d in dep:
dep_list.append(d.or_dependencies[0].name)
dep = pkg.candidate.get_dependencies("Depends")
for d in dep:
dep_list.append(d.or_dependencies[0].name)
package_deps.update({pkg:dep_list})
'''
def run(options, # type: Options
rootdir, # type: str
# mem_log, # type: StringIO
@ -2904,6 +3019,7 @@ def run(options, # type: Options
# if not is_update_day():
# return UnattendedUpgradesResult(True)
logging.info(_("Starting unattended upgrades script"))
reload_options_config()
# check if u-u should be stopped already
if should_stop():
return UnattendedUpgradesResult(False)
@ -3034,6 +3150,7 @@ def run(options, # type: Options
#auto_removable = get_auto_removable(cache)
# find out about the packages that are upgradable (in an allowed_origin)
pkgs_to_upgrade = calculate_upgradable_pkgs(cache, options,white_list_with_version)
pkgs_to_upgrade.sort(key=lambda p: p.name)
@ -3689,6 +3806,7 @@ if __name__ == "__main__":
logging.warning("control center running ,exiting...")
sys.exit(1)
'''
# package_deps = {}
kylin_system_updater.ConnectToSignals()
kylin_system_updater.UpdateDetect()
kylin_system_updater.RunMainloop()