Class: Runpuppet::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/runpuppet/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Agent

Returns a new instance of Agent.



6
7
8
9
# File 'lib/runpuppet/agent.rb', line 6

def initialize(context)
  @config      = context.config
  @run_options = context.run_options
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/runpuppet/agent.rb', line 4

def config
  @config
end

#run_optionsObject

Returns the value of attribute run_options.



5
6
7
# File 'lib/runpuppet/agent.rb', line 5

def run_options
  @run_options
end

Instance Method Details

#append_params_to_post(params) ⇒ Object



60
61
62
63
64
65
# File 'lib/runpuppet/agent.rb', line 60

def append_params_to_post(params)
  params            = params.dup
  params[:hostname] = config.hostname
  params[:ip]       = config.local_ip
  return params
end

#append_params_to_url(url) ⇒ Object



67
68
69
70
71
# File 'lib/runpuppet/agent.rb', line 67

def append_params_to_url(url)
  url += (url.include?('?') ? '&' : '?')
  url += "hostname=#{config.hostname}&ip=#{config.local_ip}"
  url
end

#base_url(path) ⇒ Object



73
74
75
# File 'lib/runpuppet/agent.rb', line 73

def base_url(path)
  "#{config.puppet_controller_url.gsub(/\/$/, '')}/#{path.gsub(/^\//, '')}"
end

#check_statusObject

puppet



13
14
15
16
17
# File 'lib/runpuppet/agent.rb', line 13

def check_status
  action, branch = get("/puppet/run", @run_options).split('-') rescue []
  branch ||= (run_options[:branch] || config.default_branch)
  return action, branch
end

#get(path) ⇒ Object

base



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/runpuppet/agent.rb', line 39

def get(path)
  begin
    url = append_params_to_url(base_url(path))
    puts "Getting #{url}" if run_options[:verbose]
    result = RestClient.get(url)
    puts "got #{result}" if run_options[:verbose]
  rescue Exception => e
    puts e.inspect
    puts "WARNING: error connecting in GET (PuppetMaster)"
  end
end

#post(path, params) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/runpuppet/agent.rb', line 51

def post(path, params)
  begin
    RestClient.post base_url(path), append_params_to_post(params)
  rescue Exception => e
    puts e.inspect
    puts "WARNING: error connecting in POST (PuppetMaster)"
  end
end

#report_factsObject



31
32
33
34
35
# File 'lib/runpuppet/agent.rb', line 31

def report_facts
  require 'facter/application'
  facts = Facter::Application.run([])
  post('/puppet/facts', :facts => facts.to_hash)
end

#report_failureObject



27
28
29
# File 'lib/runpuppet/agent.rb', line 27

def report_failure
  get '/puppet/status?status=error'
end

#report_startObject



19
20
21
# File 'lib/runpuppet/agent.rb', line 19

def report_start
  get '/puppet/status?status=started'
end

#report_successObject



23
24
25
# File 'lib/runpuppet/agent.rb', line 23

def report_success
  get '/puppet/status?status=finished'
end