delano / rye

Safe, parallel access to Unix shells from Ruby
http://delano.github.com/rye
MIT License
234 stars 32 forks source link

strange behavior with Rye::Box instances in a Hash #47

Open torstello opened 10 years ago

torstello commented 10 years ago

Hi, i'm a sysadmin working on a tool for doing my daily tasks on my server farm. Due to the fact that i have around 70 hosts and also put them in logical groups together, i do initialize Rye:Boxes and Rye::Sets in Hashes and later work with them.

That leads to an behavior, i can not work with and also absolutely don't understand:

My ENV:

0 % ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]

0 % gem list G rye
rye (0.9.11)

Example in irb:

> a = {}
=> {}

> hostname = 'localhost'
=> "localhost"

> a[hostname] = Rye::Box.new(host="#{hostname}", :safe => false)
=> #<Rye::Box:localhost name=localhost cwd= umask= env="" safe=false opts={:port=>nil, :keys=>["/Users/ts/.ssh/id_rsa"]} keys=["/Users/ts/.ssh/id_rsa"]>

now comes the (not so) funny part, this doesn't work:

> a.each_value {|v| v.uptime}
=> {"localhost"=>#<Rye::Box:localhost name=localhost cwd= umask= env="" safe=false opts={:port=>nil, :keys=>["/Users/ts/.ssh/id_rsa"], :user=>"ts"} keys=["/Users/ts/.ssh/id_rsa"]>}

but i'm working with an Rye::Box object with an existing method 'uptime'

> a.each_value {|v| puts v.class}
Rye::Box

> a.each_value {|v| puts v.public_methods.grep(/uptime/)}
uptime

but this works:

> a['localhost'].uptime
=> [17:59  up 13 days,  3:27, 2 users, load averages: 0.69 0.63 0.64, , 0, ]

I also create Rye::Sets in a Hash and populate them with the boxes out of another hash but this somehow leads to losing my ssh-key, so i get a password prompt... But i first want to understand this simple example above.

If you could help me out i would really appreciate that.

Best, Torsten

windoz commented 10 years ago

Hmm that's odd. Thanks for all the detail. I'll have to take a look. On Jan 4, 2014 12:34 PM, "Torsten Schmidt" notifications@github.com wrote:

Hi, i'm a sysadmin working on a tool for doing my daily tasks on my server farm. Due to the fact that i have around 70 hosts and also put them in logical groups together, i do initialize Rye:Boxes and Rye::Sets in Hashes and later work with them.

That leads to an behavior, i can not work with and also absolutely don't understand:

My ENV:

0 % ruby -v ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]

0 % gem list G rye rye (0.9.11)

Example in irb:

a = {} => {}

hostname = 'localhost' => "localhost"

a[hostname] = Rye::Box.new(host="#{hostname}", :safe => false) => #<Rye::Box:localhost name=localhost cwd= umask= env="" safe=false opts={:port=>nil, :keys=>["/Users/ts/.ssh/id_rsa"]} keys=["/Users/ts/.ssh/id_rsa"]>

now comes the (not so) funny part, this doesn't work:

a.each_value {|v| v.uptime} => {"localhost"=>#<Rye::Box:localhost name=localhost cwd= umask= env="" safe=false opts={:port=>nil, :keys=>["/Users/ts/.ssh/id_rsa"], :user=>"ts"} keys=["/Users/ts/.ssh/id_rsa"]>}

but i'm working with an Rye::Box object with an existing method 'uptime'

a.each_value {|v| puts v.class} Rye::Box

a.each_value {|v| puts v.public_methods.grep(/uptime/)} uptime

but this works:

irb(main):012:0> a['localhost'].uptime => [17:59 up 13 days, 3:27, 2 users, load averages: 0.69 0.63 0.64, , 0, ]

I also create Rye::Sets in a Hash and populate them with the boxes out of another hash but this somehow leads to losing my ssh-key, so i get a password prompt... But i first want to understand this simple example above.

If you could help me out i would really appreciate that.

Best, Torsten

— Reply to this email directly or view it on GitHubhttps://github.com/delano/rye/issues/47 .

torstello commented 10 years ago

in debug mode i saw that the command is actually executed. So this works

irb(main):005:0> a.each_value do |v|
irb(main):006:1* r = v.uptime
irb(main):007:1> puts r
irb(main):008:1> end
10:37  up  2:24, 2 users, load averages: 0.99 0.84 0.80
=> {"localhost"=>#<Rye::Box:localhost name=localhost cwd= umask= env="" safe=false opts={:port=>nil, :keys=>["/Users/ts/.ssh/id_rsa"], :user=>"ts"} keys=["/Users/ts/.ssh/id_rsa"]>}

so i'm fine with that :)

Also forget my comment about the Rye::Sets, i found my mistake in adding the boxes, so everything is working now.

T.

windoz commented 10 years ago

Thanks for the update. Glad to here it's working. On Jan 6, 2014 4:44 AM, "Torsten Schmidt" notifications@github.com wrote:

in debug mode i saw that the command is actually executed. So this works

irb(main):005:0> a.each_value do |v| irb(main):006:1* r = v.uptime irb(main):007:1> puts r irb(main):008:1> end

10:37 up 2:24, 2 users, load averages: 0.99 0.84 0.80 => {"localhost"=>#<Rye::Box:localhost name=localhost cwd= umask= env="" safe=false opts={:port=>nil, :keys=>["/Users/ts/.ssh/id_rsa"], :user=>"ts"} keys=["/Users/ts/.ssh/id_rsa"]>}

so i'm fine with that :)

Also forget my comment about the Rye::Sets, i found my mistake in adding the boxes, so everything is working now.

T.

— Reply to this email directly or view it on GitHubhttps://github.com/delano/rye/issues/47#issuecomment-31636843 .

dennybaa commented 8 years ago

+1 have the same issue. for the code retrieving box from a hash I've got the same error

  private
  # Retrieve or set rye_session
  def fetch_rye(node=nil, ssh_config=nil)
    if @@rye_session[node].nil?
      puts "==> new"
      @@rye_session = Rye::Box.new(node, ssh_config || self.ssh_config)
    else
      puts "==> here"
      @@rye_session[node]
    end
  end

cwd is havoced

 => #<Remote:0x00000002659e18 @ssh_config={:keys=>["/root/.ssh/busybee"], :auth_methods=>["publickey"]}> 
2.2.1 :059 > remote.send(:fetch_rye, "172.17.0.170")
==> new
 => #<Rye::Box:172.17.0.170 name=172.17.0.170 cwd= umask= env="" safe=true opts={:port=>nil, :keys=>["/root/.ssh/busybee"], :auth_methods=>["publickey"], :paranoid=>true} keys=[]> 
2.2.1 :060 > remote.send(:fetch_rye, "172.17.0.170")
==> here
 => #<Rye::Box:172.17.0.170 name=172.17.0.170 cwd=172.17.0.170/172.17.0.170 umask= env="" safe=true opts={:port=>nil, :keys=>["/root/.ssh/busybee"], :auth_methods=>["publickey"], :paranoid=>true} keys=[]> 
2.2.1 :061 >