s

Posts Tagged with "script"

Download Files with Ruby

Feb 17, 2010  -  Comments

This weekend, I remembered an old podcast I used to listen to that I wanted to hear again. It wasn't available on iTunes anymore, so I did a Google search for it. I found the show's Web site, and saw that they had a complete archive of all four seasons of the podcast (20 episodes per season). The problem was, each episode was on it's own page, with a separate page to access the download link. I started to manually try to download each episode, but that got old REALLY quickly. Why not write a simple script to do the work for me?

Here's the Ruby script I whipped up in about 15 minutes:

require 'net/http'

1.upto(4) do |season|
  1.upto(20) do |episode|
    episode = '%02d' % episode
    puts "Downloading Season ##{season}, Episode ##{episode}"
    Net::HTTP.start("website.com") do |http|
      resp = http.get("/episode_archive/s#{season}ep#{episode}.mp3")
      open("s#{season}ep#{episode}.mp3", "w") do |file|
        file.write(resp.body)
      end
    end
    puts "Episode downloaded!"
    puts
  end
end

puts "All files downloaded!!"

An explanation of what's going on here:

Lines #3-4: There are 4 seasons and 20 episodes per season, so iterate through them.
Line #5: We need to left pad the episode numbers, because that's how the files are named.
Line #7: Connect to the host.
Line #8: Get the episode (named like "s1ep05.mp3").
Lines #9-11: Save the file using the same name.

Tagged: rubyscript

CentOS Setup Script with Ruby, Apache, mySQL, Subverion, Git, Passenger, and ImageMagick

Aug 31, 2009  -  Comments

Recently, I participated in the Rails Rumble competition. If you've never heard of the Rails Rumble, it's a 48-hour coding contest where teams of 4 programmers/designers have to complete a fully-working application over the course of a weekend.

Linode, one of the sponsors, provided each team with virtual private server to host their application. This was great, but the servers were bare-bones and required a fair amount of setup. Knowing this in advance, I hoped to save some time by writing a script that would automate the process for our team so I didn't have to spend 30-45 minutes just trying to configure the server.

Here are the steps I took to get Ruby, Apache, mySQL, Subversion, Git, Passenger, and ImageMagick set up on our CentOS server.

Step 1: Add custom repository listing

The default CentOS repo only has Ruby 1.8.6. While there's nothing wrong with this, I wanted to get 1.8.7. To do that, copy the following text into a file called /etc/yum.repos.d/rubyworks.repo:

[rubyworks]
name=RubyWorks
baseurl=http://rubyworks.rubyforge.org/redhat/$releasever/RPMS/$basearch
enabled=1
gpgcheck=1
gpgkey=http://rubyworks.rubyforge.org/RubyWorks.GPG.key
priority=1

Step 2: Bash script

The only other step is to copy the following bash script to a file (called anything you'd like), chmod it to be executable, and just run it as root (very important!). It's magic!

#!/bin/bash

# You must run as sudo!!!!!

# update package list
yum update -y

# get all the packages required for compiling source
yum groupinstall 'Development Tools' -y

# install apache
yum install httpd mod_ssl -y

# install mySQL
yum --enablerepo=rubyworks install mysql-server mysql mysql-devel mysql-ruby -y

# set mysql and apache to start on boot
/sbin/chkconfig httpd on
/sbin/chkconfig --add mysqld
/sbin/chkconfig mysqld on
/sbin/service httpd start
/sbin/service mysqld start

# install postfix and subversion
yum install postfix subversion -y

# install git and dependencies
yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel -y
wget http://kernel.org/pub/software/scm/git/git-1.6.4.2.tar.gz
tar zxvf git-1.6.4.2.tar.gz
cd git-1.6.4.2
make prefix=/usr/local all 
sudo make prefix=/usr/local install
cd ..

# install ruby
yum install --enablerepo=rubyworks ruby ruby-devel ruby-irb ruby-rdoc ruby-ri

# install ImageMagick and all of it's required packages
yum install tcl-devel libpng-devel libjpeg-devel ghostscript-devel bzip2-devel freetype-devel libtiff-devel -y
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar zxvf ImageMagick.tar.gz
cd ImageMagick-*
./configure --prefix=/usr --with-bzlib=yes --with-fontconfig=yes --with-freetype=yes --with-gslib=yes --with-gvc=yes --with-jpeg=yes --with-jp2=yes --with-png=yes --with-tiff=yes
make clean
make
make install
cd ..

# get rubygems
wget http://rubyforge.org/frs/download.php/57643/rubygems-1.3.4.tgz
tar xvzf rubygems-1.3.4.tgz
cd rubygems-1.3.4
ruby setup.rb
cd ..

# install rails
gem install rails --no-rdoc --no-ri

# install rmagick
gem install rmagick --no-rdoc --no-ri

# install mysql gem
gem install mysql --with-mysql-config=/usr/bin/mysql_config --no-ri --no-rdoc

# install passenger and apache module
gem install passenger --no-ri --no-rdoc
passenger-install-apache2-module

DISCLAIMER: This worked great for me on a Linode 360 running CentOS 5.2. If you have any additions or changes, leave them in the comments and I'll be sure to add them. You can also get the entire script in a gist here.

Tagged: scriptcentos

Ruby Script to Add Apache Virtual Host Entry

Aug 10, 2009  -  Comments

When you're working with Rails, you never really have to add a virtual host entry for development (unless you use Passenger). You can always just fire up script/server and navigate to http://localhost:3000.

At my new job, I'm doing a lot of work on PHP and Drupal sites, which require you to add an entry to your host file and add a virtual host conf file for Apache. After only going through this process twice, I was already tired of it. I wrote the following script to automate the process for me.

#!/usr/bin/ruby

########################################
##### VARIABLES YOU NEED TO CHANGE #####
########################################

host_dir  = '/etc/hosts/index.html'            # path to your hosts file
sites_dir = '/Library/Webserver/index.html'    # path to the directory where you keep your sites (NO TRAILING SLASH!!!)
conf_dir  = '/etc/apache2/sites/index.html'    # path to directory where named conf files live

########################################

unless ARGV[0]
  puts "Usage: add_site sitename [hostname_for_url]"
  puts "Example: add_site sample sample.dev"
  exit
end

name = ARGV[0].strip
hostname = ARGV[1].nil? ? ARGV[0] : ARGV[1].strip

# first things first: make sure named conf file doesn't exist already
if File.exists?("#{conf_dir}/#{name}.conf")
  puts "Conf file named #{name}.conf already exists!"
  exit
end

# check to make sure host file exists
if File.exists?(host_dir)
  
  puts "Adding entry to #{host_dir}."
  File.open(host_dir, 'a') do |host_file|
    # append host entry to end of file
    host_file.puts "127.0.0.1\t#{hostname}"
  end
  puts "Host entry added!"
  
  puts "Adding named conf file."
  File.open("#{conf_dir}/#{name}.conf", 'a') do |host_file|
    # add entry
    host_file.puts <<EOF
<VirtualHost *:80>
  ServerName #{hostname}

  DocumentRoot "#{sites_dir}/#{name}"
	DirectoryIndex index.php
	<Directory "#{sites_dir}/#{name}">
		Options FollowSymLinks MultiViews Includes
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>
EOF
  end
  puts "Conf entry added!\n"
  
  puts "Restarting apache.\n"
  system "apachectl graceful"
  puts "Done!"
  
end

What you need to do to get this to work:

  1. Change the variables at the top of the file (path to your hosts file, path to the folder where you keep your development site, and path to the directory where you want to keep your named conf files).
  2. Rename the file to add_site(with no extension) and move to your /usr/bin directory.
  3. Chmod the file to be executable.

Now you can run the add_site command and provide it with the name of the site folder and optionally the name of the local domain you'd like to use.

Tagged: rubyscriptapache

Generate random text with Ruby

Jun 07, 2007  -  Comments

UPDATE: Changed the generate_password method slightly based on commenter Dave Burt's suggestion.

I'm working on a project where the user can reset their password if they've forgotten it. So, I need to generate a random password and email it to them so they can login and change it. Fortunately, I found this neat little Ruby snippet that will generate random text of a given length:

def generate_password(length=6)
  chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789'
  password = ''
  length.times { |i| password << chars[rand(chars.length)] }
  password
end

Some examples:

generate_password
>> U48ydn

generate_password(10)
>> QzWXdAkDy5

Tagged: rubyscripttutorial

Ruby compressor for JavaScript and CSS files

Dec 11, 2006  -  Comments

On Friday, Adam and I were talking about the different JavaScript libraries that are available and how bloated they are. They can easily double the page weight of your site. But, if you want to use the pre-canned goodness of Prototype or Scriptaculous, then that's the price you pay. We were trying to find ways to decrease the amount of files a user downloads when they visit our company Web site. One idea was to compress the CSS file and all of the large JavaScript files that we use. Well, I found a cool online service to compress and combine JavaScript files, but there were no readily available sources for shrinking any other kind of file that I could easily find. So, Adam told me to use sed via the command line and use the power of regular expressions to strip line breaks, tabs, and spaces out of the given file. Well, I don't know anything about regular expressions, and Adam couldn't recall the exact syntax off the top of his head. So, he retired to his corner and whipped up a nice little Ruby program to do it for us.

Enter the Shrinker!

You can download the Ruby file below. If you're on a Mac, just copy it to /usr/local/bin then CHMOD 777 it. After that, you can simply run sudo shrinker path/to/file and it will spit out a new file with the extension .shrunk that you can now name anything you want. Windows users can dump the file anywhere they want and then run, from the command line: ruby path/to/shrinker.rb path/to/file to get the same thing.

File: shrinker.rb (UPDATE: This file is no longer available, sorry.)

Tagged: rubyscriptjavascriptcss