upstream.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #
  12. # Actions:
  13. #
  14. # Requires:
  15. #
  16. # Sample Usage:
  17. # nginx::resource::upstream { 'proxypass':
  18. # ensure => present,
  19. # members => [
  20. # 'localhost:3000',
  21. # 'localhost:3001',
  22. # 'localhost:3002',
  23. # ],
  24. # }
  25. #
  26. # Custom config example to use ip_hash, and 20 keepalive connections
  27. # create a hash with any extra custom config you want.
  28. # $my_config = {
  29. # 'ip_hash' => '',
  30. # 'keepalive' => '20',
  31. # }
  32. # nginx::resource::upstream { 'proxypass':
  33. # ensure => present,
  34. # members => [
  35. # 'localhost:3000',
  36. # 'localhost:3001',
  37. # 'localhost:3002',
  38. # ],
  39. # upstream_cfg_prepend => $my_config,
  40. # }
  41. define nginx::resource::upstream (
  42. $members = undef,
  43. $ensure = 'present',
  44. $upstream_cfg_prepend = undef,
  45. $upstream_fail_timeout = '10s',
  46. ) {
  47. if $members != undef {
  48. validate_array($members)
  49. }
  50. validate_re($ensure, '^(present|absent)$',
  51. "${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
  52. if ($upstream_cfg_prepend != undef) {
  53. validate_hash($upstream_cfg_prepend)
  54. }
  55. include nginx::params
  56. $root_group = $nginx::params::root_group
  57. $ensure_real = $ensure ? {
  58. 'absent' => absent,
  59. default => present,
  60. }
  61. Concat {
  62. owner => 'root',
  63. group => $root_group,
  64. mode => '0644',
  65. }
  66. concat { "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf":
  67. ensure => $ensure_real,
  68. notify => Class['nginx::service'],
  69. }
  70. # Uses: $name, $upstream_cfg_prepend
  71. concat::fragment { "${name}_upstream_header":
  72. target => "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf",
  73. order => 10,
  74. content => template('nginx/conf.d/upstream_header.erb'),
  75. }
  76. if $members != undef {
  77. # Uses: $members, $upstream_fail_timeout
  78. concat::fragment { "${name}_upstream_members":
  79. target => "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf",
  80. order => 50,
  81. content => template('nginx/conf.d/upstream_members.erb'),
  82. }
  83. } else {
  84. # Collect exported members:
  85. Nginx::Resource::Upstream::Member <<| upstream == $name |>>
  86. }
  87. concat::fragment { "${name}_upstream_footer":
  88. target => "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf",
  89. order => 90,
  90. content => "}\n",
  91. }
  92. }