dev/provisioning/modules/elasticsearch/manifests/init.pp
changeset 28 b0b56e0f8c7f
child 406 cf0f23803a53
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 # == Class: elasticsearch
       
     2 #
       
     3 # This class is able to install or remove elasticsearch on a node.
       
     4 # It manages the status of the related service.
       
     5 #
       
     6 # === Parameters
       
     7 #
       
     8 # [*ensure*]
       
     9 #   String. Controls if the managed resources shall be <tt>present</tt> or
       
    10 #   <tt>absent</tt>. If set to <tt>absent</tt>:
       
    11 #   * The managed software packages are being uninstalled.
       
    12 #   * Any traces of the packages will be purged as good as possible. This may
       
    13 #     include existing configuration files. The exact behavior is provider
       
    14 #     dependent. Q.v.:
       
    15 #     * Puppet type reference: {package, "purgeable"}[http://j.mp/xbxmNP]
       
    16 #     * {Puppet's package provider source code}[http://j.mp/wtVCaL]
       
    17 #   * System modifications (if any) will be reverted as good as possible
       
    18 #     (e.g. removal of created users, services, changed log settings, ...).
       
    19 #   * This is thus destructive and should be used with care.
       
    20 #   Defaults to <tt>present</tt>.
       
    21 #
       
    22 # [*autoupgrade*]
       
    23 #   Boolean. If set to <tt>true</tt>, any managed package gets upgraded
       
    24 #   on each Puppet run when the package provider is able to find a newer
       
    25 #   version than the present one. The exact behavior is provider dependent.
       
    26 #   Q.v.:
       
    27 #   * Puppet type reference: {package, "upgradeable"}[http://j.mp/xbxmNP]
       
    28 #   * {Puppet's package provider source code}[http://j.mp/wtVCaL]
       
    29 #   Defaults to <tt>false</tt>.
       
    30 #
       
    31 # [*status*]
       
    32 #   String to define the status of the service. Possible values:
       
    33 #   * <tt>enabled</tt>: Service is running and will be started at boot time.
       
    34 #   * <tt>disabled</tt>: Service is stopped and will not be started at boot
       
    35 #     time.
       
    36 #   * <tt>running</tt>: Service is running but will not be started at boot time.
       
    37 #     You can use this to start a service on the first Puppet run instead of
       
    38 #     the system startup.
       
    39 #   * <tt>unmanaged</tt>: Service will not be started at boot time and Puppet
       
    40 #     does not care whether the service is running or not. For example, this may
       
    41 #     be useful if a cluster management software is used to decide when to start
       
    42 #     the service plus assuring it is running on the desired node.
       
    43 #   Defaults to <tt>enabled</tt>. The singular form ("service") is used for the
       
    44 #   sake of convenience. Of course, the defined status affects all services if
       
    45 #   more than one is managed (see <tt>service.pp</tt> to check if this is the
       
    46 #   case).
       
    47 #
       
    48 # [*version*]
       
    49 #   String to set the specific version you want to install.
       
    50 #   Defaults to <tt>false</tt>.
       
    51 #
       
    52 # [*restart_on_change*]
       
    53 #   Boolean that determines if the application should be automatically restarted
       
    54 #   whenever the configuration changes. Disabling automatic restarts on config
       
    55 #   changes may be desired in an environment where you need to ensure restarts
       
    56 #   occur in a controlled/rolling manner rather than during a Puppet run.
       
    57 #
       
    58 #   Defaults to <tt>true</tt>, which will restart the application on any config
       
    59 #   change. Setting to <tt>false</tt> disables the automatic restart.
       
    60 #
       
    61 # [*configdir*]
       
    62 #   Path to directory containing the elasticsearch configuration.
       
    63 #   Use this setting if your packages deviate from the norm (/etc/elasticsearch)
       
    64 #
       
    65 # [*plugindir*]
       
    66 #   Path to directory containing the elasticsearch plugins
       
    67 #   Use this setting if your packages deviate from the norm (/usr/share/elasticsearch/plugins)
       
    68 #
       
    69 # [*plugintool*]
       
    70 #   Path to directory containing the elasticsearch plugin installation script
       
    71 #   Use this setting if your packages deviate from the norm (/usr/share/elasticsearch/bin/plugin)
       
    72 #
       
    73 # [*package_url*]
       
    74 #   Url to the package to download.
       
    75 #   This can be a http,https or ftp resource for remote packages
       
    76 #   puppet:// resource or file:/ for local packages
       
    77 #
       
    78 # [*package_provider*]
       
    79 #   Way to install the packages, currently only packages are supported.
       
    80 #
       
    81 # [*package_dir*]
       
    82 #   Directory where the packages are downloaded to
       
    83 #
       
    84 # [*package_name*]
       
    85 #   Name of the package to install
       
    86 #
       
    87 # [*purge_package_dir*]
       
    88 #   Purge package directory on removal
       
    89 #
       
    90 # [*package_dl_timeout*]
       
    91 #   For http,https and ftp downloads you can set howlong the exec resource may take.
       
    92 #   Defaults to: 600 seconds
       
    93 #
       
    94 # [*proxy_url*]
       
    95 #   For http and https downloads you can set a proxy server to use
       
    96 #   Format: proto://[user:pass@]server[:port]/
       
    97 #   Defaults to: undef (proxy disabled)
       
    98 #
       
    99 # [*elasticsearch_user*]
       
   100 #   The user Elasticsearch should run as. This also sets the file rights.
       
   101 #
       
   102 # [*elasticsearch_group*]
       
   103 #   The group Elasticsearch should run as. This also sets the file rights
       
   104 #
       
   105 # [*purge_configdir*]
       
   106 #   Purge the config directory for any unmanaged files
       
   107 #
       
   108 # [*service_provider*]
       
   109 #   Service provider to use. By Default when a single service provider is possibe that one is selected.
       
   110 #
       
   111 # [*init_defaults*]
       
   112 #   Defaults file content in hash representation
       
   113 #
       
   114 # [*init_defaults_file*]
       
   115 #   Defaults file as puppet resource
       
   116 #
       
   117 # [*init_template*]
       
   118 #   Service file as a template
       
   119 #
       
   120 # [*config*]
       
   121 #   Elasticsearch configuration hash
       
   122 #
       
   123 # [*datadir*]
       
   124 #   Allows you to set the data directory of Elasticsearch
       
   125 #
       
   126 # [*java_install*]
       
   127 #  Install java which is required for Elasticsearch.
       
   128 #  Defaults to: false
       
   129 #
       
   130 # [*java_package*]
       
   131 #   If you like to install a custom java package, put the name here.
       
   132 #
       
   133 # [*manage_repo*]
       
   134 #   Enable repo management by enabling our official repositories
       
   135 #
       
   136 # [*repo_version*]
       
   137 #   Our repositories are versioned per major version (0.90, 1.0) select here which version you want
       
   138 #
       
   139 # [*repo_key_id*]
       
   140 #   String.  The apt GPG key id
       
   141 #   Default: D88E42B4
       
   142 #
       
   143 # [*repo_key_source*]
       
   144 #   String.  URL of the apt GPG key
       
   145 #   Default: http://packages.elastic.co/GPG-KEY-elasticsearch
       
   146 #
       
   147 # [*logging_config*]
       
   148 #   Hash representation of information you want in the logging.yml file
       
   149 #
       
   150 # [*logging_file*]
       
   151 #   Instead of a hash you can supply a puppet:// file source for the logging.yml file
       
   152 #
       
   153 # [*logging_template*]
       
   154 #  Use a custom logging template - just supply the reative path ie ${module}/elasticsearch/logging.yml.erb
       
   155 #
       
   156 # [*default_logging_level*]
       
   157 #   Default logging level for Elasticsearch.
       
   158 #   Defaults to: INFO
       
   159 #
       
   160 # [*repo_stage*]
       
   161 #   Use stdlib stage setup for managing the repo, instead of anchoring
       
   162 #
       
   163 # [*instances*]
       
   164 #   Define instances via a hash. This is mainly used with Hiera's auto binding
       
   165 #   Defaults to: undef
       
   166 #
       
   167 # [*instances_hiera_merge*]
       
   168 #   Enable Hiera's merging function for the instances
       
   169 #   Defaults to: false
       
   170 #
       
   171 # [*plugins*]
       
   172 #   Define plugins via a hash. This is mainly used with Hiera's auto binding
       
   173 #   Defaults to: undef
       
   174 #
       
   175 # [*plugins_hiera_merge*]
       
   176 #   Enable Hiera's merging function for the plugins
       
   177 #   Defaults to: false
       
   178 #
       
   179 # [*package_pin*]
       
   180 #   Enables package version pinning.
       
   181 #   This pins the package version to the set version number and avoids
       
   182 #   package upgrades.
       
   183 #   Defaults to: true
       
   184 #
       
   185 # The default values for the parameters are set in elasticsearch::params. Have
       
   186 # a look at the corresponding <tt>params.pp</tt> manifest file if you need more
       
   187 # technical information about them.
       
   188 #
       
   189 # === Examples
       
   190 #
       
   191 # * Installation, make sure service is running and will be started at boot time:
       
   192 #     class { 'elasticsearch': }
       
   193 #
       
   194 # * Removal/decommissioning:
       
   195 #     class { 'elasticsearch':
       
   196 #       ensure => 'absent',
       
   197 #     }
       
   198 #
       
   199 # * Install everything but disable service(s) afterwards
       
   200 #     class { 'elasticsearch':
       
   201 #       status => 'disabled',
       
   202 #     }
       
   203 #
       
   204 #
       
   205 # === Authors
       
   206 #
       
   207 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
       
   208 #
       
   209 class elasticsearch(
       
   210   $ensure                = $elasticsearch::params::ensure,
       
   211   $status                = $elasticsearch::params::status,
       
   212   $restart_on_change     = $elasticsearch::params::restart_on_change,
       
   213   $autoupgrade           = $elasticsearch::params::autoupgrade,
       
   214   $version               = false,
       
   215   $package_provider      = 'package',
       
   216   $package_url           = undef,
       
   217   $package_dir           = $elasticsearch::params::package_dir,
       
   218   $package_name          = $elasticsearch::params::package,
       
   219   $package_pin           = true,
       
   220   $purge_package_dir     = $elasticsearch::params::purge_package_dir,
       
   221   $package_dl_timeout    = $elasticsearch::params::package_dl_timeout,
       
   222   $proxy_url             = undef,
       
   223   $elasticsearch_user    = $elasticsearch::params::elasticsearch_user,
       
   224   $elasticsearch_group   = $elasticsearch::params::elasticsearch_group,
       
   225   $configdir             = $elasticsearch::params::configdir,
       
   226   $purge_configdir       = $elasticsearch::params::purge_configdir,
       
   227   $service_provider      = 'init',
       
   228   $init_defaults         = undef,
       
   229   $init_defaults_file    = undef,
       
   230   $init_template         = undef,
       
   231   $config                = undef,
       
   232   $datadir               = $elasticsearch::params::datadir,
       
   233   $plugindir             = $elasticsearch::params::plugindir,
       
   234   $plugintool            = $elasticsearch::params::plugintool,
       
   235   $java_install          = false,
       
   236   $java_package          = undef,
       
   237   $manage_repo           = false,
       
   238   $repo_version          = undef,
       
   239   $repo_key_id           = 'D88E42B4',
       
   240   $repo_key_source       = 'http://packages.elastic.co/GPG-KEY-elasticsearch',
       
   241   $logging_file          = undef,
       
   242   $logging_config        = undef,
       
   243   $logging_template      = undef,
       
   244   $default_logging_level = $elasticsearch::params::default_logging_level,
       
   245   $repo_stage            = false,
       
   246   $instances             = undef,
       
   247   $instances_hiera_merge = false,
       
   248   $plugins               = undef,
       
   249   $plugins_hiera_merge   = false
       
   250 ) inherits elasticsearch::params {
       
   251 
       
   252   anchor {'elasticsearch::begin': }
       
   253 
       
   254 
       
   255   #### Validate parameters
       
   256 
       
   257   # ensure
       
   258   if ! ($ensure in [ 'present', 'absent' ]) {
       
   259     fail("\"${ensure}\" is not a valid ensure parameter value")
       
   260   }
       
   261 
       
   262   # autoupgrade
       
   263   validate_bool($autoupgrade)
       
   264 
       
   265   # service status
       
   266   if ! ($status in [ 'enabled', 'disabled', 'running', 'unmanaged' ]) {
       
   267     fail("\"${status}\" is not a valid status parameter value")
       
   268   }
       
   269 
       
   270   # restart on change
       
   271   validate_bool($restart_on_change)
       
   272 
       
   273   # purge conf dir
       
   274   validate_bool($purge_configdir)
       
   275 
       
   276   if is_array($elasticsearch::params::service_providers) {
       
   277     # Verify the service provider given is in the array
       
   278     if ! ($service_provider in $elasticsearch::params::service_providers) {
       
   279       fail("\"${service_provider}\" is not a valid provider for \"${::operatingsystem}\"")
       
   280     }
       
   281     $real_service_provider = $service_provider
       
   282   } else {
       
   283     # There is only one option so simply set it
       
   284     $real_service_provider = $elasticsearch::params::service_providers
       
   285   }
       
   286 
       
   287   if ($package_url != undef and $version != false) {
       
   288     fail('Unable to set the version number when using package_url option.')
       
   289   }
       
   290 
       
   291   if $ensure == 'present' {
       
   292     # validate config hash
       
   293     if ($config != undef) {
       
   294       validate_hash($config)
       
   295     }
       
   296   }
       
   297 
       
   298   # java install validation
       
   299   validate_bool($java_install)
       
   300 
       
   301   validate_bool($manage_repo)
       
   302 
       
   303   if ($manage_repo == true) {
       
   304     if $repo_version == undef {
       
   305       fail('Please fill in a repository version at $repo_version')
       
   306     } else {
       
   307       validate_string($repo_version)
       
   308     }
       
   309   }
       
   310 
       
   311   if ($version != false) {
       
   312     case $::osfamily {
       
   313       'RedHat', 'Linux', 'Suse': {
       
   314         if ($version =~ /.+-\d/) {
       
   315           $pkg_version = $version
       
   316         } else {
       
   317           $pkg_version = "${version}-1"
       
   318         }
       
   319       }
       
   320       default: {
       
   321         $pkg_version = $version
       
   322       }
       
   323     }
       
   324   }
       
   325 
       
   326   #### Manage actions
       
   327 
       
   328   # package(s)
       
   329   class { 'elasticsearch::package': }
       
   330 
       
   331   # configuration
       
   332   class { 'elasticsearch::config': }
       
   333 
       
   334   # Hiera support for instances
       
   335   validate_bool($instances_hiera_merge)
       
   336 
       
   337   if $instances_hiera_merge == true {
       
   338     $x_instances = hiera_hash('elasticsearch::instances', $::elasticsearch::instances)
       
   339   } else {
       
   340     $x_instances = $instances
       
   341   }
       
   342 
       
   343   if $x_instances {
       
   344     validate_hash($x_instances)
       
   345     create_resources('elasticsearch::instance', $x_instances)
       
   346   }
       
   347 
       
   348   # Hiera support for plugins
       
   349   validate_bool($plugins_hiera_merge)
       
   350 
       
   351   if $plugins_hiera_merge == true {
       
   352     $x_plugins = hiera_hash('elasticsearch::plugins', $::elasticsearch::plugins)
       
   353   } else {
       
   354     $x_plugins = $plugins
       
   355   }
       
   356 
       
   357   if $x_plugins {
       
   358     validate_hash($x_plugins)
       
   359     create_resources('elasticsearch::plugin', $x_plugins)
       
   360   }
       
   361 
       
   362 
       
   363   if $java_install == true {
       
   364     # Install java
       
   365     class { '::java':
       
   366       package      => $java_package,
       
   367       distribution => 'jre',
       
   368     }
       
   369 
       
   370     # ensure we first install java, the package and then the rest
       
   371     Anchor['elasticsearch::begin']
       
   372     -> Class['::java']
       
   373     -> Class['elasticsearch::package']
       
   374   }
       
   375 
       
   376   if ($manage_repo == true) {
       
   377 
       
   378     if ($repo_stage == false) {
       
   379       # use anchor for ordering
       
   380 
       
   381       # Set up repositories
       
   382       class { 'elasticsearch::repo': }
       
   383 
       
   384       # Ensure that we set up the repositories before trying to install
       
   385       # the packages
       
   386       Anchor['elasticsearch::begin']
       
   387       -> Class['elasticsearch::repo']
       
   388       -> Class['elasticsearch::package']
       
   389 
       
   390     } else {
       
   391       # use staging for ordering
       
   392 
       
   393       if !(defined(Stage[$repo_stage])) {
       
   394         stage { $repo_stage:  before => Stage['main'] }
       
   395       }
       
   396 
       
   397       class { 'elasticsearch::repo':
       
   398         stage => $repo_stage,
       
   399       }
       
   400     }
       
   401   }
       
   402 
       
   403   #### Manage relationships
       
   404 
       
   405   if $ensure == 'present' {
       
   406 
       
   407     # we need the software before configuring it
       
   408     Anchor['elasticsearch::begin']
       
   409     -> Class['elasticsearch::package']
       
   410     -> Class['elasticsearch::config']
       
   411     -> Elasticsearch::Plugin <| |>
       
   412     -> Elasticsearch::Instance <| |>
       
   413     -> Elasticsearch::Template <| |>
       
   414 
       
   415   } else {
       
   416 
       
   417     # make sure all services are getting stopped before software removal
       
   418     Anchor['elasticsearch::begin']
       
   419     -> Elasticsearch::Instance <| |>
       
   420     -> Class['elasticsearch::config']
       
   421     -> Class['elasticsearch::package']
       
   422 
       
   423   }
       
   424 
       
   425 }