summaryrefslogtreecommitdiffstats
path: root/lib/puppet/agent/runner.rb
blob: 705b6c269d0be344a8862bffade17d2c6efa1f96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'puppet/agent'
require 'puppet/configurer'
require 'puppet/indirector'

# A basic class for running the agent.  Used by
# puppetrun to kick off agents remotely.
class Puppet::Agent::Runner
    extend Puppet::Indirector
    indirects :runner, :terminus_class => :rest

    attr_reader :status, :background, :options

    def agent
        Puppet::Agent.new(Puppet::Configurer)
    end

    def background?
        background
    end

    def initialize(options = {})
        if options.include?(:background)
            @background = options[:background]
            options.delete(:background)
        end

        valid_options = [:tags, :ignoreschedules]
        options.each do |key, value|
            raise ArgumentError, "Runner does not accept %s" % key unless valid_options.include?(key)
        end

        @options = options
    end

    def log_run
        msg = ""
        msg += "triggered run" %
        if options[:tags]
            msg += " with tags %s" % options[:tags]
        end

        if options[:ignoreschedules]
            msg += " ignoring schedules"
        end

        Puppet.notice msg
    end

    def run
        if agent.running?
            @status = "running"
            return
        end

        log_run()

        if background?
            Thread.new { agent.run(options) }
        else
            agent.run(options)
        end

        @status = "success"
    end
end