# -*- coding: utf-8 -*- from setuphelpers import * import os import _winreg import tempfile import shutil import hashlib import time import pythoncom from win32com.taskscheduler import taskscheduler import platform import codecs # registry key(s) where WAPT will find how to remove the application(s) uninstallkey = [] import types,re class Version(object): """Version object of form 0.0.0 can compare with respect to natural numbering and not alphabetical >>> Version('0.10.2') > Version('0.2.5') True >>> Version('0.1.2') < Version('0.2.5') True >>> Version('0.1.2') == Version('0.1.2') True """ def __init__(self,version,members_count=None): if version is None: version = '' assert isinstance(version,types.ModuleType) or isinstance(version,str) or isinstance(version,unicode) or isinstance(version,Version) if isinstance(version,types.ModuleType): self.versionstring = version.__version__ elif isinstance(version,Version): self.versionstring = version.versionstring else: self.versionstring = version self.members = [ v.strip() for v in self.versionstring.split('.')] if members_count is not None: if len(self.members) %(created_on)s WAPT %(run_on)s %(expired_on)s PT1H true S-1-5-18 HighestAvailable IgnoreNew false false true true false true false true true false false false PT1H PT0S 7 %(cmd)s %(parameters)s """ def create_onetime_task(name,cmd,parameters=None, delay_minutes=2,max_runtime=10, retry_count=3,retry_delay_minutes=1): """creates a one time Windows scheduled task and activate it. """ run_time = time.localtime(time.time() + delay_minutes*60) # task if windows_version() <= Version('5.2',2): # for win XP system_account = r'"NT AUTHORITY\SYSTEM"' # windows xp doesn't support one time startup task /Z nor /F hour_min = time.strftime('%H:%M:%S', run_time) run_notfatal('schtasks /Delete /TN "%s" /F'%name) return run('schtasks /Create /SC ONCE /TN "%s" /TR "%s %s" /ST %s /RU %s' % (name,cmd,parameters,hour_min,system_account)) else: system_account = 'SYSTEM' xmlfile = tempfile.mktemp('.xml') created_on = time.strftime('%Y-%m-%dT%H:%M:%S',time.localtime(time.time())) run_on = time.strftime('%Y-%m-%dT%H:%M:%S',run_time) expired_on = time.strftime('%Y-%m-%dT%H:%M:%S',time.localtime(time.time() + 24*3600)) codecs.open(xmlfile,'wb',encoding='utf8').write(TASK_TEMPLATE % locals()) result = run('schtasks /Create /F /TN "%s" /XML "%s"' % (name,xmlfile)) if isfile(xmlfile): remove_file(xmlfile) return result def full_waptagent_install(min_version,at_startup=False): # get it from waptagent_path = makepath(tempfile.gettempdir(),'waptagent.exe') waptdeploy_path = makepath(tempfile.gettempdir(),'waptdeploy.exe') if isfile(waptdeploy_path): killalltasks('waptdeploy.exe') killalltasks('waptagent.exe') remove_file(waptdeploy_path) filecopyto(makepath('patchs','waptdeploy.exe'),waptdeploy_path) expected_sha256 = open('waptagent.sha256','r').read().splitlines()[0].split()[0] if isfile('waptagent.exe'): filecopyto('waptagent.exe',waptagent_path) if not isfile(waptagent_path) or sha256_for_file(waptagent_path) != expected_sha256: download_waptagent(waptagent_path,expected_sha256) #create_onetime_task('fullwaptupgrade',waptagent_path,'/VERYSILENT',delay_minutes=15) if at_startup or isrunning('waptexit.exe'): cmd = '%s --hash=%s --waptsetupurl=%s --wait=15 --temporary --force --minversion=%s' %(waptdeploy_path,expected_sha256,waptagent_path,min_version) if not at_startup: print('waptexit is running, scheduling a one time task at system startup with command %s'%cmd) # task at system startup try: print run('schtasks /Create /RU SYSTEM /SC ONSTART /TN fullwaptupgrade /TR "%s" /F /V1 /Z' % cmd) except: # windows xp doesn't support one time startup task /Z nor /F run_notfatal('schtasks /Delete /TN fullwaptupgrade /F') print run('schtasks /Create /RU SYSTEM /SC ONSTART /TN fullwaptupgrade /TR "%s"' % cmd) else: # use embedded waptagent.exe, wait 15 minutes for other tasks to complete. print create_onetime_task('fullwaptupgrade',waptdeploy_path,'--hash=%s --waptsetupurl=%s --wait=15 --temporary --force --minversion=%s'%(expected_sha256,waptagent_path,min_version),delay_minutes=1) def install(): # if you want to modify the keys depending on environment (win32/win64... params..) import common if installed_softwares('WAPT Server_is1'): error('Wapt server installed on this host. Aborting') """ installed_wapt = installed_softwares(uninstallkey='WAPT_is1') if installed_wapt: wapt_version = installed_wapt[0]['version'].replace('WAPT ','') if '-' in wapt_version: (installed_wapt_version,installed_packaging) = wapt_version.split('-') installed_packaging = int(installed_packaging) else: installed_wapt_version = wapt_version installed_packaging = 0 else: print('WAPT current version can not be found in registry (key is "WAPT_is1") ...full upgrade') installed_wapt_version = '0.0.0' installed_packaging = 0 """ status = WAPT.wapt_status() installed_wapt_version = status['wapt-exe-version'] # get upgrade package informations (package_wapt_version,package_packaging) = control.version.split('-') package_packaging = int(package_packaging) if Version(installed_wapt_version,3) > Version(package_wapt_version,3): print('Your current wapt (%s) is more recent than the upgrade package (%s). Skipping...'%(installed_wapt_version,control.version)) elif Version(installed_wapt_version,3) < Version(package_wapt_version,3): print('Your current wapt version (%s) is too old to be upgraded, a full waptagent.exe install will be planned for %s'%(installed_wapt_version,time.ctime(time.time() + 1*60))) full_waptagent_install(str(Version(package_wapt_version,4))) else: print(u'Partial upgrade of WAPT client') killalltasks('wapttray.exe') killalltasks('waptconsole.exe') def onerror(srcname,dstname,e): print u"Error %s %s %s" %(srcname,dstname,ensure_unicode(e)) if e[0] == 5: # locked filecopyto(srcname,dstname+'.pending') replace_at_next_reboot(None, dstname) else: raise e def check_exe_version(src,dst): if os.path.splitext(dst)[1] in ('.exe','.dll'): try: ov = get_file_properties(dst)['FileVersion'] nv = get_file_properties(src)['FileVersion'] return Version(ov)