dev/provisioning/modules/elasticsearch/manifests/instance.pp
changeset 28 b0b56e0f8c7f
child 406 cf0f23803a53
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 # == Define: elasticsearch::instance
       
     2 #
       
     3 #  This define allows you to create or remove an elasticsearch instance
       
     4 #
       
     5 # === Parameters
       
     6 #
       
     7 # [*ensure*]
       
     8 #   String. Controls if the managed resources shall be <tt>present</tt> or
       
     9 #   <tt>absent</tt>. If set to <tt>absent</tt>:
       
    10 #   * The managed software packages are being uninstalled.
       
    11 #   * Any traces of the packages will be purged as good as possible. This may
       
    12 #     include existing configuration files. The exact behavior is provider
       
    13 #     dependent. Q.v.:
       
    14 #     * Puppet type reference: {package, "purgeable"}[http://j.mp/xbxmNP]
       
    15 #     * {Puppet's package provider source code}[http://j.mp/wtVCaL]
       
    16 #   * System modifications (if any) will be reverted as good as possible
       
    17 #     (e.g. removal of created users, services, changed log settings, ...).
       
    18 #   * This is thus destructive and should be used with care.
       
    19 #   Defaults to <tt>present</tt>.
       
    20 #
       
    21 # [*status*]
       
    22 #   String to define the status of the service. Possible values:
       
    23 #   * <tt>enabled</tt>: Service is running and will be started at boot time.
       
    24 #   * <tt>disabled</tt>: Service is stopped and will not be started at boot
       
    25 #     time.
       
    26 #   * <tt>running</tt>: Service is running but will not be started at boot time.
       
    27 #     You can use this to start a service on the first Puppet run instead of
       
    28 #     the system startup.
       
    29 #   * <tt>unmanaged</tt>: Service will not be started at boot time and Puppet
       
    30 #     does not care whether the service is running or not. For example, this may
       
    31 #     be useful if a cluster management software is used to decide when to start
       
    32 #     the service plus assuring it is running on the desired node.
       
    33 #   Defaults to <tt>enabled</tt>. The singular form ("service") is used for the
       
    34 #   sake of convenience. Of course, the defined status affects all services if
       
    35 #   more than one is managed (see <tt>service.pp</tt> to check if this is the
       
    36 #   case).
       
    37 #
       
    38 # [*config*]
       
    39 #   Elasticsearch configuration hash
       
    40 #
       
    41 # [*configdir*]
       
    42 #   Path to directory containing the elasticsearch configuration.
       
    43 #   Use this setting if your packages deviate from the norm (/etc/elasticsearch)
       
    44 #
       
    45 # [*datadir*]
       
    46 #   Allows you to set the data directory of Elasticsearch
       
    47 #
       
    48 # [*logging_file*]
       
    49 #   Instead of a hash you can supply a puppet:// file source for the logging.yml file
       
    50 #
       
    51 # [*logging_config*]
       
    52 #   Hash representation of information you want in the logging.yml file
       
    53 #
       
    54 # [*logging_template*]
       
    55 #  Use a custom logging template - just supply the reative path ie ${module}/elasticsearch/logging.yml.erb
       
    56 #
       
    57 # [*logging_level*]
       
    58 #   Default logging level for Elasticsearch.
       
    59 #   Defaults to: INFO
       
    60 #
       
    61 # [*init_defaults*]
       
    62 #   Defaults file content in hash representation
       
    63 #
       
    64 # [*init_defaults_file*]
       
    65 #   Defaults file as puppet resource
       
    66 #
       
    67 # === Authors
       
    68 #
       
    69 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
       
    70 #
       
    71 define elasticsearch::instance(
       
    72   $ensure             = $elasticsearch::ensure,
       
    73   $status             = $elasticsearch::status,
       
    74   $config             = undef,
       
    75   $configdir          = undef,
       
    76   $datadir            = undef,
       
    77   $logging_file       = undef,
       
    78   $logging_config     = undef,
       
    79   $logging_template   = undef,
       
    80   $logging_level      = $elasticsearch::default_logging_level,
       
    81   $init_defaults      = undef,
       
    82   $init_defaults_file = undef
       
    83 ) {
       
    84 
       
    85   require elasticsearch::params
       
    86 
       
    87   File {
       
    88     owner => $elasticsearch::elasticsearch_user,
       
    89     group => $elasticsearch::elasticsearch_group,
       
    90   }
       
    91 
       
    92   Exec {
       
    93     path => [ '/bin', '/usr/bin', '/usr/local/bin' ],
       
    94     cwd  => '/',
       
    95   }
       
    96 
       
    97   # ensure
       
    98   if ! ($ensure in [ 'present', 'absent' ]) {
       
    99     fail("\"${ensure}\" is not a valid ensure parameter value")
       
   100   }
       
   101 
       
   102   $notify_service = $elasticsearch::restart_on_change ? {
       
   103     true  => Elasticsearch::Service[$name],
       
   104     false => undef,
       
   105   }
       
   106 
       
   107   # Instance config directory
       
   108   if ($configdir == undef) {
       
   109     $instance_configdir = "${elasticsearch::configdir}/${name}"
       
   110   } else {
       
   111     $instance_configdir = $configdir
       
   112   }
       
   113 
       
   114   if ($ensure == 'present') {
       
   115 
       
   116     # Configuration hash
       
   117     if ($config == undef) {
       
   118       $instance_config = {}
       
   119     } else {
       
   120       validate_hash($config)
       
   121       $instance_config = $config
       
   122     }
       
   123 
       
   124     if(has_key($instance_config, 'node.name')) {
       
   125       $instance_node_name = {}
       
   126     } elsif(has_key($instance_config,'node')) {
       
   127       if(has_key($instance_config['node'], 'name')) {
       
   128         $instance_node_name = {}
       
   129       } else {
       
   130         $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
       
   131       }
       
   132     } else {
       
   133       $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
       
   134     }
       
   135 
       
   136     # String or array for data dir(s)
       
   137     if ($datadir == undef) {
       
   138       if (is_array($elasticsearch::datadir)) {
       
   139         $instance_datadir = array_suffix($elasticsearch::datadir, "/${name}")
       
   140       } else {
       
   141         $instance_datadir = "${elasticsearch::datadir}/${name}"
       
   142       }
       
   143     } else {
       
   144       $instance_datadir = $datadir
       
   145     }
       
   146 
       
   147     # Logging file or hash
       
   148     if ($logging_file != undef) {
       
   149       $logging_source = $logging_file
       
   150       $logging_content = undef
       
   151     } elsif ($elasticsearch::logging_file != undef) {
       
   152       $logging_source = $elasticsearch::logging_file
       
   153       $logging_content = undef
       
   154     } else {
       
   155 
       
   156       if(is_hash($elasticsearch::logging_config)) {
       
   157         $main_logging_config = $elasticsearch::logging_config
       
   158       } else {
       
   159         $main_logging_config = { }
       
   160       }
       
   161 
       
   162       if(is_hash($logging_config)) {
       
   163         $instance_logging_config = $logging_config
       
   164       } else {
       
   165         $instance_logging_config = { }
       
   166       }
       
   167       $logging_hash = merge($elasticsearch::params::logging_defaults, $main_logging_config, $instance_logging_config)
       
   168       if ($logging_template != undef ) {
       
   169         $logging_content = template($logging_template)
       
   170       } elsif ($elasticsearch::logging_template != undef) {
       
   171         $logging_content = template($elasticsearch::logging_template)
       
   172       } else {
       
   173         $logging_content = template("${module_name}/etc/elasticsearch/logging.yml.erb")
       
   174       }
       
   175       $logging_source = undef
       
   176     }
       
   177 
       
   178     if ($elasticsearch::config != undef) {
       
   179       $main_config = $elasticsearch::config
       
   180     } else {
       
   181       $main_config = { }
       
   182     }
       
   183 
       
   184     if(has_key($instance_config, 'path.data')) {
       
   185       $instance_datadir_config = { 'path.data' => $instance_datadir }
       
   186     } elsif(has_key($instance_config, 'path')) {
       
   187       if(has_key($instance_config['path'], 'data')) {
       
   188         $instance_datadir_config = { 'path' => { 'data' => $instance_datadir } }
       
   189       } else {
       
   190         $instance_datadir_config = { 'path.data' => $instance_datadir }
       
   191       }
       
   192     } else {
       
   193       $instance_datadir_config = { 'path.data' => $instance_datadir }
       
   194     }
       
   195 
       
   196     if(is_array($instance_datadir)) {
       
   197       $dirs = join($instance_datadir, ' ')
       
   198     } else {
       
   199       $dirs = $instance_datadir
       
   200     }
       
   201 
       
   202     exec { "mkdir_datadir_elasticsearch_${name}":
       
   203       command => "mkdir -p ${dirs}",
       
   204       creates => $instance_datadir,
       
   205       require => Class['elasticsearch::package'],
       
   206       before  => Elasticsearch::Service[$name],
       
   207     }
       
   208 
       
   209     file { $instance_datadir:
       
   210       ensure  => 'directory',
       
   211       owner   => $elasticsearch::elasticsearch_user,
       
   212       group   => undef,
       
   213       mode    => '0644',
       
   214       recurse => true,
       
   215       require => [ Exec["mkdir_datadir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
       
   216       before  => Elasticsearch::Service[$name],
       
   217     }
       
   218 
       
   219     exec { "mkdir_configdir_elasticsearch_${name}":
       
   220       command => "mkdir -p ${instance_configdir}",
       
   221       creates => $elasticsearch::configdir,
       
   222       require => Class['elasticsearch::package'],
       
   223       before  => Elasticsearch::Service[$name],
       
   224     }
       
   225 
       
   226     file { $instance_configdir:
       
   227       ensure  => 'directory',
       
   228       mode    => '0644',
       
   229       purge   => $elasticsearch::purge_configdir,
       
   230       force   => $elasticsearch::purge_configdir,
       
   231       require => [ Exec["mkdir_configdir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
       
   232       before  => Elasticsearch::Service[$name],
       
   233     }
       
   234 
       
   235     file { "${instance_configdir}/logging.yml":
       
   236       ensure  => file,
       
   237       content => $logging_content,
       
   238       source  => $logging_source,
       
   239       mode    => '0644',
       
   240       notify  => $notify_service,
       
   241       require => Class['elasticsearch::package'],
       
   242       before  => Elasticsearch::Service[$name],
       
   243     }
       
   244 
       
   245     file { "${instance_configdir}/scripts":
       
   246       ensure => 'link',
       
   247       target => "${elasticsearch::params::homedir}/scripts",
       
   248     }
       
   249 
       
   250     # build up new config
       
   251     $instance_conf = merge($main_config, $instance_node_name, $instance_config, $instance_datadir_config)
       
   252 
       
   253     # defaults file content
       
   254     # ensure user did not provide both init_defaults and init_defaults_file
       
   255     if (($init_defaults != undef) and ($init_defaults_file != undef)) {
       
   256       fail ('Only one of $init_defaults and $init_defaults_file should be defined')
       
   257     }
       
   258 
       
   259     if (is_hash($elasticsearch::init_defaults)) {
       
   260       $global_init_defaults = $elasticsearch::init_defaults
       
   261     } else {
       
   262       $global_init_defaults = { }
       
   263     }
       
   264 
       
   265     $instance_init_defaults_main = { 'CONF_DIR' => $instance_configdir, 'CONF_FILE' => "${instance_configdir}/elasticsearch.yml", 'LOG_DIR' => "/var/log/elasticsearch/${name}", 'ES_HOME' => '/usr/share/elasticsearch' }
       
   266 
       
   267     if (is_hash($init_defaults)) {
       
   268       $instance_init_defaults = $init_defaults
       
   269     } else {
       
   270       $instance_init_defaults = { }
       
   271     }
       
   272     $init_defaults_new = merge($global_init_defaults, $instance_init_defaults_main, $instance_init_defaults )
       
   273 
       
   274     $user = $elasticsearch::elasticsearch_user
       
   275     $group = $elasticsearch::elasticsearch_group
       
   276 
       
   277     datacat_fragment { "main_config_${name}":
       
   278       target => "${instance_configdir}/elasticsearch.yml",
       
   279       data   => $instance_conf,
       
   280     }
       
   281 
       
   282     datacat { "${instance_configdir}/elasticsearch.yml":
       
   283       template => "${module_name}/etc/elasticsearch/elasticsearch.yml.erb",
       
   284       notify   => $notify_service,
       
   285       require  => Class['elasticsearch::package'],
       
   286       owner    => $elasticsearch::elasticsearch_user,
       
   287       group    => $elasticsearch::elasticsearch_group,
       
   288     }
       
   289 
       
   290     $require_service = Class['elasticsearch::package']
       
   291     $before_service  = undef
       
   292 
       
   293   } else {
       
   294 
       
   295     file { $instance_configdir:
       
   296       ensure  => 'absent',
       
   297       recurse => true,
       
   298       force   => true,
       
   299     }
       
   300 
       
   301     $require_service = undef
       
   302     $before_service  = File[$instance_configdir]
       
   303 
       
   304     $init_defaults_new = {}
       
   305   }
       
   306 
       
   307   elasticsearch::service { $name:
       
   308     ensure             => $ensure,
       
   309     status             => $status,
       
   310     init_defaults      => $init_defaults_new,
       
   311     init_defaults_file => $init_defaults_file,
       
   312     init_template      => "${module_name}/etc/init.d/${elasticsearch::params::init_template}",
       
   313     require            => $require_service,
       
   314     before             => $before_service,
       
   315   }
       
   316 
       
   317 }