Monday, January 25, 2010

Automated Creation of a new app on a Mongrel Cluster

This is a script to automatically get a mongrel app up and running. It should be placed in the rails directory – i.e. /var/www/ and then run using ./filename.rb (if you name the file filename.rb). it will ask a series of questions to get an app created with the mongrel cluster file linked and installed and running. To get it running do the following:

vi filename.rb ##or whatever you want to name it
Paste all the stuff below
:wq ##to close the file
chmod +x filename.rb ##permissions
./filename.rb ##run the file

There a few uncommented options for user, etc which you could use if you really want to. Basically they approot, user and apache and can be uncommented at the top of the file and everytime you run the file it will ask for these details as well.

NB: I offer NO WARRANTY and you should check the file before you run this in case it breaks something. you use it at your own accord.

########################it starts here####################
#!/usr/bin/env ruby

approot = '/var/www'
user = 'root'
apache = '/usr/local/apache2/conf/'

#puts "What is your rails app directory?"
#approot = gets.chomp

##choose either a rails or hobo app
puts "Is this a rails or hobo application- type rails or hobo(if installed)?"
apptype = gets.chomp

##what is the name of the application
puts "What is the name of the application?"
appname = gets.chomp

##are you using mysql or sqlite, etc
puts "What database should I use?"
database = gets.chomp

##is the user root or something else that will access the DB
#puts "Whats the name of the mongrel user - use root if you not sure?"
#user = gets.chomp

##what port should the mongrel ports start on?
puts "what is the users email address?"
email = gets.chomp

##what port should the mongrel ports start on?
puts "which port number should I start mongrel on?"
port = gets.chomp

##what environment should the mongrel in?
puts "which environment should mongrel start in- type development or production?"
environment = gets.chomp

##this will be either /etc/apache2/ or it will be /usr/local/apache2/conf/
#puts "where do I find the apache conf directory - either /etc/apache2/ or /usr/local/apache2/conf/?"
#apache = gets.chomp

##create the rails or hobo application
system "#{apptype} -d #{database} #{appname}"
##go to the application directory

system "echo 'user: #{user}' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'cwd: #{approot}/#{appname}/' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'port: #{port}' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'environment: #{environment}' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'group: root' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'address: 0.0.0.0' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'pid_file: log/mongrel.pid' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'servers: 3' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "echo 'set: mongrel_clean, true' >> #{approot}/#{appname}/config/mongrel_cluster.yml"
system "ln -s /#{approot}/#{appname}/config/mongrel_cluster.yml /etc/mongrel_cluster/#{appname}.yml"

##change these line if you change the apache install option
system "echo '' >> /#{apache}/extra/#{appname}"
system "echo 'ServerAdmin #{email}' >> /#{apache}/extra/#{appname}"
system "echo 'DocumentRoot /var/www/#{appname}' >> /#{apache}/extra/#{appname}"
system "echo 'Servername #{appname}' >> /#{apache}/extra/#{appname}"
system "echo 'ServerAlias #{approot}' >> /#{apache}/extra/#{appname}"
system "echo 'ErrorLog /#{approot}/|#{appname}#{appname}' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /images!' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /stylesheets !' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /javascripts !' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /favicon.ico !' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /static !' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /holding !' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass /templates !' >> /#{apache}/extra/#{appname}"
system "echo 'ProxyPass / balancer:///#{appname}_cluster' >> /#{apache}/extra/#{appname}"
system "echo '' >> /#{apache}/extra/#{appname}"
system "echo 'BalancerMember http://127.0.0.1:#{port}' >> /#{apache}/extra/#{appname}"
system "echo 'BalancerMember http://127.0.0.1:#{port}' >> /#{apache}/extra/#{appname}"
system "echo 'BalancerMember http://127.0.0.1:#{port}' >> /#{apache}/extra/#{appname}"
system "echo '
' >> /#{apache}/extra/#{appname}"
system "echo 'RewriteEngine On' >> /#{apache}/extra/#{appname}"
system "echo 'RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f' >> /#{apache}/extra/#{appname}"
system "echo 'RewriteRule ^/(.*)$ balancer://#{appname}_cluster%{REQUEST_URI} [P,QSA,L]' >> /#{apache}/extra/#{appname}"
system "echo '
' >> /#{apache}/extra/#{appname}"

#system "cd #{approot}/#{appname}"
system "/etc/init.d/mongrel_cluster restart"

#####################end of file######################

You can also find my complete server install which automates most items in a rails server install here.

I hope it works for you. Good Luck.

No comments:

Post a Comment