Merge pull request #714 from actatux/apt-transport-https_debian8
[puppet-modules/puppetlabs-apt.git] / manifests / source.pp
1 # source.pp
2 # add an apt source
3 define apt::source(
4   Optional[String] $location                    = undef,
5   String $comment                               = $name,
6   String $ensure                                = present,
7   Optional[String] $release                     = undef,
8   String $repos                                 = 'main',
9   Optional[Variant[Hash]] $include              = {},
10   Optional[Variant[String, Hash]] $key          = undef,
11   Optional[Variant[Hash, Numeric, String]] $pin = undef,
12   Optional[String] $architecture                = undef,
13   Boolean $allow_unsigned                       = false,
14   Boolean $notify_update                        = true,
15 ) {
16
17   # This is needed for compat with 1.8.x
18   include ::apt
19
20   $_before = Apt::Setting["list-${title}"]
21
22   if !$release {
23     if $facts['lsbdistcodename'] {
24       $_release = $facts['lsbdistcodename']
25     } else {
26       fail('lsbdistcodename fact not available: release parameter required')
27     }
28   } else {
29     $_release = $release
30   }
31
32   if $ensure == 'present' {
33     if ! $location {
34       fail('cannot create a source entry without specifying a location')
35     } elsif $_release == 'jessie' {
36       $method = split($location, '[:\/]+')[0]
37       if $method == 'https' {
38         ensure_packages('apt-transport-https')
39       }
40     }
41   }
42
43   $includes = merge($::apt::include_defaults, $include)
44
45   if $key {
46     if $key =~ Hash {
47       unless $key['id'] {
48         fail('key hash must contain at least an id entry')
49       }
50       $_key = merge($::apt::source_key_defaults, $key)
51     } else {
52       $_key = { 'id' => assert_type(String[1], $key) }
53     }
54   }
55
56   $header = epp('apt/_header.epp')
57
58   $sourcelist = epp('apt/source.list.epp', {
59     'comment'          => $comment,
60     'includes'         => $includes,
61     'opt_architecture' => $architecture,
62     'allow_unsigned'   => $allow_unsigned,
63     'location'         => $location,
64     'release'          => $_release,
65     'repos'            => $repos,
66   })
67
68   apt::setting { "list-${name}":
69     ensure        => $ensure,
70     content       => "${header}${sourcelist}",
71     notify_update => $notify_update,
72   }
73
74   if $pin {
75     if $pin =~ Hash {
76       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
77     } elsif ($pin =~ Numeric or $pin =~ String) {
78       $url_split = split($location, '[:\/]+')
79       $host      = $url_split[1]
80       $_pin = {
81         'ensure'   => $ensure,
82         'priority' => $pin,
83         'before'   => $_before,
84         'origin'   => $host,
85       }
86     } else {
87       fail('Received invalid value for pin parameter')
88     }
89     create_resources('apt::pin', { "${name}" => $_pin })
90   }
91
92   # We do not want to remove keys when the source is absent.
93   if $key and ($ensure == 'present') {
94     if $_key =~ Hash {
95       apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}":
96         ensure  => present,
97         id      => $_key['id'],
98         server  => $_key['server'],
99         content => $_key['content'],
100         source  => $_key['source'],
101         options => $_key['options'],
102         before  => $_before,
103       }
104     }
105   }
106 }