Class: PuppetAgentMgr::V3::Manager

Inherits:
Object
  • Object
show all
Includes:
Common, Unix, Windows
Defined in:
lib/puppet_agent_mgr/v3/manager.rb

Instance Method Summary collapse

Methods included from Common

#atomic_file, #create_common_puppet_cli, #enabled?, #idling?, #managed_resources_count, #managing_resource?, #run_in_background, #run_in_foreground, #runonce!, #seconds_to_human, #since_lastrun, #status, #stopped?, #validate_name

Methods included from Unix

#applying?, #daemon_present?, #has_process_for_pid?, #signal_running_daemon

Constructor Details

#initialize(testing = false) ⇒ Manager

Returns a new instance of Manager.



13
14
15
16
17
18
19
20
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 13

def initialize(testing=false)
  unless testing || Puppet.settings.app_defaults_initialized?
    require 'puppet/util/run_mode'
    Puppet.settings.preferred_run_mode = :agent
    Puppet.settings.initialize_global_settings
    Puppet.settings.initialize_app_defaults(Puppet::Settings.app_defaults_for_run_mode(Puppet.run_mode))
  end
end

Instance Method Details

#disable!(msg = nil) ⇒ Object

disable the puppet agent, on version 2.x the message is ignored



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 29

def disable!(msg=nil)
  raise "Already disabled" unless enabled?

  msg = "Disabled using the Ruby API at %s" % Time.now.strftime("%c") unless msg

  atomic_file(Puppet[:agent_disabled_lockfile]) do |f|
    f.print(JSON.dump(:disabled_message => msg))
  end

  msg
end

#disabled?Boolean

is the agent disabled

Returns:

  • (Boolean)


69
70
71
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 69

def disabled?
  File.exist?(Puppet[:agent_disabled_lockfile])
end

#enable!Object

enables the puppet agent, it can now start applying catalogs again



23
24
25
26
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 23

def enable!
  raise "Already enabled" if enabled?
  File.unlink(Puppet[:agent_disabled_lockfile])
end

#lastrunObject

epoch time when the last catalog was applied



52
53
54
55
56
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 52

def lastrun
  summary = load_summary

  Integer(summary["time"].fetch("last_run", 0))
end

#load_summaryObject

loads the summary file and makes sure that some keys are always present



74
75
76
77
78
79
80
81
82
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 74

def load_summary
  summary = {"changes" => {}, "time" => {}, "resources" => {}, "version" => {}, "events" => {}}

  summary.merge!(YAML.load_file(Puppet[:lastrunfile])) if File.exist?(Puppet[:lastrunfile])

  summary["resources"] = {"failed"=>0, "changed"=>0, "total"=>0, "restarted"=>0, "out_of_sync"=>0}.merge!(summary["resources"])

  summary
end

#lock_messageObject

the current lock message, always “” on 2.0



59
60
61
62
63
64
65
66
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 59

def lock_message
  if disabled?
    lock_data = JSON.parse(File.read(Puppet[:agent_disabled_lockfile]))
    return lock_data["disabled_message"]
  else
    return ""
  end
end

#managed_resourcesObject

all the managed resources



42
43
44
45
46
47
48
49
# File 'lib/puppet_agent_mgr/v3/manager.rb', line 42

def managed_resources
  # need to add some caching here based on mtime of the resources file
  return [] unless File.exist?(Puppet[:resourcefile])

  File.readlines(Puppet[:resourcefile]).map do |resource|
    resource.chomp
  end
end