lib/tengine/resource/net_ssh.rb in tengine_resource-0.5.15 vs lib/tengine/resource/net_ssh.rb in tengine_resource-1.1.0

- old
+ new

@@ -40,10 +40,12 @@ when String user = obj1 when Hash hash = obj1.symbolize_keys when Tengine::Resource::Credential + # obj1がCredentialでobj2が指定されていない場合は、objがinvalid?ならエラー + raise ArgumentError, obj1.errors.full_messages.join if !obj1.valid? && obj2.nil? hash = obj1.auth_values.symbolize_keys else raise TypeError, "#{obj1.class} not expected (expected String)" end @@ -63,10 +65,11 @@ raise TypeError, "#{obj1.class} not expected (expected #{String})" end u2 = hash.delete(:username) k2 = hash.delete(:private_keys) + f2 = hash.delete(:private_key_file) raise ArgumentError, "username specified twice in both ordinal and optional arguments" if user and u2 user ||= u2 raise ArgumentError, "username mandatory" unless user @@ -105,30 +108,39 @@ r end raise ArgumentError, "unknown optional argument(s): #{hash.keys.join(', ')}" unless hash.empty? - if k2 - k2 = [k2] unless k2.is_a? Array + assign_options(argh, k2, f2) do + return __tengine_resource_net_ssh_backed_up_start__(host, user, argh, &block) + end + end + + def assign_options(options, private_keys, private_key_file) + if private_keys + private_keys = [private_keys] unless private_keys.is_a?(Array) Dir.mktmpdir(nil, File.expand_path("../../../../tmp", __FILE__)) do |dir| begin - k3 = k2.map do |k| + pk_files = private_keys.map do |k| fp = Tempfile.new("pk", dir) fp.write(k) fp.chmod(0400) fp.flush fp # no close end - k4 = k3.map {|i| File.expand_path(i.path) } - argh[:keys] ||= [] - argh[:keys].concat k4 - - return __tengine_resource_net_ssh_backed_up_start__ host, user, argh, &block + options[:keys] ||= [] + options[:keys].concat(pk_files.map {|i| File.expand_path(i.path) }) + yield ensure - k3.each {|i| i.close(:real) } + pk_files.each {|i| i.close(:real) } end end + elsif private_key_file + options[:keys] ||= [] + options[:keys] << private_key_file + yield else - return __tengine_resource_net_ssh_backed_up_start__ host, user, argh, &block + yield end end + end