Exemplo n.º 1
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.º 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 createApp(appName, template, **kwargs):
    appPath = _getAppPath(appName)
    if os.path.exists(appPath):
        raise admin.AlreadyExistsException(
            _("App '%s' already exists. Nothing was created.") % appName)

    if template not in getTemplates():
        raise admin.ArgValidationException(
            _("Template '%s' does not exist.") % template)

    # Make sure we don't mess the app.conf file - add a backslash at the eol
    kwargs['description'] = kwargs['description'].replace('\n', '\\\n')

    # Generate files for the app
    bundle_paths.maybe_makedirs(appPath)
    os.chdir(appPath)

    templatePath = os.path.join(TEMPLATES_PATH, template)

    for root, dirs, files in os.walk(templatePath):
        # determine relative path
        relPath = root[len(templatePath) + 1:]

        # create subdirs
        for dir in dirs:
            bundle_paths.maybe_makedirs(os.path.join(relPath, dir))

        # Read template files and apply param values then save
        for fn in files:
            try:
                # use params to create custom file names
                inFilePath = os.path.join(root, fn)
                # filter by file type
                fn, isText = _isTextFile(fn)
                outFilePath = os.path.join(appPath, relPath, fn)
                if not isText:
                    comm.copyItem(inFilePath, outFilePath)
                    continue

                with open(inFilePath, 'r') as f_in:
                    content = f_in.read()
                    content = SafeTemplate(content).substitute(kwargs)
                    with open(outFilePath, 'w') as f_out:
                        f_out.write(content)

            except:
                print traceback.print_exc(file=sys.stderr)
                pass

    return '%s/app/%s' % (_getSplunkWebUri(), appName)
def createApp(appName, template, **kwargs):
    appPath = _getAppPath(appName)
    if os.path.exists(appPath):
        raise admin.AlreadyExistsException(_("App '%s' already exists. Nothing was created.") % appName)
    
    if template not in getTemplates():
        raise admin.ArgValidationException(_("Template '%s' does not exist.") % template)

    # Make sure we don't mess the app.conf file - add a backslash at the eol
    kwargs['description'] = kwargs['description'].replace('\n', '\\\n')        
        
    # Generate files for the app 			
    bundle_paths.maybe_makedirs(appPath)
    os.chdir(appPath)	
    
    templatePath = os.path.join(TEMPLATES_PATH, template)
    
    for root, dirs, files in os.walk(templatePath):
        # determine relative path  
        relPath = root[len(templatePath)+1:]

        # create subdirs
        for dir in dirs:
            bundle_paths.maybe_makedirs(os.path.join(relPath,dir))

        # Read template files and apply param values then save
        for fn in files:
            try:
                # use params to create custom file names
                inFilePath = os.path.join(root, fn)
                # filter by file type
                fn, isText = _isTextFile(fn)
                outFilePath = os.path.join(appPath, relPath, fn)
                if not isText:
                    comm.copyItem(inFilePath, outFilePath)
                    continue

                with open(inFilePath, 'r') as f_in:
                    content = f_in.read()
                    content = SafeTemplate(content).substitute(kwargs)
                    with open(outFilePath, 'w') as f_out:
                        f_out.write(content)
                        
            except:
                print traceback.print_exc(file=sys.stderr)
                pass

    return '%s/app/%s' % (_getSplunkWebUri(), appName)
Exemplo n.º 5
0
def firstTimeRun(args, fromCLI):
    """
  All of our first time run checks that used to happen in the former bin/splunk shell script.
  Does any number of things, such as config migration, directory validation, and so on.  For
  the most up to date info, read the code.  It tends to be fairly well documented.
  """

    paramReq = (
        ARG_DRYRUN,
        ARG_FRESHINST,
    )
    paramOpt = (ARG_LOGFILE, )
    comm.validateArgs(paramReq, paramOpt, args)

    isFirstInstall = comm.getBoolValue(ARG_FRESHINST, args[ARG_FRESHINST])
    isDryRun = comm.getBoolValue(ARG_DRYRUN, args[ARG_DRYRUN])
    retDict = {}

    # ...arg parsing done now.

    # NOTE:
    # none of the changes that are made in this function are subjected to isDryRun.
    # these things just have to be done - they're not considered to be migration.

    ##### if user doesn't have a ldap.conf, put our default in its place.
    if not os.path.exists(migration.PATH_LDAP_CONF):
        comm.copyItem(migration.PATH_LDAP_CONF_DEF, migration.PATH_LDAP_CONF)

    if not os.path.exists(PATH_AUDIT_PRIV_KEY) and not os.path.exists(
            PATH_AUDIT_PUB_KEY):
        kCmd = ["splunk", "createssl", "audit-keys"]
        kPriv, kPub, kDir = PATH_AUDIT_PRIV_KEY, PATH_AUDIT_PUB_KEY, PATH_AUDIT_KEY_DIR
        retCode = comm.runAndLog(kCmd + ["-p", kPriv, "-k", kPub, "-d", kDir])
        if 0 != retCode:
            raise cex.FilePath, "Could not create audit keys (returned %d)." % retCode

    try:
        keyScript = comm.getConfKeyValue("distsearch", "tokenExchKeys",
                                         "genKeyScript")
        keyCmdList = [
            os.path.expandvars(x.strip()) for x in keyScript.split(",")
            if len(x) > 0
        ]  # a,b,,d -> [a,b,d]
        pubFilename = comm.getConfKeyValue("distsearch", "tokenExchKeys",
                                           "publicKey")
        privateFilename = comm.getConfKeyValue("distsearch", "tokenExchKeys",
                                               "privateKey")
        certDir = comm.getConfKeyValue("distsearch", "tokenExchKeys",
                                       "certDir")
        certDir = os.path.expandvars(certDir)
        privateFilename = os.path.join(certDir, privateFilename)
        pubFilename = os.path.join(certDir, pubFilename)
        if not (os.path.exists(os.path.join(certDir, privateFilename))
                or os.path.exists(os.path.join(certDir, pubFilename))):
            cmdList = keyCmdList + [
                "-p", privateFilename, "-k", pubFilename, "-d", certDir
            ]
            success = comm.runAndLog(cmdList) == 0
            if not success:
                logger.warn("Unable to generate distributed search keys.")
                #TK mgn 06/19/09
                raise cex.FilePath, "Unable to generate distributed search keys."  #TK mgn 06/19/09
    except:
        logger.warn("Unable to generate distributed search keys.")
        #TK mgn 06/19/09
        raise

    if isFirstInstall:
        ##### if user doesn't have a ui modules dir, put our default in its place. only run this in this block - otherwise,
        #     in an upgrade, we run the same code during migration and show an incorrect warning ("oh noes dir is missing").
        if not os.path.exists(migration.PATH_UI_MOD_ACTIVE):
            comm.moveItem(migration.PATH_UI_MOD_NEW,
                          migration.PATH_UI_MOD_ACTIVE)
    ##### we're in an upgrade situation.
    else:
        ##### now do the actual migration (or fake it, if the user wants).
        #     upon faking, this function will throw an exception.
        if not ARG_LOGFILE in args:
            raise cex.ArgError, "Cannot migrate without the '%s' parameter." % ARG_LOGFILE
        migration.autoMigrate(args[ARG_LOGFILE], isDryRun)

    ##### FTR succeeded.  johnvey's never gonna have eggs. T_T

    # --- done w/ FTR, now i can has bucket?? ---
    return retDict
Exemplo n.º 6
0
def firstTimeRun(args, fromCLI):
  """
  All of our first time run checks that used to happen in the former bin/splunk shell script.
  Does any number of things, such as config migration, directory validation, and so on.  For
  the most up to date info, read the code.  It tends to be fairly well documented.
  """

  paramReq = (ARG_DRYRUN, ARG_FRESHINST,)
  paramOpt = (ARG_LOGFILE,)
  comm.validateArgs(paramReq, paramOpt, args)

  isFirstInstall = comm.getBoolValue(ARG_FRESHINST, args[ARG_FRESHINST])
  isDryRun = comm.getBoolValue(ARG_DRYRUN, args[ARG_DRYRUN])
  retDict  = {}

  # ...arg parsing done now.

  # NOTE:
  # none of the changes that are made in this function are subjected to isDryRun.
  # these things just have to be done - they're not considered to be migration.

  ##### if user doesn't have a ldap.conf, put our default in its place.
  if not os.path.exists(migration.PATH_LDAP_CONF):
    comm.copyItem(migration.PATH_LDAP_CONF_DEF, migration.PATH_LDAP_CONF)

  if not os.path.exists(PATH_AUDIT_PRIV_KEY) and not os.path.exists(PATH_AUDIT_PUB_KEY):
    kCmd = ["splunk", "createssl", "audit-keys"]
    kPriv, kPub, kDir = PATH_AUDIT_PRIV_KEY, PATH_AUDIT_PUB_KEY, PATH_AUDIT_KEY_DIR
    retCode = comm.runAndLog(kCmd + ["-p", kPriv, "-k", kPub, "-d", kDir])
    if 0 != retCode:
      raise cex.FilePath, "Could not create audit keys (returned %d)." % retCode

  try:    
    keyScript = comm.getConfKeyValue("distsearch", "tokenExchKeys", "genKeyScript" );
    keyCmdList = [os.path.expandvars(x.strip()) for x in keyScript.split(",") if len(x) > 0] # a,b,,d -> [a,b,d]
    pubFilename = comm.getConfKeyValue("distsearch", "tokenExchKeys", "publicKey" );
    privateFilename = comm.getConfKeyValue("distsearch", "tokenExchKeys", "privateKey" );
    certDir = comm.getConfKeyValue("distsearch", "tokenExchKeys", "certDir" )
    certDir = os.path.expandvars( certDir )
    privateFilename = os.path.join( certDir,privateFilename )
    pubFilename = os.path.join( certDir, pubFilename )
    if not ( os.path.exists( os.path.join( certDir,privateFilename ) ) or os.path.exists( os.path.join( certDir, pubFilename ) ) ):
      cmdList = keyCmdList + [ "-p", privateFilename, "-k", pubFilename,"-d", certDir ]
      success = comm.runAndLog( cmdList ) == 0
      if not success:
        logger.warn("Unable to generate distributed search keys."); #TK mgn 06/19/09
        raise cex.FilePath, "Unable to generate distributed search keys." #TK mgn 06/19/09
  except:
    logger.warn("Unable to generate distributed search keys."); #TK mgn 06/19/09
    raise 

  if isFirstInstall:
    ##### if user doesn't have a ui modules dir, put our default in its place. only run this in this block - otherwise,
    #     in an upgrade, we run the same code during migration and show an incorrect warning ("oh noes dir is missing").
    if not os.path.exists(migration.PATH_UI_MOD_ACTIVE):
      comm.moveItem(migration.PATH_UI_MOD_NEW, migration.PATH_UI_MOD_ACTIVE)
  ##### we're in an upgrade situation.
  else:
    ##### now do the actual migration (or fake it, if the user wants).
    #     upon faking, this function will throw an exception.
    if not ARG_LOGFILE in args:
      raise cex.ArgError, "Cannot migrate without the '%s' parameter." % ARG_LOGFILE
    migration.autoMigrate(args[ARG_LOGFILE], isDryRun)


  ##### FTR succeeded.  johnvey's never gonna have eggs. T_T

  # --- done w/ FTR, now i can has bucket?? ---
  return retDict