def test_coerce(self): """Things that can coerce right straight to booleans.""" self.assertTrue(asbool(True)) self.assertTrue(asbool(1)) self.assertTrue(asbool(42)) self.assertFalse(asbool(False)) self.assertFalse(asbool(0))
def sudoCmd(cmd, user = None): ''' decorate a command with sudo @param cmd: cmd to be decorated, can be list or string @param user: sudo as user, default None @return: decorated cmd as list or string ''' agentuser = pylons.config['agent_user_account'] sudoDisabled = asbool(pylons.config['disable_sudo']) isList = isinstance(cmd, list) sudoCmd = None if not sudoDisabled: if user is None: # no user, sudo sudoCmd = ['sudo'] elif agentuser == user: # agent user, no need to sudo sudoCmd = [] else: # other user, sudo -u sudoCmd = ['sudo', '-u', user] # append the original cmd if isList: for i in cmd: sudoCmd.append(i) else: sudoCmd.append(cmd) # return the same type sudofied cmd back to caller return cmd if sudoDisabled else (sudoCmd if isList else ' '.join(sudoCmd))
def post(self, service, manifest): """ Create a new service object """ from agent.lib.agent_thread.manifest_create import ManifestCreate try: LOG.info('Post for service (%s) and manifest (%s) with body: %s', service, manifest, request.body) # check to see if the manifest already exists path = ManifestController.manifestPath(service, manifest) if (os.path.isdir(path)): return doneResult(request, response, httpStatus = 201, controller = self) # parse the body if (request.body == ""): return errorResult(request, response, Errors.MANIFEST_PACKAGE_PARSING_ERROR, 'No body found in post command', controller = self) body = json.loads(request.body) packages = body['package'] forcedPackages = body['forcePackageName'] if 'forcePackageName' in body else None skipProp = asbool(body['skipProp']) if 'skipProp' in body else configutil.getConfigAsBool('download_skip_prop') LOG.debug('pkgs = %s, %s', packages, forcedPackages) # parse the package list for idx, package in enumerate(packages): # to support reuse of an package from an existing manifest (active if possible) # without sending the complete package location in request body if package.startswith('/'): packageRef = package tokens = package.split('/') pkgnamePrefix = tokens[-1].rstrip() fullPkgLoc = manifestutil.getPackageByName(service, manifest = None, pkgnamePrefix = pkgnamePrefix) if fullPkgLoc is None: return errorResult(request, response, Errors.MANIFEST_PACKAGE_DOES_NOT_EXIST, 'manifest (%s/%s) package (%s) does not exist' % (service, manifest, packages), controller = self) else: LOG.info('expanding package reuse ref %s with full package location %s' % (packageRef, fullPkgLoc)) packages[idx] = fullPkgLoc appGlobal = config['pylons.app_globals'] # start a thread to create the package manThread = ManifestCreate(appGlobal.threadMgr, service, manifest, packages, forcePackages = forcedPackages, skipProp = skipProp) self.injectJobCtx(manThread) manThread.start() manThread.threadMgrEvent.wait() return statusResult(request, response, manThread, controller = self) except AgentException as excep: return errorResult(request, response, error = excep.getCode(), errorMsg = excep.getMsg(), controller = self) except Exception as excep: return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)), controller = self)
def selfupdate(self): """ agent selfupdate through api """ LOG.info('selfupdate agent with body: %s', request.body) try: appGlobal = config['pylons.app_globals'] wisbVersion = None wisbSource = None if request.body: requestjson = json.loads(request.body) if 'version' not in requestjson: raise AgentException(Errors.INVALID_REQUEST, 'version is required') wisbVersion = requestjson['version'] wisbSource = requestjson['wisbSource'] if 'wisbSource' in requestjson else configutil.getConfig('selfupdate_source') skipProp = asbool(requestjson['skipProp']) if 'skipProp' in requestjson else True updateThread = AgentUpdate(appGlobal.threadMgr, wisbVersion, wisbSource, skipProp = skipProp) self.injectJobCtx(updateThread) updateThread.start() updateThread.threadMgrEvent.wait() return statusResult(request, response, updateThread, controller = self) except AgentException as aexcep: return errorResult(request, response, error = aexcep.getCode(), errorMsg = aexcep.getMsg(), controller = self) except Exception as excep: msg = 'Unknown error for agent update(%s) - %s - %s' % (wisbVersion, str(excep), traceback.format_exc(2)) return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = msg, controller = self)
def __init__(self, packagePath, threadMgr): ''' constructor @param packagePath: path to package @param threadMgr: thread manager to execute package scripts ''' if not os.path.isdir(packagePath): raise PackagePathError("package path '%s' not found" % packagePath) self.__packagePath = packagePath self.__threadMgr = threadMgr useAppUser = asbool(pylons.config['use_app_user']) self.__userName = pylons.config['app_user_account'] if useAppUser else None
def __init__(self, packagePath, threadMgr): ''' constructor @param packagePath: path to package @param threadMgr: thread manager to execute package scripts ''' if not os.path.isdir(packagePath): raise PackagePathError("package path '%s' not found" % packagePath) self.__packagePath = packagePath self.__threadMgr = threadMgr useAppUser = asbool(pylons.config['use_app_user']) self.__userName = pylons.config[ 'app_user_account'] if useAppUser else None
def getAppUser(): """ get application user, this we don't allow to override """ useAppUser = asbool(pylons.config['use_app_user']) return pylons.config['app_user_account'] if useAppUser else pylons.config['agent_user_account']
def getConfigAsBool(key): """ get config as bool """ value = getConfig(key) return asbool(value) if value else False
def test_truth_str(self): for v in ("true", "yes", "on", "y", "t", "1"): self.assertTrue(asbool(v), "%s should be considered True" % (v, )) v = v.upper() self.assertTrue(asbool(v), "%s should be considered True" % (v, ))
def test_false_str(self): for v in ("false", "no", "off", "n", "f", "0"): self.assertFalse(asbool(v), v) v = v.upper() self.assertFalse(asbool(v), v)
def test_truth_str(self): for v in ('true', 'yes', 'on', 'y', 't', '1'): self.assertTrue(asbool(v), "%s should be considered True" % (v,)) v = v.upper() self.assertTrue(asbool(v), "%s should be considered True" % (v,))
def test_false_str(self): for v in ('false', 'no', 'off', 'n', 'f', '0'): self.assertFalse(asbool(v), v) v = v.upper() self.assertFalse(asbool(v), v)
def post(self, service, manifest): """ Create a new service object """ from agent.lib.agent_thread.manifest_create import ManifestCreate try: LOG.info('Post for service (%s) and manifest (%s) with body: %s', service, manifest, request.body) # check to see if the manifest already exists path = ManifestController.manifestPath(service, manifest) if (os.path.isdir(path)): return doneResult(request, response, httpStatus=201, controller=self) # parse the body if (request.body == ""): return errorResult(request, response, Errors.MANIFEST_PACKAGE_PARSING_ERROR, 'No body found in post command', controller=self) body = json.loads(request.body) packages = body['package'] forcedPackages = body[ 'forcePackageName'] if 'forcePackageName' in body else None skipProp = asbool( body['skipProp'] ) if 'skipProp' in body else configutil.getConfigAsBool( 'download_skip_prop') LOG.debug('pkgs = %s, %s', packages, forcedPackages) # parse the package list for idx, package in enumerate(packages): # to support reuse of an package from an existing manifest (active if possible) # without sending the complete package location in request body if package.startswith('/'): packageRef = package tokens = package.split('/') pkgnamePrefix = tokens[-1].rstrip() fullPkgLoc = manifestutil.getPackageByName( service, manifest=None, pkgnamePrefix=pkgnamePrefix) if fullPkgLoc is None: return errorResult( request, response, Errors.MANIFEST_PACKAGE_DOES_NOT_EXIST, 'manifest (%s/%s) package (%s) does not exist' % (service, manifest, packages), controller=self) else: LOG.info( 'expanding package reuse ref %s with full package location %s' % (packageRef, fullPkgLoc)) packages[idx] = fullPkgLoc appGlobal = config['pylons.app_globals'] # start a thread to create the package manThread = ManifestCreate(appGlobal.threadMgr, service, manifest, packages, forcePackages=forcedPackages, skipProp=skipProp) self.injectJobCtx(manThread) manThread.start() manThread.threadMgrEvent.wait() return statusResult(request, response, manThread, controller=self) except AgentException as excep: return errorResult(request, response, error=excep.getCode(), errorMsg=excep.getMsg(), controller=self) except Exception as excep: return errorResult( request, response, error=Errors.UNKNOWN_ERROR, errorMsg='Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)), controller=self)