init.pp 5.9 KB

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