Module: PuppetAgentMgr::Common

Extended by:
Common
Included in:
Common, V2::Manager, V3::Manager
Defined in:
lib/puppet_agent_mgr/common.rb

Instance Method Summary collapse

Instance Method Details

#atomic_file(file) {|tempfile| ... } ⇒ Object

Yields:

  • (tempfile)


173
174
175
176
177
178
179
180
# File 'lib/puppet_agent_mgr/common.rb', line 173

def atomic_file(file)
  tempfile = Tempfile.new(File.basename(file), File.dirname(file))

  yield(tempfile)

  tempfile.close
  File.rename(tempfile.path, file)
end

#create_common_puppet_cli(noop = nil, tags = [], environment = nil, server = nil, splay = nil, splaylimit = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet_agent_mgr/common.rb', line 61

def create_common_puppet_cli(noop=nil, tags=[], environment=nil, server=nil, splay=nil, splaylimit=nil)
  opts = []
  tags = [tags].flatten.compact

  (host, port) = server.to_s.split(":")

  raise("Invalid hostname '%s' specified" % host) if host && !(host =~ /\A(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])\Z/)
  raise("Invalid master port '%s' specified" % port) if port && !(port =~ /\A\d+\Z/)
  raise("Invalid environment '%s' specified" % environment) if environment && !validate_name(environment)
  raise("Invalid splaylimit '%s' specified" % splaylimit) if splaylimit && !splaylimit.is_a?(Fixnum)

  unless tags.empty?
    [tags].flatten.each do |tag|
      tag.split("::").each do |part|
        raise("Invalid tag '%s' specified" % tag) unless validate_name(part)
      end
    end

    opts << "--tags %s" % tags.join(",") if !tags.empty?
  end

  opts << "--splay" if splay == true
  opts << "--no-splay" if splay == false
  opts << "--splaylimit %s" % splaylimit if splaylimit
  opts << "--noop" if noop == true
  opts << "--no-noop" if noop == false
  opts << "--environment %s" % environment if environment
  opts << "--server %s" % host if host
  opts << "--masterport %s" % port if port

  opts
end

#enabled?Boolean

is the agent enabled

Returns:

  • (Boolean)


16
17
18
# File 'lib/puppet_agent_mgr/common.rb', line 16

def enabled?
  !disabled?
end

#idling?Boolean

is the daemon running but not applying a catalog

Returns:

  • (Boolean)


11
12
13
# File 'lib/puppet_agent_mgr/common.rb', line 11

def idling?
  (daemon_present? && !applying?)
end

#managed_resources_countObject

how many resources are managed



31
32
33
# File 'lib/puppet_agent_mgr/common.rb', line 31

def managed_resources_count
  managed_resources.size
end

#managing_resource?(resource) ⇒ Boolean

if a resource is being managed, resource in the syntax File etc

Returns:

  • (Boolean)


26
27
28
# File 'lib/puppet_agent_mgr/common.rb', line 26

def managing_resource?(resource)
  managed_resources.include?(resource.downcase)
end

#run_in_background(clioptions, execute = true) ⇒ Object



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

def run_in_background(clioptions, execute=true)
  options =["--onetime", "--daemonize", "--color=false"].concat(clioptions)

  return options unless execute

  %x[puppet agent #{options.join(' ')}]
end

#run_in_foreground(clioptions, execute = true) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/puppet_agent_mgr/common.rb', line 35

def run_in_foreground(clioptions, execute=true)
  options = ["--test", "--color=false"].concat(clioptions)

  return options unless execute

  %x[puppet agent #{options.join(' ')}]
end

#runonce!(options = {}) ⇒ Object

do a run based on the following options:

:foreground_run - runs in the foreground a –test run :signal_daemon - if the daemon is running, sends it USR1 to wake it up :noop - enables or disabled noop mode based on true/false :tags - an array of tags to limit the run to :environment - the environment to run :server - the puppet master to use, can be some.host or some.host:port :splay - enables or disables splay based on true/false :splaylimit - set the maximum splay time

else a single background run will be attempted but this will fail if a idling daemon is present and :signal_daemon was false



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/puppet_agent_mgr/common.rb', line 107

def runonce!(options={})
  valid_options = [:noop, :signal_daemon, :foreground_run, :tags, :environment, :server, :splay, :splaylimit, :options_only]

  options.keys.each do |opt|
    raise("Unknown option %s specified" % opt) unless valid_options.include?(opt)
  end

  raise "Puppet is currently applying a catalog, cannot run now" if applying?
  raise "Puppet is disabled, cannot run now" if disabled?

  splay = options.fetch(:splay, nil)
  splaylimit = options.fetch(:splaylimit, nil)
  noop = options.fetch(:noop, nil)
  signal_daemon = options.fetch(:signal_daemon, true)
  foreground_run = options.fetch(:foreground_run, false)
  environment = options[:environment]
  tags = [ options[:tags] ].flatten.compact
  server = options[:server]

  clioptions = create_common_puppet_cli(noop, tags, environment, server, splay, splaylimit)

  if idling? && signal_daemon && !clioptions.empty?
    raise "Cannot specify any custom puppet options when the daemon is running"
  end

  if foreground_run
    return :foreground_run, run_in_foreground(clioptions, false) if options[:options_only]
    return run_in_foreground(clioptions)
  elsif idling? && signal_daemon
    return :signal_running_daemon, clioptions if options[:options_only]
    return signal_running_daemon
  else
    raise "Cannot run in the background if the daemon is present" if daemon_present?
    return :run_in_background, run_in_background(clioptions, false) if options[:options_only]
    return run_in_background(clioptions)
  end
end

#seconds_to_human(seconds) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/puppet_agent_mgr/common.rb', line 182

def seconds_to_human(seconds)
  days = seconds / 86400
  seconds -= 86400 * days

  hours = seconds / 3600
  seconds -= 3600 * hours

  minutes = seconds / 60
  seconds -= 60 * minutes

  if days > 1
    return "%d days %d hours %d minutes %02d seconds" % [days, hours, minutes, seconds]

  elsif days == 1
    return "%d day %d hours %d minutes %02d seconds" % [days, hours, minutes, seconds]

  elsif hours > 0
    return "%d hours %d minutes %02d seconds" % [hours, minutes, seconds]

  elsif minutes > 0
    return "%d minutes %02d seconds" % [minutes, seconds]

  else
    return "%02d seconds" % seconds

  end
end

#since_lastrunObject

seconds since the last catalog was applied



21
22
23
# File 'lib/puppet_agent_mgr/common.rb', line 21

def since_lastrun
  (Time.now - lastrun).to_i
end

#statusObject

simple utility to return a hash with lots of useful information about the state of the agent



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/puppet_agent_mgr/common.rb', line 146

def status
  status = {:applying => applying?,
            :enabled => enabled?,
            :daemon_present => daemon_present?,
            :lastrun => lastrun,
            :disable_message => lock_message,
            :since_lastrun => (Time.now.to_i - lastrun)}

  if !status[:enabled]
    status[:status] = "disabled"

  elsif status[:applying]
    status[:status] = "applying a catalog"

  elsif status[:daemon_present] && status[:applying]
    status[:status] = "idling"

  elsif !status[:applying]
    status[:status] = "stopped"

  end

  status[:message] = "Currently %s; last completed run %s ago" % [status[:status], seconds_to_human(status[:since_lastrun])]

  status
end

#stopped?Boolean

is a catalog being applied rigt now?

Returns:

  • (Boolean)


6
7
8
# File 'lib/puppet_agent_mgr/common.rb', line 6

def stopped?
  !applying?
end

#validate_name(name) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/puppet_agent_mgr/common.rb', line 51

def validate_name(name)
  if name.length == 1
    return false unless name =~ /\A[a-zA-Z]\Z/
  else
    return false unless name =~ /\A[a-zA-Z0-9_]+\Z/
  end

  true
end