init.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Class: apt
  2. #
  3. # This module manages the initial configuration of apt.
  4. #
  5. # Parameters:
  6. # The parameters listed here are not required in general and were
  7. # added for use cases related to development environments.
  8. #
  9. # disable_keys - disables the requirement for all packages to be signed
  10. #
  11. # always_apt_update - rather apt should be updated on every run (intended
  12. # for development environments where package updates are frequent)
  13. #
  14. # apt_update_frequency - *string* Supported values:
  15. # **always**: Will fire `apt-get update` at every puppet run. Intended to
  16. # deprecate the `always_apt_update` parameter.
  17. # **daily**: Trigger `apt-get update` if the value of the fact
  18. # `apt_update_last_success` is less than current epoch time - 86400.
  19. # *notifying the apt_update exec will trigger apt-get update regardless*
  20. # **weekly**: Trigger `apt-get update` if the value of the fact
  21. # `apt_update_last_success` is less than current epoch time - 604800.
  22. # *notifying the apt_update exec will trigger apt-get update regardless*
  23. # **reluctantly**: *Default* only run apt-get update if the exec resource `apt_update` is notified.
  24. #
  25. # purge_sources_list - Accepts true or false. Defaults to false If set to
  26. # true, Puppet will purge all unmanaged entries from sources.list
  27. #
  28. # purge_sources_list_d - Accepts true or false. Defaults to false. If set
  29. # to true, Puppet will purge all unmanaged entries from sources.list.d
  30. #
  31. # update_timeout - Overrides the exec timeout in seconds for apt-get update.
  32. # If not set defaults to Exec's default (300)
  33. #
  34. # update_tries - Number of times that `apt-get update` will be tried. Use this
  35. # to work around transient DNS and HTTP errors. By default, the command
  36. # will only be run once.
  37. #
  38. # Actions:
  39. #
  40. # Requires:
  41. # puppetlabs/stdlib
  42. # Sample Usage:
  43. # class { 'apt': }
  44. class apt(
  45. $always_apt_update = false,
  46. $apt_update_frequency = 'reluctantly',
  47. $disable_keys = undef,
  48. $proxy_host = undef,
  49. $proxy_port = '8080',
  50. $purge_sources_list = false,
  51. $purge_sources_list_d = false,
  52. $purge_preferences = false,
  53. $purge_preferences_d = false,
  54. $update_timeout = undef,
  55. $update_tries = undef,
  56. $sources = undef,
  57. $fancy_progress = undef
  58. ) {
  59. if $::osfamily != 'Debian' {
  60. fail('This module only works on Debian or derivatives like Ubuntu')
  61. }
  62. $frequency_options = ['always','daily','weekly','reluctantly']
  63. validate_re($apt_update_frequency, $frequency_options)
  64. include apt::params
  65. include apt::update
  66. validate_bool($purge_sources_list, $purge_sources_list_d,
  67. $purge_preferences, $purge_preferences_d)
  68. $sources_list_content = $purge_sources_list ? {
  69. false => undef,
  70. true => "# Repos managed by puppet.\n",
  71. }
  72. if $always_apt_update == true {
  73. Exec <| title=='apt_update' |> {
  74. refreshonly => false,
  75. }
  76. }
  77. file { '/etc/apt/apt.conf.d/15update-stamp':
  78. ensure => 'file',
  79. content => 'APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};',
  80. group => 'root',
  81. mode => '0644',
  82. owner => 'root',
  83. }
  84. $root = $apt::params::root
  85. $apt_conf_d = $apt::params::apt_conf_d
  86. $sources_list_d = $apt::params::sources_list_d
  87. $preferences_d = $apt::params::preferences_d
  88. $provider = $apt::params::provider
  89. file { 'sources.list':
  90. ensure => present,
  91. path => "${root}/sources.list",
  92. owner => root,
  93. group => root,
  94. mode => '0644',
  95. content => $sources_list_content,
  96. notify => Exec['apt_update'],
  97. }
  98. file { 'sources.list.d':
  99. ensure => directory,
  100. path => $sources_list_d,
  101. owner => root,
  102. group => root,
  103. purge => $purge_sources_list_d,
  104. recurse => $purge_sources_list_d,
  105. notify => Exec['apt_update'],
  106. }
  107. if $purge_preferences {
  108. file { 'apt-preferences':
  109. ensure => absent,
  110. path => "${root}/preferences",
  111. }
  112. }
  113. file { 'preferences.d':
  114. ensure => directory,
  115. path => $preferences_d,
  116. owner => root,
  117. group => root,
  118. purge => $purge_preferences_d,
  119. recurse => $purge_preferences_d,
  120. }
  121. case $fancy_progress {
  122. true: {
  123. file { '99progressbar':
  124. ensure => present,
  125. content => 'Dpkg::Progress-Fancy "1";',
  126. path => "${apt_conf_d}/99progressbar",
  127. }
  128. }
  129. false: {
  130. file { '99progressbar':
  131. ensure => absent,
  132. path => "${apt_conf_d}/99progressbar",
  133. }
  134. }
  135. undef: {} # do nothing
  136. default: { fail('Valid values for fancy_progress are true or false') }
  137. }
  138. case $disable_keys {
  139. true: {
  140. file { '99unauth':
  141. ensure => present,
  142. content => "APT::Get::AllowUnauthenticated 1;\n",
  143. path => "${apt_conf_d}/99unauth",
  144. }
  145. }
  146. false: {
  147. file { '99unauth':
  148. ensure => absent,
  149. path => "${apt_conf_d}/99unauth",
  150. }
  151. }
  152. undef: { } # do nothing
  153. default: { fail('Valid values for disable_keys are true or false') }
  154. }
  155. case $proxy_host {
  156. false, '', undef: {
  157. file { '01proxy':
  158. ensure => absent,
  159. path => "${apt_conf_d}/01proxy",
  160. notify => Exec['apt_update'],
  161. }
  162. }
  163. default: {
  164. file { '01proxy':
  165. ensure => present,
  166. path => "${apt_conf_d}/01proxy",
  167. content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";\n",
  168. notify => Exec['apt_update'],
  169. mode => '0644',
  170. owner => root,
  171. group => root,
  172. }
  173. }
  174. }
  175. file { 'old-proxy-file':
  176. ensure => absent,
  177. path => "${apt_conf_d}/proxy",
  178. notify => Exec['apt_update'],
  179. }
  180. # Need anchor to provide containment for dependencies.
  181. anchor { 'apt::update':
  182. require => Class['apt::update'],
  183. }
  184. # manage sources if present
  185. if $sources != undef {
  186. validate_hash($sources)
  187. create_resources('apt::source', $sources)
  188. }
  189. }