btmash

Solr-Admin Script

May 5th, 2011
967
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # This file mimics creating / updating / unloading / deleting solr cores
  4.  
  5. # Create a new core
  6. # arg 1: core name
  7. create_solr_core() {
  8.     # creates a new Solr core
  9.     if [ "$1" = "" ]; then
  10.         echo -n "Name of core to create: "
  11.         read name
  12.     else
  13.         name=$1
  14.     fi
  15.  
  16.     if [ -d "/var/lib/solr/data/$name" ]; then
  17.         echo "Cannot create $name: core with same name already exists"
  18.         exit 1
  19.     else
  20.         mkdir /var/lib/solr/data/$name
  21.         chown tomcat6.tomcat6 /var/lib/solr/data/$name
  22.  
  23.         mkdir -p /etc/solr/conf/$name/conf
  24.         cp -a /etc/solr/conftemplate/* /etc/solr/conf/$name/conf/
  25.         # Create multiple instances so that we can use this with the apachesolr module
  26.         # and the search_api module.  Can add more in the future.
  27.         find /etc/solr/conf/$name/conf -name "solrconfig.xml*" -exec sed -i "s/CORENAME/$name/" {} \;
  28.         # Use the second argument to automatically specify which solrconfig.xml file to use
  29.         if [ "$2" = "" ]; then
  30.             echo -n "Original configuration will be used"
  31.         else
  32.             config=$2
  33.             schema_filename="/etc/solr/conf/$name/conf/schema.xml.$config"
  34.             config_filename="/etc/solr/conf/$name/conf/solrconfig.xml.$config"
  35.             if [ -e "$config_filename" ] && [ -e "$schema_filename" ]; then
  36.                 cp -f $config_filename /etc/solr/conf/$name/conf/solrconfig.xml
  37.                 cp -f $schema_filename /etc/solr/conf/$name/conf/schema.xml
  38.                 echo "USING new config $config_filename"
  39.             else
  40.                 echo "config $config_filename could not be found. Using original configuration. Please update solrconfig.xml and/or schema.xml and reload the using solr-admin.sh reload $name"
  41.             fi
  42.         fi
  43.  
  44.         curl "http://localhost:8080/solr/admin/cores?action=CREATE&name=$name&instanceDir=/etc/solr/conf/$name"
  45.         echo "Please read status from solr - by all accounts (pending a proper name), core $name was created"
  46.     fi
  47.     exit 0
  48. }
  49.  
  50. # Reload existing core
  51. # arg 1: core name
  52. reload_solr_core() {
  53.     # reloads a Solr core
  54.     if [ "$1" = "" ]; then
  55.         echo -n "Name of core to reload: "
  56.         read name
  57.     else
  58.         name=$1
  59.     fi
  60.  
  61.     if [ ! -d /var/lib/solr/data/$name ] || [ $name = "" ]; then
  62.         echo "Core doesn't exist"
  63.         exit
  64.     fi
  65.  
  66.     curl "http://localhost:8080/solr/admin/cores?action=RELOAD&core=$name"
  67.     echo "Core $name has been reloaded"
  68. }
  69.  
  70. # Update existing core
  71. # arg 1: core name
  72. unload_solr_core() {
  73.     if [ "$1" = "" ]; then
  74.         echo -n "Name of core to remove: "
  75.         read name
  76.     else
  77.         name=$1
  78.     fi
  79.  
  80.     if [ -d "/var/lib/solr/data/$name" ]; then
  81.         curl "http://localhost:8080/solr/admin/cores?action=UNLOAD&core=$name"
  82.         echo "Core $name has been unloaded"
  83.     else
  84.         echo "Core $name does not exist"
  85.     fi
  86. }
  87.  
  88. # Remove existing core
  89. # arg 1: core name
  90. remove_solr_core() {
  91.     if [ "$1" = "" ]; then
  92.         echo -n "Name of core to remove: "
  93.         read name
  94.     else
  95.         name=$1
  96.     fi
  97.  
  98.     if [ -d "/var/lib/solr/data/$name" ]; then
  99.         unload_solr_core $name
  100.         rm -rf /var/lib/solr/data/$name
  101.         rm -rf /etc/solr/conf/$name
  102.         echo "Deleted configuration settings for $name"
  103.     else
  104.         echo "Core $name does not exist"
  105.     fi
  106.     exit 0
  107. }
  108.  
  109. case "$1" in
  110.     create)
  111.         create_solr_core $2 $3
  112.         ;;
  113.     reload)
  114.         reload_solr_core $2
  115.         ;;
  116.     unload)
  117.         unload_solr_core $2
  118.         ;;
  119.     remove)
  120.         remove_solr_core $2
  121.         ;;
  122. esac
  123.  
  124. exit 0
RAW Paste Data