Sha256: 2e9b54b7f5d65d1ad43e4a386e40f7ec815c4d6b37a3bf795439cc91700dc994

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'rest-client'
require 'addressable/uri'
require 'json'
require 'silver_spurs/knife_interface'
require 'silver_spurs/client/exceptions'
require 'silver_spurs/client/bootstrap_run'
require 'silver_spurs/client/chef_run'

module SilverSpurs
  class Client
    def initialize(host_url, options={})
      @host_url = host_url
      @timeout = options[:timeout] || 2 * 60
    end

    def start_bootstrap(ip, node_name, options = {})
      params = extract_extra_params(options).merge({:node_name => node_name})
      payload = parameterize_hash params
      headers = {:accept => :json, :content_type=> 'application/x-www-form-urlencoded'}

      response = spur_host["bootstrap/#{ip}"].put(payload, headers, &method(:dont_redirect_for_303))
      
      throw ClientException.new("unexpected response", response) unless response.code == 303

      BootstrapRun.new(response.headers[:location], :timeout => @timeout)
    end

    def start_chef_run(host_name, runlist = [])
      response = spur_host["kick/#{host_name}"].post :params => { :run => runlist }
      raise ClientException.new("the host name was not found", response) if response.code == 404
      ChefRun.new JSON.parse(response)
    end

    private
    
    def parameterize_hash(param_hash)
      uri = Addressable::URI.new
      uri.query_values = param_hash
      uri.query
    end
    
    def extract_extra_params(options)
      supported_arguments = KnifeInterface.supported_arguments
      options.select {|k,v| supported_arguments.include? k}  
    end

    def spur_host
      RestClient::Resource.new(@host_url, :timeout => @timeout)
    end

    def dont_redirect_for_303(response, origin, orig_result, &block)
      if response.code == 303
        response
      else
        response.return! &block
      end
    end
                
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
silver_spurs-2.0.0.rc1 lib/silver_spurs/client.rb