Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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')

            updateThread = AgentUpdate(appGlobal.threadMgr, wisbVersion, wisbSource)
            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)
Ejemplo n.º 3
0
    def delete(self, service, manifest):
        """ Delete a new service object """
        try:
            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)

            # first check that this isn't the active manifest
            path = manifestutil.manifestPath(service)
            if (os.path.exists(path)):
                activePath = os.path.basename(readlink(path))
                deletePath = os.path.basename(manifestutil.manifestPath(service, manifest))

                if (activePath == deletePath):
                    return errorResult(request, response, Errors.MANIFEST_DELETING_ACTIVE_MANIFEST,
                                       'Manifest(%s, %s) attempting to delete active manifest'
                                       % (service, manifest),
                                       controller=self)

            # now try to delete the manifest directory
            appGlobal = config['pylons.app_globals']
            manThread = ManifestDelete(appGlobal.threadMgr, service, manifest)
            self.injectJobCtx(manThread)
            manThread.start()
            manThread.threadMgrEvent.wait()

            return statusResult(request, response, manThread, controller=self)

        except Exception as excep:
            return errorResult(request, response, error=Errors.UNKNOWN_ERROR,
                               errorMsg='Unknown error for delete manifest(%s/%s) - %s - %s' % 
                               (service, manifest, str(excep), traceback.format_exc(2)),
                               controller=self)
Ejemplo n.º 4
0
    def post(self, module):
        """ Create a new module object """
        try:

            LOG.info('Got a post request for module ' + module)

            if (request.body == ""):
                return errorResult(request, response, Errors.MODULE_PACKAGE_PARSING_ERROR,
                                   'No body found in post command',
                                   controller = self)

            body = json.loads(request.body)

            package = body['package']

            LOG.debug('pkgs = %s', package)

            appGlobal = config['pylons.app_globals']
            # start a thread to create the package
            moduleThread = ModuleCreate(appGlobal.threadMgr, module, package)
            self.injectJobCtx(moduleThread)
            moduleThread.start()
            moduleThread.threadMgrEvent.wait()

            return statusResult(request, response, moduleThread, 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 create module(%s/%s) - %s - %s' %
                               ('agent', module, str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 5
0
    def startdownload(self):
        """
        Download using http. The protocol is chosen based on package uri.
        Target folder is packageloc relative to repo_root.
        """
        try:
            utils.checkDiskFull()
            reqjson = json.loads(request.body)
            package = str(reqjson['package'])
            packageloc = str(reqjson['packageloc'])
            skipProp = asbool(reqjson['skipProp']) if 'skipProp' in reqjson else configutil.getConfigAsBool('download_skip_prop')
            
            LOG.info('Request received for StartDownload %s' % packageloc)
            appGlobal = config['pylons.app_globals']
            downloadThread = None

            cat = 'DIST_SD' + packageloc

            if not downloadThread:
                LOG.info('Starting a new StartDownload Thread %s' % packageloc)
                downloadThread = DownloadThread(appGlobal.threadMgr, package, packageloc, category = [cat], skipProp = skipProp)
                downloadThread.setMergeOnFound(True)
                self.injectJobCtx(downloadThread)
                downloadThread.start()
                downloadThread.threadMgrEvent.wait()

            return statusResult(request, response, downloadThread, controller = self)
        except AgentException as excep:
            return errorResult(request, response, error = excep.getCode(), errorMsg = excep.getMsg(), controller = self)
        except Exception as excp:
            errorMsg = 'Exception downloading %s - traceback %s' % (str(excp), traceback.format_exc(2))
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = errorMsg, controller = self)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    def post(self):
        """ enroll agent """
        try:
            body = json.loads(request.body)
            if 'hardwarePath' not in body:
                LOG.error('failed when enrolling: hardwarePath is not provided')
                return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg='hardwarePath is not specified', controller = self)

            with self.lock:
                LOG.info('enroll agent with hardwarePath %s' % (body['hardwarePath']))
                
                hardwarePath = body['hardwarePath']
                manifestutil.updateServiceMetaFile('agent', {'hwPath': hardwarePath,})

                appGlobal = config['pylons.app_globals']
                appGlobal.hwPath = hardwarePath
                appGlobal.enrolled = True
                
                appGlobal.agentMonitor.reloadMonitors()

                return doneResult(request, response, controller = self)

            return errorResult(request, response,
                               error = Errors.THREAD_ALREADY_ADDED,
                               errorMsg = "Cann't acquire lock for enrollment",
                               controller = self )

        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error for enroll - %s - %s' %
                               (str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 8
0
    def reset(self, service):
        ''' Controller to reset service '''
        LOG.info('reset for service(%s)', service)

        try:
            appGlobal = config['pylons.app_globals']

            if not os.path.exists(manifestutil.manifestPath(service, 'active')):
                return errorResult(request, response, Errors.ACTIVEMANIFEST_MANIFEST_MISSING,
                                   'Active Manifest(%s) path missing' % (service),
                                   controller = self)
            resetThread = ActivateManifest(appGlobal.threadMgr, service, 
                                           manifestutil.ACTIVE_MANIFEST,
                                           action = ActivateManifest.ACTION_RESET)
            self.injectJobCtx(resetThread)
            resetThread.start()
            resetThread.threadMgrEvent.wait()

            return statusResult(request, response, resetThread, controller = self)

        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error for restart service(%s) - %s - %s' %
                               (service, str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 9
0
    def get(self, uuid):
        """ Get the status of this particular thread.
        Use the uuid to grab the correct thread.
        return the progress and status of the thread. """

        try:
            appGlobal = config['pylons.app_globals']

            thread = appGlobal.threadMgr.getThreadByUuid(uuid)
            if (thread == None):
                script = manifestutil.getPackageScriptPath('agent', 'active', 'agent', 'uuid')
                tmp = [script, uuid]
                cmds = []
                for cmd in tmp:
                    cmds.append(cmd.encode('ascii', 'ignore'))
                cmdout = utils.runsyscmdwstdout(cmds)
                if cmdout:
                    # make sure it's a valid json
                    result = json.loads(cmdout) 
                    # return status as raw
                    return statusResultRaw(request, response, result)
                else:
                    return errorResult(request, response, Errors.STATUS_UUID_NOT_FOUND,
                                       'Unable to find thread with uuid(%s)' % uuid,
                                       controller = self)
            
        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error for get status(%s) - %s, %s' %
                               (uuid, str(excep), traceback.format_exc()),
                               controller = self)
            
        return statusResult(request, response, thread, controller = self, maxProgress = 99 if thread.isAlive() else 100)
Ejemplo n.º 10
0
    def rollbackservice(self, service):
        """ rollback an existing service """
        LOG.info('rollback to last active manifest for service(%s) ', service)
        manifest = None
        try:
            appGlobal = config['pylons.app_globals']
            manifest = serviceutil.getPastManifest(service, 1)
            
            if manifest:
                activateThread = ActivateManifest(appGlobal.threadMgr, service, manifest,
                                                  action=ActivateManifest.ACTION_ACTIVATION)
                self.injectJobCtx(activateThread)
                activateThread.start()
                activateThread.threadMgrEvent.wait()
            else:
                raise AgentException(Errors.MANIFEST_NOT_FOUND, "No rollback manifest found")

            return statusResult(request, response, activateThread, controller=self)

        except AgentException as excep:
            return errorResult(request, response, error=excep.getCode(), errorMsg=excep.getMsg(), controller=self)

        except Exception as excep:
            msg = 'Unknown error for rollback service(%s) - %s - %s' % (service, str(excep), traceback.format_exc(2))
            return errorResult(request, response, error=Errors.UNKNOWN_ERROR,
                               errorMsg=msg, controller=self)
Ejemplo n.º 11
0
    def startdownload(self):
        """
        Download using http. The protocol is chosen based on package uri.
        Target folder is packageloc relative to repo_root.
        """
        try:
            utils.checkDiskFull()
            reqjson = json.loads(request.body)
            
            package = reqjson['package']
            packageloc = str(reqjson['packageloc'])

            LOG.info('Request received for StartDownload %s' % packageloc)
            appGlobal = config['pylons.app_globals']
            
            LOG.info('Starting a new StartDownload Thread %s' % package)
            if type(package) == list:
                downloadThread = PackageDownload(appGlobal.threadMgr, package)
            else:
                cat = PackageUtil.getPackageKey(package)
                downloadThread = DownloadThread(appGlobal.threadMgr, package, packageloc, category = [cat])

            self.injectJobCtx(downloadThread)
            downloadThread.start()
            downloadThread.threadMgrEvent.wait()

            return statusResult(request, response, downloadThread, controller = self)
        except AgentException as excep:
            return errorResult(request, response, error = excep.getCode(), errorMsg = excep.getMsg(), controller = self)
        except Exception as excp:
            errorMsg = 'Exception downloading %s - traceback %s' % (str(excp), traceback.format_exc(2))
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = errorMsg, controller = self)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    def shutdown(self, service):
        ''' Controller to shutdown service '''
        LOG.info('shutdown for service(%s)', service)

        try:
            appGlobal = config['pylons.app_globals']

            if not os.path.exists(manifestutil.manifestPath(service,
                                                            'active')):
                return errorResult(request,
                                   response,
                                   Errors.ACTIVEMANIFEST_MANIFEST_MISSING,
                                   'Active Manifest(%s) path missing' %
                                   (service),
                                   controller=self)
            shutdownThread = StartStopService(appGlobal.threadMgr, service,
                                              StartStopService.ACTION_SHUTDOWN)
            self.injectJobCtx(shutdownThread)
            shutdownThread.start()
            shutdownThread.threadMgrEvent.wait()

            return statusResult(request,
                                response,
                                shutdownThread,
                                controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error for shutdown service(%s) - %s - %s' %
                (service, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 15
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.º 16
0
    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)
Ejemplo n.º 17
0
    def delete(self, service):
        """ Delete a new service object """
        from agent.lib.agent_thread.service_delete import ServiceDelete
        try:
            LOG.info('Got a delete request for service ' + service)

            path = ServiceController.servicePath(service)

            if (not os.path.exists(path) and not os.path.isdir(path)):
                return errorResult(request, response, Errors.SERVICE_NOT_FOUND, "No service(%s) found" % service, controller = self)

            # see if active manifest exist for the service
            if manifestutil.hasActiveManifest(service):
                return errorResult(request, response, Errors.MANIFEST_DELETING_ACTIVE_MANIFEST,
                                   'Active manifest exists for service %s, deactivate the manifest first before deleting service' % (service),
                                   controller = self)

            # start the delete thread
            appGlobal = config['pylons.app_globals']
            deleteThread = ServiceDelete(appGlobal.threadMgr, service, path)
            self.injectJobCtx(deleteThread)
            deleteThread.start()
            deleteThread.threadMgrEvent.wait()

            return statusResult(request, response, deleteThread, controller = self)
        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error when deleting service(%s) %s - %s' %
                               (service, str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 18
0
    def post(self, service):
        """ Create a new service object """
        try:

            path = ServiceController.servicePath(service)

            # skip if the path already exists
            if os.path.exists(path) and os.path.isdir(path):
                return doneResult(request, response, httpStatus = 201, result = service, controller = self)
            else:
                serviceutil.createServiceIfNeeded(service)

            # save metadata from payload if any
            if request.body:
                lcm_meta = json.loads(request.body)
                serviceutil.updateLcmMeta(service, lcm_meta)

            return doneResult(request, response, result = service, controller = self)

        except OSError:
            return errorResult(request, response, Errors.SERVICE_EXISTS,
                               "Service(%s) already exists(or %s)" % (service, traceback.format_exc(2)),
                               controller = self)
        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error when posting services %s - %s' %
                               (str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 19
0
    def deactivatemanifest(self, service):
        """ deactivate a manifest """
        LOG.info('activateManifest for service(%s) with body: %s', service,
                 request.body)
        try:
            appGlobal = config['pylons.app_globals']
            if not os.path.exists(manifestutil.manifestPath(service,
                                                            'active')):
                return errorResult(request,
                                   response,
                                   Errors.ACTIVEMANIFEST_MANIFEST_MISSING,
                                   'Deactivate Manifest(%s) path missing' %
                                   (service),
                                   controller=self)

            deactivateThread = DeactivateManifest(appGlobal.threadMgr, service)
            self.injectJobCtx(deactivateThread)
            deactivateThread.start()
            deactivateThread.threadMgrEvent.wait()

            return statusResult(request,
                                response,
                                deactivateThread,
                                controller=self)

        except Exception as excep:
            msg = 'Unknown error for deactivateManifest(%s) - %s - %s' % (
                service, str(excep), traceback.format_exc(2))
            return errorResult(request,
                               response,
                               error=Errors.UNKNOWN_ERROR,
                               errorMsg=msg,
                               controller=self)
Ejemplo n.º 20
0
    def get(self, module):
        """ Get a new service object """
        try:
            # make sure the service path exists
            path = manifestutil.modulePath('agent', module)
            if (not os.path.exists(path)):
                return errorResult(request,
                                   response,
                                   error=Errors.SERVICE_NOT_FOUND,
                                   errorMsg='Unable to find module (%s)' %
                                   module,
                                   controller=self)

            result = {}
            modulePackage = readlink(path)
            result['package'] = modulePackage

            return doneResult(request,
                              response,
                              result=result,
                              controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error when getting module (%s) %s - %s' %
                (module, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 21
0
    def delete(self):
        """ un-enroll agent """
        LOG.info('unenroll agent')
        try:
            with self.lock:
                appGlobal = config['pylons.app_globals']
                if hasattr(appGlobal, 'hwPath'):
                    setattr(appGlobal, 'hwPath', None)
                manifestutil.updateServiceMetaFile('agent', {'hwPath': None})
                appGlobal.enrolled = False
                appGlobal.agentMonitor.reloadMonitors()

                return doneResult(request, response, controller=self)

            return errorResult(
                request,
                response,
                error=Errors.THREAD_ALREADY_ADDED,
                errorMsg="Cann't acquire lock for un-enrollment",
                controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error for un-enroll - %s - %s' %
                (str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 22
0
    def get(self, module):
        """ Get a new service object """
        try:
            # make sure the service path exists
            path = manifestutil.modulePath("agent", module)
            if not os.path.exists(path):
                return errorResult(
                    request,
                    response,
                    error=Errors.SERVICE_NOT_FOUND,
                    errorMsg="Unable to find module (%s)" % module,
                    controller=self,
                )

            result = {}
            modulePackage = readlink(path)
            result["package"] = modulePackage

            return doneResult(request, response, result=result, controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg="Unknown error when getting module (%s) %s - %s"
                % (module, str(excep), traceback.format_exc(2)),
                controller=self,
            )
Ejemplo n.º 23
0
    def addKey(self, key):
        """ adding a new public key to agent"""
        try:
            pubKeyDir = os.path.join(manifestutil.appDataPath('agent'), 'secure')
            pubKeyFiles = [f for f in os.listdir(pubKeyDir) if re.match(r'.*\.pub', f)]

            if not key.endswith('.pub'):
                return errorResult(request, response, Errors.INVALID_REQUEST, 'Key %s must end with .pub' % key, controller = self)
                
            if key in pubKeyFiles:
                return errorResult(request, response, Errors.INVALID_REQUEST, 'Key %s already exist' % key, controller = self)
                
            if (not request.body or request.body == ""):
                LOG.error('invalid body found in post command')
                return errorResult(request, response, 10001, 'No body found', controller = self)

            body = json.loads(request.body)
            
            if 'content' not in body:
                return errorResult(request, response, Errors.INVALID_REQUEST, 'No key content found', controller = self)
            
            keycontent = body['content']
            filepath = os.path.join(pubKeyDir, key)
            utils.writeFile(filepath, keycontent)
            
            return doneResult(request, response, controller = self)

        except Exception as excp:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error adding key %s, %s - %s' %
                               (key, str(excp), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 24
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.º 25
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.º 26
0
    def executeCmd(self):
        """ execute a command synchronously """
        try:
            # parse the body
            if (not request.body or request.body == ""):
                LOG.error('invalid body found in post command')
                return errorResult(request,
                                   response,
                                   10001,
                                   'No body found',
                                   controller=self)

            body = json.loads(request.body)
            cmd0 = body['cmd'] if 'cmd' in body else None
            needsudo = asbool(
                body['need-sudo']) if 'need-sudo' in body else False
            sudotgt = body['sudo-target'] if 'sudo-target' in body else None
            paramobj = body['params'] if 'params' in body else []
            params = paramobj if type(paramobj) == list else paramobj.split()
            LOG.info('%s %s %s %s' % (cmd0, needsudo, sudotgt, params))

            if cmd0 is None or cmd0 == '':
                return errorResult(request,
                                   response,
                                   10002,
                                   'No command found',
                                   controller=self)

            cmd = [cmd0.encode('ascii', 'ignore')]
            if needsudo:
                cmd.insert(0, 'sudo')
                if sudotgt is not None:
                    sudotgt = sudotgt.encode('ascii', 'ignore')
                    cmd.insert(1, sudotgt)
                    cmd.insert(1, '-u')

            for param in params:
                param = param.encode('ascii', 'ignore')
                cmd.append(param)

            appGlobal = config['pylons.app_globals']
            execThread = ExecThread(appGlobal.threadMgr, cmd)
            execThread.setLogLevel('info')
            execThread.start()

            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' %
                (cmd, str(excp), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 27
0
    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)
Ejemplo n.º 28
0
    def get(self, service):
        """ Get a new service object """
        try:
            from agent.lib.agent_thread.manifest_create import ManifestCreate

            # make sure the service path exists
            path = ServiceController.servicePath(service)
            if (not os.path.exists(path)):
                return errorResult(request,
                                   response,
                                   error=Errors.SERVICE_NOT_FOUND,
                                   errorMsg='Unable to find service (%s)' %
                                   service,
                                   controller=self)

            path = ServiceController.manifestPath(service)

            activeManifest = None
            manifestList = []
            for manifest in os.listdir(path):
                if (ManifestCreate.isInProgress(manifest)):
                    continue

                manifestPath = os.path.join(path, manifest)
                if (manifest == 'active'):
                    activeLink = readlink(manifestPath)

                    if (activeLink == None):
                        manifestList.append(manifest)
                    else:
                        activeManifest = os.path.basename(activeLink)
                else:
                    manifestList.append(manifest)

            result = {}
            manifestList.sort()
            result['manifest'] = manifestList
            result['activemanifest'] = activeManifest

            return doneResult(request,
                              response,
                              result=result,
                              controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error when getting service (%s) %s - %s' %
                (service, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 29
0
    def post(self, service):
        """ Create a new service object """
        try:

            LOG.info('Got a post request for service ' + service)
            path = ServiceController.servicePath(service)

            # skip if the path already exists
            if os.path.exists(path) and os.path.isdir(path):
                return doneResult(request,
                                  response,
                                  httpStatus=201,
                                  result=service,
                                  controller=self)

            os.makedirs(path)
            os.makedirs(os.path.join(path, 'manifests'))
            os.makedirs(os.path.join(path, 'installed-packages'))
            os.makedirs(os.path.join(path, 'modules'))
            os.makedirs(os.path.join(path, 'downloaded-packages'))
            os.makedirs(os.path.join(path, '.appdata'))
            os.makedirs(os.path.join(path, '.data'))
            utils.rchmod(os.path.join(path, '.appdata'), '+rwx')

            # verify that the path exists
            if (not os.path.isdir(path)):
                return errorResult(request,
                                   response,
                                   Errors.UNKNOWN_ERROR,
                                   "Service(%s) was not created" % service,
                                   controller=self)

            return doneResult(request,
                              response,
                              result=service,
                              controller=self)

        except OSError:
            return errorResult(request,
                               response,
                               Errors.SERVICE_EXISTS,
                               "Service(%s) already exists(or %s)" %
                               (service, traceback.format_exc(2)),
                               controller=self)
        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error when posting services %s - %s' %
                (str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 30
0
    def get(self, service):
        """ Get a new service object """
        try:
            from agent.lib.agent_thread.manifest_create import ManifestCreate

            # make sure the service path exists
            path = ServiceController.servicePath(service)
            if not os.path.exists(path):
                return errorResult(
                    request,
                    response,
                    error=Errors.SERVICE_NOT_FOUND,
                    errorMsg="Unable to find service (%s)" % service,
                    controller=self,
                )

            path = ServiceController.manifestPath(service)

            activeManifest = None
            manifestList = []
            for manifest in os.listdir(path):
                if ManifestCreate.isInProgress(manifest):
                    continue

                manifestPath = os.path.join(path, manifest)
                if manifest == "active":
                    activeLink = readlink(manifestPath)

                    if activeLink == None:
                        manifestList.append(manifest)
                    else:
                        activeManifest = os.path.basename(activeLink)
                else:
                    manifestList.append(manifest)

            result = {}
            manifestList.sort()
            result["manifest"] = manifestList
            result["activemanifest"] = activeManifest

            return doneResult(request, response, result=result, controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg="Unknown error when getting service (%s) %s - %s"
                % (service, str(excep), traceback.format_exc(2)),
                controller=self,
            )
Ejemplo n.º 31
0
    def delete(self, module):
        """ Delete a new service object """
        try:
            LOG.info('Got a delete request for module ' + module)

            path = manifestutil.modulePath('agent', module)

            if (not os.path.exists(path) and not os.path.isdir(path)):
                return doneResult(request, response, controller=self)

            # start the delete thread
            appGlobal = config['pylons.app_globals']
            deleteThread = ModuleDelete(appGlobal.threadMgr, module)
            self.injectJobCtx(deleteThread)
            deleteThread.start()
            deleteThread.threadMgrEvent.wait()

            return statusResult(request,
                                response,
                                deleteThread,
                                controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error when deleting module(%s) %s - %s' %
                (module, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 32
0
    def updateMetadata(self, service):
        """ create or update .metadata file for a service """
        # now connect to state server to store metadata for the service in .metadata
        metadata = None
        if request.body:
            # two flavor of updatemetadata call, one with body, one without
            body = json.loads(request.body)
            if 'metadata' in body:
                metadata = body['metadata']

        try:
            result = {}
            
            appGlobal = pylons.config['pylons.app_globals']

            if metadata is not None and 'monitoring.metric.tags' in metadata:
                appGlobal.agentMonitor.reloadMonitors()
            
            result = manifestutil.updateServiceMetaFile(service, metadata)
            
            return doneResult(request, response, result = result, controller = self)

        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error when update service metadata (%s) %s - %s' %
                               (service, str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 33
0
    def delete(self, module):
        """ Delete a new service object """
        try:
            LOG.info("Got a delete request for module " + module)

            path = manifestutil.modulePath("agent", module)

            if not os.path.exists(path) and not os.path.isdir(path):
                return doneResult(request, response, controller=self)

            # start the delete thread
            appGlobal = config["pylons.app_globals"]
            deleteThread = ModuleDelete(appGlobal.threadMgr, module)
            self.injectJobCtx(deleteThread)
            deleteThread.start()
            deleteThread.threadMgrEvent.wait()

            return statusResult(request, response, deleteThread, controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg="Unknown error when deleting module(%s) %s - %s"
                % (module, str(excep), traceback.format_exc(2)),
                controller=self,
            )
Ejemplo n.º 34
0
    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)
Ejemplo n.º 35
0
    def delete(self, service, manifest):
        """ Delete a new service object """
        try:
            path = ManifestController.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)

            # first check that this isn't the active manifest
            path = os.path.join(ServiceController.manifestPath(service),
                                'active')
            if (os.path.exists(path)):
                activePath = os.path.basename(readlink(path))
                deletePath = os.path.basename(
                    ManifestController.manifestPath(service, manifest))

                if (activePath == deletePath):
                    return errorResult(
                        request,
                        response,
                        Errors.MANIFEST_DELETING_ACTIVE_MANIFEST,
                        'Manifest(%s, %s) attempting to delete active manifest'
                        % (service, manifest),
                        controller=self)

            # now try to delete the manifest directory
            appGlobal = config['pylons.app_globals']
            manThread = ManifestDelete(appGlobal.threadMgr, service, manifest)
            self.injectJobCtx(manThread)
            manThread.start()
            manThread.threadMgrEvent.wait()

            return statusResult(request, response, manThread, controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error for delete manifest(%s/%s) - %s - %s' %
                (service, manifest, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 36
0
    def get(self, service, manifest):
        """ Get a new service object """
        LOG.info('Get for service (%s) and manifest (%s)', service, manifest)

        try:
            # first check that the manifest directory exists
            path = ManifestController.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)

            # now go through the list of packages in the manifest
            packages = []
            packageLinkNames = glob.glob(
                os.path.join(self.manifestPath(service, manifest), '*'))
            for packageLink in packageLinkNames:

                package = readlink(packageLink)

                LOG.debug('Get package (%s) in manifest (%s)', package,
                          manifest)

                # deal with the case where the package has a / or \ (for windoz) at the end
                package = package.rstrip('/')
                package = package.rstrip('\\')

                # the name of the package can be constructed from the last two path components
                (head, version) = os.path.split(package)
                (head, name) = os.path.split(head)

                LOG.debug('Add package %s-%s.cronus' % (name, version))
                packages.append('%s-%s.cronus' % (name, version))

        except OSError as excp:
            return errorResult(request,
                               response,
                               Errors.MANIFEST_PATH_ERROR,
                               'Manifest(%s, %s) path error: %s' %
                               (service, manifest, str(excp)),
                               controller=self)

        return doneResult(request, response, result=packages, controller=self)
Ejemplo n.º 37
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.º 38
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.º 39
0
    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)
Ejemplo n.º 40
0
    def post(self, module):
        """ Create a new module object """
        try:

            LOG.info('Got a post request for module ' + module)

            if (request.body == ""):
                return errorResult(request,
                                   response,
                                   Errors.MODULE_PACKAGE_PARSING_ERROR,
                                   'No body found in post command',
                                   controller=self)

            body = json.loads(request.body)

            package = body['package']

            LOG.debug('pkgs = %s', package)

            appGlobal = config['pylons.app_globals']
            # start a thread to create the package
            moduleThread = ModuleCreate(appGlobal.threadMgr, module, package)
            self.injectJobCtx(moduleThread)
            moduleThread.start()
            moduleThread.threadMgrEvent.wait()

            return statusResult(request,
                                response,
                                moduleThread,
                                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 create module(%s/%s) - %s - %s' %
                ('agent', module, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 41
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.º 42
0
    def delete(self, service):
        """ Delete a new service object """
        from agent.lib.agent_thread.service_delete import ServiceDelete
        try:
            LOG.info('Got a delete request for service ' + service)

            path = ServiceController.servicePath(service)

            if (not os.path.exists(path) and not os.path.isdir(path)):
                return errorResult(request,
                                   response,
                                   Errors.SERVICE_NOT_FOUND,
                                   "No service(%s) found" % service,
                                   controller=self)

            # see if active manifest exist for the service
            if manifestutil.hasActiveManifest(service):
                return errorResult(
                    request,
                    response,
                    Errors.MANIFEST_DELETING_ACTIVE_MANIFEST,
                    'Active manifest exists for service %s, deactivate the manifest first before deleting service'
                    % (service),
                    controller=self)

            # start the delete thread
            appGlobal = config['pylons.app_globals']
            deleteThread = ServiceDelete(appGlobal.threadMgr, service, path)
            self.injectJobCtx(deleteThread)
            deleteThread.start()
            deleteThread.threadMgrEvent.wait()

            return statusResult(request,
                                response,
                                deleteThread,
                                controller=self)
        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error when deleting service(%s) %s - %s' %
                (service, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 43
0
    def post(self, service):
        """ Create a new service object """
        try:

            LOG.info("Got a post request for service " + service)
            path = ServiceController.servicePath(service)

            # skip if the path already exists
            if os.path.exists(path) and os.path.isdir(path):
                return doneResult(request, response, httpStatus=201, result=service, controller=self)

            os.makedirs(path)
            os.makedirs(os.path.join(path, "manifests"))
            os.makedirs(os.path.join(path, "installed-packages"))
            os.makedirs(os.path.join(path, "modules"))
            os.makedirs(os.path.join(path, "downloaded-packages"))
            os.makedirs(os.path.join(path, ".appdata"))
            os.makedirs(os.path.join(path, ".data"))
            utils.rchmod(os.path.join(path, ".appdata"), "+rwx")

            # verify that the path exists
            if not os.path.isdir(path):
                return errorResult(
                    request, response, Errors.UNKNOWN_ERROR, "Service(%s) was not created" % service, controller=self
                )

            return doneResult(request, response, result=service, controller=self)

        except OSError:
            return errorResult(
                request,
                response,
                Errors.SERVICE_EXISTS,
                "Service(%s) already exists(or %s)" % (service, traceback.format_exc(2)),
                controller=self,
            )
        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg="Unknown error when posting services %s - %s" % (str(excep), traceback.format_exc(2)),
                controller=self,
            )
Ejemplo n.º 44
0
 def getServicesSummary(self):
     """ service summary, all about service in one call """
     try:
         services_data = serviceutil.getServiceSummary()
         return  doneResult(request, response, result = services_data, controller = self)
     except Exception as excep:
         return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                            errorMsg = 'Unknown error when get services summary %s - %s' %
                            (str(excep), traceback.format_exc(2)),
                            controller = self)
Ejemplo n.º 45
0
    def post(self):
        """ enroll agent """
        try:
            body = json.loads(request.body)
            if 'hardwarePath' not in body:
                LOG.error(
                    'failed when enrolling: hardwarePath is not provided')
                return errorResult(request,
                                   response,
                                   error=Errors.UNKNOWN_ERROR,
                                   errorMsg='hardwarePath is not specified',
                                   controller=self)

            with self.lock:
                LOG.info('enroll agent with hardwarePath %s' %
                         (body['hardwarePath']))

                hardwarePath = body['hardwarePath']
                manifestutil.updateServiceMetaFile('agent', {
                    'hwPath': hardwarePath,
                })

                appGlobal = config['pylons.app_globals']
                appGlobal.hwPath = hardwarePath
                appGlobal.enrolled = True

                appGlobal.agentMonitor.reloadMonitors()

                return doneResult(request, response, controller=self)

            return errorResult(request,
                               response,
                               error=Errors.THREAD_ALREADY_ADDED,
                               errorMsg="Cann't acquire lock for enrollment",
                               controller=self)

        except Exception as excep:
            return errorResult(request,
                               response,
                               error=Errors.UNKNOWN_ERROR,
                               errorMsg='Unknown error for enroll - %s - %s' %
                               (str(excep), traceback.format_exc(2)),
                               controller=self)
Ejemplo n.º 46
0
 def listServices(self):
     """ list all the services in the agent """
     try:
         services = ServiceController.getServices()
         return  doneResult(request, response, result = services, controller = self)
     except Exception as excep:
         return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                            errorMsg = 'Unknown error when listing services %s - %s' %
                            (str(excep), traceback.format_exc(2)),
                            controller = self)
Ejemplo n.º 47
0
    def getMetadata(self, service):
        """ get .metadata file for a service """
        try:
            result = manifestutil.readJsonServiceMeta(service)
            return doneResult(request, response, result = result, controller = self)

        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error get service metadata (%s) %s - %s' %
                               (service, str(excep), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 48
0
    def removeKey(self, key):
        """ remove a key from agent """
        try:
            pubKeyDir = os.path.join(manifestutil.appDataPath('agent'), 'secure')
            pubKeyFiles = [f for f in os.listdir(pubKeyDir) if re.match(r'.*\.pub', f)]
        
            if key in pubKeyFiles: 
                if len(pubKeyFiles) == 1:
                    return errorResult(request, response, Errors.INVALID_REQUEST, "Cannot delete the last key", controller = self)
        
                utils.rmrf(os.path.join(pubKeyDir, key))
            else:
                return errorResult(request, response, Errors.INVALID_REQUEST, "Invalid key %s to delete" % key, controller = self)
            return doneResult(request, response, controller = self)

        except Exception as excp:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error removing key %s,  %s - %s' %
                               (key, str(excp), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 49
0
    def get(self, uuid):
        """ Get the status of this particular thread.
        Use the uuid to grab the correct thread.
        return the progress and status of the thread. """

        try:
            appGlobal = config['pylons.app_globals']

            thread = appGlobal.threadMgr.getThreadByUuid(uuid)
            if (thread == None):
                return errorResult(request, response, Errors.STATUS_UUID_NOT_FOUND,
                                   'Unable to find thread with uuid(%s)' % uuid,
                                   controller = self)
            
        except Exception as excep:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error for get status(%s) - %s, %s' %
                               (uuid, str(excep), traceback.format_exc()),
                               controller = self)
            
        return statusResult(request, response, thread, controller = self, maxProgress = 99 if thread.isAlive() else 100)
Ejemplo n.º 50
0
 def getConfig(self):
     """ get config overrides """
     LOG.info('get config overrides for agent')
     result = {}
     try:
         result = manifestutil.readJsonServiceMeta('agent', ['configs'])
         return doneResult(request, response, result = result, controller = self)
         
     except Exception as excep:
         return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                            errorMsg = 'Unknown error when clean agent configs %s - %s' %
                            (str(excep), traceback.format_exc(2)), controller = self)
Ejemplo n.º 51
0
    def uploadPackage(self):
        """ take an upload file """
        try:
            utils.checkDiskFull()

            agt_root = pylons.config['agent_root']
            pkg_root = pylons.config['repo_root']

            md5 = str(request.POST['md5']) if 'md5' in request.POST else None
            dest = str(request.POST['dest']) if 'dest' in request.POST else None

            myfile = request.POST['file']
            filename = myfile.filename.lstrip(os.sep)
            permanent_file = open(os.path.join(pkg_root, filename), 'w')

            shutil.copyfileobj(myfile.file, permanent_file)
            myfile.file.close()
            permanent_file.close()
            
            if md5:
                md5Sum = utils.md5sum(permanent_file)
                if (md5Sum != md5):
                    msg = 'package md5 = %s : %s' % (md5Sum, md5)
                    LOG.warning(msg)
                    raise AgentException(Errors.DC_FAILED_VALIDATE, msg)
            
            if filename and dest:
                utils.copyFile(pkg_root, os.path.join(agt_root, 'service_nodes', dest), filename) 

            return doneResult(request, response, controller=self)
        
        except AgentException as excp:
            return errorResult(request, response, excp.getCode(), excp.getMsg(), controller = self)
        
        except Exception as excp:
            errorMsg = 'Exception downloading %s - traceback %s' % (str(excp), traceback.format_exc(2))
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = errorMsg, controller = self)
Ejemplo n.º 52
0
 def cleanConfig(self):
     """ clean all configs """
     LOG.info('clean agent config overrides')
     result = {}
     try:
         result = manifestutil.updateServiceMetaFile('agent', {'configs': None})
         return doneResult(request, response, result = result, controller = self)
         
     except Exception as excep:
         return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                            errorMsg = 'Unknown error when clean agent configs %s - %s' %
                            (str(excep), traceback.format_exc(2)), controller = self)
     finally:
         # reload config overrides
         configutil.loadConfigOverrides()
Ejemplo n.º 53
0
    def deletePackage(self, package):
        """ secret !!hack!! API to delete one cronus package in the packages folder. """
        try:
            LOG.info('secret delete called for package %s' % package)
            packagesPath = PackageMgr.packagePath()
            thisPkgPath = os.path.join(packagesPath, package + '.cronus')
            thisPropPath = os.path.join(packagesPath, package + '.cronus.prop')
            thisInprogressPath = PackageUtil.inProgressPath(thisPkgPath)

            PackageUtil.cleanUpPackage(thisInprogressPath, thisPkgPath, thisPropPath)
            return doneResult(request, response, controller=self)
        except Exception as excp:
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = 'Unknown error when deleting package %s,  %s - %s' %
                               (package, str(excp), traceback.format_exc(2)),
                               controller = self)
Ejemplo n.º 54
0
 def listServices(self):
     """ list all the services in the agent """
     try:
         LOG.info('Got a list service request')
         services = ServiceController.getServices()
         return doneResult(request,
                           response,
                           result=services,
                           controller=self)
     except Exception as excep:
         return errorResult(
             request,
             response,
             error=Errors.UNKNOWN_ERROR,
             errorMsg='Unknown error when listing services %s - %s' %
             (str(excep), traceback.format_exc(2)),
             controller=self)
Ejemplo n.º 55
0
    def getMetadata(self, service):
        """ get .metadata file for a service """
        try:
            result = manifestutil.readJsonServiceMeta(service)
            return doneResult(request,
                              response,
                              result=result,
                              controller=self)

        except Exception as excep:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error get service metadata (%s) %s - %s' %
                (service, str(excep), traceback.format_exc(2)),
                controller=self)
Ejemplo n.º 56
0
 def getExecOutput(self, uuid):
     """ get script output with an uuid """
     LOG.info('get ouput for %s' % uuid)
     try:
         script = os.path.join(manifestutil.manifestPath('agent'), 'agent', 'cronus', 'scripts', 'execoutput')
         LOG.info('execoutput script %s' % script)
         if not uuid or uuid == '':
             raise AgentException(Errors.INVALID_REQUEST, 'uuid cannot be empty')
         tmp = [script, uuid]
         cmds = []
         for cmd in tmp:
             cmds.append(cmd.encode('ascii', 'ignore'))
         cmdout = utils.runsyscmdwstdout(cmds)
         return cmdout
         
     except Exception as excep:
         return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                            errorMsg = 'Error when get execoutput %s - %s' %
                            (str(excep), traceback.format_exc(2)), controller = self)
Ejemplo n.º 57
0
 def getTaskSlaReport(self, task, threshold=0, starttime=0, fmt='raw'):
     """ get task SLA report """
     LOG.info('generating task SLA report')
     try:
         script = os.path.join(manifestutil.manifestPath('agent'), 'agent', 'cronus', 'scripts', 'perfmetric')
         LOG.info('task sla report script %s' % script)
         if not task or task == '':
             raise AgentException(Errors.INVALID_REQUEST, 'task name cannot be empty')
         tmp = [script, task, str(threshold), str(starttime), fmt]
         cmds = []
         for cmd in tmp:
             cmds.append(cmd.encode('ascii', 'ignore'))
         cmdout = utils.runsyscmdwstdout(cmds)
         result = json.loads(cmdout)
         return doneResult(request, response, result=result, controller = self)
         
     except Exception as excep:
         return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                            errorMsg = 'Error when getting task sla report %s - %s' %
                            (str(excep), traceback.format_exc(2)), controller = self)
Ejemplo n.º 58
0
    def safeshutdown(self):
        """ This controller shutdown the agent.
        This should not be exposed to external usage.
        This should only be used for testing and also for self update.
        """

        LOG.info('[AGENT_SUICIDE] safe shutdown called.  exiting the agent. This is an expected behavior when rest api shutdown is called. ')
        try:
            appGlobal = config['pylons.app_globals']
            shutdownThread = SafeShutdown(appGlobal.threadMgr)
            self.injectJobCtx(shutdownThread)
            shutdownThread.start()
            shutdownThread.threadMgrEvent.wait()

            return statusResult(request, response, shutdownThread, controller = self)

        except Exception as excep:
            msg = 'Unknown error for safeshutdown %s - %s' % (str(excep), traceback.format_exc(2))
            return errorResult(request, response, error = Errors.UNKNOWN_ERROR,
                               errorMsg = msg, controller = self)
Ejemplo n.º 59
0
    def cleanupProcess(self):
        """ cleanup services """
        try:
            LOG.info('cleanup all application services')
            appGlobal = config['pylons.app_globals']
            cleanupThread = ServicesCleanup(appGlobal.threadMgr, stopOnly=True)
            self.injectJobCtx(cleanupThread)
            cleanupThread.start()
            cleanupThread.threadMgrEvent.wait()

            return statusResult(request,
                                response,
                                cleanupThread,
                                controller=self)
        except Exception as excp:
            return errorResult(
                request,
                response,
                error=Errors.UNKNOWN_ERROR,
                errorMsg='Unknown error when deleting services %s - %s' %
                (str(excp), traceback.format_exc(2)),
                controller=self)