39852d1b025d1d0af0a89bff45e0fb07b60cfc94
[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   $script_path = "/opt/puppetlabs/puppet/cache/add-apt-repository-${dash_filename_no_specialchars}-${release}.sh"
72
73   if $ensure == 'present' {
74     if $package_manage {
75       ensure_packages($package_name)
76       $_require = [File['sources.list.d'], Package[$package_name]]
77     } else {
78       $_require = File['sources.list.d']
79     }
80
81     $_proxy = $::apt::_proxy
82     if $_proxy['host'] {
83       if $_proxy['https'] {
84         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}", "https_proxy=https://${$_proxy['host']}:${$_proxy['port']}"]
85       } else {
86         $_proxy_env = ["http_proxy=http://${$_proxy['host']}:${$_proxy['port']}"]
87       }
88     } else {
89       $_proxy_env = []
90     }
91
92     unless $sources_list_d_filename in $facts['apt_sources'] {
93       $script_content = epp('apt/add-apt-repository.sh.epp', {
94         command                 => ['/usr/bin/add-apt-repository', shell_join($options), $name],
95         sources_list_d_path     => $::apt::sources_list_d,
96         sources_list_d_filename => $sources_list_d_filename,
97       })
98
99       file { "add-apt-repository-script-${name}":
100         ensure  => 'file',
101         path    => $script_path,
102         content => $script_content,
103         mode    => '0755',
104       }
105
106       exec { "add-apt-repository-${name}":
107         environment => $_proxy_env,
108         command     => $script_path,
109         logoutput   => 'on_failure',
110         notify      => Class['apt::update'],
111         require     => $_require,
112       }
113     }
114   }
115   else {
116     tidy { "remove-apt-repository-script-${name}":
117       path => $script_path,
118     }
119
120     tidy { "remove-apt-repository-${name}":
121       path   => "${::apt::sources_list_d}/${sources_list_d_filename}",
122       notify => Class['apt::update'],
123     }
124   }
125 }