Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FreeBSD platform #799

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions files/etc/init.d/elasticsearch.FreeBSD.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/bin/sh
#
# This file is managed via PUPPET
#
# PROVIDE: elasticsearch
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable elasticsearch:
#
# elasticsearch_enable="YES"
#
# elasticsearch_user (username): Set to elasticsearch by default.
# Set it to required username.
# elasticsearch_group (group): Set to elasticsearch by default.
# Set it to required group.
# elasticsearch_config (path): Set to /usr/local/etc/elasticsearch/elasticsearch.yml by default.
# Set it to the config file location.
# elasticsearch_min_mem (num): Minumum JVM heap size, 256m by default.
# elasticsearch_max_mem (num): Maximum JVM heap size, 1g by default.
# elasticsearch_props (args): Additional java properties or arguments.
# elasticsearch_tmp (path): Set to /var/tmp/elasticsearch by default.
# Set it to the path to be used for temp files.
#
. /etc/rc.subr

name=elasticsearch
rcvar=elasticsearch_enable

load_rc_config ${name}

: ${elasticsearch_enable:="NO"}
: ${elasticsearch_user:=elasticsearch}
: ${elasticsearch_group:=elasticsearch}
: ${elasticsearch_config:="/usr/local/etc/elasticsearch/<%= @resource[:instance] %>/elasticsearch.yml"}
: ${elasticsearch_min_mem:="256m"}
: ${elasticsearch_max_mem:="1g"}
: ${elasticsearch_props:=""}
: ${elasticsearch_tmp:="/var/tmp/elasticsearch"}

# Force the JVM to use IPv4 stack
# elasticshearch_props"-Djava.net.preferIPv4Stack=true"

required_files="${elasticsearch_config}"
pidfile="/var/run/${name}.pid"

ES_LIB="/usr/local/lib/elasticsearch"
ES_CLASSPATH=$ES_LIB/elasticsearch-1.7.5.jar:$ES_LIB/*:$ES_LIB/sigar/*

java_options=" -server \
-Xms${elasticsearch_min_mem} \
-Xmx${elasticsearch_max_mem} \
-Xss256k \
-Djava.awt.headless=true \
-XX:+UseParNewGC \
-XX:+UseConcMarkSweepGC \
-XX:CMSInitiatingOccupancyFraction=75 \
-XX:+UseCMSInitiatingOccupancyOnly \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:+DisableExplicitGC \
-Delasticsearch \
-Des.config=${elasticsearch_config} \
-cp ${ES_CLASSPATH}"

extra_commands="console status"
console_cmd="elasticsearch_console"
start_precmd="elasticsearch_precmd"
status_cmd="elasticsearch_status"
stop_cmd="elasticsearch_stop"
command="/usr/sbin/daemon"
command_args="-f /usr/local/bin/java -Des.pidfile=${pidfile} ${elasticsearch_props} ${java_options} org.elasticsearch.bootstrap.Elasticsearch"

elasticsearch_precmd()
{
rc_pid=$(elasticsearch_check_pidfile $pidfile)

if [ -n "$rc_pid" ]; then
[ -n "$rc_fast" ] && return 0
echo "${name} is already running: $rc_pid"
return 1
fi
touch ${pidfile}
chown ${elasticsearch_user}:${elasticsearch_group} ${pidfile}
/usr/bin/install -d -o ${elasticsearch_user} -g ${elasticsearch_group} -m 750 ${elasticsearch_tmp}
/usr/bin/install -d -o ${elasticsearch_user} -g ${elasticsearch_group} -m 750 /var/db/elasticsearch
/usr/bin/install -d -o ${elasticsearch_user} -g ${elasticsearch_group} -m 750 /var/log/elasticsearch
}

elasticsearch_console()
{
rc_pid=$(elasticsearch_check_pidfile $pidfile)

if [ -n "$rc_pid" ]; then
[ -n "$rc_fast" ] && return 0
echo "${name} is already running: $rc_pid"
return 1
fi
/usr/local/bin/java -Des.foreground=yes ${elasticsearch_props} ${java_options} org.elasticsearch.bootstrap.Elasticsearch
}


elasticsearch_stop()
{
rc_pid=$(elasticsearch_check_pidfile $pidfile)

if [ -z "$rc_pid" ]; then
[ -n "$rc_fast" ] && return 0
echo "${name} not running? (check $pidfile)."
return 1
fi

echo "Stopping ${name}."
kill ${rc_pid} 2> /dev/null
}

elasticsearch_status()
{
rc_pid=$(elasticsearch_check_pidfile $pidfile)

if [ -z "$rc_pid" ]; then
[ -n "$rc_fast" ] && return 0
echo "${name} not running? (check $pidfile)."
return 1
fi
echo "${name} is running as pid ${rc_pid}."
}

elasticsearch_check_pidfile()
{
_pidfile=$1
if [ -z "$_pidfile" ]; then
err 3 'USAGE: elasticsearch_check_pidfile pidfile'
fi
if [ ! -f $_pidfile ]; then
debug "pid file ($_pidfile): not readable."
return
fi
read _pid _junk < $_pidfile
if [ -z "$_pid" ]; then
debug "pid file ($_pidfile): no pid in file."
return
fi
if [ -n "`/usr/bin/su -m ${elasticsearch_user} -c '/usr/local/bin/jps -l' | grep -e "^$_pid"`" ]; then
echo -n $_pid
fi
}

run_rc_command "$1"
29 changes: 28 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
$elasticsearch_user = '_elasticsearch'
$elasticsearch_group = '_elasticsearch'
}
'FreeBSD': {
$elasticsearch_user = 'elasticsearch'
$elasticsearch_group = 'elasticsearch'
}
default: {
fail("\"${module_name}\" provides no user/group default value
for \"${::kernel}\"")
Expand All @@ -102,6 +106,9 @@
'OpenBSD': {
$download_tool = 'ftp -o'
}
'FreeBSD': {
$download_tool = 'fetch -o'
}
default: {
fail("\"${module_name}\" provides no download tool default value
for \"${::kernel}\"")
Expand All @@ -128,6 +135,15 @@
$plugindir = "${homedir}/plugins"
$datadir = '/var/elasticsearch/data'
}
'FreeBSD': {
$configdir = '/usr/local/etc/elasticsearch'
$logdir = '/var/log/elasticsearch'
$package_dir = '/var/cache/elasticsearch'
$installpath = undef
$homedir = '/usr/local/lib/elasticsearch'
$plugindir = "${homedir}/plugins"
$datadir = '/var/db/elasticsearch'
}
default: {
fail("\"${module_name}\" provides no config directory default value
for \"${::kernel}\"")
Expand All @@ -137,7 +153,7 @@
# packages
case $::operatingsystem {
'RedHat', 'CentOS', 'Fedora', 'Scientific', 'Amazon', 'OracleLinux', 'SLC',
'Debian', 'Ubuntu', 'OpenSuSE', 'SLES', 'OpenBSD': {
'Debian', 'Ubuntu', 'OpenSuSE', 'SLES', 'OpenBSD', 'FreeBSD': {
$package = 'elasticsearch'
}
'Gentoo': {
Expand Down Expand Up @@ -284,6 +300,17 @@
$init_template = 'elasticsearch.OpenBSD.erb'
$pid_dir = '/var/run/elasticsearch'
}
'FreeBSD': {
$service_name = 'elasticsearch'
$service_hasrestart = true
$service_hasstatus = true
$service_pattern = undef
$service_providers = 'freebsd'
$systemd_service_path = undef
$defaults_location = undef
$init_template = 'elasticsearch.FreeBSD.erb'
$pid_dir = '/var/run/elasticsearch'
}
default: {
fail("\"${module_name}\" provides no service parameters
for \"${::operatingsystem}\"")
Expand Down
8 changes: 8 additions & 0 deletions manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
service_flags => $service_flags,
}
}
'freebsd': {
elasticsearch::service::freebsd { $name:
ensure => $ensure,
status => $status,
init_template => $init_template,
service_flags => $service_flags,
}
}
'systemd': {
elasticsearch::service::systemd { $name:
ensure => $ensure,
Expand Down
162 changes: 162 additions & 0 deletions manifests/service/freebsd.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# == Define: elasticsearch::service::freebsd
#
# This class exists to coordinate all service management related actions,
# functionality and logical units in a central place.
#
# <b>Note:</b> "service" is the Puppet term and type for background processes
# in general and is used in a platform-independent way. E.g. "service" means
# "daemon" in relation to Unix-like systems.
#
#
# === Parameters
#
# [*ensure*]
# String. Controls if the managed resources shall be <tt>present</tt> or
# <tt>absent</tt>. If set to <tt>absent</tt>:
# * The managed software packages are being uninstalled.
# * Any traces of the packages will be purged as good as possible. This may
# include existing configuration files. The exact behavior is provider
# dependent. Q.v.:
# * Puppet type reference: {package, "purgeable"}[http://j.mp/xbxmNP]
# * {Puppet's package provider source code}[http://j.mp/wtVCaL]
# * System modifications (if any) will be reverted as good as possible
# (e.g. removal of created users, services, changed log settings, ...).
# * This is thus destructive and should be used with care.
# Defaults to <tt>present</tt>.
#
# [*status*]
# String to define the status of the service. Possible values:
# * <tt>enabled</tt>: Service is running and will be started at boot time.
# * <tt>disabled</tt>: Service is stopped and will not be started at boot
# time.
# * <tt>running</tt>: Service is running but will not be started at boot time.
# You can use this to start a service on the first Puppet run instead of
# the system startup.
# * <tt>unmanaged</tt>: Service will not be started at boot time and Puppet
# does not care whether the service is running or not. For example, this may
# be useful if a cluster management software is used to decide when to start
# the service plus assuring it is running on the desired node.
# Defaults to <tt>enabled</tt>. The singular form ("service") is used for the
# sake of convenience. Of course, the defined status affects all services if
# more than one is managed (see <tt>service.pp</tt> to check if this is the
# case).
#
# [*pid_dir*]
# String, directory where to store the serice pid file
#
# [*init_template*]
# Service file as a template
#
# [*service_flags*]
# String, flags to pass to the service
#
# === Authors
#
# * Richard Pijnenburg <mailto:[email protected]>
#
define elasticsearch::service::freebsd(
$ensure = $elasticsearch::ensure,
$status = $elasticsearch::status,
$pid_dir = $elasticsearch::pid_dir,
$init_template = $elasticsearch::init_template,
$service_flags = undef,
) {

#### Service management

# set params: in operation
if $ensure == 'present' {

case $status {
# make sure service is currently running, start it on boot
'enabled': {
$service_ensure = 'running'
$service_enable = true
}
# make sure service is currently stopped, do not start it on boot
'disabled': {
$service_ensure = 'stopped'
$service_enable = false
}
# make sure service is currently running, do not start it on boot
'running': {
$service_ensure = 'running'
$service_enable = false
}
# do not start service on boot, do not care whether currently running
# or not
'unmanaged': {
$service_ensure = undef
$service_enable = false
}
# unknown status
# note: don't forget to update the parameter check in init.pp if you
# add a new or change an existing status.
default: {
fail("\"${status}\" is an unknown service status value")
}
}

# set params: removal
} else {

# make sure the service is stopped and disabled (the removal itself will be
# done by package.pp)
$service_ensure = 'stopped'
$service_enable = false

}

$notify_service = $elasticsearch::restart_config_change ? {
true => Service["elasticsearch-instance-${name}"],
false => undef,
}

if ( $status != 'unmanaged' and $ensure == 'present' ) {

# init file from template
if ($init_template != undef) {

elasticsearch_service_file { "/usr/local/etc/rc.d/elasticsearch_${name}":
ensure => $ensure,
content => file($init_template),
instance => $name,
notify => $notify_service,
package_name => $elasticsearch::package_name,
}
-> file { "/usr/local/etc/rc.d/elasticsearch_${name}":
ensure => $ensure,
owner => 'root',
group => '0',
mode => '0555',
before => Service["elasticsearch-instance-${name}"],
notify => $notify_service,
}

}

} elsif ($status != 'unmanaged') {

file { "/usr/local/etc/rc.d/elasticsearch_${name}":
ensure => 'absent',
subscribe => Service["elasticsearch-instance-${name}"],
}

}

if ( $status != 'unmanaged') {

# action
service { "elasticsearch-instance-${name}":
ensure => $service_ensure,
enable => $service_enable,
name => "elasticsearch_${name}",
flags => $service_flags,
hasstatus => $elasticsearch::params::service_hasstatus,
hasrestart => $elasticsearch::params::service_hasrestart,
pattern => $elasticsearch::params::service_pattern,
}

}

}