Skip to content

Commit

Permalink
Merge pull request #964 from kenyon/use-modern-os-facts
Browse files Browse the repository at this point in the history
Use modern os facts
  • Loading branch information
carabasdaniel authored Jan 18, 2021
2 parents c5259f8 + 45341ae commit 18c44ad
Show file tree
Hide file tree
Showing 26 changed files with 736 additions and 313 deletions.
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#### Table of Contents


1. [Module Description - What the module does and why it is useful](#module-description)
1. [Setup - The basics of getting started with apt](#setup)
* [What apt affects](#what-apt-affects)
Expand All @@ -20,18 +19,23 @@
1. [Development - Guide for contributing to the module](#development)

<a id="module-description"></a>

## Module Description

The apt module lets you use Puppet to manage APT (Advanced Package Tool) sources, keys, and other configuration options.

APT is a package manager available on Debian, Ubuntu, and several other operating systems. The apt module provides a series of classes, defines, types, and facts to help you automate APT package management.

**Note**: For this module to correctly autodetect which version of Debian/Ubuntu (or derivative) you're running, you need to make sure the 'lsb-release' package is installed. We highly recommend you either make this part of your provisioning layer, if you run many Debian or derivative systems, or ensure that you have Facter 2.2.0 or later installed, which will pull this dependency in for you.
**Note**: Prior to Puppet 7, for this module to correctly autodetect which version of
Debian/Ubuntu (or derivative) you're running, you need to make sure the `lsb-release` package is
installed. With Puppet 7 the `lsb-release` package is not needed.

<a id="setup"></a>

## Setup

<a id="what-apt-affects"></a>

### What apt affects

* Your system's `preferences` file and `preferences.d` directory
Expand All @@ -43,6 +47,7 @@ APT is a package manager available on Debian, Ubuntu, and several other operatin
**Note:** This module offers `purge` parameters which, if set to `true`, **destroy** any configuration on the node's `sources.list(.d)`, `preferences(.d)` and `apt.conf.d` that you haven't declared through Puppet. The default for these parameters is `false`.

<a id="beginning-with-apt"></a>

### Beginning with apt

To use the apt module with default parameters, declare the `apt` class.
Expand All @@ -54,9 +59,11 @@ include apt
**Note:** The main `apt` class is required by all other classes, types, and defined types in this module. You must declare it whenever you use the module.

<a id="usage"></a>

## Usage

<a id="add-gpg-keys"></a>

### Add GPG keys

**Warning:** Using short key IDs presents a serious security issue, potentially leaving you open to collision attacks. We recommend you always use full fingerprints to identify your GPG keys. This module allows short keys, but issues a security warning if you use them.
Expand All @@ -72,6 +79,7 @@ apt::key { 'puppetlabs':
```

<a id="prioritize-backports"></a>

### Prioritize backports

```puppet
Expand All @@ -85,9 +93,10 @@ By default, the `apt::backports` class drops a pin file for backports, pinning i
If you raise the priority through the `pin` parameter to 500, normal policy goes into effect and Apt installs or upgrades to the newest version. This means that if a package is available from backports, it and its dependencies are pulled in from backports unless you explicitly set the `ensure` attribute of the `package` resource to `installed`/`present` or a specific version.

<a id="update-the-list-of-packages"></a>

### Update the list of packages

By default, Puppet runs `apt-get update` on the first Puppet run after you include the `apt` class, and anytime `notify => Exec['apt_update']` occurs; i.e., whenever config files get updated or other relevant changes occur. If you set `update['frequency']` to 'always', the update runs on every Puppet run. You can also set `update['frequency']` to 'daily' or 'weekly':
By default, Puppet runs `apt-get update` on the first Puppet run after you include the `apt` class, and anytime `notify => Exec['apt_update']` occurs; i.e., whenever config files get updated or other relevant changes occur. If you set `update['frequency']` to 'always', the update runs on every Puppet run. You can also set `update['frequency']` to 'daily' or 'weekly':

```puppet
class { 'apt':
Expand All @@ -96,6 +105,7 @@ class { 'apt':
},
}
```

When `Exec['apt_update']` is triggered, it generates a `notice`-level message. Because the default [logging level for agents](https://puppet.com/docs/puppet/latest/configuration.html#loglevel) is `notice`, this causes the repository update to appear in agent logs. To silence these updates from the default log output, set the [loglevel](https://puppet.com/docs/puppet/latest/metaparameter.html#loglevel) metaparameter for `Exec['apt_update']` above the agent logging level:

```puppet
Expand All @@ -110,6 +120,7 @@ class { 'apt':
> **NOTE:** Every `Exec['apt_update']` run will generate a corrective change, even if the apt caches are not updated. For example, setting an update frequency of `always` can result in every Puppet run resulting in a corrective change. This is a known issue. For details, see [MODULES-10763](https://tickets.puppetlabs.com/browse/MODULES-10763).
<a id="pin-a-specific-release"></a>

### Pin a specific release

```puppet
Expand All @@ -133,6 +144,7 @@ apt::pin { 'stable':
To pin multiple packages, pass them to the `packages` parameter as an array or a space-delimited string.

<a id="add-a-personal-package-archive-repository"></a>

### Add a Personal Package Archive (PPA) repository

```puppet
Expand Down Expand Up @@ -173,6 +185,7 @@ apt::source { 'puppetlabs':
```

<a id="configure-apt-from-hiera"></a>

### Configure Apt from Hiera

Instead of specifying your sources directly as resources, you can instead just include the `apt` class, which will pick up the values automatically from hiera.
Expand Down Expand Up @@ -201,36 +214,37 @@ apt::sources:
```
<a id="replace-the-default-sourceslist-file"></a>
### Replace the default `sources.list` file

The following example replaces the default `/etc/apt/sources.list`. Along with this code, be sure to use the `purge` parameter, or you might get duplicate source warnings when running Apt.

```puppet
apt::source { "archive.ubuntu.com-${lsbdistcodename}":
apt::source { "archive.ubuntu.com-${facts['os']['distro']['codename']}":
location => 'http://archive.ubuntu.com/ubuntu',
key => '630239CC130E1A7FD81A27B140976EAF437D05B5',
repos => 'main universe multiverse restricted',
}
apt::source { "archive.ubuntu.com-${lsbdistcodename}-security":
apt::source { "archive.ubuntu.com-${facts['os']['distro']['codename']}-security":
location => 'http://archive.ubuntu.com/ubuntu',
key => '630239CC130E1A7FD81A27B140976EAF437D05B5',
repos => 'main universe multiverse restricted',
release => "${lsbdistcodename}-security"
release => "${facts['os']['distro']['codename']}-security"
}
apt::source { "archive.ubuntu.com-${lsbdistcodename}-updates":
apt::source { "archive.ubuntu.com-${facts['os']['distro']['codename']}-updates":
location => 'http://archive.ubuntu.com/ubuntu',
key => '630239CC130E1A7FD81A27B140976EAF437D05B5',
repos => 'main universe multiverse restricted',
release => "${lsbdistcodename}-updates"
release => "${facts['os']['distro']['codename']}-updates"
}
apt::source { "archive.ubuntu.com-${lsbdistcodename}-backports":
apt::source { "archive.ubuntu.com-${facts['os']['distro']['codename']}-backports":
location => 'http://archive.ubuntu.com/ubuntu',
key => '630239CC130E1A7FD81A27B140976EAF437D05B5',
repos => 'main universe multiverse restricted',
release => "${lsbdistcodename}-backports"
release => "${facts['os']['distro']['codename']}-backports"
}
```

Expand Down Expand Up @@ -264,6 +278,7 @@ class { 'apt':
```

<a id="reference"></a>

## Reference

### Facts
Expand All @@ -288,7 +303,8 @@ class { 'apt':

See [REFERENCE.md](https://github.com/puppetlabs/puppetlabs-apt/blob/main/REFERENCE.md) for all other reference documentation.

<a id="limitations"></a>
<a id="limitations"></a>

## Limitations

This module is not designed to be split across [run stages](https://docs.puppetlabs.com/puppet/latest/reference/lang_run_stages.html).
Expand All @@ -306,7 +322,7 @@ Class['apt::update'] -> Package <| provider == 'apt' |>
## Development

Acceptance tests for this module leverage [puppet_litmus](https://github.com/puppetlabs/puppet_litmus).
To run the acceptance tests follow the instructions [here](https://github.com/puppetlabs/puppet_litmus/wiki/Tutorial:-use-Litmus-to-execute-acceptance-tests-with-a-sample-module-(MoTD)#install-the-necessary-gems-for-the-module).
To run the acceptance tests follow the instructions [here](https://puppetlabs.github.io/litmus/Running-acceptance-tests.html).
You can also find a tutorial and walkthrough of using Litmus and the PDK on [YouTube](https://www.youtube.com/watch?v=FYfR7ZEGHoE).

If you run into an issue with this module, or if you would like to request a feature, please [file a ticket](https://tickets.puppetlabs.com/browse/MODULES/).
Expand Down
10 changes: 5 additions & 5 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Default value: ``undef``
Data type: `Optional[String]`

Specifies a distribution of the Apt repository containing the backports to manage. Used in populating the `source.list` configuration file.
Default: on Debian and Ubuntu, '${lsbdistcodename}-backports'. We recommend keeping this default, except on other operating
Default: on Debian and Ubuntu, `${facts['os']['distro']['codename']}-backports`. We recommend keeping this default, except on other operating
systems.

Default value: ``undef``
Expand Down Expand Up @@ -729,19 +729,19 @@ Default value: `$::apt::ppa_options`

Data type: `Optional[String]`

Optional if lsb-release is installed (unless you're using a different release than indicated by lsb-release, e.g., Linux Mint).
Specifies the operating system of your node. Valid options: a string containing a valid LSB distribution codename.
Optional if `puppet facts show os.distro.codename` returns your correct distribution release codename.

Default value: `$facts['lsbdistcodename']`
Default value: `$facts['os']['distro']['codename']`

##### `dist`

Data type: `Optional[String]`

Optional if lsb-release is installed (unless you're using a different release than indicated by lsb-release, e.g., Linux Mint).
Specifies the distribution of your node. Valid options: a string containing a valid distribution codename.
Optional if `puppet facts show os.name` returns your correct distribution name.

Default value: `$facts['lsbdistid']`
Default value: `$facts['os']['name']`

##### `package_name`

Expand Down
4 changes: 2 additions & 2 deletions examples/backports.pp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Set up a backport for linuxmint qiana
# Set up a backport for Linux Mint qiana
class { 'apt': }
apt::backports { 'qiana':
class { 'apt::backports':
location => 'http://us.archive.ubuntu.com/ubuntu',
release => 'trusty-backports',
repos => 'main universe multiverse restricted',
Expand Down
2 changes: 1 addition & 1 deletion examples/source.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class { 'apt': }

# Install the puppetlabs apt source
# Release is automatically obtained from lsbdistcodename fact if available.
# Release is automatically obtained from facts.
apt::source { 'puppetlabs':
location => 'http://apt.puppetlabs.com',
repos => 'main',
Expand Down
8 changes: 4 additions & 4 deletions locales/ja/puppetlabs-apt.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Copyright (C) 2018 Puppet, Inc.
# This file is distributed under the same license as the puppetlabs-apt package.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2018.
#
#
# #-#-#-#-# puppetlabs-apt_metadata.pot (PACKAGE VERSION) #-#-#-#-#
#
#
#, fuzzy
msgid ""
msgstr ""
Expand Down Expand Up @@ -69,8 +69,8 @@ msgid "parameters release and origin are mutually exclusive"
msgstr "パラメータreleaseとoriginは相互に排他的です。"

#. ./manifests/ppa.pp:30 ./manifests/source.pp:79
msgid "lsbdistcodename fact not available: release parameter required"
msgstr "lsbdistcodenameのfactが使用できません。releaseパラメータが必要です。"
msgid "os.distro.codename fact not available: release parameter required"
msgstr "os.distro.codenameのfactが使用できません。releaseパラメータが必要です。"

#. ./manifests/ppa.pp:34
msgid "apt::ppa is not currently supported on Debian."
Expand Down
2 changes: 1 addition & 1 deletion locales/puppetlabs-apt.pot
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ msgid "parameters release and origin are mutually exclusive"
msgstr ""

#. ./manifests/ppa.pp:30 ./manifests/source.pp:79
msgid "lsbdistcodename fact not available: release parameter required"
msgid "os.distro.codename fact not available: release parameter required"
msgstr ""

#. ./manifests/ppa.pp:34
Expand Down
21 changes: 10 additions & 11 deletions manifests/backports.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
# }
#
# @param location
# Specifies an Apt repository containing the backports to manage. Valid options: a string containing a URL. Default value for Debian and
# Specifies an Apt repository containing the backports to manage. Valid options: a string containing a URL. Default value for Debian and
# Ubuntu varies:
#
# - Debian: 'http://deb.debian.org/debian'
#
# - Ubuntu: 'http://archive.ubuntu.com/ubuntu'
#
# @param release
# Specifies a distribution of the Apt repository containing the backports to manage. Used in populating the `source.list` configuration file.
# Default: on Debian and Ubuntu, '${lsbdistcodename}-backports'. We recommend keeping this default, except on other operating
# Specifies a distribution of the Apt repository containing the backports to manage. Used in populating the `source.list` configuration file.
# Default: on Debian and Ubuntu, `${facts['os']['distro']['codename']}-backports`. We recommend keeping this default, except on other operating
# systems.
#
# @param repos
# Specifies a component of the Apt repository containing the backports to manage. Used in populating the `source.list` configuration file.
# Specifies a component of the Apt repository containing the backports to manage. Used in populating the `source.list` configuration file.
# Default value for Debian and Ubuntu varies:
#
#
# - Debian: 'main contrib non-free'
#
# - Ubuntu: 'main universe multiverse restricted'
#
# @param key
# Specifies a key to authenticate the backports. Valid options: a string to be passed to the id parameter of the apt::key defined type, or a
# Specifies a key to authenticate the backports. Valid options: a string to be passed to the id parameter of the apt::key defined type, or a
# hash of parameter => value pairs to be passed to apt::key's id, server, content, source, and/or options parameters. Default value
# for Debian and Ubuntu varies:
#
Expand All @@ -42,7 +42,7 @@
# - Ubuntu: '630239CC130E1A7FD81A27B140976EAF437D05B5'
#
# @param pin
# Specifies a pin priority for the backports. Valid options: a number or string to be passed to the `id` parameter of the `apt::pin` defined
# Specifies a pin priority for the backports. Valid options: a number or string to be passed to the `id` parameter of the `apt::pin` defined
# type, or a hash of `parameter => value` pairs to be passed to `apt::pin`'s corresponding parameters.
#
# @param include
Expand All @@ -55,8 +55,7 @@
Optional[Variant[String, Hash]] $key = undef,
Optional[Variant[Integer, String, Hash]] $pin = 200,
Optional[Variant[Hash]] $include = {},
){

) {
include apt

if $location {
Expand All @@ -71,7 +70,7 @@
if $key {
$_key = $key
}
if (!($facts['lsbdistid'] == 'Debian' or $facts['lsbdistid'] == 'Ubuntu')) {
if (!($facts['os']['name'] == 'Debian' or $facts['os']['name'] == 'Ubuntu')) {
unless $location and $release and $repos and $key {
fail(translate('If not on Debian or Ubuntu, you must explicitly pass location, release, repos, and key'))
}
Expand All @@ -80,7 +79,7 @@
$_location = $::apt::backports['location']
}
unless $release {
$_release = "${facts['lsbdistcodename']}-backports"
$_release = "${facts['os']['distro']['codename']}-backports"
}
unless $repos {
$_repos = $::apt::backports['repos']
Expand Down
2 changes: 1 addition & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
Boolean $sources_list_force = $apt::params::sources_list_force,
) inherits apt::params {

if $facts['osfamily'] != 'Debian' {
if $facts['os']['family'] != 'Debian' {
fail(translate('This module only works on Debian or derivatives like Ubuntu'))
}

Expand Down
4 changes: 2 additions & 2 deletions manifests/params.pp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# @summary Provides defaults for the Apt module parameters.
#
#
# @api private
#
class apt::params {

if $::osfamily != 'Debian' {
if $facts['os']['family'] != 'Debian' {
fail(translate('This module only works on Debian or derivatives like Ubuntu'))
}

Expand Down
Loading

0 comments on commit 18c44ad

Please sign in to comment.