(GH-cat-9) syntax:hiera:yaml fixes
[puppet-modules/puppetlabs-apt.git] / manifests / ppa.pp
1 # @summary Manages PPA repositories using `add-apt-repository`. Not supported on Debian.
2 #
3 # @example Example declaration of an Apt PPA
4 #   apt::ppa{ 'ppa:openstack-ppa/bleeding-edge': }
5 #
6 # @param ensure
7 #   Specifies whether the PPA should exist. Valid options: 'present' and 'absent'.
8 #
9 # @param options
10 #   Supplies options to be passed to the `add-apt-repository` command. Default: '-y'.
11 #
12 # @param release
13 #   Specifies the operating system of your node. Valid options: a string containing a valid LSB distribution codename.
14 #   Optional if `puppet facts show os.distro.codename` returns your correct distribution release codename.
15 #
16 # @param dist
17 #   Specifies the distribution of your node. Valid options: a string containing a valid distribution codename.
18 #   Optional if `puppet facts show os.name` returns your correct distribution name.
19 #
20 # @param package_name
21 #   Names the package that provides the `apt-add-repository` command. Default: 'software-properties-common'.
22 #
23 # @param package_manage
24 #   Specifies whether Puppet should manage the package that provides `apt-add-repository`.
25 #
26 define apt::ppa (
27   String $ensure                 = 'present',
28   Optional[String] $options      = $::apt::ppa_options,
29   Optional[String] $release      = fact('os.distro.codename'),
30   Optional[String] $dist         = $facts['os']['name'],
31   Optional[String] $package_name = $::apt::ppa_package,
32   Boolean $package_manage        = false,
33 ) {
34   unless $release {
35     fail('os.distro.codename fact not available: release parameter required')
36   }
37
38   if $dist == 'Debian' {
39     fail('apt::ppa is not currently supported on Debian.')
40   }
41
42   if versioncmp($facts['os']['release']['full'], '14.10') >= 0 {
43     $distid = downcase($dist)
44     $dash_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1-${distid}-\\2")
45     $underscore_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1_${distid}_\\2")
46   } else {
47     $dash_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1-\\2")
48     $underscore_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1_\\2")
49   }
50
51   $dash_filename_no_slashes      = regsubst($dash_filename, '/', '-', 'G')
52   $dash_filename_no_specialchars = regsubst($dash_filename_no_slashes, '[\.\+]', '_', 'G')
53   $underscore_filename_no_slashes      = regsubst($underscore_filename, '/', '-', 'G')
54   $underscore_filename_no_specialchars = regsubst($underscore_filename_no_slashes, '[\.\+]', '_', 'G')
55
56   $sources_list_d_filename  = "${dash_filename_no_specialchars}-${release}.list"
57
58   if versioncmp($facts['os']['release']['full'], '15.10') >= 0 and
59     versioncmp($facts['os']['release']['full'], '21.04') < 0 {
60     $trusted_gpg_d_filename = "${underscore_filename_no_specialchars}.gpg"
61   } else {
62     $trusted_gpg_d_filename = "${dash_filename_no_specialchars}.gpg"
63   }
64
65   if $ensure == 'present' {
66     if $package_manage {
67       ensure_packages($package_name)
68       $_require = [File['sources.list.d'], Package[$package_name]]
69     } else {
70       $_require = File['sources.list.d']
71     }
72
73     $_proxy = $::apt::_proxy
74     if $_proxy['host'] {
75       if $_proxy['https'] {
76         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}", "https_proxy=https://${$_proxy['host']}:${$_proxy['port']}"]
77       } else {
78         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}"]
79       }
80     } else {
81       $_proxy_env = []
82     }
83
84     exec { "add-apt-repository-${name}":
85       environment => $_proxy_env,
86       command     => "/usr/bin/add-apt-repository ${options} ${name} || (rm ${::apt::sources_list_d}/${sources_list_d_filename} && false)",
87       unless      => "/usr/bin/test -f ${::apt::sources_list_d}/${sources_list_d_filename} && /usr/bin/test -f ${::apt::trusted_gpg_d}/${trusted_gpg_d_filename}",
88       user        => 'root',
89       logoutput   => 'on_failure',
90       notify      => Class['apt::update'],
91       require     => $_require,
92     }
93
94     file { "${::apt::sources_list_d}/${sources_list_d_filename}":
95       ensure  => file,
96       require => Exec["add-apt-repository-${name}"],
97     }
98   }
99   else {
100     file { "${::apt::sources_list_d}/${sources_list_d_filename}":
101       ensure => 'absent',
102       notify => Class['apt::update'],
103     }
104   }
105 }