From 5ed23e5267f08582453b0da8cb5f14ac97e7ec60 Mon Sep 17 00:00:00 2001
From: "A. Karl Kornel" <akkornel@stanford.edu>
Date: Mon, 12 Sep 2016 12:51:56 -0700
Subject: [PATCH] debian: Move Aptitude code into a new phase

All of the aptitude code gets moved into a new class, which gets put into its own phase of the Puppet process.
---
 manifests/os/debian.pp | 202 ++++++++++++++++++++++-------------------
 1 file changed, 110 insertions(+), 92 deletions(-)

diff --git a/manifests/os/debian.pp b/manifests/os/debian.pp
index c6a30df..609f218 100644
--- a/manifests/os/debian.pp
+++ b/manifests/os/debian.pp
@@ -36,98 +36,26 @@ class base::os::debian (
 ){
   include base::newsyslog
 
-  # This really needs to be put somewhere else so that all possible uses of
-  # package inherit from it.  Here, it only affects this particular class.
-  Package {
-    require => [ File['/etc/apt/apt.conf.d/10recommends'],
-                  File['/etc/apt/preferences'],
-                  File['/etc/apt/preferences.d'],
-                  File['/etc/apt/sources.list'],
-                  File['/etc/apt/sources.list.d'] ]
+  # Do all of the apt configuration in a separate stage, to ensure that it
+  # completes before anything else.
+  stage { 'aptitude':
+    before => Stage['main'],
+  }
+  class { 'base::os::debian::apt':
+    apt_cache_notin_tmp => $apt_cache_notin_tmp,
+    apt_cache_tmp_dir   => $apt_cache_tmp_dir,
+    stage               => 'aptitude',
   }
 
   # Install basic configuration files.
   file {
-    '/etc/apt/apt.conf.d/10recommends':
-      source => 'puppet:///modules/base/os/etc/apt/apt.conf.d/10recommends';
     '/etc/default/rcS':
       source => 'puppet:///modules/base/os/etc/default/rcS';
     '/etc/filter-syslog/debian':
       source => 'puppet:///modules/base/os/etc/filter-syslog/debian';
   }
 
-  # On wheezy, for right now we have to disable pdiffs due to problems with
-  # the Translation files.
-  if $::lsbdistcodename == 'wheezy' {
-    file { '/etc/apt/apt.conf.d/30no-pdiffs':
-      source => 'puppet:///modules/base/os/etc/apt/apt.conf.d/30no-pdiffs',
-    }
-  }
-
-  # Install APT sources configuration.  This is generally handled via
-  # templates.
-  # NOTE: We hold off on messing with the master sources list until we've
-  # created the other sources files.  We do this to ensure that a source
-  # never gets removed entirely.  Luckily, `aptitude update` doesn't error
-  # out if a source is listed multiple times.
-  file {
-    '/etc/apt/sources.list.d':
-      ensure  => 'directory',
-      recurse => true,
-      purge   => true,
-      notify  => Exec['aptitude update'];
-    '/etc/apt/sources.list.d/backports.list':
-      content => template('base/os/sources/backports.list.erb'),
-      notify  => Exec['aptitude update'];
-    '/etc/apt/sources.list.d/stanford.list':
-      content => template('base/os/sources/stanford.list.erb'),
-      notify  => Exec['aptitude update'];
-    '/etc/apt/sources.list':
-      content => template('base/os/sources/sources.list.erb'),
-      require => [
-                  File['/etc/apt/sources.list.d/backports.list'],
-                  File['/etc/apt/sources.list.d/stanford.list'],
-                 ],
-      notify  => Exec['aptitude update'];
-  }
-
-  # Install APT preferences.  We should never use /etc/apt/preferences
-  # since the preferences.d directory is supported
-  file { '/etc/apt/preferences.d':
-    ensure  => directory,
-    recurse => true,
-    purge   => true,
-  }
-  if $::lsbdistcodename == 'wheezy' {
-    file { '/etc/apt/preferences.d/rsyslog':
-      content => template('base/os/preferences/rsyslog.erb')
-    }
-  }
-  file {
-    '/etc/apt/preferences':
-      content => '';
-    '/etc/apt/preferences.d/backports':
-      content => template('base/os/preferences/backports.erb');
-  }
-
-  if ($apt_cache_notin_tmp) {
-    # If we did NOT override the apt cache directory make sure that
-    # '/var/cache/apt/tmp' exists.
-    if ($apt_cache_tmp_dir == '/var/cache/apt/tmp') {
-      file { $apt_cache_tmp_dir:
-        ensure  => directory,
-      }
-    }
-
-    file { '/etc/apt/apt.conf.d/apt_cache_tmp':
-      content => template('base/os/etc/apt/apt.conf.d/apt_cache_tmp.erb'),
-      require => File[$apt_cache_tmp_dir],
-    }
-  }
-
-  # lsb-release pulls in all of lsb unless we disable recommends handling
-  # first, so make sure that we've done that.  That should be handled by the
-  # global Package require set above.
+  # Bring in common packages.
   package {
     'bsd-mailx':       ensure => present;
     'dmidecode':       ensure => present;
@@ -135,9 +63,6 @@ class base::os::debian (
     'locate':          ensure => present;
     'lsb-release':     ensure => present;
     'kstart':          ensure => present;
-    'stanford-keyring':
-      ensure => present,
-      notify => Exec['aptitude update'];
     'stanford-server': ensure => present;
   }
 
@@ -189,13 +114,6 @@ class base::os::debian (
       mode   => '0755';
   }
 
-  # Triggered to refresh local package lists.
-  exec { 'aptitude update':
-    command     => 'aptitude update',
-    path        => '/usr/bin',
-    refreshonly => true,
-  }
-
   # allow non-root users to use ping in Jessie
   if ($::lsbdistcodename == 'jessie') {
     exec { 'setcap ping':
@@ -204,5 +122,105 @@ class base::os::debian (
       unless  => "getcap /bin/ping | grep -q 'cap_net_raw+ep'",
     }
   }
+}
+
+
+# base::os::debian::apt has all of the Puppet code to set up our package
+# sources, preferences, etc.  It is meant to be run in its own phase, before
+# the main phase.  This is needed so that we can install packages without the
+# possibility of weird aptitude errors.
+class base::os::debian::apt (
+  $apt_cache_notin_tmp = false,
+  $apt_cache_tmp_dir   = '/var/cache/apt/tmp'
+){
+
+  # Define aptitude update as a command we can run
+  exec { 'aptitude update':
+    command     => 'aptitude update',
+    path        => '/usr/bin',
+    refreshonly => true,
+  }
+
+  # Install basic apt configuration files.
+  # Make sure this gets created BEFORE `aptitude update` runs.
+  file { '/etc/apt/apt.conf.d/10recommends':
+    source => 'puppet:///modules/base/os/etc/apt/apt.conf.d/10recommends',
+    before => Exec['aptitude update'],
+  }
+
+  # If aptitude is using something other than /tmp, set that up first
+  if ($apt_cache_notin_tmp) {
+    # If we did NOT override the apt cache directory make sure that
+    # '/var/cache/apt/tmp' exists.
+    if ($apt_cache_tmp_dir == '/var/cache/apt/tmp') {
+      file { $apt_cache_tmp_dir:
+        ensure  => directory,
+      }
+    }
+
+    # Create the configuration file to set our apt temp directory
+    # Make sure this gets created BEFORE `aptitude update`.
+    file { '/etc/apt/apt.conf.d/apt_cache_tmp':
+      content => template('base/os/etc/apt/apt.conf.d/apt_cache_tmp.erb'),
+      require => File[$apt_cache_tmp_dir],
+      before  => Exec['aptitude update'],
+    }
+  }
+
+  # On wheezy, for right now we have to disable pdiffs due to problems with
+  # the Translation files.
+  if $::lsbdistcodename == 'wheezy' {
+    file { '/etc/apt/apt.conf.d/30no-pdiffs':
+      source => 'puppet:///modules/base/os/etc/apt/apt.conf.d/30no-pdiffs',
+      before => Exec['aptitude update'],
+    }
+  }
+
+  # Install APT sources configuration.  This is generally handled via
+  # templates.
+  file {
+    '/etc/apt/sources.list':
+      content => template('base/os/sources/sources.list.erb'),
+      notify  => Exec['aptitude update'];
+    '/etc/apt/sources.list.d':
+      ensure  => 'directory',
+      recurse => true,
+      purge   => true,
+      notify  => Exec['aptitude update'];
+    '/etc/apt/sources.list.d/backports.list':
+      content => template('base/os/sources/backports.list.erb'),
+      notify  => Exec['aptitude update'];
+    '/etc/apt/sources.list.d/stanford.list':
+      content => template('base/os/sources/stanford.list.erb'),
+      notify  => Exec['aptitude update'];
+  }
+
+  # Install APT preferences.  We should never use /etc/apt/preferences
+  # since the preferences.d directory is supported
+  file { '/etc/apt/preferences.d':
+    ensure  => directory,
+    recurse => true,
+    purge   => true,
+  }
+  if $::lsbdistcodename == 'wheezy' {
+    file { '/etc/apt/preferences.d/rsyslog':
+      content => template('base/os/preferences/rsyslog.erb'),
+      notify  => Exec['aptitude update'],
+    }
+  }
+  file {
+    '/etc/apt/preferences':
+      content => '',
+      notify  => Exec['aptitude update'];
+    '/etc/apt/preferences.d/backports':
+      content => template('base/os/preferences/backports.erb'),
+      notify  => Exec['aptitude update'];
+  }
 
+  # Install the stanford-keyring package
+  package { 'stanford-keyring':
+    require         => File['/etc/apt/sources.list.d/stanford.list'],
+    install_options => [ '-y' ],
+    notify          => Exec['aptitude update'],
+  }
 }
-- 
GitLab