~ubuntu-branches/ubuntu/quantal/puppet/quantal

« back to all changes in this revision

Viewing changes to .pc/puppet-12844/lib/puppet/agent.rb

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-07-14 01:56:30 UTC
  • mfrom: (1.1.29) (3.1.43 sid)
  • Revision ID: package-import@ubuntu.com-20120714015630-ntj41rkvkq4zph4y
Tags: 2.7.18-1ubuntu1
* Resynchronise with Debian. (LP: #1023931) Remaining changes:
  - debian/puppetmaster-passenger.postinst: Make sure we error if puppet
    config print doesn't work
  - debian/puppetmaster-passenger.postinst: Ensure upgrades from
    <= 2.7.11-1 fixup passenger apache configuration.
* Dropped upstreamed patches:
  - debian/patches/CVE-2012-1906_CVE-2012-1986_to_CVE-2012-1989.patch
  - debian/patches/puppet-12844
  - debian/patches/2.7.17-Puppet-July-2012-CVE-fixes.patch
* Drop Build-Depends on ruby-rspec (in universe):
  - debian/control: remove ruby-rspec from Build-Depends
  - debian/patches/no-rspec.patch: make Rakefile work anyway if rspec
    isn't installed so we can use it in debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'sync'
2
 
require 'puppet/external/event-loop'
3
 
require 'puppet/application'
4
 
 
5
 
# A general class for triggering a run of another
6
 
# class.
7
 
class Puppet::Agent
8
 
  require 'puppet/agent/locker'
9
 
  include Puppet::Agent::Locker
10
 
 
11
 
  require 'puppet/agent/disabler'
12
 
  include Puppet::Agent::Disabler
13
 
 
14
 
  attr_reader :client_class, :client, :splayed
15
 
 
16
 
  # Just so we can specify that we are "the" instance.
17
 
  def initialize(client_class)
18
 
    @splayed = false
19
 
 
20
 
    @client_class = client_class
21
 
  end
22
 
 
23
 
  def lockfile_path
24
 
    client_class.lockfile_path
25
 
  end
26
 
 
27
 
  def needing_restart?
28
 
    Puppet::Application.restart_requested?
29
 
  end
30
 
 
31
 
  # Perform a run with our client.
32
 
  def run(*args)
33
 
    if running?
34
 
      Puppet.notice "Run of #{client_class} already in progress; skipping"
35
 
      return
36
 
    end
37
 
    if disabled?
38
 
      Puppet.notice "Skipping run of #{client_class}; administratively disabled: #{disable_message}"
39
 
      return
40
 
    end
41
 
    result = nil
42
 
    block_run = Puppet::Application.controlled_run do
43
 
      splay
44
 
      with_client do |client|
45
 
        begin
46
 
          sync.synchronize { lock { result = client.run(*args) } }
47
 
        rescue SystemExit,NoMemoryError
48
 
          raise
49
 
        rescue Exception => detail
50
 
          puts detail.backtrace if Puppet[:trace]
51
 
          Puppet.err "Could not run #{client_class}: #{detail}"
52
 
        end
53
 
      end
54
 
      true
55
 
    end
56
 
    Puppet.notice "Shutdown/restart in progress; skipping run" unless block_run
57
 
    result
58
 
  end
59
 
 
60
 
  def stopping?
61
 
    Puppet::Application.stop_requested?
62
 
  end
63
 
 
64
 
  # Have we splayed already?
65
 
  def splayed?
66
 
    splayed
67
 
  end
68
 
 
69
 
  # Sleep when splay is enabled; else just return.
70
 
  def splay
71
 
    return unless Puppet[:splay]
72
 
    return if splayed?
73
 
 
74
 
    time = rand(Integer(Puppet[:splaylimit]) + 1)
75
 
    Puppet.info "Sleeping for #{time} seconds (splay is enabled)"
76
 
    sleep(time)
77
 
    @splayed = true
78
 
  end
79
 
 
80
 
  # Start listening for events.  We're pretty much just listening for
81
 
  # timer events here.
82
 
  def start
83
 
    # Create our timer.  Puppet will handle observing it and such.
84
 
    timer = EventLoop::Timer.new(:interval => Puppet[:runinterval], :tolerance => 1, :start? => true) do
85
 
      run
86
 
    end
87
 
 
88
 
    # Run once before we start following the timer
89
 
    timer.sound_alarm
90
 
  end
91
 
 
92
 
  def sync
93
 
    @sync ||= Sync.new
94
 
  end
95
 
 
96
 
  private
97
 
 
98
 
  # Create and yield a client instance, keeping a reference
99
 
  # to it during the yield.
100
 
  def with_client
101
 
    begin
102
 
      @client = client_class.new
103
 
    rescue SystemExit,NoMemoryError
104
 
      raise
105
 
    rescue Exception => detail
106
 
      puts detail.backtrace if Puppet[:trace]
107
 
      Puppet.err "Could not create instance of #{client_class}: #{detail}"
108
 
      return
109
 
    end
110
 
    yield @client
111
 
  ensure
112
 
    @client = nil
113
 
  end
114
 
end