Bulk create opendj indexes

This is a little script I developed to bulk add indexes to an OpenDJ directory.


The input to this script would be a comma separated file that looks like this:

companyname,eq pres sub
departmentnumber,eq pres sub
description,eq pres sub
displayname,eq pres sub

I’ve got another script that will generate a file like the above from the output of the DSEE (Oracle Directory Server Enterprise Edition) “dsconf list-indexes” command.

Now for the script proper:

#!/usr/bin/perl
# setindexesodj.pl Create command file to set up OpenDJ indexes.
# properties (which later get made comma separated for parsing).
# Exclude elements you don't want.
use strict;
use Custom::Text::ParseWords;

my $HOME = $ENV{'HOME'};
my $DSHOME = "/opt/opendj/ds-user1";
my $USRHOME = "/home/opendj";
my $PORT = "5444";

my $infile = "$HOME/data/user-indexes.csv";
my $outfile = "$HOME/data/setindexes.sh";

# Array of attributes you don't want to create indexes for (usually these
# are attributes that are already indexed).
my @existing = qw(objectclass uniqueMember givenName sn cn aci entryUUID
		  telephoneNumber ds-sync-conflict ds-sync-hist uid);
my $existing;
foreach my $attr(@existing) {
	$existing = $existing . "|" . $attr;
}
for($existing) { s/^|//; s/|$//; }

open FH, "$outfile" or die $!;

print FH1 "#!/bin/bashn";
print FH1 "# Commands to create prod indexesn";
print FH1 "n";
print FH1 "DSHOME=$DSHOMEn";
print FH1 "HOME=$USRHOMEn";
print FH1 "n";

while() {
     chomp;

 my ($indexname,
     $indexprops

 ) = ( &parse_line(',',0,$_));

 if($indexname !~ /$existing/i) {

   print $indexname;
   print FH1 "$DSHOME/bin/dsconfig create-local-db-index \n";
   print FH1 "--backend-name userRoot \n";
   print FH1 "--index-name $indexname \n";

   if($indexprops =~ /sub/g) {
      print FH1 "--set index-type:substring \n";
   }
   else {
        print FH1 "--set index-type:equality \n";
   }
   print FH1 "-p $PORT -j $HOME/etc/pwd.txt \n";
   print FH1 "-X -n n";
   print FH1 "n";
   print "n";
 }
}
close FH1;
close FH;

__END__;

Note the @existing array of attributes to be excluded. You could accomplish the same thing by editing the input file, but for me it was easier to just list them there.

While executing this as an unprivileged user (my “opendj” system account) with sudo rights, I found it necessary to define the directory and user home paths in the script in order to not lose that important environment information. That’s why those variables are explicitly defined at the top of the script.

This is what the output would look:

#!/bin/bash
# Commands to create prod indexes
DSHOME=/opt/opendj/ds-user1
HOME=/home/opendj

$DSHOME/bin/dsconfig create-local-db-index 
--backend-name userRoot 
--index-name companyname 
--set index-type:substring 
-p 5444 -j $HOME/etc/pwd.txt 
-X -n 

$DSHOME/bin/dsconfig create-local-db-index 
--backend-name userRoot 
--index-name departmentnumber 
--set index-type:equality 
-p 5444 -j $HOME/etc/pwd.txt 
-X -n 

Finally, be sure to run the “rebuild-index –rebuildDegraded” procedure:

$DSHOME/bin/rebuild-index 
--rebuildDegraded 
-b dc=example,dc=com 
-p 5444 -X 
-j /home/opendj/etc/pwd.txt 
-t 0

To verify all is well, use the dbtest utility:

sudo $DSHOME/bin/dbtest list-index-status 
-n userRoot 
-b dc=example,dc=com

Note per attribute index entry limits are not set in this example. The assumption is that the default limit will be set absurdly high because the current version of OpenDJ will behave badly if you try setting to unlimited (“0”). Believe me, you don’t want to go there.