How do i monitor independed applications with god? - ruby

I'm currently looking into options for process monitoring of Rails/Ruby Projects and quite like god.
But i cant really find anything on how to monitor multiple applications (for example 2 rails projects running on one machine) with god.
As far as i see it i just set up god (system ruby) and have each project add its own configuration (maybe somehow in a deploy-hook).
This should also work with the projects running different ruby versions (rbenv, rvm) or bundler, since the god ruby does not have to access any project code.
Does anyone already use it like this? Or is there a better approach?

I use god to watch all my stuff (unicorn, redis, resque workers). Basic setup is like this:
God is installed globally, loads on system startup and reads its config file, /etc/god/all.god.
/etc/god/all.god
files = Dir.glob "/etc/god/**/*.god"
files.each do |f|
next if f == '/etc/god/all.god'
God.load f
end
This file loads all config files in /etc/god and its children. Deploy scripts put config files there and tell god to (re)load them.
$ ls -l /etc/god
total 16
-rw-r--r-- 1 root root 108 2012-02-23 16:26 all.god
drwxr-xr-x 2 sergio sergio 4096 2012-03-20 20:59 app1_production
drwxr-xr-x 2 sergio sergio 4096 2012-03-27 00:58 app2_production
drwxr-xr-x 2 root root 4096 2012-04-23 01:37 util
$ ls -l /etc/god/app1_production/
total 0
lrwxrwxrwx 1 sergio sergio 55 2012-03-20 20:59 redis.god -> /srv/app1_production/current/config/god/redis.god
lrwxrwxrwx 1 sergio sergio 56 2012-03-20 20:59 resque.god -> /srv/app1_production/current/config/god/resque.god
lrwxrwxrwx 1 sergio sergio 57 2012-03-20 20:59 unicorn.god -> /srv/app1_production/current/config/god/unicorn.god
Here's a head of unicorn.god.
rails_env = "production"
pid_dir = "/srv/app1_#{rails_env}/shared/pids"
rails_root = "/srv/app1_#{rails_env}/current"
God.watch do |w|
w.name = "unicorn-#{rails_env}"
w.interval = 30.seconds # default
# unicorn needs to be run from the rails root
w.start = "cd #{rails_root} && /home/sergio/.rvm/bin/r193_bundle exec unicorn_rails -c #{rails_root}/config/unicorn/unicorn.#{rails_env}.rb -E #{rails_env} -D"
# QUIT gracefully shuts down workers
w.stop = "kill -QUIT `cat #{pid_dir}/unicorn.pid`"
# USR2 causes the master to re-create itself and spawn a new worker pool
w.restart = "kill -USR2 `cat #{pid_dir}/unicorn.pid`"
As you can see, unicorns are launched via rvm wrappers and therefore each new app can use its own ruby. Also, you provide your own start, stop and restart commands, so you can use god to watch any piece of software.
This approach works very well for me (so far).

Related

cgi script can't write to world writable file under apache

I'm attempting to create a simple CGI script using CentOS 7, Apache 2.4 and Ruby 2.0. All tools installed from official packages.
My script, which resides at /var/www/cgi-bin/test.cgi is:
#!/usr/bin/ruby
puts "Content-Type: text/plain\n\n"
begin
file = File.open("test.log", "a")
file.puts("foobar")
file.close
rescue Exception
puts "pwd: #{`pwd`}"
puts $!.inspect
end
When I load http://myhost/cgi-bin/test.cgi, I get the following:
pwd: /var/www/cgi-bin
#<Errno::EACCES: Permission denied - test.log>
However:
[root#host cgi-bin]# ls -l /var/www/cgi-bin
total 8
-rwxr-xr-x. 1 root root 153 Jul 10 22:03 env.cgi
-rwxr-xr-x. 1 root root 359 Jul 11 00:45 test.cgi
-rw-rw-rw-. 1 root root 0 Jul 11 00:42 test.log
How (and where) can I write data from inside this cgi script if not to a world-writable file in the script's own working directory?
SELinux was blocking the file writes. "setenforce Permissive" allowed them to go through. Edited /etc/sysconfig/selinux and rebooted to make permanent.

Use newsyslog to rotate log files, but only if they have a certain size

I'm on OS X 10.9.4 and trying to use newsyslog to rotate my app development log files.
More specifically, I want to rotate the files daily but only if they are not empty (newsyslog writes one or two lines to every logfile it rotates, so let's say I only want to rotate logs that are at least 1kb).
I created a file /etc/newsyslog.d/code.conf:
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
/Users/manuel/code/**/log/*.log manuel:staff 644 7 1 $D0 GN
The way I understand the man page for the configuration file is that size and when conditions should work in combination, so logfiles should be rotated every night at midnight only if they are 1kb or larger.
Unfortunately this is not what happens. The log files are rotated every night, no matter if they only the rotation message from newsyslog or anything else:
~/code/myapp/log (master) $ ls
total 32
drwxr-xr-x 6 manuel staff 204B Aug 8 00:17 .
drwxr-xr-x 22 manuel staff 748B Jul 25 14:56 ..
-rw-r--r-- 1 manuel staff 64B Aug 8 00:17 development.log
-rw-r--r-- 1 manuel staff 153B Aug 8 00:17 development.log.0
~/code/myapp/log (master) $ cat development.log
Aug 8 00:17:41 localhost newsyslog[81858]: logfile turned over
~/code/myapp/log (master) $ cat development.log.0
Aug 7 00:45:17 Manuels-MacBook-Pro newsyslog[34434]: logfile turned over due to size>1K
Aug 8 00:17:41 localhost newsyslog[81858]: logfile turned over
Any tips on how to get this working would be appreciated!
What you're looking for (rotate files daily unless they haven't logged anything) isn't possible using newsyslog. The man page you referenced doesn't say anything about size and when being combined other than to say that if when isn't specified, than it is as-if only size was specified. The reality is that the log is rotated when either condition is met. If the utility is like its FreeBSD counterpart, it won't rotate logs less than 512 bytes in size unless the binary flag is set.
MacOS' newer replacement for newsyslog, ASL, also doesn't have the behavior you desire. As far as I know, the only utility which has this is logrotate using its notifempty configuration option. You can install logrotate on your Mac using Homebrew

Can't embed bash command in to Chef Recipe

I'm attempting to embed a shell command in to my Chef Recipe, however when Chef executes the command it seems to get things wrong. Here's the resource in question:
script "create libs symlink" do
interpreter "bash"
user "root"
cwd "/home/robin/test"
code <<-EOH
ln -s $(ls -1 | grep '^[0-9.-]\+$') curr-version-libs
EOH
end
The /home/robin/test directory contains a folder called 19.26-3, so I'm expecting a symlink called curr-version-libs pointing at 19.26-3.
Instead, I'm ending up with a circular symlink:
drwxr-xr-x 4 root root 4096 Jan 17 22:35 19.26-3
drwxr-xr-x 2 root root 4096 Jan 17 22:35 config
lrwxrwxrwx 1 root root 17 Jan 28 17:31 curr-version-libs -> curr-version-libs
It seems that the $(ls -1 | grep '^[0-9.-]+$') is being removed and I'm ending up with the command ln -s curr-version-libs.
Does anyone know what's going on here? I've tried using an execute resource, but I get the same results.
If your 19.26-3 directory exists before chef run starts, then it is easy. If you are creating a symbolic link, I would recommend using link resource for that.
version = `ls /home/robin/test/ -1 | grep '^[0-9.-]+$'`.strip
link "/home/robin/test/curr-version-libs" do
to ::File.join( "/home/robin/test", version )
end
But if it is not there, I would recommend using ruby_block and defining your link resource dynamically.
ruby_block "create libs symlink" do
block do
version = `ls /home/robin/test/ -1 | grep '^[0-9.-]+$'`.strip
res = Chef::Resource::Link.new( "/home/robin/test/curr-version-libs", run_context )
res.to ::File.join( "/home/robin/test", version )
res.run_action :create
end
end
Edit: I corrected the answer by fixing regex and and calling strip before assigning to version as Robin proposed.
It appears you are calling out to the shell to create a sym link. In that case, a much better way to do this is to use the Chef link resource. I would never use the script or execute resource to do what you are doing.
Using the link resource, you would do the following:
link "/home/robin/test/curr-version-libs" do
to '/home/robin/test/19.26-3'
user 'root'
group 'root'
link_type :symbolic
action :create
end
A quick side commentary: I have mentored and tutored a number of folks to come up on Chef. Those who come to understand what is offered by the resources, providers, and lightweight resources (aka. LWRPs) are much happier and effective than those who just try to drop their old shell scripts into their cookbooks.
I highly recommend reading the Resource and Providers and Lightweight Resources Documentation
have you tried escaping dollar sign?
ln -s \$(ls -1 | grep '^[0-9.-]\+$') curr-version-libs

Checking output from "command" should contain unexpected crash with NilClass

In an effort to use Cucumber for a command-line script, I've installed the aruba gem as per the instructions provided. It's in my Gemfile, I can verify that the correct version is installed and I've included
require 'aruba/cucumber'
in 'features/env.rb'
In order to ensure it works, I wrote the following scenario:
#announce
Scenario: Testing cucumber/aruba
Given a blank slate
Then the output from "ls -la" should contain "drw"
assuming the thing should fail.
It does fail, but it fails for the wrong reasons:
#announce
Scenario: Testing cucumber/aruba
Given a blank slate
Then the output from "ls -la" should contain "drw"
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[] (NoMethodError)
features/dataloader.feature:9:in `Then the output from "ls -la" should contain "drw"'
Anyone have any ideas why this isn't working? This seems to be very basic aruba behavior.
You are missing a 'When' step - the aruba "output should contain" step requires the command to have already run (it does not run it itself, it only looks it up).
#announce
Scenario: Testing cucumber/aruba
Given a blank slate
When I run `ls -la`
Then the output from "ls -la" should contain "drw"
This produces, on my machine:
#announce
Scenario: Testing cucumber/aruba # features/test_aruba.feature:8
When I run `ls -la` # aruba-0.4.11/lib/aruba/cucumber.rb:56
$ cd /Users/d.chetlin/dev/mine/ladder/tmp/aruba
$ ls -la
total 0
drwx------ 2 d.chetlin staff 68 Feb 15 23:38 .
drwx------ 7 d.chetlin staff 238 Feb 15 23:38 ..
Then the output from "ls -la" should contain "drw" # aruba-0.4.11/lib/aruba/cucumber.rb:86
1 scenario (1 passed)
2 steps (2 passed)
0m0.465s

Curl::Multi.download is broken?

When I do
ruby-1.9.2-p180 :002 > Curl::Multi.download(["http://www.bbc.com"])
=> nil
The downloaded file is of zero size
ls -l
-rw-r--r-- 1 staff 0 Jan 5 19:51 www.bbc.com
But the same works with Curl::Easy.download
Curl::Easy.download("http://www.bbc.com")
=> #<Curl::Easy http://www.bbc.com>
ls -l
-rw-r--r-- 1 staff 111549 Jan 5 19:53 www.bbc.com
Am I missing something?
You're not - Curl::Multi.http (and Curl::Multi.download as well, since it actually calls Curl::Multi.http) have been broken in Curb since version 0.7.15.
This totally baffled me for awhile but I figured out what was wrong and have submitted a patch (see https://github.com/taf2/curb/pull/115).
Hopefully this gets merged upstream - in the meantime, you should stick to Curb 0.7.14 and below.
UPDATE: My pull request has been merged: https://github.com/taf2/curb/commit/dceafcd931c4fb7ad2c8d356537358fd1286e4ae - you can grab curb/master for the fix.

Resources