apt: Change how update is managed.
[puppet-modules/puppetlabs-apt.git] / manifests / update.pp
1 class apt::update {
2   #TODO: to catch if $::apt_update_last_success has the value of -1 here. If we
3   #opt to do this, a info/warn would likely be all you'd need likely to happen
4   #on the first run, but if it's not run in awhile something is likely borked
5   #with apt and we'd want to know about it.
6
7   if $::apt::_update['always'] == false {
8     #if always_apt_update is true there's no point in parsing this logic.
9
10     case $::apt::_update['frequency'] {
11       'always': {
12         $_kick_apt = true
13       }
14       'daily': {
15         #compare current date with the apt_update_last_success fact to determine
16         #if we should kick apt_update.
17         $daily_threshold = (strftime('%s') - 86400)
18         if $::apt_update_last_success {
19           if $::apt_update_last_success < $daily_threshold {
20             $_kick_apt = true
21           } else {
22             $_kick_apt = false
23           }
24         } else {
25           #if apt-get update has not successfully run, we should kick apt_update
26           $_kick_apt = true
27         }
28       }
29       'weekly':{
30         #compare current date with the apt_update_last_success fact to determine
31         #if we should kick apt_update.
32         $weekly_threshold = (strftime('%s') - 604800)
33         if $::apt_update_last_success {
34           if ( $::apt_update_last_success < $weekly_threshold ) {
35             $_kick_apt = true
36           } else {
37             $_kick_apt = false
38           }
39         } else {
40           #if apt-get update has not successfully run, we should kick apt_update
41           $_kick_apt = true
42         }
43       }
44       default: {
45         #catches 'recluctantly', and any other value (which should not occur).
46         #do nothing.
47         $_kick_apt = false
48       }
49     }
50   } else {
51     $_kick_apt = false
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     logoutput   => 'on_failure',
62     refreshonly => $_refresh,
63     timeout     => $::apt::_update['timeout'],
64     tries       => $::apt::_update['tries'],
65     try_sleep   => 1
66   }
67 }