Run script before running tests in cocoa unit test target - ios

I'm currently testing an iOS app that communicates with an JSON API. I need to start a sinatra server before running the tests. The server works as a mock for the real API.
Is there any way to run a one line script like this one ruby /path/to/server.rb ?
Thanks

Go to "Mange Schemes" and select you scheme, then expand "Tests" and select "Pre-actions" and add a new run script:
Select "Provide build settings from:"
I think the variable you are looking for is ${SRCROOT}

In addition to #Sebastian answer make sure to add & after your ruby command since without it sinatra will block your tests execution.
Also it's useful to take care about post-action where you need to kill the ruby process.
Following example uses bundler and rackup to start sinatra.
Example for pre-action script:
exec > /tmp/tests-pre-actions.log 2>&1
source ~/.bash_profile
SERVER_PATH="${PROJECT_DIR}"/"Server"
cd "$SERVER_PATH"
bundle exec rackup > /tmp/server.log 2>&1 &
#get the PID of the process
PID=$!
#save PID to file
echo $PID > /tmp/sinatra.pid
Example for post-action script:
exec > /tmp/tests-pre-actions.log 2>&1
source ~/.bash_profile
PID=$(</tmp/sinatra.pid)
echo "Sinatra server pid $PID"
kill -9 $PID
config.ru for rackup gem:
require './server'
trap('TERM') {Process.kill 'INT', Process.pid}
puts 'Run sinatra'
run Sinatra::Application

Related

bundle command not found on bash script

I'm wrote a script to automatically run when reboot on crontab
this is my configuration in crontab -e
#reboot /home/deploy/startup_script >> /home/deploy/startup_script.log 2>$1
This start the script and create logs in /home/deploy
Then this is the startup_script
#!/bin/bash
echo "Changing directory"
cd /home/deploy/source/myapp
echo $PWD
echo "Pulling Dev Branch..."
git pull origin dev_branch
echo "Running Bundle Install"
sudo gem install bundler
bundle install
echo "Deploying to Staging..."
bundle exec cap staging deploy
when I run this script manually using ./startup_script it runs properly but when I run it automatically in crontab it shoes bundle command not found even I install the bundler already.
Here's the logs from startup_script.log
Changing directory
/home/deploy/source/myapp
Pulling Dev Branch...
From ssh://1.xx.xx.xx.io:20194/xx/myapp
* branch dev_branch -> FETCH_HEAD
Already up-to-date.
Running Bundle Install
Successfully installed bundler-2.0.2
Parsing documentation for bundler-2.0.2
Done installing documentation for bundler after 5 seconds
1 gem installed
/home/deploy/startup_script: line 12: bundle: command not found
Deploying to Staging...
/home/deploy/startup_script: line 15: bundle: command not found
The cron often clears the whole environment, including this $PATH variable. Therefore, the script may behave differently in your cron compared to the behavior in the shell. To avoid having to type the absolute path to a command, shells introduced the $PATH environment variable, each directory is separated by a : and searches are done from left to right.
Option I: You can use absolute path:
Run which bundle as sudoer to get the full path for the bundle command. If the output is /usr/bin/bundle, your bundle command in the script would look like:
/usr/bin/bundle install
Option II: Set the PATH variable:
Run echo "$PATH" as user who runs this script to get the $PATH variable and make sure this variable is available in your cron script too. For example, if the output was /usr/local/bin:/usr/bin:/bin, you would put the below line in the top of your shell script:
export PATH="/usr/local/bin:/usr/bin:/bin"
The environment that your crontab uses is going to be different than your regular login shell.
Now, I might be wrong about this, but I think when the crontab executes, it's not a login shell, so it doesn't have anything you've added to your path in your .bashrc or .bash_profile.
The best practice here would be to use the full path of the executable for bundle.
Redirecting stderr to stdout, there should be 2>&1
Is the path where the gem packages are installed is added to the $PATH variable? Try to provide the full path to this script
I suggest you make an entry to see what environment variables you have for crontab:
* * * * * printenv > ~/printenv.log

User job not enabled

i have a codestains.conf file in ~/.init folder
description "Codestains"
author "Varun Mundra"
start on virtual-filesystems
stop on runlevel [06]
env PATH=/opt/www/codestains.com/current/bin:/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/bin:/us$
env RAILS_ENV=production
env RACK_ENV=production
setuid ubuntu
setgid sudo
chdir /opt/www/codestains.com
pre-start script
exec >/home/ubuntu/codestains.log 2>&1
exec /opt/www/codestains.com/current/bin/unicorn -D -c /opt/www/codestains.com/current/config/unicorn.rb $
end script
post-stop script
exec kill 'cat /tmp/unicorn.codestains.pid'
end script
I have added https://gist.github.com/bradleyayers/1660182 in /etc/dbus-1/system.d/Upstart.conf` to enable Upstart user jobs
But everytime I run
start codestains
sudo start codestains
I get "start: Unknown job: codestains".
I have tried a lot of things available online. Nothing seems to help.
Also,
init-checkconf codestains.conf
gives "File codestains.conf: syntax ok"
I spot one error that is certainly a problem; I do not know if it is the only problem. I haven't made any attempt to test it. However, this bit:
exec kill 'cat /tmp/unicorn.codestains.pid'
is definitely wrong, it would pass the string cat /tmp/unicorn.codestains.pid to the kill command, which will not do what you want.
You may have seen an example, and missed that they are backtick characters, which causes the shell to execute cat /tmp/unicorn.codestains.pid, capture its STDOUT, and then interpolate the result where you put the backticks; IOW it passes the contents of that pid file to the kill command.
Like this:
exec kill `cat /tmp/unicorn.codestains.pid`
Note the subtly different backtick character
Which shells (bash, at least) will treat specially as I described: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html
(see the section on "Command substitution")
HTH

Invoke rake jobs:work automatically after running rails s in console

Can I invoke "rake jobs:work" automatically after running "rails s" in console?
Currently, after running rails s in cmd I will also run rake jobs:work in the other console, what i want to happen is After running "rails s" the jobs:work will automatically start.
The right way to go about this would be to use a process manager, like Invoker or Foreman. There is ample documentation on the links, but it boils down to the following steps:
Install the software
Create a configuration file where you declare what processes do you intend to run. Both support Procfile style declaration.
Use the command line client to start the process manager.
Based on my personal experience, I highly recommend Invoker, it goes beyond just a process manager, and packs in a few more handy features, like support for .dev local domain.
One you can do is simply:
rails server & rake jobs:work
It'll run rails server as background job, which you can get back to foreground with fg. It can be annoying that you'll get output from both processes mixed.
I'm not sure what are your needs and what you expect but maybe it would be good for you to use screen (or tmux) to run them in parallel and be able to switch between.
You can do your own .screenrc script which will run the server and any other commands when automatically for you.
There is a little problem that if you run the server from it and you close it (ctrl+c) than you'll loose it's screen window. Fortunately there is a solution for that as well (worked-out on the SO as well - you can read more about it here)
So, I use some helper script for that .run_screen (don't forget to chmod +x it):
#!/bin/bash
/bin/bash -i <<<"$*; exec </dev/tty"
Than I have .screenrc_rails file:
#shell -${SHELL}
caption always "%n(%t) %= %{b}#%H[%l] : %{r}%c:%s"
termcapinfo xterm ti#:te#
termcap xterm 'AF=\E[3%dm:AB=\E[4%dm'
terminfo xterm 'AF=\E[3%p1%dm:AB=\E[4%p1%dm'
startup_message off
screen -t server 2 ${HOME}/.run_screen rails s
screen -t spork 3 ${HOME}/.run_screen bundle exec spork
screen -t dev_log 4 ${HOME}/.run_screen tail -f ./log/development.log
screen -t test_log 5 ${HOME}/.run_screen tail -f ./log/test.log
screen -t bash 0
screen -t bash 1
And an alias ( screenr(ails) ) defined at .bash_profile:
alias screenr='screen -c ~/.screenrc_rails'
If you don't know screen than start from ctrl+a, ". ctrl+a, ? will give you some help.
I hope you'll enjoy it.

Bash Script to run three different rails apps on the local server?

I have three apps that I want to run with the rails server at the same time, and I also want the option to kill all the servers from one location.
I don't have much experience with Bash so I'm not sure what command I would use to launch the server for a specific app. Since the script won't be in the app directory plain rails s won't work.
From there, I suppose if I can gather the PIDs of the processes the three servers are running on, I can have the script prompt for user input and whenever something is entered kill the three processes. I'm just unsure of how to get the PIDs.
Additionally, each app has a few environment variables that I wanted to have different values than those assigned in the apps config files. Previously, I was using export var=value before rails s, but I'm not sure how to guarantee each separate process is getting the right variables.
Any help is much appreciated!
The Script
You could try something like the following:
#!/bin/bash
case "$1" in
start)
pushd app/directory
(export FOO=bar; rails s ...; echo $! > pid1)
(export FOO=bar; rails s ...; echo $! > pid2)
(export FOO=bar; rails s ...; echo $! > pid3)
popd
;;
stop)
kill $(cat pid1)
kill $(cat pid2)
kill $(cat pid3)
rm pid1 pid2 pid3
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
exit 0
Save this script into a file such as script.sh and chmod +x script.sh. You'd start the servers with a ./script.sh start, and you can kill them all with a ./script.sh stop. You'll need to fill in all the details in the three lines that startup the servers.
Explanation
First is the pushd: this will change the directory to where your apps live. The popd after the three startup lines will return you back to the location where the script lives. The parentheses around the (export blah blah) create a subshell so the environment variables that you set inside the parentheses, via export, shouldn't exist outside of the parentheses. Additionally, if your three apps live in different directories, you could put a cd inside each of the three parantheses to move to the app's directory before the rails s. The lines would then look something like: export FOO=bar; cd app1/directory; rails s ...; echo $! > pid1. Don't forget that semicolon after the cd command! In this case, you can also remove the pushd and popd lines.
In Bash, $! is the process ID of the last command. We echo that and redirect (with >) to a file called pid1 (or pid2 or pid3). Later, when we want to kill the servers, we run kill $(cat pid1). The $(...) runs a command and returns the output inline. Since the pid files only contain the process ID, cat pid1 will just return the process ID number, which is then passed to kill. We also delete the pid files after we've killed the servers.
Disclaimer
This script could use some more work in terms of error checking and configuration, and I haven't tested it, but it should work. At the very least, it should give you a good starting point for writing your own script.
Additional Info
My favorite bash resource is the Advanced Bash-Scripting Guide. Bash is actually a fairly powerful language with some neat features. I definitely recommend learning how bash works!
Why don't you try capistrano, framework for executing commands in parallel on multiple remote machines, via SSH. Its has lots of recipes to do this.
You are probably better off setting up pow.cx, which would run each server as it's needed, rather than having to spin up and shut down servers manually.
You could use Foreman to run, monitor, and manage your processes.
I realize I'm late to the party here, but after searching the internet for a good solution to this (and finding this page but few others and none with a full solution) and after trying unsuccessfully to get prax working, I decided to write my own solution to this problem and give it back to the community!
Check out my rdev bash script gist - a bash script you put in your ~/bin directory. This will create a new tab in gnome-terminal for each rails app with the app name and port in the tab's title. It verifies the app launched successfully by checking the port is in use and the process is actually running. It also verifies the rails app shutdown is successful by ensuring the port is no longer in use and the process is no longer running.
Setup is super easy, just change these two config values:
# collection of rails apps you want to start in development (should match directory name of rails project)
# note: the first app in the collection will receive port 3000, the second 3001 and so on
#
rails_apps=(app1 app2 app3 etc)
#
# The root directory of your rails projects (~/ is assumed, do not include)
#
projects_root="ruby/projects/root/path"
With this script you can start all your rails apps in one command or stop them all and you can stop, start and restart individual rails apps as well. While the OP requested 3 apps run, this will allow you to run as many as you need with port being assigned in order starting with 3000 for the first app in the list. Each app is started using the proper ruby version thanks to chruby and the .env is sourced on the way up so your app will have everything it needs. Once you are done developing just rdev stop and all your rails apps will be killed and the terminal windows closed.
# Usage Examples:
#
# Show Help
# ~/> rdev
# Usage: rdev {start|stop|restart} [app port]
#
# start all rails apps
# ~/> rdev start
#
# start a single rails app
# ~/> rdev start app port
#
# stop all rails apps
# ~/> rdev stop
#
# stop a single rails app
# ~/> rdev stop app port
#
# restart a single rails app
# ~/> rdev restart app port
For the record, all testing was done on Ubuntu 18.04. This script requires: bash, chruby, gnome-terminal, lsof and takes advantage of the BASH_POST_RC trick.

RVM isnt setting environment with cron

I'm having a rough time executing script/runner with a cron and RVM. I believe the issues lie with the rvm environment not being set before the runner is executed.
currently im throwing the error
/bin/sh: 1.sql: command not found
which is more than i've gotten earlier, so i guess that's good.
I've read this thread Need to set up rvm environment prior to every cron job but im still not really getting it. Part of the problem i think is the error reporting.
this is my runner thus far.
*/1 * * * * * /bin/bash -l -c 'rvm use 1.8.7-p352#2310; cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"'
as per the above link, i tried making a rvm_cron_runner.
i created a file and placed this in it:
#!/bin/sh
source "/Users/dude/.rvm/scripts/rvm"
exec $1
then i updated my crontab to this.
*/1 * * * * * /bin/bash -l -c '/Users/dude/development/app/my_app2310/rvm_cron_runner; rvm use 1.8.7-p352#2310; cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"'
This also has made no difference. i get no error. nothing.
Can anyone see what i'm doing incorrectly?
P.S i hope my code formatting worked.
Could you try to place the code you want to run in a separate script, and then use the rvm_cron_runner ?
So place your actions in a file called /path/cron_job
rvm use 1.8.7-p352#2310
cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"
and then in your crontab write
1 2 * * * /path/rvm_cron_runner /path/cron_job
The differences:
this does not start a separate shell
use the parameter of the rvm_cron_runner
If you would use an .rvmrc file, you could even drop the rvm use ... line, I think.
You don't need to write a second cron runner (following that logic, you might as well write a third cron runner runner). Please keep things simple. All you need to do is configure your cron job to launch a bash shell, and make that bash shell load your environment.
The shebang line in your script should not refer directly to a ruby executable, but to rvm's ruby:
#!/usr/bin/env ruby
This instructs the script to load the environment and run ruby as we would on the command line with rvm loaded.
On many UNIX derived systems, crontabs can have a configuration section before the actual lines that define the jobs to be run. If this is the case, you would then specify:
SHELL=/path/to/bash
This will ensure that the cron job will be spawned from bash. Still, your environment is missing, so to instruct bash to load your environment, you will want to add to the configuration section the following:
BASH_ENV=/path/to/environment (typically .bash_profile or .bashrc)
HOME is automatically derived from the /etc/passwd line of the crontab owner, but you can override it.
HOME=/path/to/home
After this, a cron job might look like this:
15 14 1 * * $HOME/rvm_script.rb
What if your crontab doesn't support the configuration section. Well, you will have to give all the environment directives in one line, with the job itself. For example,
15 14 1 * * export BASH_ENV=/path/to/environment && /full/path/to/bash -c '/full/path/to/rvm_script.rb'
Full blog post on the subject
You can use rvm wrappers:
/home/deploy/.rvm/wrappers/ruby-2.2.4/ruby
Source: https://rvm.io/deployment/cron#direct

Resources