def testPkgInitConfig(self): createManifest(self, manifest='bar', service='foo') activateManifest(self, manifest='bar', service='foo') inifilepath = manifestutil.packagePath( 'foo', 'bar', 'perlserver') + os.path.sep + 'cronus' inifilename = os.path.join(inifilepath, 'cronus.ini') uname = pylons.config['app_user_account'] utils.runsyscmd(utils.sudoCmd('chmod -R ga+w %s' % inifilepath, uname)) data = {"key": "value", "key2": "value2"} # test json format with open(inifilename, 'w') as propFile: json.dump(data, propFile) pkgPath = packagePath('foo', 'bar', 'perlserver') pkgInitConfig = manifestutil.PkgInitConfig(pkgPath) configs = pkgInitConfig.getConfigs() assert configs is not None assert isinstance(configs, dict) print configs assert configs['key'] == 'value' assert configs['key2'] == 'value2' # test eval() format with open(inifilename, 'wb+') as fp: jsonStr = json.dumps(data) fp.write(jsonStr) fp.write("\n") pkgInitConfig = manifestutil.PkgInitConfig(pkgPath) configs = pkgInitConfig.getConfigs() assert configs is not None assert isinstance(configs, dict) print configs assert configs['key'] == 'value' assert configs['key2'] == 'value2'
def testPkgInitConfig(self): createManifest(self, manifest = 'bar', service = 'foo') activateManifest(self, manifest = 'bar', service = 'foo') inifilepath = manifestutil.packagePath('foo', 'bar', 'perlserver') + os.path.sep + 'cronus' inifilename = os.path.join(inifilepath, 'cronus.ini') uname = pylons.config['app_user_account'] utils.runsyscmd(utils.sudoCmd('chmod -R ga+w %s' % inifilepath, uname)) data = {"key":"value","key2":"value2"} # test json format with open(inifilename, 'w') as propFile: json.dump(data, propFile) pkgPath = packagePath('foo', 'bar', 'perlserver') pkgInitConfig = manifestutil.PkgInitConfig(pkgPath) configs = pkgInitConfig.getConfigs() assert configs is not None assert isinstance(configs, dict) print configs assert configs['key'] == 'value' assert configs['key2'] == 'value2' # test eval() format with open(inifilename, 'wb+') as fp: jsonStr = json.dumps(data) fp.write(jsonStr) fp.write("\n") pkgInitConfig = manifestutil.PkgInitConfig(pkgPath) configs = pkgInitConfig.getConfigs() assert configs is not None assert isinstance(configs, dict) print configs assert configs['key'] == 'value' assert configs['key2'] == 'value2'
def __postActivation(self, service, manifest, curProg): """ place to run post activation operations 1. customize application configuration files based on metadata 2. install upstart service """ # replace application config files based on lcm_meta.env, "env" valeu set in post request for package in packagesInManifest(service, manifest): self._LOG.info('check map config files in %s' % (package)) pkgRoot = packagePath(service, ACTIVE_MANIFEST, package) pkgCfg = PkgInitConfig(pkgRoot) # read cfgfilemap from cronus.ini env = serviceutil.getEnv(service) if env: cfgFiles = pkgCfg.getConfig(PkgInitConfig.KEY_CFGFILES, []) for cfgFile in cfgFiles: srcFile = os.path.join(pkgRoot, '%s.%s' % (cfgFile, env.lower())) destFile = os.path.join(pkgRoot, cfgFile) if os.path.exists(srcFile): self._LOG.info('copy cfg file %s to %s' % (srcFile, destFile)) cmd = utils.sudoCmd(['cp', '-f', srcFile, destFile], configutil.getAppUser()) execThread = ExecThread(self._threadMgr, cmd, None, self.getUuid()) self._runExecThreads([execThread], curProg, curProg+1) # install daemon based on metadata.lcm_meta.daemon isDaemon, daemonType = serviceutil.isDaemonServiceWisb(service) if isDaemon: installed = False self._LOG.info('daemon enabled, setup service in %s' % daemonType) for package in packagesInManifest(service, manifest): pkgRoot = packagePath(service, ACTIVE_MANIFEST, package) appUser = configutil.getAppUser() if (os.path.exists(os.path.join(pkgRoot, 'cronus', 'upstart.conf')) or os.path.exists(os.path.join(pkgRoot, 'cronus', 'systemd.service'))): self._LOG.info('found daemon config in %s' % (package)) script = manifestutil.getActiveScriptPath('agent', 'agent', 'setupserviced') cmd = utils.sudoCmd([script, pkgRoot, appUser, service, daemonType]) execThread = ExecThread(self._threadMgr, cmd, None, self.getUuid()) self._runExecThreads([execThread], curProg+1, curProg+2) installed = True break if not installed: self._LOG.info('daemon config not found, service not installed') serviceutil.setDaemonServiceWisb(service, None)
def doManifestLogFile(action): """ do manifest log file """ service = request.params.get('service', '') package = request.params.get('package', '') manifest = request.params.get('manifest', 'active') if (service == '' or package == ''): c.errorMsg = 'missing service or package parameter from request' c.errorCode = Errors.LOG_PARAM_REQUIRED return render('/derived/error.html') print 'service/package/manifest ', service, package, manifest packagePathStr = packagePath(service, package, manifest) print 'package path ', packagePathStr pkgInit = PkgInitConfig(service, package, manifest) pkgConfig = pkgInit.getConfigs() if not pkgConfig: c.errorMsg = 'cronus.ini does not exist, view log needs cronus.ini to know log location, please check your cronus package to make sure cronus.ini exist and have property logAppDir' c.errorCode = Errors.CRONUS_INI_EMPTY_NOT_FOUND return render('/derived/error.html') print '*** packageConfig ', pkgConfig manifestFileName = pkgConfig['logManifestFile'] if (action == 'download'): return downloadFile(os.path.join(packagePathStr, manifestFileName)) if (action == 'tail'): lines = request.params.get('size', '100') return tailFile(os.path.join(packagePathStr, manifestFileName), lines) if (action == 'list'): return manifestFileName
def executeScript(self, service, scriptname): """ execute a script from remote location""" scriptpath = None try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller=self) body = json.loads(request.body) paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s' % (params)) scriptpath = None for package in manifestutil.packagesInManifest(service): scriptpathtmp = os.path.join( manifestutil.packagePath(service, 'active', package), 'cronus', 'scripts', scriptname) if os.path.exists(scriptpathtmp): scriptpath = scriptpathtmp break if not scriptpath: return errorResult(request, response, Errors.INVALID_REQUEST, 'script %s not found' % scriptname, controller=self) cmd = ['sudo', '-u', 'cronusapp', scriptpath] for param in params: param = param.encode('ascii', 'ignore') cmd.append(param) LOG.info('cmd = %s' % cmd) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') execThread.start() execThread.threadMgrEvent.wait() return statusResult(request, response, execThread, controller=self) except Exception as excp: return errorResult( request, response, error=Errors.UNKNOWN_ERROR, errorMsg='Unknown error when executing cmd %s, %s - %s' % (scriptpath, str(excp), traceback.format_exc(2)), controller=self)
def doAppLogFile(): """ listAppLog """ service = request.params.get('service', '') package = request.params.get('package', '') manifest = request.params.get('manifest', 'active') if (service == '' or package == ''): c.errorMsg = 'Missing service or package from request' c.errorCode = Errors.LOG_PARAM_REQUIRED return render('/derived/error.html') packagePathStr = packagePath(service, manifest, package) if (not os.path.isdir(packagePathStr)): c.errorMsg = 'Invalid service, manifest, or package from request' c.errorCode = Errors.LOG_PARAM_REQUIRED return render('/derived/error.html') LOG.debug('In ApplogController doAppLogFile ' + packagePathStr) pkgPath = packagePath(service, manifest, package) pkgInit = PkgInitConfig(pkgPath) logDirList = pkgInit.getConfig(PkgInitConfig.KEY_LOGDIRS) if not logDirList: c.errorMsg = 'No cronus.ini in package, please check your cronus package to make sure cronus.ini exist and have property logDirs' c.errorCode = Errors.CRONUS_INI_EMPTY_NOT_FOUND return render('/derived/error.html') dirName = request.params.get('dirName', '') fileName = request.params.get('fileName', '') if (fileName != '' and dirName != ''): dirName = os.path.join(dirName, fileName) if (dirName != ''): return ApplogController.listAppLogDir(service, manifest, package, dirName, os.path.join(packagePathStr, dirName)) else: return ApplogController.listAllAppLogDir(service, manifest, package, dirName, packagePathStr, logDirList)
def _obtainCmdExecThreads(self, exeName, service, manifest, activateFlow): """start the threads to execute cmd in each package in the service/manifest""" execThreads = [] packages = ManifestController.getPackages(service, os.path.basename(manifest)) # Executing the deactivate in reversed order where the last package is first deactivated. if (not activateFlow): packages.reverse() # make sure there's something to do, otherwise just return here if (len(packages) == 0): return execThreads for package in packages: pkgPath = manifestutil.packagePath(service, manifest, package) pkgInit = PkgInitConfig(pkgPath) pkgConfig = pkgInit.getConfigs() # check command type and package startup process if (exeName == 'reboot' or exeName == 'restart'): # default action for reboot is to startup, noop for restart agent action = 'startup' if 'reboot' == exeName else 'noop' if exeName == 'reboot' and pkgConfig and pkgConfig.has_key( 'lcm_on_system_reboot'): action = pkgConfig['lcm_on_system_reboot'] if (exeName == 'restart' and pkgConfig and pkgConfig.has_key('lcm_on_agent_restart')): action = pkgConfig['lcm_on_agent_restart'] if action == 'reset': execThread, dummy = self._getBuiltThread( service, manifest, package, "shutdown") execThreads.append([execThread, dummy]) execThread, dummy = self._getBuiltThread( service, manifest, package, "deactivate") execThreads.append([execThread, dummy]) execThread, dummy = self._getBuiltThread( service, manifest, package, "activate") execThreads.append([execThread, dummy]) if (action == 'reset' or action == 'startup'): execThread, dummy = self._getBuiltThread( service, manifest, package, 'startup') execThreads.append([execThread, dummy]) else: execThread, dummy = self._getBuiltThread( service, manifest, package, exeName) execThreads.append([execThread, dummy]) return execThreads
def doAppLogFile(action): """ listAppLog """ service = request.params.get('service', '') package = request.params.get('package', '') manifest = request.params.get('manifest', 'active') if (service == '' or package == ''): c.errorMsg = 'missing service or package parameter from request' c.errorCode = Errors.LOG_PARAM_REQUIRED return render('/derived/error.html') packagePathStr = packagePath(service, manifest, package) if (not os.path.isdir(packagePathStr)): return ModulelogController.listManifests(service) LOG.info('In ModuleLogController doAppLogFile ' + packagePathStr) pkgInit = PkgInitConfig(service, manifest, package) pkgConfig = pkgInit.getConfigs() if not pkgConfig: c.errorMsg = 'cronus.ini does not exist, view log needs cronus.ini to know log location, please check your cronus package to make sure cronus.ini exist and have property logAppDir' c.errorCode = Errors.CRONUS_INI_EMPTY_NOT_FOUND return render('/derived/error.html') logAppDirList = pkgConfig['logAppDir'] dirName = request.params.get('dirName', '') fileName = request.params.get('fileName', '') if (action == 'list'): if (fileName != '' and dirName != ''): dirName = os.path.join(dirName, fileName) if (dirName != ''): return ModulelogController.listAppLogDir( service, manifest, package, dirName, os.path.join(packagePathStr, dirName)) else: return ModulelogController.listAllAppLogDir( service, manifest, package, dirName, packagePathStr, logAppDirList) if (dirName == '' or fileName == ''): c.errorMsg = 'tail/download App log requires dirName & FileName params missing' c.errorCode = Errors.NOT_ENOUGH_PARAMS return render('/derived/error.html') if (action == 'tail'): lines = request.params.get('size', '100') return tailFile(os.path.join(packagePathStr, dirName, fileName), lines) if (action == 'download'): return downloadFile(os.path.join(packagePathStr, dirName, fileName))
def _obtainCmdExecThreads(self, exeName, service, manifest, activateFlow): """start the threads to execute cmd in each package in the service/manifest""" execThreads = [] packages = ManifestController.getPackages(service, os.path.basename(manifest)) # Executing the deactivate in reversed order where the last package is first deactivated. if (not activateFlow): packages.reverse() # make sure there's something to do, otherwise just return here if (len(packages) == 0): return execThreads for package in packages: pkgPath = manifestutil.packagePath(service, manifest, package) pkgInit = PkgInitConfig(pkgPath) pkgConfig = pkgInit.getConfigs() # check command type and package startup process if (exeName == 'reboot' or exeName == 'restart'): # default action for reboot is to startup, noop for restart agent action = 'startup' if 'reboot' == exeName else 'noop' if exeName == 'reboot' and pkgConfig and pkgConfig.has_key('lcm_on_system_reboot'): action = pkgConfig['lcm_on_system_reboot'] if (exeName == 'restart' and pkgConfig and pkgConfig.has_key('lcm_on_agent_restart')): action = pkgConfig['lcm_on_agent_restart'] if action == 'reset': execThread, dummy = self._getBuiltThread(service, manifest, package, "shutdown") execThreads.append([execThread, dummy]) execThread, dummy = self._getBuiltThread(service, manifest, package, "deactivate") execThreads.append([execThread, dummy]) execThread, dummy = self._getBuiltThread(service, manifest, package, "activate") execThreads.append([execThread, dummy]) if (action == 'reset' or action == 'startup'): execThread, dummy = self._getBuiltThread(service, manifest, package, 'startup') execThreads.append([execThread, dummy]) else: execThread, dummy = self._getBuiltThread(service, manifest, package, exeName) execThreads.append([execThread, dummy]) return execThreads
def executeScript(self, service, scriptname): """ execute a script from remote location""" scriptpath = None try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller = self) body = json.loads(request.body) paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s' % (params)) scriptpath = None for package in manifestutil.packagesInManifest(service): scriptpathtmp = os.path.join(manifestutil.packagePath(service, 'active', package), 'cronus', 'scripts', scriptname) if os.path.exists(scriptpathtmp): scriptpath = scriptpathtmp break if not scriptpath: return errorResult(request, response, Errors.INVALID_REQUEST, 'script %s not found' % scriptname, controller = self) cmd = ['sudo', '-u', 'cronusapp', scriptpath] for param in params: param = param.encode('ascii', 'ignore') cmd.append(param) LOG.info('cmd = %s' % cmd) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') execThread.start() execThread.threadMgrEvent.wait() return statusResult(request, response, execThread, controller = self) except Exception as excp: return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = 'Unknown error when executing cmd %s, %s - %s' % (scriptpath, str(excp), traceback.format_exc(2)), controller = self)
def doAppLogFile(action): """ listAppLog """ service = request.params.get('service', '') package = request.params.get('package', '') manifest = request.params.get('manifest', 'active') if (service == '' or package == ''): c.errorMsg = 'missing service or package parameter from request' c.errorCode = Errors.LOG_PARAM_REQUIRED return render('/derived/error.html') packagePathStr = packagePath(service, manifest, package) if (not os.path.isdir(packagePathStr)): return ModulelogController.listManifests(service) LOG.info('In ModuleLogController doAppLogFile ' + packagePathStr) pkgInit = PkgInitConfig(service, manifest, package) pkgConfig = pkgInit.getConfigs() if not pkgConfig: c.errorMsg = 'cronus.ini does not exist, view log needs cronus.ini to know log location, please check your cronus package to make sure cronus.ini exist and have property logAppDir' c.errorCode = Errors.CRONUS_INI_EMPTY_NOT_FOUND return render('/derived/error.html') logAppDirList = pkgConfig['logAppDir'] dirName = request.params.get('dirName', '') fileName = request.params.get('fileName', '') if (action == 'list'): if (fileName != '' and dirName != ''): dirName = os.path.join(dirName, fileName) if (dirName != ''): return ModulelogController.listAppLogDir(service, manifest, package, dirName, os.path.join(packagePathStr, dirName)) else: return ModulelogController.listAllAppLogDir(service, manifest, package, dirName, packagePathStr, logAppDirList) if (dirName == '' or fileName == ''): c.errorMsg = 'tail/download App log requires dirName & FileName params missing' c.errorCode = Errors.NOT_ENOUGH_PARAMS return render('/derived/error.html') if (action == 'tail'): lines = request.params.get('size', '100') return tailFile(os.path.join(packagePathStr, dirName, fileName), lines) if (action == 'download'): return downloadFile(os.path.join(packagePathStr, dirName, fileName))
def testPackagePath(self): pkgpath = 'foo' + os.path.sep + 'manifests' + os.path.sep + 'bar' + os.path.sep + 'perlserver' assert manifestutil.packagePath('foo', 'bar', 'perlserver').endswith(pkgpath)