upstream.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # define: nginx::resource::upstream
  2. #
  3. # This definition creates a new upstream proxy entry for NGINX
  4. #
  5. # Parameters:
  6. # [*members*] - Array of member URIs for NGINX to connect to. Must follow valid NGINX syntax.
  7. # If omitted, individual members should be defined with nginx::resource::upstream::member
  8. # [*ensure*] - Enables or disables the specified location (present|absent)
  9. # [*upstream_cfg_prepend*] - It expects a hash with custom directives to put before anything else inside upstream
  10. # [*upstream_fail_timeout*] - Set the fail_timeout for the upstream. Default is 10 seconds - As that is what Nginx does normally.
  11. # [*upstream_max_fails*] - Set the max_fails for the upstream. Default is to use nginx default value which is 1.
  12. #
  13. # Actions:
  14. #
  15. # Requires:
  16. #
  17. # Sample Usage:
  18. # nginx::resource::upstream { 'proxypass':
  19. # ensure => present,
  20. # members => [
  21. # 'localhost:3000',
  22. # 'localhost:3001',
  23. # 'localhost:3002',
  24. # ],
  25. # }
  26. #
  27. # Custom config example to use ip_hash, and 20 keepalive connections
  28. # create a hash with any extra custom config you want.
  29. # $my_config = {
  30. # 'ip_hash' => '',
  31. # 'keepalive' => '20',
  32. # }
  33. # nginx::resource::upstream { 'proxypass':
  34. # ensure => present,
  35. # members => [
  36. # 'localhost:3000',
  37. # 'localhost:3001',
  38. # 'localhost:3002',
  39. # ],
  40. # upstream_cfg_prepend => $my_config,
  41. # }
  42. define nginx::resource::upstream (
  43. $members = undef,
  44. $ensure = 'present',
  45. $upstream_cfg_prepend = undef,
  46. $upstream_fail_timeout = '10s',
  47. $upstream_max_fails = undef,
  48. $upstream_context = 'http',
  49. ) {
  50. if $members != undef {
  51. validate_array($members)
  52. }
  53. validate_re($ensure, '^(present|absent)$',
  54. "${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
  55. validate_re($upstream_context, '^(http|stream)$',
  56. "${upstream_context} is not supported for upstream_context. Allowed values are 'http' and 'stream'.")
  57. if ($upstream_cfg_prepend != undef) {
  58. validate_hash($upstream_cfg_prepend)
  59. }
  60. $root_group = $::nginx::config::root_group
  61. $ensure_real = $ensure ? {
  62. 'absent' => absent,
  63. default => present,
  64. }
  65. $conf_dir_real = $upstream_context ? {
  66. 'stream' => 'conf.stream.d',
  67. default => 'conf.d',
  68. }
  69. Concat {
  70. owner => 'root',
  71. group => $root_group,
  72. mode => '0644',
  73. }
  74. concat { "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf":
  75. ensure => $ensure_real,
  76. notify => Class['::nginx::service'],
  77. }
  78. # Uses: $name, $upstream_cfg_prepend
  79. concat::fragment { "${name}_upstream_header":
  80. target => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
  81. order => '10',
  82. content => template('nginx/conf.d/upstream_header.erb'),
  83. }
  84. if $members != undef {
  85. # Uses: $members, $upstream_fail_timeout
  86. concat::fragment { "${name}_upstream_members":
  87. target => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
  88. order => '50',
  89. content => template('nginx/conf.d/upstream_members.erb'),
  90. }
  91. } else {
  92. class { 'nginx::resource::upstream::collect':
  93. # Collect exported members
  94. upstream_name => $name,
  95. }
  96. }
  97. concat::fragment { "${name}_upstream_footer":
  98. target => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
  99. order => '90',
  100. content => "}\n",
  101. }
  102. }