add start-up download;fix download pop-up;add release id and version update

This commit is contained in:
shenyafeng 2022-05-10 14:37:06 +08:00
parent b0cd678b0f
commit 13b0cc0184
3 changed files with 140 additions and 50 deletions

3
debian/control vendored
View File

@ -13,7 +13,8 @@ Build-Depends: debhelper (>= 9),
python3-gi (>= 3.8),
python3-yaml,
lsb-release,
apt-clone (>= 0.2.3~ubuntu1)
apt-clone (>= 0.2.3~ubuntu1),
python3-apscheduler
Build-Depends-Indep: intltool,
python3-coverage,
python3-distro-info,

View File

@ -93,8 +93,11 @@ import configparser
import fcntl
import time
import subprocess
import json
OTA_RESULT_FILE_PATH="/opt/apt_result/ota_result"
VERSION_FILE = '/etc/kylin-version/kylin-system-version.conf'
OTA_RESULT_FILE_PATH="/opt/apt_result/"
OTA_RESULT_FILE="/opt/apt_result/ota_result"
SYSTEM_UPDATER_CORE_LIB_PATH="/usr/share/kylin-system-updater/SystemUpdater/Core"
sys.path.append(SYSTEM_UPDATER_CORE_LIB_PATH)
from OriginFilter import UnattendUpgradeFilter
@ -124,7 +127,7 @@ DEVEL_UNTIL_RELEASE = datetime.timedelta(days=21)
# progress information is written here
PROGRESS_LOG = "/var/run/kylin-unattended-upgrades.progress"
PID_FILE = "/var/run/kylin-unattended-upgrades.pid"
LOCK_FILE = "/var/run/kylin-unattended-upgrades.lock"
LOCK_FILE = "/var/run/kylin-unattended-upgrade.lock"
NOTIFICATION_PIPE = '/tmp/notification.pipe'
# 禁止关机锁文件路径
FILELOCK_PATH = "/tmp/lock/"
@ -133,6 +136,24 @@ pidfile = None
# set from the sigint signal handler
SIGNAL_STOP_REQUEST = False
def get_default_version():
version = ""
INPUT_CONFIG_PATH = '/usr/share/kylin-update-desktop-config/data/kylin-update-desktop-system.json'
if os.path.isfile(INPUT_CONFIG_PATH): # 存在
# 读取JSON文件
with open(INPUT_CONFIG_PATH, "r") as f:
try :
data = json.load(f)
except json.JSONDecodeError as e:
logging.error(str(e))
try:
version = data['version']
if "=" in version:
version = version.split('=')[-1].strip()
except Exception as e:
logging.error(str(e))
return version
def ReadOsRelease(file):
osreleasedict={}
try:
@ -208,8 +229,9 @@ def unLockedEnableShutdown():
def get_white_list_with_version(srclist,list,namelist):
for name_with_version in srclist:
nvlist = name_with_version.strip().split('=',1)
list.append(nvlist)
namelist.append(nvlist[0])
if nvlist[0] != '' and nvlist[1] != '':
list.append(nvlist)
namelist.append(nvlist[0])
#global timeStamp
@ -2484,21 +2506,55 @@ def main(options, rootdir="/"):
try:
res = run(options, rootdir, logfile_dpkg)
localtime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
config_to_result = configparser.ConfigParser(allow_no_value=True)
config_to_result.add_section("OTA")
config_to_result.set("OTA","time",localtime)
config_to_result.set("OTA","version","1.0")
config_to_result.set("OTA","upgrade","0")
config_to_result.set("OTA","status","failed")
if res.success:
if options.mode == 'shutdown':
config_to_result.set("OTA","status","success")
if len(res.pkgs) > 0 :
config_to_result.set("OTA","upgrade","1")
with open(OTA_RESULT_FILE_PATH,"w+") as f:
config_to_result.write(f)
os.chmod(OTA_RESULT_FILE_PATH,stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
release = ''
version = ''
original_version = ''
if 'KYLIN_RELEASE_ID' in os_release_info:
release = os_release_info['KYLIN_RELEASE_ID']
version = ReadValueFromFile(VERSION_FILE,'SYSTEM','version')
original_version = get_default_version()
logging.debug("release:%s,version:%s,json version:%s"%(release,version,original_version))
if version == '' and release != '':
version = release
elif release == '' and version != '':
release = version
else:
release = original_version
release = original_version
if res.success and len(res.pkgs) > 0 and res.result_str == "total_install":
logging.info("total install")
kylin_system_updater.SetConfigValue("SYSTEM","kylin_release_id",release)
kylin_system_updater.SetConfigValue("SYSTEM","version",version)
else:
pass
if 'PROJECT_CODENAME' in os_release_info:
if os_release_info['PROJECT_CODENAME']=='V10SP1-edu':
if 'SUB_PROJECT_CODENAME' in os_release_info:
if os_release_info['SUB_PROJECT_CODENAME']=='mavis':
localtime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
config_to_result = configparser.ConfigParser(allow_no_value=True)
config_to_result.add_section("OTA")
config_to_result.set("OTA","time",localtime)
config_to_result.set("OTA","version","1.0")
config_to_result.set("OTA","upgrade","0")
config_to_result.set("OTA","status","failed")
if res.success:
if options.mode == 'shutdown':
config_to_result.set("OTA","status","success")
if len(res.pkgs) > 0 :
config_to_result.set("OTA","upgrade","1")
if not os.path.exists(OTA_RESULT_FILE_PATH):
os.makedirs(OTA_RESULT_FILE_PATH)
if not os.path.exists(OTA_RESULT_FILE):
f = open(OTA_RESULT_FILE,'w')
f.close()
with open(OTA_RESULT_FILE,"w+") as f:
config_to_result.write(f)
os.chmod(OTA_RESULT_FILE,stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
WriteValueToFile(UNATTENDED_UPGRADE_CONFIG_FILE_PATH,"UNATTENDED_UPGRADE","autoupdate_run_status","idle")
'''
if res.success and res.result_str:
@ -2589,7 +2645,7 @@ def run(options, # type: Options
# check if today is a patch day
# if not is_update_day():
# return UnattendedUpgradesResult(True)
logging.info(_("Starting unattended upgrades script at"))
logging.info(_("Starting unattended upgrades script"))
# check if u-u should be stopped already
if should_stop():
return UnattendedUpgradesResult(False)
@ -2780,7 +2836,7 @@ def run(options, # type: Options
else:
logging.info("no pkgs need to download")
#return UnattendedUpgradesResult(True,_("there're no pkgs to download"))
retry_times=apt_pkg.config.find_i("Unattended-Upgrade::RetryTimes")
retry_times=10
if retry_times<0:
retry_times = 1
while retry_times >0:
@ -2791,6 +2847,7 @@ def run(options, # type: Options
logging.error("fetch.run() result: %s", e)
fetcher_statistics.ResetFetcher(fetcher)
fetcher_statistics.GetAquireStatisticsOfPkgs()
logging.debug("incomplete download pkg number:%d"%fetcher_statistics.incomplete_pkg_amount)
retry_times-=1
if fetcher_statistics.incomplete_pkg_amount >0:
logging.debug("%d incomplete pkgs,%d try times left")
@ -2800,26 +2857,31 @@ def run(options, # type: Options
except SystemError as e:
logging.error(_("GetArchives() failed: %s"), e)
else:
docker_image_fetch_result = None
#docker image fetch for mavis and laika
if os.path.exists("/usr/bin/service_runtime_ota.sh"):
docker_image_fetch_result = subprocess.run(["/usr/bin/service_runtime_ota.sh"], shell=True)
if docker_image_fetch_result.returncode == 0:
logging.info("all pkgs downloaded")
insmod = ReadValueFromFile("/var/lib/unattended-upgrades/unattended-upgrades-policy.conf","autoUpgradePolicy","installmode")
if insmod == 'bshutdown':
if len(pkgs_to_upgrade) > 0:
configfilemanager.AddFileName("OTA_PKGS_TO_INSTALL")
subprocess.Popen('dbus-send --system --type=signal / com.kylin.update.notification.DownloadFinish', shell=True)
kylin_system_updater.SetConfigValue('InstallMode','auto_install','True')
elif insmod == 'timing':
pass
else:
pass
return UnattendedUpgradesResult(True,_("all pkgs downloaded"))
break
fetcher_statistics.ResetFetcher(fetcher)
fetcher_statistics.GetAquireStatisticsOfPkgs()
if fetcher_statistics.incomplete_pkg_amount == 0:
docker_image_fetch_result = None
#docker image fetch for mavis and laika
if os.path.exists("/usr/bin/service_runtime_ota.sh"):
docker_image_fetch_result = subprocess.run(["/usr/bin/service_runtime_ota.sh"], shell=True)
if docker_image_fetch_result.returncode == 0:
logging.info("all pkgs downloaded")
insmod = ReadValueFromFile("/var/lib/unattended-upgrades/unattended-upgrades-policy.conf","autoUpgradePolicy","installmode")
if insmod == 'bshutdown':
if len(pkgs_to_upgrade) > 0:
configfilemanager.AddFileName("OTA_PKGS_TO_INSTALL")
subprocess.Popen('dbus-send --system --type=signal / com.kylin.update.notification.DownloadFinish', shell=True)
kylin_system_updater.SetConfigValue('InstallMode','auto_install','True')
elif insmod == 'timing':
pass
else:
return UnattendedUpgradesResult(False,_("docker fetch failed"))
return UnattendedUpgradesResult(False,_("some pkgs incompletely fetched"))
pass
return UnattendedUpgradesResult(True,_("all pkgs downloaded"))
else:
return UnattendedUpgradesResult(False,_("docker fetch failed"))
else:
return UnattendedUpgradesResult(False,_("some pkgs incompletely fetched"))
else:
logging.debug(_("option is not install-only or download-only"))
try:
@ -3015,6 +3077,12 @@ def run(options, # type: Options
logging.error("cannot get shutdown lock,exiting...")
WriteValueToFile(UNATTENDED_UPGRADE_CONFIG_FILE_PATH,"UNATTENDED_UPGRADE","autoupdate_run_status","idle")
sys.exit(1)
logging.debug("InstCount=%i DelCount=%i BrokenCount=%i"
% (cache._depcache.inst_count,
cache._depcache.del_count,
cache._depcache.broken_count))
pkg_install_success = do_install(cache,
pkgs,
options,
@ -3040,8 +3108,8 @@ def run(options, # type: Options
False, _("Cache has broken packages, exiting"), pkgs=pkgs)
'''
# make sure we start autoremovals with a clear cache
if cache.get_changes():
cache.clear()
# if cache.get_changes():
# cache.clear()
'''
# the user wants *all* auto-removals to be removed
@ -3072,15 +3140,15 @@ def run(options, # type: Options
options.verbose or options.debug,
options.dry_run)
successful_run = successful_run and pkg_remove_success
'''
logging.debug("InstCount=%i DelCount=%i BrokenCount=%i"
% (cache._depcache.inst_count,
cache._depcache.del_count,
cache._depcache.broken_count))
# clean after success install (if needed)
#keep_key = "Unattended-Upgrade::Keep-Debs-After-Install"
'''
clean after success install (if needed)
keep_key = "Unattended-Upgrade::Keep-Debs-After-Install"
if (not apt_pkg.config.find_b(keep_key, False)
and not options.dry_run
and pkg_install_success):
@ -3094,8 +3162,13 @@ def run(options, # type: Options
kernel_pkgs_kept_installed + pkgs_kept_installed,
update_stamp=True)
'''
install_result = ''
if len(pkgs) == len(namelist_with_version):
install_result = "total_install"
else:
install_result = "partial_install"
return UnattendedUpgradesResult(pkg_install_success,
_("All upgrades installed"),
install_result,
pkgs)
@ -3218,7 +3291,7 @@ if __name__ == "__main__":
_setup_logging(options,logfile)
#get os release info
os_release_info = ReadOsRelease('/etc/os-release')
print(os_release_info)
#print(os_release_info)
config_manager = ConfigFileManager(CONFIG_FILE_ROOT_PATH)
kylin_system_updater = KylinSystemUpdater()
allow_autoupdate = kylin_system_updater.GetDatabaseInfo("display","autoupdate_allow")

View File

@ -346,6 +346,18 @@ class UnattendedUpgradesShutdown():
self.update_interface = dbus.Interface(self.update_proxy,dbus_interface='com.kylin.systemupgrade.interface')
return self.update_interface
def set_max_inhibit_time(self,time):
login_proxy = self.get_logind_proxy()
#首先设置systemd默认延长时间为1800
try:
getter_interface = dbus.Interface(
login_proxy,
dbus_interface='org.freedesktop.login1.Manager')
ret = getter_interface.SetInhibitDelayMaxSec(time)
except Exception as e:
logging.error(e)
def get_logind_proxy(self):
""" Get logind dbus proxy object """
if not self.logind_proxy:
@ -585,6 +597,7 @@ class UnattendedUpgradesShutdown():
self.get_update_interface()
self.update_proxy.connect_to_signal("ChangeUpgradePolicy",change_upgrade_policy_handler)
self.update_proxy.connect_to_signal("UpgradeAllNow",upgrade_all_now_handler)
self.set_max_inhibit_time(1800)
try:
self.get_logind_proxy().connect_to_signal(
"PrepareForShutdown", prepare_for_shutdown_handler)
@ -594,7 +607,7 @@ class UnattendedUpgradesShutdown():
"instead."))
logging.warning(
_("Maybe systemd-logind service is not running."))
self.run_polling(signal_handler)
# self.run_polling(signal_handler)
return
logging.debug("Waiting for signal to start operation ")
@ -609,6 +622,9 @@ class UnattendedUpgradesShutdown():
self.download_job = self.scheduler.add_job(self.timing_download, 'cron', hour=self.download_time_r['h'], minute=self.download_time_r['m'])
if os_release_info['PROJECT_CODENAME'] == 'V10SP1-edu' and os_release_info['SUB_PROJECT_CODENAME']=='mavis':
logging.info("mavis project")
logging.info("setting download timer")
t = threading.Timer(300,self.timing_download)
t.start()
else:
self.install_job = self.scheduler.add_job(self.timing_install, 'cron', hour=self.install_time_r['h'], minute=self.install_time_r['m'])
TimerThread(self.scheduler).start()