Debian

如何通過 Puppet 在 Tomcat 上安裝 Solr 4.x?

  • May 28, 2015

到目前為止,我一直在使用以下方法在 Tomcat 6 上安裝 Solr 3.x:

package { 'openjdk-6-jdk' :
 ensure => installed,
}
package { 'solr-tomcat' :
 ensure => installed,
}

exec { 'tomcat-port':
 command => "sed -i.bak 's|port=\"8080\"|port=\"8983\"|g' /etc/tomcat6/server.xml",
 path => ["/bin", "/usr/bin"],
}

service { 'tomcat6' :
 ensure => running,
 require => Package['solr-tomcat'],
}

但是我想在 Tomcat 7 上安裝 Solr 4.x,但似乎 Debian 沒有適用於 Solr 的軟體包(例如solr-tomcat)。

到目前為止,我的 Tomcat 7 通過以下方式工作:

package { [ 'tomcat7', 'tomcat7-admin' ]: ensure => installed, }
service { 'tomcat7' : ensure => running, }
exec { 'tomcat-port':
 command => "sed -i.bak 's|port=\"8080\"|port=\"8983\"|g' /etc/tomcat7/server.xml",
 path => ["/bin", "/usr/bin"],
}

但是如何安裝 Solr 4.x?

最簡單的方法是定義exec,例如:

exec { "solr-download":
 command => "/usr/bin/wget http://archive.apache.org/dist/lucene/solr/4.9.1/solr-4.9.1.tgz && /bin/tar -xf solr-4.9.1.tgz"
 cwd => "/opt",
 creates => "/opt/solr-4.9.1.tgz",
}

對於更靈活的解決方案,您必須編寫一個類(solr4-tomcat.pp),例如:

class solr {

 # variables
 $version = '4.10.4'
 $data_dir = "/opt/solr/data"
 $home_dir = "/opt/solr/home"

 $tomcat_version = "tomcat7"
 $tomcat_base = "/var/lib/${tomcat_version}"
 $tomcat_user = $operatingsystem ? {
   /RedHat|CentOS/ => "tomcat", 
   /Debian|Ubuntu/ => $tomcat_version,
 }

 # includes
 include solr::files

 class { 'tomcat': install_from_source => false }

 # Ensure Java libraries are installed
 package { "java":
   ensure => present,
   name => $operatingsystem ? {
     'Centos' => $operatingsystemrelease ? {
       '6.0' => "java-1.6.0-openjdk.$hardwaremodel",
        '*' => 'openjdk-7-jre',
     },
     /Debian|Ubuntu/ => 'openjdk-7-jre-headless',
   },
 }

 # Set port 8983 for Tomcat 7 and make sure it's running.
 tomcat::instance {'default': package_name => $tomcat_version} ->
   tomcat::service  {'default':
     service_name => 'tomcat7',
     service_ensure => 'running',
     use_jsvc => false, use_init => true,
# use_jsvc => true, java_home => "/usr/lib/jvm/java-7-openjdk-amd64",
   }->
   tomcat::config::server { $tomcat_version:
     catalina_base => $tomcat_base,
     port          => 8983,
   }

 class solr::files {

   # Ensure solr directories are present.
   file { 'home_dir':
     path => $solr::home_dir,
     ensure => directory,
     recurse => true,
   }
   file { 'data_dir':
     path => $solr::data_dir,
     ensure => directory,
     recurse => true,
     owner => $solr::tomcat_user,
     group => $solr::tomcat_user,
   }

   # Download and extract solr
   exec { "solr-extract":
     path    => ['/usr/bin', '/usr/sbin', '/bin'],
     command => "curl -s http://archive.apache.org/dist/lucene/solr/${solr::version}/solr-${solr::version}.tgz | tar zxf -",
     cwd => "/opt/solr",
     require => [File["data_dir"], File["home_dir"]],
     creates => "/opt/solr/solr-${solr::version}/dist/solr-${solr::version}.war",
   }
   # Logging setup for Tomcat.
   exec { 'solr-install-logging-jars':
     path      => ['/usr/bin', '/usr/sbin', '/bin'],
     cwd       => "/opt/solr",
     command   => "cp -v /opt/solr/solr-${solr::version}/example/lib/ext/*.jar /opt/solr/solr-${solr::version}/dist/solrj-lib/*.jar /opt/solr/solr-${solr::version}/example/resources/log4j.properties ${solr::tomcat_base}/webapps/solr/WEB-INF/lib",
     onlyif    => "test ! -f ${solr::tomcat_base}/webapps/solr/WEB-INF/lib/log4j-1.2.17.jar",
     require   => Exec['solr-extract'],
   }
 }

 tomcat::war { 'solr.war':
   catalina_base => $tomcat_base,
   war_source => "/opt/solr/solr-${solr::version}/dist/solr-${solr::version}.war",
   require => Class["solr::files"],
   notify => Service[$tomcat_version],
 }

}

檢查替代清單:

您可以創建自己的 Debian 軟體包並將其託管在您自己的儲存庫中。像 FPM ( https://github.com/jordansissel/fpm ) 這樣的項目在這裡有很大幫助。另一種選擇是從項目網站檢索 Solr tarball 並管理一系列exec語句以將其解壓縮到 Tomcat 安裝中的 webapps 目錄中。

引用自:https://serverfault.com/questions/679116