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.

Automatically install a Full Ruby on Rails Mongrel Server

I have created the install files for a Simple Easy to get running Linux Ruby on Rails Server. Once you have installed your server using the openssh server option (or type 'apt-get install ssh openssh-server -y' at the command line), login to the server remotely. Then all you do is copy the code below to a notepad(or it breaks some of the -- links) file then do the following:

vi railsinstall ##the name of the file, no extension
Paste all the stuff below
:wq #to close the file
chmod +x railsinstall
./railsinstall #run the file to get the process rolling

It will need some info for mysql, etc so some attendance is needed, but it does most things without attendance. Here it is:
#############File starts here#################
##things to do before you run the script:
##visit the following sites and get the download links for the latest programs by right clicking on the correct extensions and copying the link location.
## http://rubyforge.org/frs/?group_id=126&release_id=37073
## http://apache.org/dyn/closer.cgi
##Also place your linux install cd in the cdrom drive as some installs will require it
##change your git details before you run this file

echo "making system adjustments" >> install.txt
locale-gen en_US.UTF-8
/usr/sbin/update-locale LANG=en_US.UTF-8

echo "creating the sources directory" >> install.txt
mkdir ~/sources
mkdir /var/www/
cd ~/sources

echo "copying sources file and uncommenting all repositories" >> /root/sources/install.txt
cp /etc/apt/sources.list /etc/apt/sources.list.orig2
sed -i -e "s/# deb/deb/g" /etc/apt/sources.list

echo "updating your system prior to the install" >> /root/sources/install.txt
aptitude update
aptitude safe-upgrade -y
aptitude full-upgrade -y
apt-get -f install
dpkg --configure -a

echo "installing build-essentials" >> /root/sources/install.txt
apt-get install build-essential -y


echo "Installing basic system requirements" >> /root/sources/install.txt
apt-get install vim
apt-get install ssh openssh-server

echo "Installing ruby" >> /root/sources/install.txt
sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby ruby1.8-dev -y
sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

echo "Installing rubygems" >> /root/sources/install.txt
wget "http http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz"
tar -xvzf rubygems-1.3.5.tgz
rm rubygems-1.3.5.tgz
cd rubygems-1.3.5
sudo ruby setup.rb
cd ..
rm -r rubygems-1.3.5
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system

echo "Installing apache2" >> /root/sources/install.txt
apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
sudo a2enmod proxy
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart
#wget http://apache.is.co.za/httpd/httpd-2.2.14.tar.gz
#tar -xzf httpd-2.2.14.tar.gz
#cd httpd-2.2.14
#./configure -–enable-rewrite –-enable-proxy
#make
#make install
#/usr/local/apache2/bin/httpd -k start
#cd ..

echo "Installing mysql" >> /root/sources/install.txt
sudo gem install mysql
sudo apt-get install libopenssl-ruby -y
sudo apt-get install libmysql-ruby mysql-server -y
sudo apt-get install libmysqlclient15-dev -y

echo "Installing rails" >> /root/sources/install.txt
sudo gem install rails

echo "Installing mongrel" >> /root/sources/install.txt
sudo gem install mongrel
gem install mongrel -–remote –-version=1.2.4
gem install mongrel_cluster
mkdir /etc/mongrel_cluster
cd /etc/mongrel_cluster
cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d
chmod +x /etc/init.d/mongrel_cluster
/usr/sbin/update-rc.d -f mongrel_cluster defaults

Echo “install hobo” >> /root/sources/install.txt
gem install hobo

echo "Updating system" >> /root/sources/install.txt
gem update --system
gem update
echo "Please add the final touches to your system. You should now be able to install your application." >> /root/sources/install.txt


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

You may want to comment out hobo if you dont need it or any of the other items. Also check out the new app creation file also on this blog posted here which will automate your mongrel_cluster creation.

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.

I hope it works for you. Good Luck.

Installing a complete Ruby on Rails Server on Ubuntu

I have installed a few Rails servers using Ruby, Rails, apache, mongrel, etc. I got to a point that I got tired of following a file to do and having to monitor it. I tried passenger_stack and had problems with capistrano so eventually when I couldn't find the help I needed, I created my own fresh ubuntu linux 9.04 and newer(I think 8.04. will work as well if you really have to), based install file that automated the whole process. In order to get this running you will obviously need your install cd and preferably some knowledge of ruby, etc. Also, read the file as it requests you to get some file links for the latest stuff from relevant sites.

I usually right click on the download link and copy the link location and then change it in the script where its applicable. But, check the script if you not sure.

I hope this helps someone as its made my life a little simpler and its works for me 100%. There is also an added file which I drop into rails directory (/var/www/) and then edit the few options. When I run the file using ./filename.rb(whatever I named it.rb) it asks me the relevant questions, creates the application and the mongrel required files and sets it all up for me. Once I have edited the database and create the first controller for hobo or rails, it gives me the required screens. Then you ready to create your app without all the "copy to here" links, etc to get an app running. It also create the apache file, etc as it should be.

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.

Hopefully you can make this work for yourself. The links are:

Install a Rails server with Mongrel

Create Mongrel clusters easily

Good Luck.