How to write chef spec for following cookbook? - ruby

This is my recipe.rb:
require 'mixlib/shellout'
require "pry"
find = Mixlib::ShellOut.new("echo hostname -I")
find.run_command.stdout
What should I write it in my spec?

You don't need the echo command to get the output. you can directly run the command through mixlib/shellout and get then get the command output from stdout.
require 'mixlib/shellout'
find = Mixlib::ShellOut.new("hostname -I")
find.run_command.stdout

Related

Ruby - Running rb files from script (2)

I'd like to write a ruby script that then calls another ruby script. Example, I'd like to run the "test1.rb" from my script. The test1.rb has been simplified to just do this:
print "1"
Then get the result (-> 1).
I ask to help others to fix this problem, and they suggested the popen3 command:
require 'open3'
cmd = 'ruby "test1.rb"'
puts Dir.pwd
Open3.popen3(cmd) do |stdin, stdout|
var = stdout.read
puts var
end
However, the script send an error message:
X:/Ruby22-x64/lib/ruby/2.2.0/open3.rb:193:in `spawn': No such file or directory
- ruby "test1.rb" (Errno::ENOENT)
Please help me.

How I can run 'cmd' command inside rake process using Rails 4

I want to save data after running below command.
ip = '12.33.44.55' #it will be dynamic.
traceroute -q 1 -n ip
But problem is how can I run this command inside rake task.
Normally it runs in command line of Linux.
Any gem or ruby library to do this?
Any help please....
It possible to run any shell command with magic quote "`" in any ruby code.
ip = '12.33.44.55'
`traceroute -q 1 -n #{ip}`

How would I modify this rake task to start a thin server with support for local debugging?

Question
I want to be able to debug my Sinatra website hosted by Thin on my local machine, and I want to be able to initiate this by using rake.
I'm not able to accept answers suggesting the use of different technologies (e.g. Windows, Rails, Java) or other servers (e.g. unicorn, passenger, puma); however, if what I'm asking for is impossible, then I will accept that answer.
What I've tried
My current Rakefile contains a task :start which starts the Thin server, but when it hits the breakpoint no output is displayed on my terminal. If I start Thin directly from the terminal, then I see the (rdb:1) prompt when it hits the breakpoint as expected. In either case, the Thin server is correctly running the site (confirmed by commenting out the breakpoint).
Gemfile
source :rubygems
gem 'sinatra'
gem 'thin'
gem 'debugger-pry'
Rakefile
task :start do
conf = File.expand_path('config.ru', File.dirname(__FILE__))
`thin -e development -R #{conf} --debug start`
end
config.ru
require File.expand_path('app', File.dirname(__FILE__))
run ModularExample::App.new
app.rb
require 'sinatra'
require 'debugger/pry'
module ModularExample
class App < Sinatra::Base
get '/' do
debugger
"Hello, world"
end
end
end
You're not outputting to STDOUT. The backticks execute the command and return the output as a string so you could do something like
puts `thin -e development -R #{conf} --debug start`
but you want to stream the processes output to STDOUT so you actually want to do:
task :start do
conf = File.expand_path('config.ru', File.dirname(__FILE__))
exec("thin -e development -R #{conf} --debug start")
end
Learn more about calling command line calls from this question.

How to print to stdout from system command in Rake?

I have the following Rakefile
desc "Runs tests"
namespace :test do
task :api do
`mocha`
end
end
When I run the command rake test:api, I don't get the nice output of dots that I would if I just ran the command mocha.
How do I print the output of a command real-time in a rake task?
You can just put the output:
puts `mocha`
The backticks ` are calling the command mocha and return the output of the command.
You may also use %x{}:
puts %x{mocha}
Or you use system:
system('mocha')
Or you store the output for later use in a variable:
output = `mocha`
puts output

How do I to run a command in Linux as a Ruby script?

Let's say I have some terminal commands like:
sudo mycommand1
mycommand2
#.....
What should I do run them via ruby script (not bash) in Ubuntu?
UPDATE:
I have a ruby script:
def my_method1()
#calculating something.....
end
def method2(var1, var2)
#how do I sudo mycommand1 and any other Lunix command from here?
end
def method3(var4)
#calculating something2....
end
You can do system, exec, or place the command in backticks.
exec("mycommand") will replace the current process so that's really only pratical at the end of your ruby script.
system("mycommand") will create a new process and return true if the command succeeded and nil otherwise.
If you need to use the output of your command in your Ruby script use backticks:
response = 'mycommand`
There are many questions on SO that answer this. However you can run a command in many ways using system, exec, (backticks), %x{} or using open3. I prefer to use open3 -
require 'open3'
log = File.new("#{your_log_dir}/script.log", "w+")
command = "ls -altr ${HOME}"
Open3.popen3(command) do |stdin, stdout, stderr|
log.puts "[OUTPUT]:\n#{stdout.read}\n"
unless (err = stderr.read).empty? then
log.puts "[ERROR]:\n#{err}\n"
end
end
If you want to know more about other options you can refer to Ruby, Difference between exec, system and %x() or Backticks for links to relevant documentation.
You can try these approaches:
%x[command]
Kernel.system"command"
run "command"
make some file.rb with:
#!/path/to/ruby
system %{sudo mycommand1}
system %{mycommand2}
and the chmod the file with exec permissions (e.g. 755)
It you need to pass variables between the two commands, run them together:
system %{sudo mycommand1; \
mycommand2}

Resources