(FM-7318) - Added Puppet Strings formatting to documentation
[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 #   Optional if lsb-release is installed (unless you're using a different release than indicated by lsb-release, e.g., Linux Mint). 
14 #   Specifies the operating system of your node. Valid options: a string containing a valid LSB distribution codename.
15 #
16 # @param package_name
17 #   Names the package that provides the `apt-add-repository` command. Default: 'software-properties-common'.
18 #
19 # @param package_manage
20 #   Specifies whether Puppet should manage the package that provides `apt-add-repository`.
21 #
22 define apt::ppa(
23   String $ensure                 = 'present',
24   Optional[String] $options      = $::apt::ppa_options,
25   Optional[String] $release      = $facts['lsbdistcodename'],
26   Optional[String] $package_name = $::apt::ppa_package,
27   Boolean $package_manage        = false,
28 ) {
29   unless $release {
30     fail('lsbdistcodename fact not available: release parameter required')
31   }
32
33   if $facts['lsbdistid'] == 'Debian' {
34     fail('apt::ppa is not currently supported on Debian.')
35   }
36
37   if versioncmp($facts['lsbdistrelease'], '14.10') >= 0 {
38     $distid = downcase($facts['lsbdistid'])
39     $dash_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1-${distid}-\\2")
40     $underscore_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1_${distid}_\\2")
41   } else {
42     $dash_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1-\\2")
43     $underscore_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1_\\2")
44   }
45
46   $dash_filename_no_slashes      = regsubst($dash_filename, '/', '-', 'G')
47   $dash_filename_no_specialchars = regsubst($dash_filename_no_slashes, '[\.\+]', '_', 'G')
48   $underscore_filename_no_slashes      = regsubst($underscore_filename, '/', '-', 'G')
49   $underscore_filename_no_specialchars = regsubst($underscore_filename_no_slashes, '[\.\+]', '_', 'G')
50
51   $sources_list_d_filename  = "${dash_filename_no_specialchars}-${release}.list"
52
53   if versioncmp($facts['lsbdistrelease'], '15.10') >= 0 {
54     $trusted_gpg_d_filename = "${underscore_filename_no_specialchars}.gpg"
55   } else {
56     $trusted_gpg_d_filename = "${dash_filename_no_specialchars}.gpg"
57   }
58
59   if $ensure == 'present' {
60     if $package_manage {
61       ensure_packages($package_name)
62       $_require = [File['sources.list.d'], Package[$package_name]]
63     } else {
64       $_require = File['sources.list.d']
65     }
66
67     $_proxy = $::apt::_proxy
68     if $_proxy['host'] {
69       if $_proxy['https'] {
70         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}", "https_proxy=https://${$_proxy['host']}:${$_proxy['port']}"]
71       } else {
72         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}"]
73       }
74     } else {
75       $_proxy_env = []
76     }
77
78     exec { "add-apt-repository-${name}":
79       environment => $_proxy_env,
80       command     => "/usr/bin/add-apt-repository ${options} ${name} || (rm ${::apt::sources_list_d}/${sources_list_d_filename} && false)",
81       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}",
82       user        => 'root',
83       logoutput   => 'on_failure',
84       notify      => Class['apt::update'],
85       require     => $_require,
86     }
87
88     file { "${::apt::sources_list_d}/${sources_list_d_filename}":
89       ensure  => file,
90       require => Exec["add-apt-repository-${name}"],
91     }
92   }
93   else {
94     file { "${::apt::sources_list_d}/${sources_list_d_filename}":
95       ensure => 'absent',
96       notify => Class['apt::update'],
97     }
98   }
99 }