4th Floor Devbloggin

Asynchronous ramblings on development from the Valley Library

4th Floor Devbloggin

Rails Development in VirtualBox

September 23rd, 2009 · No Comments · Uncategorized

I took notes while I set up a VirtualBox machine for Rails development a few weeks back with the intent of writing another blog post similar to my post on creating a Django machine.

Here is what I did to create a dev environment running Ruby 1.8.7 with Rails 2.3.4 and  Posgtresql 8.3.7 on Ubutnu Server 9.04.

The first step was creating the virtual machine:

Download Ubuntu Server edition for your architecture and set up the box with minimal packages. Once you have the box up and running, power it down and add the following to your <ExtraData> section in ~/.VirtualBox/Machines/<machine name>/<machine name>.xml:

<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" value="2222"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" value="22"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" value="TCP"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/rails/HostPort" value="3001"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/rails/GuestPort" value="3000"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/rails/Protocol" value="TCP"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/pg/HostPort" value="3002"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/pg/GuestPort" value="5432"/>
 <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/pg/Protocol" value="TCP"/>

After saving the XML file, start up and update the guest machine:

sudo apt-get update
sudo apt-get upgrade

Set up ssh on the guest machine:

sudo apt-get install openssh-server

Now we can connect from the host machine’s terminal:

ssh localhost -p 2222

Set up postgresql:
Make backup copies of these files before changing!
in the file /etc/postgresql/8.3/main/postgresql.conf
add:

#listen on local network ip:
listen_addresses = '10.0.2.15'

I got the address of my guest machine (10.0.2.15) using ifconfig

in /etc/postgresql/8.3/main/pg_hba.conf add:

# allow my host machine to connect with a password
host    all         all         10.0.2.2	255.255.255.0    md5

This will allow you to connect to the Postgresql server from both the Rails app and from a Postgresql management tool running on your host machine such as PgAdminIII.

I got the address of my host machine (10.0.2.2) after a failed connection in /etc/logs/postgresql/postgresql-8.3-main.log
restart the postgresql server:

sudo /etc/init.d/postgresql-8.3 restart

Create a user and database:

sudo -u postgres createuser -D -A -P username
sudo -u postgres createdb -O username databasename

Set up Ruby:
Install with apt:

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 \
libruby1.8 libopenssl-ruby build-essential

(no line break)

Apt names ruby 1.8 with the version appended to the executable’s file name. Create a symbolic link to make things easier:

sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby

Install ruby gems:

wget http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz
tar xvzf rubygems-1.3.2.tgz
cd rubygems-1.3.2
sudo ruby setup.rb

Apt names these with the version appended as well, create symlinks:

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

Update ruby gems:

sudo gem update --system

Install rails:

sudo gem install rails

Install the postgres connector:
We need the postgresql dev library:

sudo apt-get install libpq-dev
sudo gem install pg

OK Now lets test it all out
Make a new rails project:

rails -d postgres applicationname

Edit applicationname/config/database.yml

adapter: postgresql
encoding: unicode
database: yourdatabasename
pool: 5
username: yourusername
password: yourpassword
host: 10.0.0.15

Rake it up!

rake db:create

You should get the message:
yourdatabasename already exists

If your rails app can connect to the database

Now start the server and start working!

script/server

In firefox: http://localhost:3001/

<pre>

Tags:

Django Development in VirtualBox

June 24th, 2009 · 1 Comment · Uncategorized

I am starting work on a Django project and wanted to familiarize myself with the Python language and Django’s concepts.

Instead of clogging up my workstation with the Django libraries and running my Django applications locally, I opted to run Django inside of a virtual machine. This gives me a sandbox to experiment with Django in without worrying about chainging settings or adding bloat on my workstation.

I am using VirtualBox 2.2.4 to host Ubuntu Server 9.04 running Python 2.6.2 and Django 1.0.2.

The first step was creating the VirtualBox Machine, I titled mine “djangobox” and gave it a conservative 8G of HDD and 512MB of RAM.

I then downloaded the Ubuntu Server .iso and mounted it in VirtualBox using the Settings -> CD/DVD-ROM menu.

After running through the default Ubuntu setup with no packages selected, my guest machine was up and running. The next step was to install SSH and Django on my virtual machine. The previous links were all I needed to get these running.

After following the default Django application instructions I was able to verify my setup was working by accessing my first Django application via Lynx from my guest machine, but how to access my development server from my workstation’s shell and browser?

I needed to configure VirtualBox to forward the SSH and Django dev server ports from my host machine to the guest machine. This was accomplished by adding the following lines to my guest machine’s configuration file, located in

~/.VirtualBox/Machines/<name of virtual machine>/<name of virtual machine>.xml

within the <ExtraData> section:

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/HostPort" value="2222"/>

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/GuestPort" value="22"/>

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/Protocol" value="TCP"/>

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/django/HostPort" value="8000"/>

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/django/GuestPort" value="8000"/>

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/django/Protocol" value="TCP"/>

Notice the use of “Devices/e1000“: Most of the tutorials I read were using an older version of VirtualBox and specified another device name here. I found the correct device name by looking through the log file in

~/.VirtualBox/Machines/<name of virtual machine>/Logs


After powering down and restarting the virtual machine, I could SSH into my virtual machine using ‘ssh -p 2222 localhost‘ from my workstation, but even after restarting my virtual machine and the Django dev server, I was unable to browse to my Django dev server from my workstation… After reading through the dev server documentation I noticed the following information:

“Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0.”

Restarting the Django dev server with “python manage.py runserver 0:8000” allowed me to access my Django dev server from my host machine at http://127.0.0.1:8000/

That’s it! Ready to start Django development on my virtual machine…

Tags:

Open Source Bridge ’09

June 23rd, 2009 · No Comments · Uncategorized

Open Source Bridge was a good experience and although a lot of the session’s topics didn’t relate to my current work focus, there were a few sessions that I learned a lot from and will be applying to both future and current projects.

The sessions were very open for dialogue and seemed to have a welcoming tone that encouraged people to speak up, ask questions, and share their own knowledge. The speakers at the sessions I attended were well informed and usually covered a wide breadth of information in the short periods.

I think the least useful section of the conference was the final day’s “unconference.” The topics seemed to veer off course from Open Source software, development, or even technology (examples: Where are the Geeky Girls and What are They Doing?, Telecommunication During a Plague, and Free Money – P2P Currency and Barter). It seemed to be a bit of a crapshoot as to which topics would be helpful or useful.

The best sessions I attended were:

PHP – Architecting and Profiling for Performance
This session was the most helpful for my current work focus and as a long-time PHP developer (for better or worse). Rasmus Lerdorf gave a brief history of the evolution of PHP over the last 14 years and framed his speech with the idea that the PHP was aimed at simplicity, speed and security.

The speech covered a number of useful tools for investigating the performance of your PHP applications. Some of the tools he covered were YSlow for examining your page’s bottlenecks, using strace to log your Apache server process, the PECL :: inclued package for looking at your application’s include hierarchy, as well as Callgrind and XDebug for looking at your program’s function flow and finding bottlenecks.

Notes for the speech are available here.

Building a SQL Database that Works
This session took a generalized look at creating high performance and sane relational databases. One of the most interesting topics Josh Berkus covered was the use of natural vs. surrogate keys in your database tables. This is a subject that has interested me since I first began creating MySQL databases for web applications.

Most modern frameworks force developers to create surrogate keys for the relational ‘magic’ that the frameworks perform on your data tables and this becomes a mute point, however, it is interesting to think about the best possible data schema vs. the schema you settle on due to framework and practical constraints.

Other topics covered included the history of the relational database and other pointers for creating a normalized and speedy data schema.

Making Twitter Suck Less with Perl
Twitter gets a bad rap from a lot of the developers I know. A lot of people see it as the next social media ‘fad’ site or a trivial way to chit-chat with friends or stalk celebrities. I agree to a certain extent that Twitter may not be the most innovative and impressive technical feat the world has ever seen, but I do think it is a valuable tool and a great way to keep updated on topics that interest me.

Johnathan Leto covered a number of Perl modules that make working with Twitter a snap, including Net::Twitter and some other interesting modules such as Log::Dispatch::Twitter which caused a bit of a stir with the more serious-minded Open Source fans. (“Who would want their server logs on Twitter!?!?”).

He also covered some of his own contributions to the world of Perl+Twitter such as his “itsabot” command line script and some of his contributions to the Net::Twitter module.

Tags: