ef37f52db04f1bfa19da690bb054b51c6cbda897
[puppet-modules/puppetlabs-apt.git] / manifests / update.pp
1 # @summary Updates the list of available packages using apt-get update.
2 #
3 # @api private
4 #
5 class apt::update {
6   assert_private()
7
8   #TODO: to catch if $apt_update_last_success has the value of -1 here. If we
9   #opt to do this, a info/warn would likely be all you'd need likely to happen
10   #on the first run, but if it's not run in awhile something is likely borked
11   #with apt and we'd want to know about it.
12
13   case $apt::_update['frequency'] {
14     'always': {
15       $_kick_apt = true
16     }
17     'daily': {
18       #compare current date with the apt_update_last_success fact to determine
19       #if we should kick apt_update.
20       $daily_threshold = (Integer(Timestamp().strftime('%s')) - 86400)
21       if $apt::apt_update_last_success {
22         if $apt::apt_update_last_success + 0 < $daily_threshold {
23           $_kick_apt = true
24         } else {
25           $_kick_apt = false
26         }
27       } else {
28         #if apt-get update has not successfully run, we should kick apt_update
29         $_kick_apt = true
30       }
31     }
32     'weekly':{
33       #compare current date with the apt_update_last_success fact to determine
34       #if we should kick apt_update.
35       $weekly_threshold = (Integer(Timestamp().strftime('%s')) - 604800)
36       if $apt::apt_update_last_success {
37         if ( $apt::apt_update_last_success + 0 < $weekly_threshold ) {
38           $_kick_apt = true
39         } else {
40           $_kick_apt = false
41         }
42       } else {
43         #if apt-get update has not successfully run, we should kick apt_update
44         $_kick_apt = true
45       }
46     }
47     default: {
48       #catches 'reluctantly', and any other value (which should not occur).
49       #do nothing.
50       $_kick_apt = false
51     }
52   }
53
54   if $_kick_apt {
55     $_refresh = false
56   } else {
57     $_refresh = true
58   }
59   exec { 'apt_update':
60     command     => "${apt::provider} update",
61     loglevel    => $apt::_update['loglevel'],
62     logoutput   => 'on_failure',
63     refreshonly => $_refresh,
64     timeout     => $apt::_update['timeout'],
65     tries       => $apt::_update['tries'],
66     try_sleep   => 1,
67   }
68 }