Merge pull request #1058 from puppetlabs/issue-1057
[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[Array[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   # Validate the resource name
43   if $name !~ /^ppa:([a-zA-Z0-9\-_]+)\/([a-zA-z0-9\-_\.]+)$/ {
44     fail("Invalid PPA name: ${name}")
45   }
46
47   if versioncmp($facts['os']['release']['full'], '14.10') >= 0 {
48     $distid = downcase($dist)
49     $dash_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1-${distid}-\\2")
50     $underscore_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1_${distid}_\\2")
51   } else {
52     $dash_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1-\\2")
53     $underscore_filename = regsubst($name, '^ppa:([^/]+)/(.+)$', "\\1_\\2")
54   }
55
56   $dash_filename_no_slashes      = regsubst($dash_filename, '/', '-', 'G')
57   $dash_filename_no_specialchars = regsubst($dash_filename_no_slashes, '[\.\+]', '_', 'G')
58   $underscore_filename_no_slashes      = regsubst($underscore_filename, '/', '-', 'G')
59   $underscore_filename_no_specialchars = regsubst($underscore_filename_no_slashes, '[\.\+]', '_', 'G')
60
61   $sources_list_d_filename  = "${dash_filename_no_specialchars}-${release}.list"
62
63   if versioncmp($facts['os']['release']['full'], '15.10') >= 0 and
64   versioncmp($facts['os']['release']['full'], '21.04') < 0 {
65     $trusted_gpg_d_filename = "${underscore_filename_no_specialchars}.gpg"
66   } else {
67     $trusted_gpg_d_filename = "${dash_filename_no_specialchars}.gpg"
68   }
69
70   # This is the location of our main exec script.
71   $cache_path = $facts['puppet_vardir']
72   $script_path = "${cache_path}/add-apt-repository-${dash_filename_no_specialchars}-${release}.sh"
73
74   if $ensure == 'present' {
75     if $package_manage {
76       ensure_packages($package_name)
77       $_require = [File['sources.list.d'], Package[$package_name]]
78     } else {
79       $_require = File['sources.list.d']
80     }
81
82     $_proxy = $apt::_proxy
83     if $_proxy['host'] {
84       if $_proxy['https'] {
85         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}", "https_proxy=https://${$_proxy['host']}:${$_proxy['port']}"]
86       } else {
87         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}"]
88       }
89     } else {
90       $_proxy_env = []
91     }
92
93     unless $sources_list_d_filename in $facts['apt_sources'] {
94       $script_content = epp('apt/add-apt-repository.sh.epp', {
95           command                 => ['/usr/bin/add-apt-repository', shell_join($options), $name],
96           sources_list_d_path     => $apt::sources_list_d,
97           sources_list_d_filename => $sources_list_d_filename,
98         }
99       )
100
101       file { "add-apt-repository-script-${name}":
102         ensure  => 'file',
103         path    => $script_path,
104         content => $script_content,
105         mode    => '0755',
106       }
107
108       exec { "add-apt-repository-${name}":
109         environment => $_proxy_env,
110         command     => $script_path,
111         logoutput   => 'on_failure',
112         notify      => Class['apt::update'],
113         require     => $_require,
114       }
115     }
116   }
117   else {
118     tidy { "remove-apt-repository-script-${name}":
119       path => $script_path,
120     }
121
122     tidy { "remove-apt-repository-${name}":
123       path   => "${apt::sources_list_d}/${sources_list_d_filename}",
124       notify => Class['apt::update'],
125     }
126   }
127 }