Saturday, December 29, 2007

Create a subversion repository for rails



First you need a directory to house your repository. Since I work on multiple projects at home and in the office I need a master directory first. And just to make it simple I use /repos. This could be a link or a real directory.
(To clean-out a old svn directory use the following find . -name .svn -exec ‘rm -rf {}\;‘)
Step 1:

# create /repos

Step 2 (Create a repository):

# svnadmin create /repos/rails_project

Step 3 (Create rails specific directories):

# export REPOS=file:///repos/rails_project
# svn mkdir --message="Initial project layout" $REPOS/trunk $REPOS/tags $REPOS/branches

Step 4 (Initial Import of project):

# cd /rails_project
# svn import . file:///repos/rails_project/trunk -m "Initial Import..." */ This command import everything from the current directory to the svn server running locally using the file:/// structure */

Step 5 (check-out):

# svn co file:///repos/rails_project/trunk rails_project
# svn co svn://subversion_server/rails_project/trunk rails_project

Both of the above command will checkout the trunk of rails_project into a directory called rails_project.

Step 6 (Rails clean-up):

Go into the newly checked-out rails project

*/ Remove and Ignore the log files */  

# svn remove log/* 
# svn commit -m 'removing all log files from subversion' 
# svn propset svn:ignore "*.log" log/ 
# svn update log/ 
# svn commit -m 'Ignoring all files in /log/ ending in .log */ Remove and Ignore tmp directory */   
# svn remove tmp/* 
# svn commit -m 'removing all tmp artifacts from subversion' 
# svn propset svn:ignore "*" tmp/ 
# svn update tmp/ 
# svn commit -m "ignore tmp/ content from now on" */ Move the database config file to an example file and ignore */   
# svn move config/database.yml config/database.example 
# svn commit -m "Moving database.yml to database.example to provide a template for anyone who checks out the code" 
# svn propset svn:ignore "database.yml" config/ 
# svn update config/ 
# svn commit -m "Ignoring database.yml"

 

Tuesday, November 27, 2007

MySQL Dump shell script


I found this script over at http://crazytoon.com/ just thought I would put it on my post-it site to remember.

#!/bin/bash
db=$1
if [ “$db” = “” ]; then
echo “Usage: $0 db_name”
exit 1
fi
mkdir $$
cd $$
clear
for table in `mysql $db -e ’show tables’ | egrep -v ‘Tables_in_’ `; do
echo “Dumping $table”
mysqldump –opt $db $table > $table.sql
done
if [ “$table” = “” ]; then
echo “No tables found in db: $db”
fi

Wednesday, November 21, 2007

Working with Dell RAC

Well again I’m having issues with something. This time is the awesome easy to use greatly improved, best in class super administration toll RAC (Remote access console)from Dell. Because of the way that I’m located in the world, and my vpn connection to the datacenter I can’t get the rac browser plugin to work but there is supposed to be a ssh connection available also.

Got ssh working (enabled by default) just ssh root@server_ip and used the same password as the web client.

Now we need to enable the serial console features:

    $ racadm config -g cfgSerial -o cfgSerialConsoleEnable 1
    $ racadm config -g cfgSerial -o cfgSerialTelnetEnable 1
    $ racadm getconfig -g cfgSerial

    cfgSerialBaudRate=57600
    cfgSerialConsoleEnable=1
    cfgSerialConsoleQuitKey=^\
    cfgSerialConsoleIdleTimeout=300
    cfgSerialConsoleNoAuth=0
    cfgSerialConsoleCommand=
    cfgSerialHistorySize=8192
    cfgSerialCom2RedirEnable=1
    cfgSerialTelnetEnable=1
    cfgSerialSshEnable=1

Modify the Grand Unified Bootloader (GRUB) configuration.
In the Linux configuration file /etc/grub.conf, add the following
two lines in the general settings section of the file:
serial -–unit=0 -–speed=57600
terminal -–timeout=10 serial
Next, append the kernel and console=ttyS0,57600 options
to the kernel line. For example:
kernel /vmlinuz-2.4.20-8smp ro root=LABEL=/
console=ttyS0,57600
If a splashimage directive such as splashimage=(hd0,2)/
grub/splash.xpm.gz appears, comment it out.

Enable login to the console after boot. In the Linux configurationfile /etc/inittab, add a line to configure a getty on the
COM1 serial port as follows:
co:2345:respawn:/sbin/agetty -h -L 57600 ttyS0
vt100

Grant permission to initiate the session. In the Linux
configuration file /etc/securetty, add ttyS0 to the list of
supported ports.

Monday, November 19, 2007

Working with Linux LVM

Growing your lvm partition.

  • Find free space:
  • lvm vgs (VFree is amount of free space)
  • Find the the logical volum:
  • lvdisplay (LV Name is what you want)
  • Extend the drive (Add 50GB to current size) : 
  • lvextend –size +50G /dev/drive
  • unmount the drive (If drive is busy use lsof | grep /mountpoint to find the process that is using the mountpoint)
  • e2fsck -f /dev/drive
  • resize2fs /dev/drivemount
  • done

Its best if the drive is umounted before the lvextend command runs. (I have never had issues with this but you never know. ) 

Thursday, November 8, 2007

hobo 0.64 with rails



ok I know I’m newbo and this is lame BUT here are the steps to get hobo installed and running.There are NO documentation except blogs and new groups for the latests version so here goes…Install latest rails and hobo gems

$ gem install rails –source http://gems.rubyonrails.org$ gem install hobo 

Now get the rails version (its going to be rails 1.99.seomthing) and set the env variable:

$ gem list rails$ export RAILS_GEM_VERSION=1.99.0.8178 
$ hobo myapp
$ cd myapp
$ script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

update the myapp/config/database.yml with the correct database info and your are good-to-go.This is mine:

    development:  adapter: mysql  encoding: utf8  database: myapp_development  username: hobo  password: hobo_password  host: 10.0.0.200 

we also need to create the users table and admin user.

$ ./script/generate hobo_migration create_initial_tables$ g (for [g]enerate migration, don’t migrate yet.)

Now open the migration file db/migrate/001_creat_initial_tables and add the following after the create table end:

def self.up    
create_table :users do |t|      
t.string   :crypted_password, :limit => 40      
t.string   :salt, :limit => 40      
t.string   :remember_token      
t.datetime :remember_token_expires_at      
t.string   :username      
t.datetime :created_at      
t.datetime :updated_at    
end   

# create the admin user now because we can’t do it    
# through the web UI as it would fail validation    
admin = User.new    
admin.crypted_password = ‘ea87f9851d923a9a287631dd082799b83c0fa473′    
admin.salt = ‘7d8b386a7dfdcd0f268794038104791112cd4acd’    
admin.username = ‘admin’    
admin.save_without_validation  
end 

This will create a user ‘admin’ with password => password.
Now start the server

$ ./script/server

go to http://localhost:3000DONE!

Thursday, October 25, 2007

Deploying with Subversion tags in Capistrano



This is a 0.1 deploy method :) I have tested this and we are using this version in production BUT its is not stupid proof…

In the ./config/deploy.rb

# Setup the repository with the TAG variable ()
tag = (ENV[”TAG”] || “”)
set :repository, “http://svn.mypostit.local/#{application}/tags/#{tag}”
set :repository, “http://svn.mypostit.local/#{application}/trunk” if tag.empty?

Now you can deploy with:
[deployer@heimdull ]# cap deploy TAG=’my_tag’

Wow is that easy.. If the TAG environment variable is empty then it will use the trunk for the repository or else it will use whatever you put in TAG.

I guess I should check if the tag exists before continuing … maybe 0.2 :)

Monday, October 1, 2007

Show MySQL users from command line

Some times when you only have the MySQL command line client available and you need to “see” what users/grants are setup on the server here is a great little piece that gives you this information:

# mysql -e “SELECT User,Host,Password,Super_Priv,Grant_Priv,Repl_slave_priv,Repl_client_priv FROM mysql.user;”
+——+———-+———-+————+————+—————+—————-+
| User | Host | Password | Super_Priv | Grant_Priv | Repl_slave_priv | Repl_client_priv |
+——+———-+———-+————+————+—————+—————-+
| root | localhost | | Y | Y | Y | Y |
| root | hostname | | Y | Y | Y | Y |
| root | 127.0.0.1 | | Y | Y | Y | Y |
+——+———-+———-+————+————+—————+—————-+

Thursday, September 27, 2007

Deploying with capistrano to servers behind a firewall



First lets install the rsync_with_remote_cache gem:

Check-out from source
svn co http://svn.extendviget.com/lab/trunk/gems/capistrano_rsync_with_remote_cache

Add patch so we will deploy to all roles not just the :app role(this will ensure that migration will work)
vi lib/capistrano/recipes/deploy/strategy/rsync_with_remote_cache.rb

change this:

# Step 2: Update the remote cache.
logger.trace “copying local cache to remote”
find_servers(:roles => :app, :except => { :no_release => true }).each do |server|
system(”rsync -az –delete #{local_cache}/ #{user}@#{server.host}:#{repository_cache}/”)
# TODO: an alternate version that excludes SVN, if we wanted to use an export-like strategy,
# from Andrew Kreiling - however, if we do an svn export locally and then sync it up, it wouldn’t
# include .svn anyway so this may be moot
# system(”rsync -az –delete –exclude=.svn –delete-excluded #{local_cache}/ #{user}@#{server.host}:#{repository_cache}/”)
end

to this:

# Step 2: Update the remote cache.
logger.trace “copying local cache to remote”
find_servers(:except => { :no_release => true }).each do |server|
system(”rsync -az –delete #{local_cache}/ #{user}@#{server.host}:#{repository_cache}/”)
# TODO: an alternate version that excludes SVN, if we wanted to use an export-like strategy,
# from Andrew Kreiling - however, if we do an svn export locally and then sync it up, it wouldn’t
# include .svn anyway so this may be moot
# system(”rsync -az –delete –exclude=.svn –delete-excluded #{local_cache}/ #{user}@#{server.host}:#{repository_cache}/”)
end

Create the gem
rake package

Install the gem
gem install pkg/capistrano_rsync_with_remote_cache-2.1.gem

Verify
gem list capistrano_rsync_with_remote_cache

Now inside of the capistranos deploy.rb file add this
set :deploy_via, :rsync_with_remote_cache

This strategy maintains two cache directories:

    * The local cache directory is a checkout from the Subversion repository. The local cache directory is specified with the local_cache variable in the configuration. If not specified, it will default to “.rsync_cache” in the same directory as the Capfile.

    * The remote cache directory is an rsync copy of the local cache directory. The remote cache directory is specified with the repository_cache variable in the configuration (this name comes from the remote_cache strategy that ships with Capistrano, and has been maintained for compatibility.) If not specified, it will default to “shared/cached-copy” (again, for compatibility with remote_cache.)

Deployment happens in three major steps. First, the local cache directory is processed. If it does not exist, it is created with a checkout of the revision to be deployed. If it exists, but is not a checkout from the repository specified in the configuration, the cache directory is removed, then recreated as if it did not exist. If it exists, and is a checkout from the repository specified, it is updated to the revision to be deployed.

Second, rsync runs on the local side to sync the remote cache to the local cache. When the rsync is complete, the remote cache should be an exact replica of the local cache.

Finally, a copy of the remote cache is made in the appropriate release directory. The end result is the same as if the code had been checked out directly on the remote server, as in the default strategy.

Thursday, July 26, 2007

How to change the timestamp of a file



I ran in to this problem when moving log files around that the timestamp on some of the files changed. And just for consistence I wanted to change the timestamp of these files..

I found that you can specify the data using the touch command, like this:

[heimdull@hostname ~]# touch -t 200709280930 httpd.conf
[heimdull@hostname ~]# ll httpd.conf
-rw-r–r– 1 root root 1558 Sep 28 2007 httpd.conf
[heimdull@hostname ~]#

If you don’t want to change all the timestamps of the file use these options:
-a change only the access time
-m change only the modification time

View the access timestamp(last time the file was accessed)

ls -ul httpd.conf

View the modification time(Last time the files was changed)

ls -tl httpd.conf

Sunday, July 1, 2007

Capistrano with multiple environments



A few months ago I started poking around with Capistrano and now cap is all I use everywhere… This thing rocks. The first problem I had was multiple environments, we have Production, Staging, performance, integration, qa1,qa2,stability1 and stability2 ?!?! dude can you say excessive.. anyways heres how I managed around this:

This goes in your deploy.rb file:

desc "Set the target stage to production"
task :production do
before “deploy”, “mysql:backup”
set :deploy_via, :rsync_with_remote_cache
set :stage, “#{current_task.name.to_s}”
role :web, “192.168.1.1″, :ssl => true
role :app, “192.168.1.2″
role :db, “192.168.1.3″, :primary => true
role :db, “192.168.1.4″, :no_release => true, :replica => true
role :db, “192.168.1.5″, :no_release => true, :replica => true
end
desc “Set the target stage to staging”
task :staging do
before “deploy”, “mysql:backup”
set :deploy_via, :rsync_with_remote_cache
set :stage, “#{current_task.name.to_s}”
role :web, “192.168.2.1″, :ssl => true
role :app, “192.168.2.2″
role :db, “192.168.2.3″, :primary => true
end

desc "Set the target stage to perf"
task :perf do
set :stage, “#{current_task.name.to_s}”
role :web, “192.168.3.1″, :ssl => true
role :app, “192.168.3.2″
role :db, “192.168.3.3″, :primary => true
end

I think this worked better than the multi-stage extension the was added to Capistrano. (It’s very similar except you put all the stages in their own files.)

Now when you call a task add the environment

cap production deploy:cold

I also have two different ways of deploying since the production servers are behind a firewall I added the :deploy_via in the task which overrides the deploy method and uses rsync for these hosts. (Btw there is a bug in the rsync code which I blogged about here)