def test_multiple_manifest_create(self): """ when a manifest creation is in progress for a service, another creation should block """ packages = ["http://repository.qa.ebay.com/cronus/test-data/agent/pkgA-1.2.0.unix.cronus"] service = 'foo' manifest1 = 'bar' manifest2 = 'car' try: path = ServiceController.manifestPath(service) os.makedirs(path) path = ServiceController.installedPkgPath(service) os.makedirs(path) except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) body = json.dumps({'package' : packages}) response1 = self.app.post(url(controller = 'manifest', action = 'post', service = service, manifest = manifest1), headers = {'Content-Type' : 'application/json'}, params = body) self.assertEquals(response1.status_int, 200, 'Manifest1 Post assert - should go through') try: response2 = self.app.post(url(controller = 'manifest', action = 'post', service = service, manifest = manifest2), headers = {'Content-Type' : 'application/json'}, params = body) self.assertFalse(True, 'Expected an exception but did not get one!') except AppError: pass checkStatus(self, 'create manifest bar', response1, timeout = 25) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'pkgA'))) self.assertFalse(islink(os.path.join(ManifestController.manifestPath('foo', 'car'), 'pkgA')))
def test_multiple_manifest_create(self): """ when a manifest creation is in progress for a service, another creation should block """ packages = ["http://www.stackscaling.com/agentrepo/pkgA-1.2.0.unix.cronus"] service = 'foo' manifest1 = 'bar' manifest2 = 'car' try: for pkg in packages: mockDownloadPkg(pkg) path = ServiceController.manifestPath(service) os.makedirs(path) path = ServiceController.installedPkgPath(service) os.makedirs(path) except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) body = json.dumps({'package' : packages}) response1 = self.app.post(url(controller = 'manifest', action = 'post', service = service, manifest = manifest1), headers = {'Content-Type' : 'application/json'}, params = body) self.assertEquals(response1.status_int, 200, 'Manifest1 Post assert - should go through') try: response2 = self.app.post(url(controller = 'manifest', action = 'post', service = service, manifest = manifest2), headers = {'Content-Type' : 'application/json'}, params = body) self.assertFalse(True, 'Expected an exception but did not get one!') except AppError: pass checkStatus(self, 'create manifest bar', response1, timeout = 25) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'pkgA'))) self.assertFalse(islink(os.path.join(manifestutil.manifestPath('foo', 'car'), 'pkgA')))
def getAllSymLinks(service): """ return all the symlinks from manifests to packages for a given service""" LOG.debug('calling getAllSymLinks %s' % service) linkedPaths = [] manPath = ServiceController.manifestPath(service) LOG.debug('manifestPath is %s' % manPath) for path in os.listdir(manPath): LOG.debug('path is %s' % path) if not islink(os.path.join(manPath, path)): LOG.debug('path is dir not a link') pkgPaths = [ packageDir for packageDir in os.listdir(os.path.join(manPath, path)) ] LOG.debug('pkgPaths is %s' % pkgPaths) for pkgPath in pkgPaths: try: LOG.debug('pkgPath is %s' % pkgPath) if not os.path.isfile( os.path.join( manPath, path, pkgPath)) and islink( os.path.join(manPath, path, pkgPath)): LOG.debug('pkgPaths is %s' % pkgPath) targetPath = os.path.abspath( readlink(os.path.join(manPath, path, pkgPath))) linkedPaths.append(targetPath) LOG.debug('targetPath is %s ' % targetPath) except BaseException as exc: LOG.error('failed to read link for the pkg path %s' % str(exc)) return linkedPaths
def test_post2(self): # successful post createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(manifestutil.installedPkgRootPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'perlserver'))) self.assertTrue(islink(os.path.join(manifestutil.installedPkgRootPath('foo'), 'perlserver', '1.0.0.unix', '.appdata')))
def test_post_manifest_inprogress_ok(self): service = 'foo' manifest = 'blahblah' try: path = ServiceController.servicePath(service) if os.path.exists(path): if os.name == 'nt': cmd = 'rm -r %s' % path LOG.debug("running command %s" % cmd) os.system(cmd) else: shutil.rmtree(path) path = ServiceController.manifestPath(service) os.makedirs(path) path = ServiceController.installedPkgPath(service) os.makedirs(path) inProgressPath = ManifestCreate.inProgress(ManifestController.manifestPath(service, manifest)) os.makedirs(inProgressPath) path = ServiceController.downloadedPkgPath(service) os.makedirs(path) except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) createManifest(self, ["http://github.com/yubin154/cronusagent/blob/master/agent/agent/tests/unit/packages/pkgA-1.2.0.unix.cronus"], manifest = 'blahblah', createDirs = False) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'blahblah'), 'pkgA')))
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False activeManifest = None try: activePath = manifestutil.manifestPath(self._service, 'active') # make sure that the active path exists and it is a link # Should we check this again since we already have a check in action controller if not os.path.exists(activePath) or not islink(activePath): raise AgentException(error = Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg = 'No active manifest - cannot restart service') activeManifest = os.path.basename(readlink(activePath)) self.__lcmActionManifest(self._service, activeManifest, self.__action) self._LOG.info('Done: %s service for (%s/%s)' % (self.__action, self._service, activeManifest)) self._updateStatus(progress = 100) except AgentException as exc: failed = True errorMsg = '%s Service - Agent Exception - %s' % (self.__action, exc.getMsg()) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = '%s Service - Unknown error - (%s/%s) - %s - %s' \ % (self.__action, self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self._LOG.error(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg)
def activateManifest(testController, manifest = 'bar', service = 'foo'): body = json.dumps({'manifest':manifest}) response = testController.app.post(url(controller = 'manifest', action = 'activate', service = service, manifest = manifest), headers = {'Content-Type' : 'application/json'}, params = body) assert response.status_int == 200, 'Action get assert' body = json.loads(response.body) tm = time.time() while (tm + 120 > time.time()): response = testController.app.get(body['status']) body = json.loads(response.body) LOG.debug("activateManifest ********** progress = %s" % body['progress']) if (int(body['progress']) == 100): break time.sleep(0.1) LOG.debug('status = ' + str(response.status_int)) assert response.status_int == 200, "HTTP response != 200" LOG.debug ('Status response body = %s' % str(body)) assert body['progress'] == 100 # let's make sure the link is there correctly activePath = os.path.join(ServiceController.manifestPath(service), 'active') LOG.debug ('active path = ' + activePath) assert islink(activePath) link = readlink(activePath) LOG.debug ('link = ' + link) assert link == manifest
def test_post2(self): # successful post createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'perlserver')))
def test_package_reuse(self): createManifest(self) body = json.dumps({'package' : ['/packages/perlserver']}) body = self.app.post(url(controller = 'manifest', action = 'post', service = "foo", manifest = "baz"), headers = {'Content-Type' : 'application/json'}, params = body, expect_errors = True) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'perlserver'))) for _ in range(10): if islink(os.path.join(ManifestController.manifestPath('foo', 'baz'), 'perlserver')): break time.sleep(1) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'baz'), 'perlserver')))
def test_package_reuse(self): createManifest(self) body = json.dumps({'package' : ['/packages/perlserver']}) body = self.app.post(url(controller = 'manifest', action = 'post', service = "foo", manifest = "baz"), headers = {'Content-Type' : 'application/json'}, params = body, expect_errors = True) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'perlserver'))) for _ in range(10): if islink(os.path.join(manifestutil.manifestPath('foo', 'baz'), 'perlserver')): break time.sleep(1) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'baz'), 'perlserver')))
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False try: activePath = os.path.join( ServiceController.manifestPath(self._service), 'active') oldManifest = None # make sure that if the active path exists, it's a link # if not log that and delete the link if (os.path.exists(activePath) and not os.name == 'nt' and not islink(activePath)): self.__LOG.error('%s is not a link. Attempted to delete' % activePath) shutil.rmtree(activePath) if (os.path.exists(activePath)): oldManifest = os.path.basename(readlink(activePath)) else: raise AgentException( error=Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg='No active manifest - cannot deactivate service') self.__deactivateManifest(self._service, oldManifest) self.__removeSymlink(self._service) except SystemExit as exc: failed = True if (len(exc.args) == 2): # ok we got {err code, err msg} errorCode = exc.args[0] errorMsg = exc.args[1] raise exc except AgentException as exc: failed = True errorMsg = 'Deactivate Manifest - Agent Exception - %s' % exc.getMsg( ) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Deactivate Manifest - Unknown error - (%s) - %s - %s' \ % (self._service, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self.__LOG.warning(errorMsg) self._updateStatus(httpStatus=500, error=errorCode, errorMsg=errorMsg) self.__LOG.debug('Done: activate manifest for (%s)' % (self._service)) self._updateProgress(100)
def test_same_pkg_download_parallel(self): packages = ['http://www.stackscaling.com/agentrepo/pkgA-1.2.0.unix.cronus'] manifest1 = 'bar' manifest2 = 'blah' service1 = 'foo' service2 = 'lah' try: for pkg in packages: mockDownloadPkg(pkg) path = ServiceController.manifestPath(service1) os.makedirs(path) path = ServiceController.installedPkgPath(service1) os.makedirs(path) path = ServiceController.downloadedPkgPath(service1) os.makedirs(path) path = ServiceController.manifestPath(service2) os.makedirs(path) path = ServiceController.installedPkgPath(service2) os.makedirs(path) path = ServiceController.downloadedPkgPath(service2) os.makedirs(path) except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) body = json.dumps({'package' : packages}) response1 = self.app.post(url(controller = 'manifest', action = 'post', service = service1, manifest = manifest1), headers = {'Content-Type' : 'application/json'}, params = body) assert response1.status_int == 200, 'Manifest Post assert' response2 = self.app.post(url(controller = 'manifest', action = 'post', service = service2, manifest = manifest2), headers = {'Content-Type' : 'application/json'}, params = body) assert response2.status_int == 200, 'Manifest Post assert' checkStatus(self, 'create manifest bar', response1, timeout = 25) checkStatus(self, 'create manifest baz', response2, timeout = 25) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'pkgA-1.2.0.unix.cronus'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'pkgA'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('lah', 'blah'), 'pkgA')))
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False activeManifest = None try: activePath = manifestutil.manifestPath(self._service, 'active') # make sure that the active path exists and it is a link # Should we check this again since we already have a check in action controller if not os.path.exists(activePath) or not islink(activePath): raise AgentException(error = Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg = 'Service %s has no active manifest' % self._service) activeManifest = os.path.basename(readlink(activePath)) if self.__action == StartStopService.ACTION_SHUTDOWN: self._shutdownManifest(self._service, activeManifest, 50, 90) elif self.__action == StartStopService.ACTION_STARTUP: self._startupManifest(self._service, activeManifest, 50, 90) elif self.__action == StartStopService.ACTION_RESTART: self._restartManifest(self._service, activeManifest, 10, 90) else: raise AgentException(error = Errors.INVALID_LIFECYCLE_ACTION, errorMsg = 'Invalid life cycle action - %s' % self.__action) self._LOG.info('Done: %s service for (%s/%s)' % (self.__action, self._service, activeManifest)) self._updateStatus(progress = 100) except AgentException as exc: failed = True errorMsg = '%s Service - Agent Exception - %s' % (self.__action, exc.getMsg()) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = '%s Service - Unknown error - (%s/%s) - %s - %s' \ % (self.__action, self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self._LOG.error(errorMsg) if not self._skipCleanupOnFailure() and self.__action != StartStopService.ACTION_SHUTDOWN and self._service and activeManifest: try: self._LOG.info('%s Service %s failed, shutdown to cleanup' % (self.__action, self._service)) self._shutdownManifest(self._service, activeManifest, 91, 99) except BaseException as excep: self._LOG.error('Cleanup failed - %s' % str(excep)) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg)
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False activeManifest = None try: activePath = os.path.join( ServiceController.manifestPath(self._service), 'active') # make sure that the active path exists and it is a link # Should we check this again since we already have a check in action controller if not os.path.exists(activePath) or not islink(activePath): raise AgentException( error=Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg='No active manifest - cannot reset service') activeManifest = os.path.basename(readlink(activePath)) self.__shutdownManifest(self._service, activeManifest) self.__deactivateManifest(self._service, activeManifest) self.__activateManifest(self._service, activeManifest) self.__startupManifest(self._service, activeManifest) self.__LOG.info('Done: reset service for (%s/%s)' % (self._service, activeManifest)) self._updateStatus(progress=100) except AgentException as exc: failed = True errorMsg = 'Activate Manifest - Agent Exception - %s' % exc.getMsg( ) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Activate Manifest - Unknown error - (%s/%s) - %s - %s' \ % (self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self.__LOG.error(errorMsg) if self._service and activeManifest: try: self.__LOG.info( 'Reset service %s failed, shutdown to cleanup' % self._service) self.__shutdownManifest(self._service, activeManifest) except BaseException as excep: self.__LOG.error('Cleanup failed - %s' % str(excep)) self._updateStatus(httpStatus=500, error=errorCode, errorMsg=errorMsg)
def test_same_pkg_download_parallel(self): packages = ['http://repository.qa.ebay.com/cronus/test-data/agent/perlserver-1.0.0.unix.cronus'] manifest1 = 'bar' manifest2 = 'blah' service1 = 'foo' service2 = 'lah' try: path = ServiceController.manifestPath(service1) os.makedirs(path) path = ServiceController.installedPkgPath(service1) os.makedirs(path) path = ServiceController.downloadedPkgPath(service1) os.makedirs(path) path = ServiceController.manifestPath(service2) os.makedirs(path) path = ServiceController.installedPkgPath(service2) os.makedirs(path) path = ServiceController.downloadedPkgPath(service2) os.makedirs(path) except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) body = json.dumps({'package' : packages}) response1 = self.app.post(url(controller = 'manifest', action = 'post', service = service1, manifest = manifest1), headers = {'Content-Type' : 'application/json'}, params = body) assert response1.status_int == 200, 'Manifest Post assert' response2 = self.app.post(url(controller = 'manifest', action = 'post', service = service2, manifest = manifest2), headers = {'Content-Type' : 'application/json'}, params = body) assert response2.status_int == 200, 'Manifest Post assert' checkStatus(self, 'create manifest bar', response1, timeout = 25) checkStatus(self, 'create manifest baz', response2, timeout = 25) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'perlserver'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('lah', 'blah'), 'perlserver')))
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False activeManifest = None try: activePath = manifestutil.manifestPath(self._service, 'active') # make sure that the active path exists and it is a link # Should we check this again since we already have a check in action controller if not os.path.exists(activePath) or not islink(activePath): raise AgentException(error = Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg = 'No active manifest - cannot restart service') activeManifest = os.path.basename(readlink(activePath)) if self.__action == StartStopService.ACTION_SHUTDOWN: self.__shutdownManifest(self._service, activeManifest) elif self.__action == StartStopService.ACTION_STARTUP: self.__startupManifest(self._service, activeManifest) elif self.__action == StartStopService.ACTION_RESTART: self.__restartManifest(self._service, activeManifest) elif self.__action == StartStopService.ACTION_REBOOT: self.__rebootManifest(self._service, activeManifest) else: raise AgentException(error = Errors.INVALID_LIFECYCLE_ACTION, errorMsg = 'Invalid life cycle action - %s' % self.__action) self.__LOG.info('Done: %s service for (%s/%s)' % (self.__action, self._service, activeManifest)) self._updateStatus(progress = 100) except AgentException as exc: failed = True errorMsg = '%s Service - Agent Exception - %s' % (self.__action, exc.getMsg()) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = '%s Service - Unknown error - (%s/%s) - %s - %s' \ % (self.__action, self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self.__LOG.error(errorMsg) if not self._skipCleanupOnFailure() and self.__action != StartStopService.ACTION_SHUTDOWN and self._service and activeManifest: try: self.__LOG.info('%s Service %s failed, shutdown to cleanup' % (self.__action, self._service)) self.__shutdownManifest(self._service, activeManifest) except BaseException as excep: self.__LOG.error('Cleanup failed - %s' % str(excep)) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg)
def getManifests(service): """ return the list of services under this agent """ manifests = [] rootPath = manifestRootPath(service) dirContent = os.listdir(rootPath) for item in dirContent: path = os.path.join(rootPath, item) if (os.path.isdir(path) and not path.endswith('.inprogress')): if (not utils.islink(path)): manifests.append(item) return sorted(manifests)
def test_post3_pkg_already_installed(self): createManifest(self) # now lets remove the manifest path path = os.path.join(manifestutil.manifestPath('foo', 'bar')) shutil.rmtree(path) # now create the manifest again createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'perlserver')))
def getManifests(service): """ return the list of manifests under a specific service """ from agent.lib.agent_thread.manifest_create import ManifestCreate manifests = [] dirContent = os.listdir(ServiceController.manifestPath(service)) for item in dirContent: path = os.path.join(ServiceController.manifestPath(service), item) if (os.path.isdir(path) and not ManifestCreate.isInProgress(path)): if (not islink(path)): manifests.append(item) return sorted(manifests)
def getAllSymLinks(service): """ return all the symlinks from manifests to packages for a given service""" LOG.debug('calling getAllSymLinks %s' % service) linkedPaths = [] manPath = manifestRootPath(service) LOG.debug('manifestPath is %s' % manPath) for path in os.listdir(manPath): LOG.debug('path is %s' % path) if not islink(os.path.join(manPath, path)): LOG.debug('path is dir not a link') pkgPaths = [ packageDir for packageDir in os.listdir(os.path.join(manPath, path)) ] LOG.debug('pkgPaths is %s' % pkgPaths) for pkgPath in pkgPaths: try: LOG.debug('pkgPath is %s' % pkgPath) if not os.path.isfile(os.path.join(manPath, path, pkgPath)) and islink(os.path.join(manPath, path, pkgPath)): LOG.debug('pkgPaths is %s' % pkgPath) targetPath = os.path.abspath(readlink(os.path.join(manPath, path, pkgPath))) linkedPaths.append(targetPath) LOG.debug('targetPath is %s ' % targetPath) except BaseException as exc: LOG.error('failed to read link for the pkg path %s' % str(exc)) return linkedPaths
def test_post3_already_installed_manifest(self): createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'perlserver'))) body = json.dumps({'package' : ['http://github.com/yubin154/cronusagent/blob/master/agent/agent/tests/unit/packages/perlserver-1.0.0.unix.cronus']}) response = self.app.post(url(controller = 'manifest', action = 'post', service = "foo", manifest = "bar"), headers = {'Content-Type' : 'application/json'}, params = body, expect_errors = True) self.assertEquals(201, response.status_int, 'Manifest Post assert') body = json.loads(response.body) assert response.status_int == 201, 'Manifest Post assert'
def test_post3_already_installed_manifest(self): createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'perlserver'))) body = json.dumps({'package' : ['http://github.com/yubin154/cronusagent/blob/master/agent/agent/tests/unit/packages/perlserver-1.0.0.unix.cronus']}) response = self.app.post(url(controller = 'manifest', action = 'post', service = "foo", manifest = "bar"), headers = {'Content-Type' : 'application/json'}, params = body, expect_errors = True) self.assertEquals(201, response.status_int, 'Manifest Post assert') body = json.loads(response.body) assert response.status_int == 201, 'Manifest Post assert'
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False try: activePath = os.path.join(ServiceController.manifestPath(self._service), 'active') oldManifest = None # make sure that if the active path exists, it's a link # if not log that and delete the link if (os.path.exists(activePath) and not os.name == 'nt' and not islink(activePath)): self.__LOG.error('%s is not a link. Attempted to delete' % activePath) shutil.rmtree(activePath) if (os.path.exists(activePath)): oldManifest = os.path.basename(readlink(activePath)) else: raise AgentException(error = Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg = 'No active manifest - cannot deactivate service') self.__deactivateManifest(self._service, oldManifest) self.__removeSymlink(self._service) except SystemExit as exc: failed = True if (len(exc.args) == 2): # ok we got {err code, err msg} errorCode = exc.args[0] errorMsg = exc.args[1] raise exc except AgentException as exc: failed = True errorMsg = 'Deactivate Manifest - Agent Exception - %s' % exc.getMsg() errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Deactivate Manifest - Unknown error - (%s) - %s - %s' \ % (self._service, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self.__LOG.warning(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg) self.__LOG.debug('Done: activate manifest for (%s)' % (self._service)) self._updateProgress(100)
def getModuleSymLinks(service = 'agent'): """ return all the symlinks from manifests to packages for a given service""" LOG.debug('calling getAllSymLinks %s' % service) linkedPaths = [] mRootPath = moduleRootPath(service) LOG.debug('moduleRootPath is %s' % mRootPath) for pkgPath in os.listdir(mRootPath): try: LOG.debug('pkgPath is %s' % pkgPath) if not os.path.isfile(os.path.join(mRootPath, pkgPath)) and islink(os.path.join(mRootPath, pkgPath)): targetPath = os.path.abspath(readlink(os.path.join(mRootPath, pkgPath))) linkedPaths.append(targetPath) LOG.debug('targetPath is %s ' % targetPath) except BaseException as exc: LOG.error('failed to read link for the pkg path %s' % str(exc)) return linkedPaths
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None failed = False activeManifest = None try: activePath = os.path.join(ServiceController.manifestPath(self._service), 'active') # make sure that the active path exists and it is a link # Should we check this again since we already have a check in action controller if not os.path.exists(activePath) or not islink(activePath): raise AgentException(error = Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg = 'No active manifest - cannot reset service') activeManifest = os.path.basename(readlink(activePath)) self.__shutdownManifest(self._service, activeManifest) self.__deactivateManifest(self._service, activeManifest) self.__activateManifest(self._service, activeManifest) self.__startupManifest(self._service, activeManifest) self.__LOG.info('Done: reset service for (%s/%s)' % (self._service, activeManifest)) self._updateStatus(progress = 100) except AgentException as exc: failed = True errorMsg = 'Activate Manifest - Agent Exception - %s' % exc.getMsg() errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Activate Manifest - Unknown error - (%s/%s) - %s - %s' \ % (self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: self.__LOG.error(errorMsg) if self._service and activeManifest: try: self.__LOG.info('Reset service %s failed, shutdown to cleanup' % self._service) self.__shutdownManifest(self._service, activeManifest) except BaseException as excep: self.__LOG.error('Cleanup failed - %s' % str(excep)) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg)
def test_inprogress_pkg_download(self): service = 'foo' try: path = ServiceController.manifestPath(service) os.makedirs(path) path = ServiceController.installedPkgPath(service) os.makedirs(path) path = ServiceController.downloadedPkgPath(service) os.makedirs(path) inprogressPath = os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus.inprogress') inprogressFile = open(inprogressPath, 'w') inprogressFile.write('somegarbage') inprogressFile.close() except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(islink(os.path.join(manifestutil.manifestPath('foo', 'bar'), 'perlserver')))
def test_inprogress_pkg_download(self): service = 'foo' try: path = ServiceController.manifestPath(service) os.makedirs(path) path = ServiceController.installedPkgPath(service) os.makedirs(path) path = ServiceController.downloadedPkgPath(service) os.makedirs(path) inprogressPath = os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus.inprogress') inprogressFile = open(inprogressPath, 'w') inprogressFile.write('somegarbage') inprogressFile.close() except Exception as excep: LOG.warning('got an OS Exception - %s' % str(excep)) createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'perlserver')))
def activateManifest(testController, manifest='bar', service='foo'): body = json.dumps({'manifest': manifest}) response = testController.app.post( url(controller='action', action='activatemanifest', service=service), headers={'Content-Type': 'application/json'}, params=body) assert response.status_int == 200, 'Action get assert' body = json.loads(response.body) tm = time.time() while (tm + 120 > time.time()): response = testController.app.get(body['status']) body = json.loads(response.body) LOG.debug("activateManifest ********** progress = %s" % body['progress']) if (int(body['progress']) == 100): break time.sleep(0.1) LOG.debug('status = ' + str(response.status_int)) assert response.status_int == 200, "HTTP response != 200" LOG.debug('Status response body = %s' % str(body)) assert body['progress'] == 100 # let's make sure the link is there correctly activePath = os.path.join(ServiceController.manifestPath(service), 'active') LOG.debug('active path = ' + activePath) assert islink(activePath) link = readlink(activePath) LOG.debug('link = ' + link) if os.name == 'nt': manifestPath = os.path.join(ServiceController.manifestPath(service), manifest) assert (link == manifestPath or link == manifest) else: assert link == manifest
def test_post3_pkg_already_installed(self): createManifest(self) # now lets remove the manifest path path = os.path.join(ManifestController.manifestPath('foo', 'bar')) if os.name == 'nt': cmd = 'rm -r %s' % path LOG.debug("running command %s" % cmd) os.system(cmd) else: shutil.rmtree(path) # now create the manifest again createManifest(self) self.assertTrue(os.path.exists(os.path.join(PackageMgr.packagePath(), 'perlserver-1.0.0.unix.cronus'))) self.assertTrue(os.path.exists(os.path.join(ServiceController.installedPkgPath('foo'), 'perlserver', '1.0.0.unix', 'cronus', 'scripts', 'activate'))) self.assertTrue(islink(os.path.join(ManifestController.manifestPath('foo', 'bar'), 'perlserver')))
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None symlinkSwitched = False failed = False try: activePath = os.path.join( ServiceController.manifestPath(self._service), 'active') oldManifest = None appGlobal = pylons.config['pylons.app_globals'] # make sure that if the active path exists, it's a link # if not log that and delete the link if (os.path.exists(activePath) and not os.name == 'nt' and not islink(activePath)): self.__LOG.error('%s is not a link. Attempted to delete' % activePath) shutil.rmtree(activePath) if (os.path.exists(activePath)): oldManifest = os.path.basename(readlink(activePath)) self.__installManifest(self._service, self._manifest) self.__deactivateManifest(self._service, oldManifest) symlinkSwitched = self.__switchSymlink(self._service, self._manifest) appGlobal = pylons.config['pylons.app_globals'] if self._service == 'agent': # START Jeff 09 19 2012: save the timestamp and old manifestPath to .recover: only for agent service. recoveryInfo = {} beforeKillTimeHuman = strftime("%Y-%m-%d %H:%M:%S", localtime()) beforeKillTimeStamp = int(time.time()) recoveryInfo['beforeKillTimeHuman'] = str(beforeKillTimeHuman) recoveryInfo['beforeKillTimeStamp'] = str(beforeKillTimeStamp) # make sure there is always quote: even if null. easy for json parsing in startup if self._manifest: recoveryInfo['newManifest'] = self._manifest else: recoveryInfo['newManifest'] = 'null' if oldManifest: recoveryInfo['oldManifest'] = oldManifest else: recoveryInfo['oldManifest'] = 'null' manifestutil.writeJson('agent', '.recovery', recoveryInfo) # END Jeff 09 19 2012: save the timestamp and .recover files with # persist threads result before shutting down if (hasattr(appGlobal, 'threadMgr') and appGlobal.threadMgr != None): killStatus = {} killStatus['httpStatus'] = 500 killStatus['error'] = Errors.THREAD_KILLED_AGENT_RESTART killStatus['errorMsg'] = 'thread killed, agent restart' appGlobal.threadMgr.snapshot(killStatus, True) self.__activateManifest(self._service, self._manifest) except SystemExit as exc: failed = True if (len(exc.args) == 2): # ok we got {err code, err msg} errorCode = exc.args[0] errorMsg = exc.args[1] raise exc except AgentException as exc: failed = True errorMsg = 'Activate Manifest - Agent Exception - %s' % exc.getMsg( ) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Activate Manifest - Unknown error - (%s/%s) - %s - %s' \ % (self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: if not self._skipCleanupOnFailure(): self.__cleanup(symlinkSwitched, errorMsg, errorCode) self.__LOG.warning(errorMsg) self._updateStatus(httpStatus=500, error=errorCode, errorMsg=errorMsg) else: self._updateProgress(100)
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None symlinkSwitched = False failed = False try: activePath = os.path.join(ServiceController.manifestPath(self._service), 'active') oldManifest = None appGlobal = pylons.config['pylons.app_globals'] # make sure that if the active path exists, it's a link # if not log that and delete the link if (os.path.exists(activePath) and not os.name == 'nt' and not islink(activePath)): self.__LOG.error('%s is not a link. Attempted to delete' % activePath) shutil.rmtree(activePath) if (os.path.exists(activePath)): oldManifest = os.path.basename(readlink(activePath)) self.__installManifest(self._service, self._manifest) self.__deactivateManifest(self._service, oldManifest) symlinkSwitched = self.__switchSymlink(self._service, self._manifest) appGlobal = pylons.config['pylons.app_globals'] if self._service == 'agent': # START Jeff 09 19 2012: save the timestamp and old manifestPath to .recover: only for agent service. recoveryInfo = {} beforeKillTimeHuman = strftime("%Y-%m-%d %H:%M:%S", localtime()) beforeKillTimeStamp = int(time.time()) recoveryInfo['beforeKillTimeHuman'] = str(beforeKillTimeHuman) recoveryInfo['beforeKillTimeStamp'] = str(beforeKillTimeStamp) # make sure there is always quote: even if null. easy for json parsing in startup if self._manifest: recoveryInfo['newManifest'] = self._manifest else: recoveryInfo['newManifest'] = 'null' if oldManifest: recoveryInfo['oldManifest'] = oldManifest else: recoveryInfo['oldManifest'] = 'null' manifestutil.writeJson('agent', '.recovery', recoveryInfo) # END Jeff 09 19 2012: save the timestamp and .recover files with # persist threads result before shutting down if (hasattr(appGlobal, 'threadMgr') and appGlobal.threadMgr != None): killStatus = {} killStatus['httpStatus'] = 500 killStatus['error'] = Errors.THREAD_KILLED_AGENT_RESTART killStatus['errorMsg'] = 'thread killed, agent restart' appGlobal.threadMgr.snapshot(killStatus, True) self.__activateManifest(self._service, self._manifest) except SystemExit as exc: failed = True if (len(exc.args) == 2): # ok we got {err code, err msg} errorCode = exc.args[0] errorMsg = exc.args[1] raise exc except AgentException as exc: failed = True errorMsg = 'Activate Manifest - Agent Exception - %s' % exc.getMsg() errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Activate Manifest - Unknown error - (%s/%s) - %s - %s' \ % (self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: if not self._skipCleanupOnFailure(): self.__cleanup(symlinkSwitched, errorMsg, errorCode) self.__LOG.warning(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg) else: self._updateProgress(100)
def doRun(self): """ Main body of the thread """ errorMsg = "" errorCode = None symlinkSwitched = False failed = False try: activePath = manifestutil.manifestPath(self._service) oldManifest = None # make sure that if the active path exists, it's a link # if not log that and delete the link if (os.path.exists(activePath) and not islink(activePath)): self._LOG.error('%s is not a link. Attempted to delete' % activePath) shutil.rmtree(activePath) if (os.path.exists(activePath)): oldManifest = os.path.basename(readlink(activePath)) # reset requires a valid active link exist else: if self.__action == ActivateManifest.ACTION_RESET: raise AgentException(error = Errors.ACTIVEMANIFEST_MANIFEST_MISSING, errorMsg = ('No active manifest - cannot %s service' % self.__action)) if self.__action == ActivateManifest.ACTION_ACTIVATION: # install new manifest self.__installManifest(self._service, self._manifest) self._deactivateManifest(self._service, oldManifest, 11, 50) if self.__action == ActivateManifest.ACTION_ACTIVATION: # special logic to handle agent upgrade # agent upgrade will not run full activation agent will shutdown before activate exit if self._service == 'agent': appGlobal = pylons.config['pylons.app_globals'] # persist threads result before shutting down if (hasattr(appGlobal, 'threadMgr') and appGlobal.threadMgr != None): killStatus = {} killStatus['httpStatus'] = 500 killStatus['error'] = Errors.THREAD_KILLED_AGENT_RESTART killStatus['errorMsg'] = 'thread killed, agent restart' appGlobal.threadMgr.snapshot(killStatus, True) # switch active symlink symlinkSwitched = self.__switchSymlink(self._service, self._manifest) # activate new manifest self._activateManifest(self._service, self._manifest, 51, 80) elif self.__action == ActivateManifest.ACTION_DEACTIVATION: # remove active link on deactivate activePath = self.__getSymlinkPath(self._service) self.__removeSymlink(activePath) else: # activate new manifest self._activateManifest(self._service, self._manifest, 51, 80) except SystemExit as exc: failed = True if (len(exc.args) == 2): # ok we got {err code, err msg} errorCode = exc.args[0] errorMsg = exc.args[1] raise exc except AgentException as exc: failed = True errorMsg = '%s manifest - Agent Exception - %s' % (self.__action, exc.getMsg()) errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = '%s manifest - Unknown error - (%s/%s) - %s - %s' \ % (self.__action, self._service, self._manifest, str(exc), traceback.format_exc(5)) errorCode = Errors.UNKNOWN_ERROR finally: if failed: if not self._skipCleanupOnFailure(): self.__cleanup(symlinkSwitched, errorMsg, errorCode) self._LOG.warning(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg) else: self._updateProgress(100)