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



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

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



75
76
77
78
79
# File 'lib/runpuppet/agent.rb', line 75

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

#base_url(path) ⇒ Object



81
82
83
# File 'lib/runpuppet/agent.rb', line 81

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

#check_statusObject

puppet



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/runpuppet/agent.rb', line 13

def check_status
  begin
    r = get("/puppet/run")
  rescue Exception => e
  end

  if r
    return (action, branch = *r.split('-'))
  else
    return nil
  end
end

#get(path) ⇒ Object

base



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/runpuppet/agent.rb', line 46

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

#post(path, params) ⇒ Object



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

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



38
39
40
41
42
# File 'lib/runpuppet/agent.rb', line 38

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

#report_failureObject



34
35
36
# File 'lib/runpuppet/agent.rb', line 34

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

#report_startObject



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

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

#report_successObject



30
31
32
# File 'lib/runpuppet/agent.rb', line 30

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