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