lib/ridley/connection.rb in ridley-0.5.2 vs lib/ridley/connection.rb in ridley-0.6.0

- old
+ new

@@ -1,11 +1,14 @@ module Ridley # @author Jamie Winsor <jamie@vialstudios.com> class Connection class << self def sync(options, &block) - new(options).sync(&block) + conn = new(options) + conn.sync(&block) + ensure + conn.terminate if conn && conn.alive? end # @raise [ArgumentError] # # @return [Boolean] @@ -14,10 +17,16 @@ unless missing.empty? missing.collect! { |opt| "'#{opt}'" } raise ArgumentError, "Missing required option(s): #{missing.join(', ')}" end + + missing_values = options.slice(*REQUIRED_OPTIONS).select { |key, value| !value.present? } + unless missing_values.empty? + values = missing_values.keys.collect { |opt| "'#{opt}'" } + raise ArgumentError, "Missing value for required option(s): '#{values.join(', ')}'" + end end # A hash of default options to be used in the Connection initializer # # @return [Hash] @@ -28,21 +37,24 @@ } end end extend Forwardable + + include Celluloid include Ridley::DSL + include Ridley::Logging - attr_reader :client_name - attr_reader :client_key attr_reader :organization - attr_reader :ssh - attr_reader :validator_client - attr_reader :validator_path - attr_reader :encrypted_data_bag_secret_path + attr_accessor :client_name + attr_accessor :client_key + attr_accessor :validator_client + attr_accessor :validator_path + attr_accessor :encrypted_data_bag_secret_path + attr_accessor :ssh attr_accessor :thread_count def_delegator :conn, :build_url def_delegator :conn, :scheme def_delegator :conn, :host @@ -58,10 +70,22 @@ def_delegator :conn, :delete def_delegator :conn, :head def_delegator :conn, :in_parallel + OPTIONS = [ + :server_url, + :client_name, + :client_key, + :organization, + :validator_client, + :validator_path, + :encrypted_data_bag_secret_path, + :thread_count, + :ssl + ].freeze + REQUIRED_OPTIONS = [ :server_url, :client_name, :client_key ].freeze @@ -71,35 +95,43 @@ # @option options [String] :server_url # URL to the Chef API # @option options [String] :client_name # name of the client used to authenticate with the Chef API # @option options [String] :client_key - # filepath to the client's private key used to authenticate with - # the Chef API + # filepath to the client's private key used to authenticate with the Chef API # @option options [String] :organization # the Organization to connect to. This is only used if you are connecting to # private Chef or hosted Chef - # @option options [String] :validator_client - # (default: nil) - # @option options [String] :validator_path - # (default: nil) - # @option options [String] :encrypted_data_bag_secret_path - # (default: nil) - # @option options [Integer] :thread_count - # @option options [Hash] :ssh - # authentication credentials for bootstrapping or connecting to nodes (default: Hash.new) + # @option options [String] :validator_client (nil) + # @option options [String] :validator_path (nil) + # @option options [String] :encrypted_data_bag_secret_path (nil) + # @option options [Integer] :thread_count (DEFAULT_THREAD_COUNT) + # @option options [Hash] :ssh (Hash.new) + # * :user (String) a shell user that will login to each node and perform the bootstrap command on (required) + # * :password (String) the password for the shell user that will perform the bootstrap + # * :keys (Array, String) an array of keys (or a single key) to authenticate the ssh user with instead of a password + # * :timeout (Float) [5.0] timeout value for SSH bootstrap + # * :sudo (Boolean) [true] bootstrap with sudo # @option options [Hash] :params # URI query unencoded key/value pairs # @option options [Hash] :headers # unencoded HTTP header key/value pairs # @option options [Hash] :request # request options # @option options [Hash] :ssl - # SSL options + # * :verify (Boolean) [true] set to false to disable SSL verification # @option options [URI, String, Hash] :proxy # URI, String, or Hash of HTTP proxy options def initialize(options = {}) + log.info { "Ridley starting..." } + configure(options) + end + + # Configure this instance of Ridley::Connection + # + # @param [Hash] options + def configure(options) options = self.class.default_options.merge(options) self.class.validate_options(options) @client_name = options[:client_name] @client_key = File.expand_path(options[:client_key]) @@ -176,9 +208,13 @@ return nil if encrypted_data_bag_secret_path.nil? IO.read(encrypted_data_bag_secret_path).chomp rescue Errno::ENOENT => e raise Errors::EncryptedDataBagSecretNotFound, "Encrypted data bag secret provided but not found at '#{encrypted_data_bag_secret_path}'" + end + + def finalize + log.info { "Ridley stopping..." } end private attr_reader :conn