def get_config(self, conf, local=False):
     """
     Retrieves local or merged dictionary of dicts local app context.
     This function creates parity for use with writeConfFile in splunk.clilib.
     Should use cli.getMergedConf(), but does not support custom conf files.
     :param conf:  Splunk conf file file name
     :param local: local config only
     :return: dictionary of dicts
     """
     cli.getMergedConf()
     conf = "%s.conf" % conf
     defaultconfpath = os.path.join(self.dir, "default", conf)
     stanzaDict = cli.readConfFile(defaultconfpath) if os.path.exists(defaultconfpath) else {}
     localconfpath = os.path.join(self.dir, "local", conf)
     if not local:
         if os.path.exists(localconfpath):
             localconf = cli.readConfFile(localconfpath)
             for setting, stanza in localconf.items():
                 if setting in stanzaDict:
                     stanzaDict[setting].update(stanza)
                 else:
                     stanzaDict[setting] = stanza
     else:
         stanzaDict = cli.readConfFile(localconfpath) if os.path.exists(localconfpath) else {}
     return stanzaDict
Exemplo n.º 2
0
 def _merger(self, src, dst, dryRun):
     try:
         if dryRun:
             # Be less verbose for dry runs. More detailed information is
             # likely to be misleading because of dry run limitations.
             logger.notice(lit("INFO_MIGRATE_MOVE_DRYRUN__S") % src)
             return
         root, ext = os.path.splitext(src)
         if ext == ".conf":
             if os.path.lexists(dst):
                 # Combine src and dst confs; don't override anything in dst.
                 combinedConf = comm.readConfFile(src)
                 dstConf = comm.readConfFile(dst)
                 for k in dstConf.keys():
                     if combinedConf.has_key(k):
                         combinedConf[k].update(dstConf[k])
                     else:
                         combinedConf[k] = dstConf[k]
                 # In case we don't have permission to truncate the
                 # file, just remove it preemptively.
                 safe_remove(dst)
                 logger.notice(lit("INFO_MIGRATE_MERGE_CONF__S_S") % (src, dst))
                 comm.writeConfFile(dst, combinedConf)
             else:
                 comm.copyItem(src, dst)
         else:
             if os.path.lexists(dst):
                 logger.notice(lit("INFO_MIGRATE_IGNORE_DUP__S_S") % (src, dst))
             else:
                 comm.copyItem(src, dst)
     except Exception, e:
         logger.warn(lit("WARN_MIGRATE_NO_CREATE__S") % dst)
         logger.exception(e)
Exemplo n.º 3
0
def migrateFieldActions(infile, outfile):
    '''
    The general function that takes an input file path and an output file path
    generates the migration and writes it out.
    
    infile => the file to read in from
    outfile => the file to write out to. 
    '''
    parsed_infile = comm.readConfFile(infile)
    
    output = {}
    for stanza in parsed_infile:
        if "default" == stanza:
            continue
        new_stanza = migrateFieldActionStanza(parsed_infile[stanza])
        if new_stanza != None:
            output[stanza] = new_stanza
    
    outdir = os.path.abspath(os.path.dirname(outfile))
    try:
        os.makedirs(outdir, 0755)
    except OSError, e:
        if e.errno == errno.EEXIST:
            pass
        else:
            logger.warn("Could not create the directory specified in '%s'." % outfile)
            raise
Exemplo n.º 4
0
def getstanza(conf, stanza):
    """
    #Returns dict object of config file settings
    #:param conf: Splunk conf file name
    #:param stanza: stanza (entry) from conf file
   #:return: returns dictionary of setting
    """
    appdir = os.path.dirname(os.path.dirname(__file__))
    conf = "%s.conf" % conf
    apikeyconfpath = os.path.join(appdir, "default", conf)
    apikeyconf = cli.readConfFile(apikeyconfpath)
    localconfpath = os.path.join(appdir, "local", conf)
    if os.path.exists(localconfpath):
        localconf = cli.readConfFile(localconfpath)
        for name, content in localconf.items():
            if name in apikeyconf:
                apikeyconf[name].update(content)
            else:
                apikeyconf[name] = content
    return apikeyconf[stanza]
def setLocalName(args, fromCLI):
  paramReq = ("name",)
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  name = args["name"]
  if name.count(" ") + name.count(":") > 0:
    raise ArgError, "Name '%s' is invalid.  Names cannot contain spaces or colons." % name

  currSetts = comm.readConfFile(fullServerConfig)
  if not serverGeneralStanza in currSetts:
    currSetts[serverGeneralStanza] = {}
  currSetts[serverGeneralStanza]["serverName"] = name
  comm.writeConfFile(fullServerConfig, currSetts)
  return {}
def getInstanceName(argsDict, fromCLI):
    reqkeys = ()
    optkeys = ()
    returnDict = {}
    comm.validateArgs(reqkeys, optkeys, argsDict )

    currSetts = comm.readConfFile(fullServerConfig)
    returnDict["instancename"] = "(unset)";
    if serverGeneralStanza in currSetts:
      if "serverName" in currSetts[serverGeneralStanza]:
        returnDict["instancename"] = currSetts[serverGeneralStanza]["serverName"]

    if fromCLI:
      logger.info("Server name: %s." % returnDict["instancename"])
    return returnDict
def setLocalHttp(args, fromCLI):
  paramReq = ("port",)
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  if fromCLI:
    validate.checkPortNum("port", args["port"])

  retDict = {}
  key = "httpport"
  currSetts = comm.readConfFile(fullWebConfig)

  if not webSettingsStanza in currSetts:
    currSetts[webSettingsStanza] = {}
  currSetts[webSettingsStanza][key] = args["port"]

  comm.writeConfFile(fullWebConfig, currSetts)
  return retDict
Exemplo n.º 8
0
 def __init__(self, bundle):
     self._dict = { }
     self._dict[self._STANZA_INSTALL] = { }
     try:
         base = bundle.location()
         self._manifest_writable = join(base, Bundle._LOCAL, self._MANIFEST)
         self._manifest_install = join(base, Bundle._DEFAULT, self._MANIFEST)
         all_candidates = [ self._manifest_writable,
                            self._manifest_install,
                            join(base, self._MANIFEST) ]
         # find actual MANIFEST to read
         for candidate in all_candidates:
             if os.path.isfile(candidate):
                 self._dict = comm.readConfFile(candidate)
                 break
     except:
         pass
def stanzaDisable(args, fromCLI):
  """
  Disables a given stanza in a given conf file.
  """
  paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
  paramsOpt = (ARG_AUTHSTR, ARG_USEFS)
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}
  stanza = args[ARG_STANZA]
  confFile = bundle_paths.make_path(args[ARG_CONFIG] + ".conf")

  authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or ""

  # see if it's currently enabled.
  currStatus = stanzaStatusInternal({ARG_CONFIG : args[ARG_CONFIG], ARG_MODNAME : args[ARG_MODNAME], ARG_STANZA : stanza, ARG_AUTHSTR: authStr}, fromCLI)
  currEnabled = currStatus["enabled"]

  # get the local .conf file.  don't use the stanzas given to us from "status",
  # because they're merged - we don't want to write out all the merged settings.
  # btw, readConfFile checks to see if the path exists and does the right thing.
  confStanzas = comm.readConfFile(confFile)

  if currEnabled: # then disable it
    returnDict["restartRequired"] = True
    # create the stanza if it's not in the .conf already (could be on via default bundle)
    if not stanza in confStanzas:
      confStanzas[stanza] = {}
    # and make sure we set the disabled key in local to be true.
    confStanzas[stanza][KEY_DISABLED] = "true"
    stanzaXml = comm.flatDictToXML(confStanzas[stanza]) 
    # the following is now always true:
    # in order to support "splunk set deploy-poll", we allow this stuff to also write to the local filesystem.
    logger.debug("Attempting to disable module via local filesystem.")
    comm.writeConfFile(confFile, confStanzas)

  if not currEnabled:
    logger.info("%s is already disabled." % args[ARG_MODNAME])
  else:
    logger.info("%s disabled." % args[ARG_MODNAME])
  return returnDict
Exemplo n.º 10
0
def stanzaEnable(args, fromCLI):
  """
  Enables a given stanza in a given conf file.
  """
  paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
  paramsOpt = (ARG_AUTHSTR, ARG_USEFS)
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}
  authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or ""
  stanza = args[ARG_STANZA]
  confFile = bundle_paths.make_path(args[ARG_CONFIG] + ".conf")
  currStatus = stanzaStatusInternal({ARG_CONFIG : args[ARG_CONFIG], ARG_MODNAME : args[ARG_MODNAME], ARG_STANZA : stanza, ARG_AUTHSTR: authStr}, fromCLI)
  currEnabled = currStatus["enabled"]

  # get the local .conf file.  don't use the stanzas given to us from "status",
  # because they're merged - we don't want to write out all the merged settings.
  # btw, readConfFile checks to see if the path exists and does the right thing.
  confStanzas = comm.readConfFile(confFile)

  if not currEnabled: # then enable it
    returnDict["restartRequired"] = True
    # create the stanza if it's not in the .conf already (if it was never enabled)
    if not stanza in confStanzas:
      confStanzas[stanza] = {}
    # at this point the only way for it to be disabled is for the disabled key to be true.
    # set it false regardless of whether or not it exists.
    confStanzas[stanza][KEY_DISABLED] = "false"
    stanzaXml = comm.flatDictToXML(confStanzas[stanza]) 
    # the following is now always true:
    # if applicable, just write to the local FS (used by splunk set deploy-poll).
    logger.debug("Attempting to enable module via local filesystem.")
    comm.writeConfFile(confFile, confStanzas)

  if currEnabled:
    logger.info("%s is already enabled." % args[ARG_MODNAME])
  else:
    logger.info("%s enabled." % args[ARG_MODNAME])
  return returnDict
def setLocalMgmtPort(args, fromCLI):
  paramReq = ("port",)
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  if fromCLI:
    validate.checkPortNum("port", args["port"])

  retDict = {}
  host = "localhost"
  key  = "mgmtHostPort"
  currSetts = comm.readConfFile(fullWebConfig)

  if not webSettingsStanza in currSetts:
    currSetts[webSettingsStanza] = {}
  if key in currSetts[webSettingsStanza]:
    try:
      # this expects the uri as "blah:8000" - or just "a:b", really.
      host, oldPortWhichIsGarbage = currSetts[webSettingsStanza][key].split(":", 1)
    except ValueError:
      pass # leaves localhost value from above

  currSetts[webSettingsStanza][key] = "%s:%s" % (host, args["port"])
  comm.writeConfFile(fullWebConfig, currSetts)
  return retDict
Exemplo n.º 12
0
        def scan_file(dirpath, name):
            if name.endswith(MANIFEST_EXTENSION) and os.path.exists(os.path.join(dirpath, name[:-len(MANIFEST_EXTENSION)] + MASTER_EXTENSION)):
                cfg_pathname = os.path.join(dirpath, name)
                if cfg_pathname in _seen_filenames:
                    return
                _seen_filenames.add(cfg_pathname)

                config = comm.readConfFile(cfg_pathname)

                def lower_keys(x):
                    if isinstance(x, dict):
                        return dict((k.lower(), lower_keys(v)) for k, v in x.iteritems())
                    return x

                '''
                ConfigParser supports case-insensitive keys, since we've moved off it 
                (because it's not BOM-aware) we must also, but the section names
                (e.g. 'param:myParam') must preserve case
                '''
                for k in config.keys():
                    config[k] = lower_keys(config[k])
 


                # assert that conf file has base classes defined
                try:
                    className = config['module']['classname']
                    superClass = config['module']['superclass'] if config['module'].has_key('superclass') else None
                except KeyError:
                    logger.warn("Manifest file %s does not contain a valid module section" % cfg_pathname)
                    return

                if className == superClass:
                    raise ModuleMapperException('%s defines className == superClass !!' % cfg_pathname)


                # assemble the parameter configuration info

                params = {}
                stickyParams = []
                persistableParams = []
                for section_name in config.keys():
                    section = config[section_name]

                    if section_name.startswith('param:'):
                        pname = section_name[6:].strip()
                        if 'default' in section.keys() and 'required' in section.keys() and splunk.util.normalizeBoolean(section['required']):
                            raise ModuleMapperException(
                                'Cannot use required=True with a default value in Manifest file %s, parameter %s' 
                                % (cfg_pathname, pname))

                        params[pname] = {
                            'required': splunk.util.normalizeBoolean(section['required']) if section.has_key('required') else False,
                            'default': section['default'] if section.has_key('default') else None,
                            'values' : [val.strip() for val in section['values'].split(',')] if section.has_key('values') else None,
                            'label': section['label'] if section.has_key('label') else None,
                            'translate': section['translate'] if section.has_key('translate') else None
                        }

                        # add params to persistence lists
                        if section.has_key('sticky') and splunk.util.normalizeBoolean(section['sticky']):
                            stickyParams.append(pname)
                        if section.has_key('persistable') and splunk.util.normalizeBoolean(section['persistable']):
                            persistableParams.append(pname)
                        
                
                # check dupes
                
                if (className in _duplicateDefender):
                    logger.error("ASSERT - duplicate definition of %s in %s/%s" % (className, dirpath, name)) 
                    return
                _duplicateDefender.add(className)


                # enable support for including other module configuration
                
                if config['module'].has_key('include'):
                    include = [ mod_name.strip() for mod_name in config['module']['include'].split(',') ]
                else:
                    include = []

                if config['module'].has_key('description'):
                    description = config['module']['description']
                else:
                    description = None


                # assemble final module definition dict

                mod = {
                    'class': className,
                    'appName': '',
                    'superClass': superClass,
                    'path': dirpath,
                    'filePrefix': name[:-len(MANIFEST_EXTENSION)],
                    'params': params,
                    'stickyParams': stickyParams,
                    'persistableParams': persistableParams,
                    'include': include,
                    'description': description
                }
                moduleList.append(mod)
                moduleHash[className] = mod
Exemplo n.º 13
0
    def configmodel(self, **kwargs):
        method = cherrypy.request.method

        if method == 'GET':
            applist = next(os.walk(deploymentapps))[1]
            applist.remove('users')
            return json.dumps(applist)

        if method == 'POST':
            data = cherrypy.request.params
            logger.info('%s  %s' % (vmodule, json.dumps(data)))
            logger.info('%s  %s' % (vmodule, type(data['apps[]'])))
            if not data:
                logger.info('%s no data received' % vmodule)
                logger.info(json.dumps(data))
                return ''
            else:
                data = json.loads(data['apps[]'])

            applist = []
            confsettings = []
            conffiles = []
            mergedconfs = {}
            logger.info('%s app config requests: %s' % (vmodule, json.dumps(data)))
            if not isinstance(data, list):
                applist.append(data)
            else:
                applist = data
            #  Iterates through each app
            for app in applist:
                logger.info('%s retrieving configs for %s' % (vmodule, app))
                apppath = os.path.join(deploymentapps, app)
                mergedfiles = {}
                mergedconfs ={}
                default = 'default'
                local = 'local'
                appdefaultpath = os.path.join(apppath, default)
                applocalpath = os.path.join(apppath, local)
                defaultconffiles = conflist(appdefaultpath) if os.path.exists(appdefaultpath) else []
                localconffiles = conflist(applocalpath) if os.path.exists(applocalpath) else []
                conffiles = conffiles + defaultconffiles + localconffiles
                try:
                    conffiles = list(set(conffiles))
                except Exception as e:
                    logger.info("%s" % e )
                # Merges default and local for app
                for conffile in conffiles:
                    logger.info("%s merging %s" % (vmodule, conffile))
                    defaultfile = os.path.join(appdefaultpath, conffile)
                    localfile = os.path.join(applocalpath, conffile)
                    defaultconf = cli.readConfFile(defaultfile) if os.path.exists(defaultfile) else {}
                    localconf = cli.readConfFile(localfile) if os.path.exists(localfile) else {}
                    if not (default and local):
                        continue
                    if defaultconf:
                        for stanza, settings in defaultconf.items():
                            for key, value in settings.items():
                                defaultconf[stanza][key] = ["%s/%s" % (app, default), value]
                    if localconf:
                        for stanza, settings in localconf.items():
                            for key, value in settings.items():
                                settings[key] = ["%s/%s" % (app, local), value]
                            if stanza in defaultconf:
                                defaultconf[stanza].update(settings)
                            else:
                                defaultconf[stanza] = settings
                    mergedfiles[conffile] = defaultconf
                confsettings.append({app: mergedfiles})

            # Iterates through each configuration file found in all select apps.
            for conffile in conffiles:
                mergedconfs[conffile] = {}
                for app in confsettings:
                    for appname, conf in app.items():
                        if conffile in conf:
                            for stanza, settings in conf[conffile].items():
                                if stanza in mergedconfs[conffile]:
                                    mergedconfs[conffile][stanza].update(settings)
                                else:
                                    mergedconfs[conffile][stanza] = settings

            return json.dumps(mergedconfs)