I have made a ruby command line "frontend" that sets different environment variables according to user input, a sort of "ncurses" frontend but only in ruby, I know there is Rake and such tools, but we made it this way because we need the variables that can be set at runtime according to need, and the frontend also can send the running results via email to my team.
This works very well in linux machines but in windows its not working.
I track down the problem to the rspec command that has a wildcard in the filename, this works perfectly in linux, but if i run the commands manually in windows, it didn't do anything. If i run the rspec command with one file it works fine, but not with wildcards.
I tried a couple of google searches, tried this workarounds i found, but without success:
(this snippet is inside a ruby file I run with "ruby FILE.rb")
system("rspec #{spec\2.2\web_pc_*} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\web_pc_/*} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\web_pc_'*'} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\web_pc_"*"} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\'web_pc_*'} -e '#{ARGV[1]}' -o log.txt")
The solution I had found, is to make a Rakefile and run the Rake command. This way all the files execute no matter what OS i'm running them.
Rakefile:
require 'rake/testtask'
Rake::TestTask.new(:all) do |t|
t.libs << "tests"
t.test_files = FileList['tests/test_*.rb']
t.verbose = true
end
task default: :all
Related
I'm using the following ruby script
describe command('curl -s -k - i %{http_code} https://localhost/xx/xxx') do
its(:stdout) { should match /200/ }
end
But I'm unsure if this script works or not. How can I make sure this script works?
Generally Serverspec is used with Chef via the Test Kitchen integration testing framework (though these days InSpec is more popular). You run your tests using the kitchen verify command.
It looks to me like what you want is:
describe command('curl -s -k -I https://localhost/xx/xxx') do
its(:stdout) { should match /200/ }
end
In my code below, I wanted to set few environment variables stored in a file. Am I missing something? Printing env in production after 'bundle exec whenever' does not show the environment variables set. Using whenever gem for a scheduled cron task and spent hours figuring this. Any other way can be suggested too.
every 1.day, :at => '2:30 am' do
# Run shell script to assign variables and continue the rake task
system "for line in `cat config/myEnvFile.env` ; do export $line ; done"
rake "task:continue_doing_my_task"
end
system is not a whenever job type. It's Kernel.system, which executes the String being passed to it when the whenever command is run, rather than converting that String to cron syntax. It looks like what you really mean is:
command "for line in `cat config/myEnvFile.env` ; do export $line ; done"
# Note: command instead of system
command is a built-in job type defined by whenever here.
Each line of code inside the every-block runs as it's own command. If you run whenever (without any arguments, so it just displays what it would put in your crontab without actually modifying the crontab, and after making the correction I describe above), you'll see that the output is something like this:
30 2 * * * * /bin/bash -l -c 'for line in `cat config/myEnvFile.env` ; do export $line ; done'
30 2 * * * * /bin/bash -l -c 'cd /path/to/project && RAILS_ENV=production bundle exec rake task:continue_doing_my_task --silent > my_log_file.log 2&>1'
Notice 2 issues:
Firstly, these 2 commands have nothing to do with each other--they are run as 2 totally separate processes.
The first one is running in cron's default directory, which is probably not where config/myEnvFile.env is located.
To fix this, you need to merge everything into a single command. By using whenever's rake job type, you will end up in the right directory, but you still to export all those variables somehow.
One way to do this, is to rename the file .ruby-env and use rvm. rvm, in addition to managing ruby versions for you, will automatically load all environment variables defined in .ruby-env when you enter the directory.
If RVM is not an option for you, or you want something more lightweight, rename the file .env and use dotenv. Their README documents exactly how to use the gem, with or without Rails. Without Rails, it's this easy:
Add dotenv to your Gemfile
Make this change to your Rakefile:
require 'dotenv/tasks' # 1. require this file
namespace :task
task continue_doing_my_task: :dotenv do # 2. make :dotenv a prerequisite for your task
I've written a webcrawler that pulls information into a report and would like to run it every day at 12:00pm. The script is run using:
ruby script.rb
I've tried using the whenever gem (https://github.com/javan/whenever).
My directory structure is this:
/config
schedule.rb
script.rb
In my script.rb file, I have the following:
every :day, :at => '12:00pm' do
command "ruby script.rb"
end
I've modified the time :at to take see if it runs and it doesn't.
I've also tried:
every :day, :at => '12:00pm' do
`ruby script.rb`
end
I've also looked into the "at" linux utility but it appears suited to one-time jobs. I'd like this to generate a report everyday.
Note: the script specifies where to output so I don't need to give it an output.
I've also tried creating a crontab but have encountered a problem with saving.
I use http://crontab-generator.org/ to generate the correct syntax.
Then I run:
crontab -e
Which opens vi and I copy the syntax. However, it exits with a status of 1 and if I run:
crontab -l
It says there's no jobs listed.
I've also tried running this as the super user, and it exits the same.
The error message is
/usr/bin/vi" exited with status 1
I just want a command to run at a given time, what am I missing?
Edit
Does it matter that I'm on a Mac?
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}`
Im using Monit to monitor a ruby script that uses Ruby daemons gem, which launches a separate process with PID - following the instructions from Monitor ruby processes with Monit
In order to execute the ruby script I need to include RVM in the Monit start and stop strings, so I have access to all the gems.
However when .monitrc executes I get the following error:
$rvm_path (/usr/local/rvm) does not exist./home/william/.rvm/scripts/rvm: line 174: rvm_is_a_shell_function: command not found
/home/william/.rvm/scripts/rvm: line 185: __rvm_teardown: command not found
'myserver_1' failed to start
Aborting event
I added PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm to the start and stop command strings to try and include RVM. However still it doesn't work
Config file .monitrc:
....
check process myserver_1
with pidfile /home/william/ruby/barclays/myapp.rb.pid
start = "/bin/bash -c 'PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm && ruby /home/william/ruby/barclays/daemonloader.rb start'"
stop = "/bin/bash -c 'PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm && ruby /home/william/ruby/barclays/daemonloader.rb stop'"
....
Thanks for your help.
EDIT
Ive got a feeling the problem is related to environment variables. Quoting from this page
You should also know that for security reasons Monit purges the
environment and only sets a spartan PATH variable that contains /bin,
/usr/bin, /sbin and /usr/sbin. If your program or script dies, the
reason could be that it expects certain environment variables or to
find certain programs via PATH. If this is the case you should set the
environment variables you need directly in the start or stop script
called by monit.
Finally, Monit uses the system call execv to execute a program or a
script. This means that you cannot write shell commands directly in
the start, stop or exec statements. To do this, you must do as above;
start a shell and issue your commands there. For example:
start program = "/bin/bash -c 'my shell command && my other
command'"
Use this:
/path/to/rvm/bin/rvm in /path/to/project do ...
Replace the paths with proper directories for rvm and project and the ... with the commands to stop/start - try:
/usr/bin/env "HOME=/home/william rvm_path=/home/william/.rvm
/home/william/.rvm/bin/rvm in /home/william/ruby/project do
ruby daemonloader.rb start"
This command will load RVM, cd into project path, load ruby for this ruby and execute given command.
You could try something like this in Monit.
start = "/bin/su - william -c 'cd /home/william/ruby/project && ~/.rvm/bin/rvm default do bundle exec ruby daemonloader.rb start'"
This worked for me.
Mentioning the gemset and ruby source solves the problem for me.
start program = "/bin/bash -c 'cd /home/project_path && source /home/user/.rvm/environments/ruby-2.4.2#global && RAILS_ENV=production bundle exec rails s'"