Parsing file names using ruby Net::SSH - ruby

Working on automating a task for my job.
Issue: We have a rather large stack of git repos hosted on a private git server at our main office. I work at a remote location.
Goal: I need to clone many (50+) repos without authenticating and manually typing out the filename ever time.
This is supposed to be the quick and dirty version written in ruby (I'm currently learning ruby so I'm trying to use it for any and all needs it's good for) which I can use as a template for more fine tuned git operations in the future.
This is what I have right now:
#!/usr/bin/ruby
require 'net/ssh'
git_server_address = "x.x.x.x"
git_server_login = "username"
puts "Enter password for #{git_server_login}"
git_server_passwd = gets.chomp
$gitnames = []
def getGitNames(address, login, passwd)
Net::SSH.start(address, login, password: passwd) do |ssh|
ssh.exec!("cd /git")
ssh.exec!("find . -type d -exec echo '{}' \\;") do |file|
$gitnames.push(file)
end
end
end
$gitnames = getGitNames(git_server_address, git_server_login, git_server_passwd)
$gitnames.each do |file|
if file.equal?('slamros.git')
puts(file)
#puts system("git clone #{git_server_login}##{git_server_address}:/git/# {file}")
#puts git_server_passwd
end
end
If this is abhorrently ugly, I apologize, I'm still very new at my job and to professional programming. I know there are some best practices I'm not following but my goal was just to get this operational by EoD.
Essentially what I'm trying to do is use the ssh.exec! and find -exec to get the names of the files in the /git folder.
Once this is working I will use the filenames I have to pull git clones on my own system. I currently have these lines commented out.
My code currently asks for my password, processes for about 5 seconds, and then gives me this error:
/GitClone.rb:21:in `<top (required)>': undefined method `each' for nil:NilClass (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
I get that what i'm doing in my getGitNames() method is wrong, but I would appreciate some help in pointing out what exactly it is or some ideas of other ways to accomplish my task with Ruby.

Related

Can I programmatically interact with an "installer" (ie. ./install.sh) [duplicate]

Is there an Expect equivalent gem for Ruby?
I tried searching on code.google and rubygems.org, but sadly it did not show up.
FYI: Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others.
Ruby comes with the PTY module for setting up pseudoterminals to drive interactive command line applications. With it comes an expect method that allows you to interact with an application kinda like Expect. For learning how to use expect, I found "What to expect from the Ruby expect library?" helpful.
As far as gems go, maybe checkout greenletters which is supposed to improve upon PTY + expect (although I haven't tried it myself).
I recently spent quite a bit of time struggling with this issue (I am stuck with 1.8.7). I found this question, this blog post and this forum thread really useful.
At the end this is my application code if anyone is interested in a little example (pass the password to rpm when signing packages):
def run_interactive command, password, promt
output = ''
begin
r, w, pid = PTY.spawn(command)
puts r.expect(promt)
sleep(0.5)
w.puts(password)
begin
r.each { |l| output += l }
rescue Errno::EIO
end
$?.exitstatus
Process.wait(pid)
rescue PTY::ChildExited => e
$stderr.puts "The child process #{e} exited! #{$!.status.exitstatus}"
end
output
end
password = "mypassword"
command = "rpm --define '_signature gpg' --define '_gpg_name #{key_id}' --addsign #{package}"
promt = %r{.*: }
expected = %r{good}
output = run_interactive(command, password, promt)
if output.match(expected)
puts output
else
abort "Error: expected: '#{expected}' got '#{output}'"
end
It has little error checking but it was all I needed.
Edit: Update the code with Process.wait(pid) to make sure it finishes before continuing and add comment about this being for 1.8.7.
checkout this rubygem: https://github.com/abates/ruby_expect. It could handle some small task for you. from its official example, it's enough to 'enter password' and login and interactive with local script.
here is an example that update the git code (which is authenticated with password):
require 'rubygems'
require 'ruby_expect'
def update_code
password = 'your password here'
exp = RubyExpect::Expect.spawn('git pull', :debug => true)
exp.procedure do
each do
expect /password: / do
send password
end
end
end
end
update_code
just run the code above, and your will see like this:
$ ruby update_code.rb
shensiwei#gforge.1ver??.net's password:
remote: Counting objects: 133, done.
remote: Compressing objects: 100% (84/84), done.
remote: Total 85 (delta 62), reused 0 (delta 0)
Unpacking objects: 100% (85/85), done.
for more example and details, please dive into its source code.
expect4r seems to do what you are asking for, though it is made specific for connections to Cisco and Juniper devices.
Perhaps even better is yax as this is "yet another expect".
RExpect
From the project's website:
RExpect is a drop in replacement for the expect.rb module in the
standard library that is faster and more robust, cabable of driving
many devices simultaneously.
parley is another one you can try, (written by me). It is inspired by Perl expect.

Ruby require_relative executing script?

I'm working on a game server in ruby, and during testing I'm having trouble testing components individually. I wasn't getting output from my launcher, just the server, so I commented out the initialisation of the server- yet eclipse still showed output from the server!
I then went to the command line, assuming eclipse was looking at the wrong file (git has messed it around before, but as you can see, the stack trace shows that Server.rb is being executed in its entirety from line 5: require_relative 'Server':
This is the text content of the file:
class Launcher
puts "File saved at #{File.mtime($0)}"
require_relative 'Server'
require_relative 'Game'
#STDOUT.sync = true
puts "Launcher started"
#server = Server.new
print "server made"
game = Game.new
#serverThread = Thread.new{server.start()}
gameThread = Thread.new{game.start()}
while (running)
print "Stop? "
input = gets.chomp
if (input.equals?("yes"))
running = false
end
end
server.stop
game.stop
gameThread.join
serverThread.join
end
and the terminal output:
C:\Users\gossfunkel\git\citadelserver\RubyCitadelServer>ruby Launcher.rb
File saved at 2013-06-22 18:16:44 +0100
Server starting up at 2013-06-22 18:16:47 +0100...
C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:20:in `recvfro
m': Interrupt
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:2
0:in `run'
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:1
5:in `start'
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:3
0:in `<class:Server>'
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:1
:in `<top (required)>'
from Launcher.rb:5:in `require_relative'
from Launcher.rb:5:in `<class:Launcher>'
from Launcher.rb:1:in `<main>'
How do I require a file without this happening, and should it be?
I can't tell without seeing the classes, but I would guess that either
game.start is doing more than you thought, and is starting the server for itself
You are, as your subject line suggests, running a different file from the one you are editing (or not saving the file once it is changed). Check by putting an obvious puts at the top of the program. Something like
puts "File saved at #{File.mtime($0)}"
should do the trick
After discussion, it seems there's a third option. The code in Server.pm creates and runs a server as well as defining the class. You need to remove the require as well as the lines that use the Server class.

How to execute local functions using code from external file in ruby?

Can a require execute a locally defined function? I guess the easiest way to describe what I need is to show an example.
I'm using ruby 1.9.3, but solutions for 1.8 and 2.0 are also welcome.
I have a file main.rb as the following:
class Stuff
def self.do_stuff(x)
puts x
end
require_relative('./custom.rb')
do_stuff("y")
end
And also have a file custom.rb in the same folder, with the following content:
do_stuff("x")
Running main.rb, I have following output:
/home/fotanus/custom.rb:1:in `<top (required)>': undefined method `do_stuff' for main:Object (NoMethodError)
from main.rb:5:in `require_relative'
from main.rb:5:in `<class:Stuff>'
from main.rb:1:in `<main>'
Note that without the require, the output is y.
I'm not sure if it is the best solution but using eval should do the trick.
class Stuff
def self.do_stuff(x)
puts x
end
eval(File.read('./custom.rb'))
do_stuff("y")
end
The output will be:
pigueiras#pigueiras$ ruby stuff.rb
x
y
In C, #include literally drops the code as-is into the file. require in Ruby is different: it actually runs the code in the required file in its own scope. This is good, since otherwise we could break required code by redefining things before the require.
If you want to read in the contents of a script and evaluate it in the current context, there are methods for doing just that: File.read and eval.

Why am I getting NoMethodError from IRB for my own Module and method

I have taken this example exactly from the Ruby Cookbook. Unfortunately for me, like a whole lot of the examples in that book, this one does not work:
my file (Find.rb - saved both locally and to Ruby\bin):
require 'find'
module Find
def match(*paths)
matched=[]
find(*paths) { |path| matched << path if yield path }
return matched
end
module_function :match
end
I try to call it this way from IRB, according to the example the book provides:
irb(main):002:0> require 'Find'
=> false
irb(main):003:0> Find.match("./") { |p| ext = p[-4...p.size]; ext && ext.downcase == "mp3" }
It SHOULD return a list of mp3 files in my recursive directory. Instead, it does this:
NoMethodError: undefined method `match' for Find:Module
from (irb):3
from C:/Ruby192/bin/irb:12:in `<main>'
What gives? I'm new at this (although I MUST say that I'm farther along with Python, and much better at it!).
How can I get IRB to use my method?
I ran into this with irb on a Mac running Snow Leopard while using the default version of ruby (and irb of course) installed with OS X. I was able to get past it by including the module in IRB after loading the module or in the file after the module definition.
include module_name
I'm not sure if this is a defect or known behavior.
The only explanation is that the code you posted is not the code you are running, since both carefully reading it and simply cut&paste&running it shows absolutely no problems whatsoever.
What directory are you calling IRB from? Try calling it from the directory where your find.rb file is located. Also, I don't know if it makes any difference but convention is to name the file the lowercase version of the module / class. So the module would be Find and the file name would be find.rb. You shouldn't need the require call in the file itself.
So, start your command prompt window, cd into the directory that contains find.rb and run irb. In IRB you should be able to require "find" and it should return true. From there you should be able to call Find.match.
I know this question is already 3 years old, but since this is the first hit on google for the problem, and I had been banging my head against the wall all afternoon with the same problem doing the tutorial here: http://ruby.learncodethehardway.org/book/ex25.html, here goes: the function definition in the module should read
module Find
def Find.match(*paths)
...
end
end

Telling rspec to not load files

I'm trying to add some commit hooks to my git repo. I want to leverage Rspec and create commit message specs that will run each time I commit. I have figured out how to run rspec outside of the 'spec' command, but I now have an interesting problem.
Here is my current code:
.git/hooks/commit-msg
#!/usr/bin/env ruby
require 'rubygems'
require 'spec/autorun'
message = File.read(ARGV[0])
describe "failing" do
it "should fail" do
true.should == false
end
end
This is throwing an error when it gets to the describe call. Basically, it thinks that the commit message it receives is the file to load and run the specs against. Here is the actually error
./.git/COMMIT_EDITMSG:1: undefined local variable or method `commit-message-here' for main:Object (NameError)
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in `load'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `each'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/options.rb:133:in `run_examples'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:61:in `run'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:45:in `autorun'
from .git/hooks/commit-msg:12
I am looking for a way to tell rspec to not load files. I have a suspicion that I will need to create my own spec runner. I came to this conclusion after viewing these lines in rspec-1.3.0/lib/spec/runner/example_group_runner.rb
def load_files(files)
$KCODE = 'u' if RUBY_VERSION.to_f < 1.9
# It's important that loading files (or choosing not to) stays the
# responsibility of the ExampleGroupRunner. Some implementations (like)
# the one using DRb may choose *not* to load files, but instead tell
# someone else to do it over the wire.
files.each do |file|
load file
end
end
But, I would like some feedback before I do that. Any thoughts?
Do you even really need all the special stuff that RSpec provides (should and the various matchers) just to verify the contents of a single file? It really seems like overkill for the problem.
spec/autorun eventually calls Spec::Runner.autorun which parses ARGV as if it held normal arguments for a spec command line.
When you install a bare “spec” file as a Git hook,
it will get arguments that are appropriate for the whatever Git hook is being used,
not spec-style arguments (spec filenames/directories/patterns and spec options).
You might be able to hack around the problem like this:
# Save original ARGV, replace its elements with spec arguments
orig_argv = ARGV.dup
%w(--format nested).inject(ARGV.clear, :<<)
require 'rubygems'
require 'spec/autorun'
# rest of your code/spec
# NOTE: to refer to the Git hook arguments use orig_argv instead of ARGV

Resources