Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def log(self, service, manifest):
        """ Get manifest logs """
        LOG.info('Get for service (%s) and manifest (%s)', service, manifest)

        try:
            # first check that the manifest directory exists
            path = manifestutil.manifestPath(service, manifest)
            if (not os.path.isdir(path)):
                return errorResult(request,
                                   response,
                                   Errors.MANIFEST_NOT_FOUND,
                                   'manifest (%s/%s) missing service' %
                                   (service, manifest),
                                   controller=self)
            packageList = manifestutil.packagesInManifest(service, manifest)
            return ModulelogController.prepareOutput(
                packageList,
                ("/log/list/applog?service=%s&manifest=%s&package=" %
                 (service, manifest)), manifestutil.manifestPath(service),
                "List Of Packages")
        except OSError as excp:
            return errorResult(request,
                               response,
                               Errors.MANIFEST_PATH_ERROR,
                               'Manifest(%s, %s) path error: %s' %
                               (service, manifest, str(excp)),
                               controller=self)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 def log(self, service):
     """ Get service logs """
     if (service == ''):
         return errorResult(request, response, Errors.LOG_PARAM_REQUIRED,
             'Missing service', controller = self)
     if (not os.path.isdir(manifestutil.manifestPath(service))):
         return errorResult(request, response, Errors.SERVICE_NOT_FOUND,
             'Service specified is not found', controller = self)
     packageList = manifestutil.packagesInManifest(service)
     return ApplogController.prepareOutput(packageList, "/log/list/applog?service=" + service + "&package=", manifestutil.manifestPath(service), "List Of Packages")
Ejemplo n.º 5
0
 def listPackages(self):
     """ listPackages """
     service = request.params.get('service', '')
     package = request.params.get('package', '')
     manifest = request.params.get('manifest', 'active')
     if (service == ''):
         c.errorMsg = 'missing service parameter from request'
         c.errorCode = Errors.LOG_PARAM_REQUIRED
         return render('/derived/error.html')
     if (package != ''):
         return ModulelogController.doAppLogFile('list')
     if (not os.path.isdir(manifestPath(service, manifest))):
         return ModulelogController.doListManifests(service)
     packageList = packagesInManifest(service, manifest)
     return ModulelogController.prepareOutput(packageList, "/log/list/applog?service=" + service + "&manifest=" + manifest + "&package=", manifestPath(service, manifest), "List Of Packages")
Ejemplo n.º 6
0
    def log(self, service, manifest):
        """ Get manifest logs """
        LOG.info('Get for service (%s) and manifest (%s)', service, manifest)

        try:
            # first check that the manifest directory exists
            path = manifestutil.manifestPath(service, manifest)
            if (not os.path.isdir(path)):
                return errorResult(request, response, Errors.MANIFEST_NOT_FOUND,
                                   'manifest (%s/%s) missing service' % (service, manifest),
                                   controller=self)
            packageList = manifestutil.packagesInManifest(service, manifest)
            return ApplogController.prepareOutput(packageList, ("/log/list/applog?service=%s&manifest=%s&package=" % (service, manifest)), manifestutil.manifestPath(service), "List Of Packages")
        except OSError as excp:
            return errorResult(request, response, Errors.MANIFEST_PATH_ERROR,
                               'Manifest(%s, %s) path error: %s' % (service, manifest, str(excp)),
                               controller=self)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
 def listPackages(self):
     """ listPackages """
     service = request.params.get('service', '')
     package = request.params.get('package', '')
     manifest = request.params.get('manifest', 'active')
     if (service == ''):
         c.errorMsg = 'missing service parameter from request'
         c.errorCode = Errors.LOG_PARAM_REQUIRED
         return render('/derived/error.html')
     if (package != ''):
         return ModulelogController.doAppLogFile('list')
     if (not os.path.isdir(manifestPath(service, manifest))):
         return ModulelogController.doListManifests(service)
     packageList = packagesInManifest(service, manifest)
     return ModulelogController.prepareOutput(
         packageList, "/log/list/applog?service=" + service +
         "&manifest=" + manifest + "&package=",
         manifestPath(service, manifest), "List Of Packages")
Ejemplo n.º 9
0
 def log(self, service):
     """ Get service logs """
     if (service == ''):
         return errorResult(request,
                            response,
                            Errors.LOG_PARAM_REQUIRED,
                            'Missing service',
                            controller=self)
     if (not os.path.isdir(manifestutil.manifestPath(service))):
         return errorResult(request,
                            response,
                            Errors.SERVICE_NOT_FOUND,
                            'Service specified is not found',
                            controller=self)
     packageList = manifestutil.packagesInManifest(service)
     return ModulelogController.prepareOutput(
         packageList, "/log/list/applog?service=" + service + "&package=",
         manifestutil.manifestPath(service), "List Of Packages")
Ejemplo n.º 10
0
    def _obtainCmdExecThreads(self, exeName, service, manifest):
        """start the threads to execute cmd in each package in the service/manifest"""

        execThreads = []
        packages = packagesInManifest(service, os.path.basename(manifest))
        
        # Executing the deactivate in reversed order where the last package is first deactivated.
        if (exeName in ManifestControl.PKG_REVERSED_SET):
            packages.reverse()

        # make sure there's something to do, otherwise just return here
        if (len(packages) == 0):
            return execThreads
        
        for package in packages:

            execThread = self._getBuiltThread(service, manifest, package, exeName)
            if execThread:
                execThreads.append(execThread)
                
        return execThreads
Ejemplo n.º 11
0
 def testPackagesInManifest(self):
     createManifest(self, manifest = 'bar', service = 'foo')
     print manifestutil.packagesInManifest('foo', 'bar')
     assert len(manifestutil.packagesInManifest('foo', 'bar')) > 0
Ejemplo n.º 12
0
 def testPackagesInManifest(self):
     createManifest(self, manifest='bar', service='foo')
     print manifestutil.packagesInManifest('foo', 'bar')
     assert len(manifestutil.packagesInManifest('foo', 'bar')) > 0
Ejemplo n.º 13
0
    def _addMonitors(self, service, activeManifest):
        ''' add monitors for a service's activeManifest '''
        monitorTasks = []
        self.__monitorTasks[(service, activeManifest)] = monitorTasks
        
        # metric tags
        metricTags = {}
        # add default metric tags
        metricTags['fqdn'] = utils.fqdn
        # read tags from agent
        agtMetricTags = manifestutil.readJsonServiceMeta('agent', [METRIC_TAGS_KEY])
        if agtMetricTags and METRIC_TAGS_KEY in agtMetricTags:
            metricTags.update(agtMetricTags[METRIC_TAGS_KEY])
        # read tags from service
        if (service != 'agent'):
            serviceMetricTags = manifestutil.readJsonServiceMeta(service, [METRIC_TAGS_KEY])
            if  serviceMetricTags or METRIC_TAGS_KEY in serviceMetricTags:
                metricTags.update(serviceMetricTags[METRIC_TAGS_KEY])

        # all packages in this manifest
        packages = manifestutil.packagesInManifest(service, os.path.basename(activeManifest))

        # metric tags for externally passed in metrics
        # monitorTags = {additionaltags_dict, datatype_dict, ctx(env.pool.host), appType(Monitor|custom), monitorType(Cronus|CronusApplication)}
        self.__monitorTags[service] = {'default': (metricTags, {}, {})}
        self.__monitorValues[(service, 'default')] = {}
        self.__monitorMessages[(service, 'default')] = {}

        # a unique index for all monitors in the service
        for package in packages:

            # load monitor setting from cronus.ini
            pkgConfigs = manifestutil.getPackageInitConfig(service, activeManifest, package)
            
            if pkgConfigs is not None and 'monitors' in pkgConfigs:
                
                metricCtx = pkgConfigs[METRIC_CONTEXT] if METRIC_CONTEXT in pkgConfigs else {}
                
                metricTypes = {}
                if METRIC_TYPE_KEY in pkgConfigs and pkgConfigs[METRIC_TYPE_KEY]:
                    metricTypes = pkgConfigs[METRIC_TYPE_KEY]
                    
                if METRIC_TAGS_KEY in pkgConfigs and pkgConfigs[METRIC_TAGS_KEY]:
                    metricTags.update(pkgConfigs[METRIC_TAGS_KEY])
                    
                
                monitors = pkgConfigs['monitors']
                for monitor in monitors:
                    try:
                        m_name = str(monitor['name'])
                        
                        # metric tags for externally passed in metrics
                        # monitorTags = {additionaltags_dict, datatype_dict, ctx(env.pool.host), appType(Monitor|custom), monitorType(Cronus|CronusApplication)}
                        self.__monitorTags[service][m_name] = (metricTags, metricTypes, metricCtx)

                        # now normalize the interval to one allowable by EVPS (downcast)
                        if 'reportIntervalSec' not in monitor:
                            continue
                        m_interval = float(monitor['reportIntervalSec'])
                        m_interval = self._normalizeResolution(m_interval)

                        m_timeout = float(monitor['timeoutSec']) if 'timeoutSec' in monitor else m_interval
                        m_timeout = max(1, min(m_interval-1, m_timeout))
                        m_items = monitor['items'] if 'items' in monitor else []

                        # prime monitor value store
                        self.__monitorValues[(service, m_name)] = {}
                        self.__monitorMessages[(service, m_name)] = {}

                        mi_idx = 0
                        for m_item in m_items:
                            if 'type' not in m_item:
                                continue
                            mi_idx += 1
                            mi_type = m_item['type']
                            mi_cmd = None
                            if 'script' == mi_type:
                                mi_script = m_item['script']
                                mi_cmd = manifestutil.getMonitorScriptPath(service, activeManifest, package, mi_script)
                            else:
                                # unknown type, skip
                                LOG.error('Unknown monitor item type %s, can only be script' % mi_type)
                                continue

                            # schedule monitor
                            m_sch_name = '%s.%s' % (service, m_name)
                            try:
                                self.__monitorMessages[(service, m_name)][mi_cmd] = []
                                task = self.__monitorSch.add_interval_task(self._runMonitor, '%s.%s' % (m_sch_name, str(mi_idx)), 0, float(m_interval),
                                    [mi_type,mi_cmd,service,m_name,m_timeout,(str(int(round(m_interval))),metricTags,metricTypes,metricCtx)],None)
                                monitorTasks.append(task)
                            except Exception as excep:
                                LOG.error('Cannot add monitor task for %s.%s - (%s) %s' % (m_sch_name, str(mi_idx), excep, traceback.format_exc(5)))

                    except Exception as excep1:
                        LOG.error('Cannot add report task for %s.%s - (%s) %s' % (service, m_name, excep1, traceback.format_exc(5)))