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