Sha256: e941c889af5f5fffe5ece960ae396957bb62f285aae0727abc040bcdc7318e12

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

### http client
module Runpuppet
  class Agent
    attr_accessor :config
    attr_accessor :run_options
    def initialize(context)
      @config      = context.config
      @run_options = context.run_options
    end

##### puppet

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

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

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

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

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

#### base

    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

    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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
runpuppet-1.0.0 lib/runpuppet/agent.rb
runpuppet-1.0.0.rc6 lib/runpuppet/agent.rb
runpuppet-1.0.0.rc5 lib/runpuppet/agent.rb
runpuppet-1.0.0.rc4 lib/runpuppet/agent.rb
runpuppet-1.0.0.rc2 lib/runpuppet/agent.rb