def activate(self, service, manifest): """ activate manifest, if already active then skip """ from agent.lib.agent_thread.activate_manifest import ActivateManifest LOG.info('activateManifest for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] if manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) else: if request.body: pushedData = json.loads(request.body) serviceutil.updateLcmMeta(service, pushedData) mf_path = os.path.join(manifestutil.manifestPath(service, manifest)) if (not os.path.exists(mf_path)): return errorResult(request, response, Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (service, manifest), controller=self) LOG.debug('Manifest path exists: %s' % (mf_path)) activateThread = ActivateManifest(appGlobal.threadMgr, service, manifest, action=ActivateManifest.ACTION_ACTIVATION) self.injectJobCtx(activateThread) activateThread.start() activateThread.threadMgrEvent.wait() return statusResult(request, response, activateThread, controller=self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error=Errors.UNKNOWN_ERROR, errorMsg=msg, controller=self)
def activatemanifest(self, service): """ activate manifest, if already active then skip """ LOG.info('activateManifest for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] manifest = '' requestjson = json.loads(request.body) manifest = requestjson['manifest'] force = asbool(requestjson['force']) if 'force' in requestjson else False if not force and manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) else: mf_path = os.path.join(ManifestController.manifestPath(service, manifest)) if (not os.path.exists(mf_path)): return errorResult(request, response, Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (service, manifest), controller = self) LOG.debug('Manifest path exists: %s' % (mf_path)) activateThread = ActivateManifest(appGlobal.threadMgr, service, manifest) self.injectJobCtx(activateThread) activateThread.start() activateThread.threadMgrEvent.wait() return statusResult(request, response, activateThread, controller = self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = msg, controller = self)
def deployservice(self, service): """ activate manifest, if already active then skip """ LOG.info('deploy service for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] # parse the body if (request.body == ""): return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller = self) requestjson = json.loads(request.body) manifest = requestjson['manifest'] packages = requestjson['package'] skipProp = asbool(requestjson['skipProp']) if 'skipProp' in requestjson else configutil.getConfigAsBool('download_skip_prop') skipActivation = asbool(requestjson['skipActivation']) if 'skipActivation' in requestjson else False # activate manifest if not already activated if manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) deployServiceThread = DeployService(appGlobal.threadMgr, service, manifest, packages, skipProp = skipProp, skipActivation = skipActivation) self.injectJobCtx(deployServiceThread) deployServiceThread.start() deployServiceThread.threadMgrEvent.wait() return statusResult(request, response, deployServiceThread, controller = self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = msg, controller = self)
def deployservice(self, service): """ activate manifest, if already active then skip """ LOG.info('deploy service for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] # parse the body if (request.body == ""): return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller=self) requestjson = json.loads(request.body) manifest = requestjson['manifest'] packages = requestjson['package'] skipProp = asbool( requestjson['skipProp'] ) if 'skipProp' in requestjson else configutil.getConfigAsBool( 'download_skip_prop') skipActivation = asbool( requestjson['skipActivation'] ) if 'skipActivation' in requestjson else False # activate manifest if not already activated if manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) deployServiceThread = DeployService(appGlobal.threadMgr, service, manifest, packages, skipProp=skipProp, skipActivation=skipActivation) self.injectJobCtx(deployServiceThread) deployServiceThread.start() deployServiceThread.threadMgrEvent.wait() return statusResult(request, response, deployServiceThread, controller=self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % ( service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error=Errors.UNKNOWN_ERROR, errorMsg=msg, controller=self)
def __delAllExceptActiveManifests(self, service): ''' Delete all manifests in the given service except active link and corresponding manifest (if one exists). Doesn't delete the corresponding dir in installed-packages (similar to 'delete manifest') ''' manifests = manifestutil.getManifests(service) active = manifestutil.getActiveManifest(service) if active and len(active) != 0: # 'active' can be none manifests.remove(active) for mft in manifests: mf_path = manifestutil.manifestPath(service, mft) ServiceDelete.deleteFolderContents(mf_path)
def getLocalPyPkg(): """ reuse of local python package instead of download it again from source of truth, this should be the common use case for selfupdate without needing to update the python package """ activeManifest = manifestutil.getActiveManifest('agent') activePyLink = os.path.join(manifestutil.manifestPath('agent', activeManifest), 'python_package') if (os.path.exists(activePyLink)): activePyPath = readlink(activePyLink) pyVersion = os.path.basename(activePyPath) pyPkgName = ('python_package-%s' % pyVersion) if AgentUpdate.nameRe.match(pyPkgName): return ('http://localhost:12020/%s.cronus' % pyPkgName) else: raise AgentException(Errors.PACKAGE_SCHEME_ERROR, 'package name %s is not valid' % pyPkgName)
def deployservice(self, service): """ activate manifest, if already active then skip """ LOG.info('deploy service for service(%s) with body: %s', service, request.body) manifest = None try: appGlobal = config['pylons.app_globals'] # parse the body if (request.body == ""): return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller = self) requestjson = json.loads(request.body) packages = requestjson['package'] if 'manifest' in requestjson: manifest = requestjson['manifest'] else: manifest = PackageUtil.getPackageVersion(packages[-1]) serviceutil.createServiceIfNeeded(service) # activate manifest if not already activated if manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) else: # save metadata from payload pushedData = {} pushedData.update(requestjson) for key in ['manifest', 'package']: if key in pushedData: del pushedData[key] serviceutil.updateLcmMeta(service, pushedData) # deploy deployServiceThread = DeployService(appGlobal.threadMgr, service, manifest, packages) self.injectJobCtx(deployServiceThread) deployServiceThread.start() deployServiceThread.threadMgrEvent.wait() return statusResult(request, response, deployServiceThread, controller = self) except Exception as excep: msg = 'Unknown error for deployService(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = msg, controller = self)
def activatemanifest(self, service): """ activate manifest, if already active then skip """ LOG.info('activateManifest for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] manifest = '' requestjson = json.loads(request.body) manifest = requestjson['manifest'] force = asbool( requestjson['force']) if 'force' in requestjson else False if not force and manifestutil.getActiveManifest( service) == manifest: return doneResult(request, response, controller=self) else: mf_path = os.path.join( ManifestController.manifestPath(service, manifest)) if (not os.path.exists(mf_path)): return errorResult(request, response, Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (service, manifest), controller=self) LOG.debug('Manifest path exists: %s' % (mf_path)) activateThread = ActivateManifest(appGlobal.threadMgr, service, manifest) self.injectJobCtx(activateThread) activateThread.start() activateThread.threadMgrEvent.wait() return statusResult(request, response, activateThread, controller=self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % ( service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error=Errors.UNKNOWN_ERROR, errorMsg=msg, controller=self)
def doRun(self): """ Main body of the thread """ self._updateProgress(1) errorMsg = "" errorCode = None failed = False try: if manifestutil.hasActiveManifest(self._service): manifest = manifestutil.getActiveManifest(self._service) self.__shutdownSuppressError(self._service, manifest) self.__deactivateSuppressError(self._service, manifest) self.__deleteService(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] except AgentException as exc: failed = True errorMsg = 'Cleanup Service - Agent Exception - %s' % exc.getMsg() errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Cleanup Service - 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.warning(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg) else: self._updateProgress(100)
def getLocalPyPkg(): """ reuse of local python package instead of download it again from source of truth, this should be the common use case for selfupdate without needing to update the python package """ activeManifest = manifestutil.getActiveManifest('agent') activePyLink = os.path.join( manifestutil.manifestPath('agent', activeManifest), 'python_package') if (os.path.exists(activePyLink)): activePyPath = readlink(activePyLink) pyVersion = os.path.basename(activePyPath) pyPkgName = ('python_package-%s' % pyVersion) if AgentUpdate.nameRe.match(pyPkgName): return ('http://localhost:12020/%s.cronus' % pyPkgName) else: raise AgentException( Errors.PACKAGE_SCHEME_ERROR, 'package name %s is not valid' % pyPkgName)
def doRun(self): """ Main body of the thread """ self._updateProgress(1) errorMsg = "" errorCode = None failed = False try: # create manifest if not already exist mpath = manifestutil.manifestPath(self._service, self._manifest) if (not os.path.exists(mpath) or not os.path.isdir(mpath)): self._LOG.debug('pkgs = %s', self.__packages) # parse the package list for idx, package in enumerate(self.__packages): if package.startswith('/'): packageRef = package tokens = package.split('/') pkgnamePrefix = tokens[-1].rstrip() fullPkgLoc = manifestutil.getPackageFullLocByName(self._service, manifest = None, pkgnamePrefix = pkgnamePrefix) if fullPkgLoc is None: raise AgentException(Errors.MANIFEST_PACKAGE_DOES_NOT_EXIST, 'manifest (%s/%s) package (%s) does not exist' % (self._service, self._manifest, self.__packages)) else: self._LOG.info('expanding package reuse ref %s with full package location %s' % (packageRef, fullPkgLoc)) self.__packages[idx] = fullPkgLoc # start a thread to create the package manThread = ManifestCreate(self._threadMgr, self._service, self._manifest, self.__packages, parentId = self.getUuid()) contextutils.copyJobContexts(self, manThread) manThread.run() self._addChildExeThreadId(manThread.getChildExeThreadIds()) status = manThread.getStatus() if (status['error'] != None): raise AgentException(status['error'], status['errorMsg']) self._updateProgress(60) if (not os.path.exists(mpath)): raise AgentException(Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (self._service, self._manifest)) # activate manifest if not already activated activateThread = ActivateManifest(self._threadMgr, self._service, self._manifest, parentId = self.getUuid()) contextutils.copyJobContexts(self, activateThread) activateThread.run() self._addChildExeThreadId(activateThread.getChildExeThreadIds()) status = activateThread.getStatus() if (status['error'] != None): raise AgentException(status['error'], status['errorMsg']) if manifestutil.getActiveManifest(self._service) != self._manifest: raise AgentException(Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (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] except AgentException as exc: failed = True errorMsg = 'Deploy Service - Agent Exception - %s' % exc.getMsg() errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Deploy Service - 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.warning(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg) else: self._updateProgress(100)
def doRun(self): """ Main body of the thread """ spath = manifestutil.servicePath(self._service) self._updateProgress(1) errorMsg = "" errorCode = None failed = False ctxNames = ['guid', 'service'] try: # create service if not already exist if not os.path.exists(spath): os.makedirs(spath) os.makedirs(os.path.join(spath, 'manifests')) os.makedirs(os.path.join(spath, 'installed-packages')) os.makedirs(os.path.join(spath, 'modules')) os.makedirs(os.path.join(spath, 'downloaded-packages')) os.makedirs(os.path.join(spath, '.appdata')) os.makedirs(os.path.join(spath, '.data')) import pwd uname = pylons.config['app_user_account'] uid = pwd.getpwnam(uname).pw_uid gid = pwd.getpwnam(uname).pw_gid utils.rchown(os.path.join(spath, '.appdata'), uid, gid) # verify that the path exists if (not os.path.isdir(spath)): raise AgentException(Errors.UNKNOWN_ERROR, "Service(%s) was not created" % self._service) self._updateProgress(20) # create manifest if not already exist mpath = manifestutil.manifestPath(self._service, self._manifest) if (not os.path.exists(mpath) or not os.path.isdir(mpath)): self.__LOG.debug('pkgs = %s', self.__packages) # parse the package list for idx, package in enumerate(self.__packages): if package.startswith('/'): packageRef = package tokens = package.split('/') pkgnamePrefix = tokens[-1].rstrip() fullPkgLoc = manifestutil.getPackageByName(self._service, manifest = None, pkgnamePrefix = pkgnamePrefix) if fullPkgLoc is None: raise AgentException(Errors.MANIFEST_PACKAGE_DOES_NOT_EXIST, 'manifest (%s/%s) package (%s) does not exist' % (self._service, self._manifest, self.__packages)) else: self.__LOG.info('expanding package reuse ref %s with full package location %s' % (packageRef, fullPkgLoc)) self.__packages[idx] = fullPkgLoc # start a thread to create the package manThread = ManifestCreate(threadmgr.NULL_THREADMGR, self._service, self._manifest, self.__packages, skipProp = self.__skipProp) contextutils.copycontexts(self, manThread, ctxNames) manThread.run() status = manThread.getStatus() if (status['error'] != None): raise AgentException(status['error'], status['errorMsg']) self._updateProgress(60) if (not os.path.exists(mpath)): raise AgentException(Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (self._service, self._manifest)) if not self.__skipActivation: activateThread = ActivateManifest(threadmgr.NULL_THREADMGR, self._service, self._manifest) contextutils.copycontexts(self, activateThread, ctxNames) activateThread.run() status = activateThread.getStatus() if (status['error'] != None): raise AgentException(status['error'], status['errorMsg']) # activte manifest if not already activated if manifestutil.getActiveManifest(self._service) != self._manifest: raise AgentException(Errors.ACTIVEMANIFEST_MANIFEST_MISSING, 'Manifest(%s, %s) path missing' % (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] except AgentException as exc: failed = True errorMsg = 'Deploy Service - Agent Exception - %s' % exc.getMsg() errorCode = exc.getCode() except Exception as exc: failed = True errorMsg = 'Deploy Service - 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.warning(errorMsg) self._updateStatus(httpStatus = 500, error = errorCode, errorMsg = errorMsg) else: self._updateProgress(100)