def _runExecThreads(self, execThreads, minProgress, maxProgress):
        """ run exec threads """
        if (len(execThreads) == 0):
            self._updateProgress(maxProgress)
            return
        
        # start with min progress
        self._updateProgress(minProgress)

        # now wait for the threads to complete and update progress
        while (True):
            self._checkStop()

            progress = 0
            running = False

            for execThread in execThreads:
                
                self.extendTimeout(execThread.getTimeout())
                status = AgentThread._runExeThread(self, execThread)

                progress += status['progress']

            if (not running):
                break

            # update the progress
            percent = progress / len(execThreads)

            self._updateProgress(calcProgress(minProgress, maxProgress, float(percent) / 100))

            time.sleep(float(pylons.config['exec_thread_sleep_time']))

        self._updateProgress(maxProgress)
    def _untarPackages(self, packages, service, untarRootPath, nicelevel=None, pgksNeedSuffix=None, pathSuffix=''):
        """
        untar all the packages

        @params packages - list of dictionary of parsed packages uri
        @throws AgentException
        """
        self._updateProgress(80)
        count = 0
        for pkgDict in packages.itervalues():
            count += 1

            # check if package path exists already
            # if the package is already untarred, move on
            # else create the package path
            pkgName = pkgDict['packageName']
            untarPath = os.path.join(untarRootPath,
                                    pkgName,
                                    '%s%s' % (pkgDict['packageVersion'], pathSuffix if ((pgksNeedSuffix is not None) and pkgName in pgksNeedSuffix) else ''))

            if os.path.exists(untarPath):
                # perhaps another thread has finished extraction, continue with next package
                continue
            
            os.makedirs(untarPath)

            try:
                self.untar(pkgDict['packagePath'], untarPath, nicelevel)

            except AgentException:
                rmrf(untarPath)

            # Note: i. atleast self should have 'rx' so that we can proceed setting 'rx' for group and others
            # ii. since cronus/cronusapp belong to diff group, lets give access to self, group and others.
            # if both belong to same group in future, then just self, group should be enough
            if (not os.name == 'nt'):
                # ensure all parent dir of scripts dir have 'rx' so that we can really navigate to scripts dir and execute
                uname = getuserofpath(untarPath)
                chmod(untarPath, '+rx', sudoUser = uname)
                cronusPath = os.path.join(untarPath, 'cronus')
                if os.path.exists(cronusPath):
                    uname = getuserofpath(cronusPath)
                    chmod(cronusPath, '+rx', sudoUser = uname)
                    # now give all scripts 'rx' permission
                    scriptsPath = os.path.join(cronusPath, 'scripts')
                    if os.path.exists(scriptsPath):
                        uname = getuserofpath(scriptsPath)
                        rchmod(scriptsPath, '+rx', sudoUser = uname)

                #Running as cronus user when higher privilege service (i.e. not chown all the package into the application user)
                if (not isHigherPrivilegeService(service)):
                    import pwd
                    uname = pylons.config['app_user_account']
                    uid = pwd.getpwnam(uname).pw_uid
                    gid = pwd.getpwnam(uname).pw_gid
                    rchown(untarPath, uid, gid)

            self._updateProgress(calcProgress(80, 99, float(count) / len(packages)))
Exemple #3
0
    def _runExecThreads(self, execThreads, minProgress, maxProgress):
        """ run exec threads """
        if (len(execThreads) == 0):
            self._updateProgress(maxProgress)
            return

        # start with min progress
        self._updateProgress(minProgress)

        # now wait for the threads to complete and update progress
        firstTime = True
        while (True):
            self._checkStop()

            progress = 0
            running = False

            for (execThread, dummy) in execThreads:
                #first time, push recovery stack
                if firstTime:
                    self._pushRecStack(execThread.getCmd())

                if dummy:
                    continue

                self.extendTimeout(execThread.getTimeout())
                status = AgentThread._runExeThread(self, (execThread, dummy))

                progress += status['progress']

            firstTime = False

            if (not running):
                break

            # update the progress
            percent = progress / len(execThreads)

            self._updateProgress(
                calcProgress(minProgress, maxProgress,
                             float(percent) / 100))

            time.sleep(float(pylons.config['exec_thread_sleep_time']))

        self._updateProgress(maxProgress)
    def _runExecThreads(self, execThreads, minProgress, maxProgress):
        """ run exec threads """
        if (len(execThreads) == 0):
            self._updateProgress(maxProgress)
            return

        # start with min progress
        self._updateProgress(minProgress)

        # now wait for the threads to complete and update progress
        firstTime = True
        while (True):
            self._checkStop()

            progress = 0
            running = False

            for (execThread, dummy) in execThreads:
                #first time, push recovery stack
                if firstTime:
                    self._pushRecStack(execThread.getCmd())
                
                if dummy:
                    continue
                
                self.extendTimeout(execThread.getTimeout())
                status = AgentThread._runExeThread(self, (execThread, dummy))

                progress += status['progress']

            firstTime = False

            if (not running):
                break

            # update the progress
            percent = progress / len(execThreads)

            self._updateProgress(calcProgress(minProgress, maxProgress, float(percent) / 100))

            time.sleep(float(pylons.config['exec_thread_sleep_time']))

        self._updateProgress(maxProgress)
    def _downloadPackages(self, packages, isFailedFatal = True):
        """
        download all the packages
        update progress
        check for timeout

        @params packages = list of package uri's
        @throws AgentException
        """
        try:
            appGlobal = pylons.config['pylons.app_globals']

            # globals all the remaining packages, download the packages
            pkgObjs = []
            for pkg in packages:
                packageKey = PackageUtil.getPackageKey(pkg)
                matchDlThreads = appGlobal.threadMgr.getThreadByCat(packageKey)
                if matchDlThreads:
                    # found an inprogress download thread
                    dlThread = matchDlThreads[0]
                else:
                    dlThread = DownloadThread(self._threadMgr, pkg, category=[packageKey], packageloc=None, parentId = self.getUuid())
                    contextutils.copyJobContexts(self, dlThread)
                    dlThread.start()

                if (dlThread.getStatus().get('progress') != 100):
                    pkgObjs.append(dlThread)

                self._checkStop()

            # check that we have packages to download
            if (len(pkgObjs) == 0):
                return [] # nothing to download, so no failed packages

            # now wait for all the packages to finish
            liveThreadCount = len(pkgObjs)
            timeoutNotSet = True
            create_sleep_time = float(pylons.config['exec_thread_sleep_time']) * liveThreadCount
            failed_packages = []
            
            while (liveThreadCount > 0):
                self._checkStop()
                LOG.info('%s packages still downloading' % liveThreadCount)
                time.sleep(create_sleep_time)

                # go through all the packages and
                # calculated average progress
                # check that all packages are alive
                totalProgress = 0
                liveThreadCount = 0

                timeouts = (0.0, 0.0)
                for dlThread in pkgObjs:
                    
                    # we are not done yet
                    if dlThread.isAlive():
                        liveThreadCount += 1
                        
                    # if one package failed, then we have to fail the entire manifest
                    threadStatus = dlThread.getStatus()
                    if (not dlThread.isAlive() and threadStatus.get('progress') != 100):
                        if isFailedFatal:
                            raise AgentException(Errors.DC_FAILED_DOWNLOAD,
                                             'failed downloading package (%s) - %s'
                                             % (dlThread.getUriDict().get('uri'), threadStatus.get('errorMsg')))
                        failed_packages.append(dlThread.getUriDict().get('uri'))
                        
                    progressTimeout = dlThread.getProgressTimeouts()
                    if (progressTimeout and timeouts):
                        timeouts = (timeouts[0] + progressTimeout[0], timeouts[1] + progressTimeout[1])
                    else:
                        timeouts = None
                    pkgProgress = threadStatus.get('progress')
                    totalProgress += pkgProgress
                    
                if (timeouts and timeoutNotSet):
                    # Setting the timeout once as it is absolute time.  Doing this after the timeout for each
                    # package is available
                    self.extendTimeout(timeouts[0])
                    timeoutNotSet = False
                    LOG.debug('Using overall timeout=%s and progress timeout=%s' % (timeouts[0], timeouts[1]))

                # calculate the new progress
                # I'm allocating 80 of the progress to be the download
                newProgress = calcProgress(1, 79, float(totalProgress) / (100 * len(pkgObjs)))

                # now update the pgoress
                self._updateProgress(newProgress)


            return failed_packages

        finally:
            # now update the pgoress
            self._updateProgress(79)
    def _untarPackages(self, packages, service, untarRootPath, nicelevel=None, pgksNeedSuffix=None, pathSuffix=''):
        """
        untar all the packages

        @params packages - list of dictionary of parsed packages uri
        @throws AgentException
        """
        self._updateProgress(80)
        count = 0
        for pkgDict in packages.itervalues():
            count += 1

            # check if package path exists already
            # if the package is already untarred, move on
            # else create the package path
            pkgName = pkgDict['packageName']
            untarPath = os.path.join(untarRootPath,
                                    pkgName,
                                    '%s%s' % (pkgDict['packageVersion'], pathSuffix if ((pgksNeedSuffix is not None) and pkgName in pgksNeedSuffix) else ''))

            if os.path.exists(untarPath):
                # perhaps another thread has finished extraction, continue with next package
                continue
            
            os.makedirs(untarPath)

            try:
                self.untar(pkgDict['packagePath'], untarPath, nicelevel)

            except AgentException:
                rmrf(untarPath)

            # Note: i. atleast self should have 'rx' so that we can proceed setting 'rx' for group and others
            # if both belong to same group in future, then just self, group should be enough
            # ensure all parent dir of scripts dir have 'rx' so that we can really navigate to scripts dir and execute
            uname = getuserofpath(untarPath)
            chmod(untarPath, '+rx', sudoUser = uname)
            cronusPath = os.path.join(untarPath, 'cronus')
            if os.path.exists(cronusPath):
                uname = getuserofpath(cronusPath)
                chmod(cronusPath, '+rx', sudoUser = uname)
                # now give all scripts 'rx' permission
                scriptsPath = os.path.join(cronusPath, 'scripts')
                if os.path.exists(scriptsPath):
                    uname = getuserofpath(scriptsPath)
                    rchmod(scriptsPath, '+rx', sudoUser = uname)

            # issue #16, now add symlink to .appdata for easy access
            appdata_path = manifestutil.appDataPath(service)
            link_path = os.path.join(untarPath, '.appdata')
            LOG.info('Create .appdata symlink from %s to %s' % (link_path, appdata_path))
            utils.symlink(appdata_path, link_path)

            
            #Running as cronus user when higher privilege service (i.e. not chown all the package into the application user)
            if (not isHigherPrivilegeService(service)):
                uname = configutil.getAppUser()
                uid, gid = utils.getUidGid(uname)
                rchown(untarPath, uid, gid)

            self._updateProgress(calcProgress(80, 99, float(count) / len(packages)))
    def _downloadPackages(self, packages, isFailedFatal = True, skipProp = True):
        """
        download all the packages
        update progress
        check for timeout

        @params packages = list of package uri's
        @throws AgentException
        """
        try:
            appGlobal = pylons.config['pylons.app_globals']

            # globals all the remaining packages, download the packages
            pkgObjs = []
            for pkg in packages:
                self._checkStop()

                dlThread = appGlobal.packageMgr.getPackageDownloadThread(pkg, skipProp = skipProp)
                if (dlThread.getStatus().get('progress') != 100):
                    pkgObjs.append(dlThread)

            # check that we have packages to download
            if (len(pkgObjs) == 0):
                return [] # nothing to download, so no failed packages

            # now wait for all the packages to finish
            liveThreadCount = len(pkgObjs)
            timeoutNotSet = True
            create_sleep_time = float(pylons.config['exec_thread_sleep_time']) * liveThreadCount
            failed_packages = []
            
            while (liveThreadCount > 0):
                self._checkStop()
                LOG.info('%s packages still downloading' % liveThreadCount)
                time.sleep(create_sleep_time)

                # go through all the packages and
                # calculated average progress
                # check that all packages are alive
                totalProgress = 0
                liveThreadCount = 0

                timeouts = (0.0, 0.0)
                for dlThread in pkgObjs:
                    
                    # we are not done yet
                    if dlThread.isAlive():
                        liveThreadCount += 1
                        
                    # if one package failed, then we have to fail the entire manifest
                    threadStatus = dlThread.getStatus()
                    if (not dlThread.isAlive() and threadStatus.get('progress') != 100):
                        if isFailedFatal:
                            raise AgentException(Errors.DC_FAILED_DOWNLOAD,
                                             'failed downloading package (%s) - %s'
                                             % (dlThread.getUriDict().get('uri'), threadStatus.get('errorMsg')))
                        failed_packages.append(dlThread.getUriDict().get('uri'))
                        
                    progressTimeout = dlThread.getProgressTimeouts()
                    if (progressTimeout and timeouts):
                        timeouts = (timeouts[0] + progressTimeout[0], timeouts[1] + progressTimeout[1])
                    else:
                        timeouts = None
                    pkgProgress = threadStatus.get('progress')
                    totalProgress += pkgProgress
                    
                if (timeouts and timeoutNotSet):
                    # Setting the timeout once as it is absolute time.  Doing this after the timeout for each
                    # package is available
                    self.extendTimeout(timeouts[0])
                    timeoutNotSet = False
                    LOG.debug('Using overall timeout=%s and progress timeout=%s' % (timeouts[0], timeouts[1]))

                # calculate the new progress
                # I'm allocating 80 of the progress to be the download
                newProgress = calcProgress(1, 79, float(totalProgress) / (100 * len(pkgObjs)))

                # now update the pgoress
                self._updateProgress(newProgress)


            return failed_packages

        finally:
            # now update the pgoress
            self._updateProgress(79)