Installing MySQL gem on OSX 10.6

Hopefully this will be a time saver for anyone else who goes through the following pain.

I have a brand spanking new OSX 10.6 installation.
I installed mysql 5.5.9-OSX10.6-X86 (because I thought everything was still i386 based…watch this space!)
I installed RVM and installed ruby 1.92 under RVM control.
I then do a ‘bundle install’ and it gripes about not having mysql2 gem installed. I do a manual install using the following command.

sudo gem install mysql2

and after a bit of crunching, we get to the following error:

Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

1.9.1/ruby/ruby.h:108: error: size of array ‘ruby_check_sizeof_long’ is negative
/Users/peter/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/ruby/ruby.h:112: error: size of array ‘ruby_check_sizeof_voidp’ is negative
In file included from /Users/peter/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/ruby/intern.h:29,
                 from /Users/peter/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/ruby/ruby.h:1327,
                 from /Users/peter/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/ruby.h:32,
                 from ./mysql2_ext.h:4,
                 from client.c:1:
/Users/peter/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/ruby/st.h:69: error: size of array ‘st_check_for_sizeof_st_index_t’ is negative

The Solution

Uninstall your MySQL using the commands below

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*

Now go and download the 64 bit version of the MySQL package – get the dmg rather than the gzip file.

The rationale behind this is that your new beaut 10.6 is actually referencing 64 bit modules by default. How to prove this I do not know – I’m confused and still searching for the answer. If I do a uname – a I get…

Darwin MacBook-Air.local 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386

Now, if I’m not mistaken that’s an i386 at the end – go figure. If anyone has any further insights please let me know.

The next step is to download the gem for your ruby/rails use

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- \
  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
  --with-mysql-include=/usr/local/mysql/include

The next error encountered is when I try to start the rails server using ‘rails s’

/Users/peter/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `require': dlopen(/Users/peter/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib (LoadError)

The answer to this problem is to run the following command…

sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle/

Bear in mind, I’m running version 1.9.2-p180 fo ruby – you will need to change the command for whatever version you’re running.

Share

Recovering the root password

A client who shall remain nameless recently required the installation of some spam filtering software (spamassassin) on their linux mail server. Not a problem I thought. “All I need is root access” I said. “Huh” he said. Turns out the their system administrator of 5 years had left to go travelling around Europe and no-one could remember where he left the folder with all the system documentation.  No root password, no history of what was installed when, no history of custom configuration changes. Nichts, Nada!

Question: How to get root access to a machine without the root password.

1. Reboot and select the grub ‘recovery’ option – you can get to this from the boot menu or if you have a linux box with a single boot option, hold down the shift key during boot (occasionally the escape key works – watch the messages during the boot sequence).

2. The next screen to display will present you with a couple of sub-options. You need to select the ‘Drop to root shell prompt’.

3. You should now be presented with a prompt sign. Now’s your chance to reset the password for the appropriate user. Enter passwd [username] where [username] is the name of the …you get the drift.

4. Next enter the new password and buy a 15ft billboard ad to display across the road from your client’s site. Of course the ad will display the encrypted version of the password:)

Share

msi wind u100, ubuntu and wireless grrrr!

Up until earlier this week, I had my lovely little netbook working nicely with Ubuntu 8.04 and a 90% satisfaction rate with the network manager utility. I could connect to my home network, office network and with abit of hassle, other free wifi spots. I started to get a message that my version of ubuntu was now unsupported, so I thought I’d set about upgrading to 10.04 (maybe 10.10 if I had the time).

I chose the default upgrade process using the system update options progressively moving from 8.04 to 9.04, 9.10 and finally settling on 10.04. I did all the upgrading with a hard-wired ethernet cable.

Bang!! unplugged the network cable and moved to the comfort of my living room to finish some minor tweaks. No wireless network connectivity. I thought network manager may have been giving me hell and I heard some good stories about wicd (which I have running with 100% success on my laptop).

Here comes a lesson…are you paying attention? I decided to uninstall network manager and install wicd (in that order). Doh!! Ok, now I have an unconnected netbook with no wireless and ethernet connectivity and I’ve spent the last two mornings on the way to work on the train trying to patch the system with uploads from a usb stick. No fun, no glory.

What I’m going to have to do is burn 10.04 to a bootable memory stick and do a re-install from scratch. Then I’m going to have to pray I can fix the original issue with wireless card. It’s an RA2860 and there’s bundles of articles in discussion forums regarding it’s incompatibility with Linux distros. Here’s praying I don’t have to install XP to get my work done on the train.

Share

Unix Shells by Example

Author Ellie Quigley

ISBN 0-13-147572-X

Published by Prentice Hall

Priced in Australia at $87.99 (June 2010)

No matter which shell you run and how ever long you’ve been using it, you’ll probably pick up a trick or two from this book.

Now in it’s 4th edition, this book is stuffed with real world examples of all the useful GNU/Linux shell tools. It focuses on awk, sed, grep and their many variations. It builds from simple examples with extensive use of regular expressions.

Although running to over 1,000 pages, the first 450 are where I found the real meat. The remainder is a comparison of the different shells and their many quirks

As far as I can see, you can approach this book in one of two ways.
Firstly as a student, set aside a few quiet Sundays and go through the examples one by one, some of it may feel like repetition, but progressively you’ll get the picture and it will be well ingrained in your brain.
Secondly as a reference. I use the bash shell, but I often find useful scripts created on csh or heaven’s forbid zsh. So the trick is knowing what’s different and what needs changing. To this end Unix Shells By Example is very handy to have on the shelf.

Share

Moving from Subversion to Git

I have been using subversion for a number of years and it wasn’t until I had seen and sampled the simplicity and power of git that I decided once and for all to bite the bullet and migrate all my subversion repositories to git.

I’d done the same from cvs to subversion in the early 2000′s and still think the move worked out well with a bit of preparation.

An unfortunate side effect of subversion is that it takes a good bit of effort to set up and manage a new repository so I ended up with two somewhat monolithic repositories…

1. archives
2. projects

The ‘archives ‘ repository contained all old project work and the ‘projects’ one contained relevant or current project work.

The result of this was that each archive had a bundle of sub-folders, each containing a project in its’ own right. Although git can handle the concept of sub-modules it’s not the best way to structure your project. In fact, it was a pretty lazy to structure my subversion projects in the first place but convenience overcame system administrative chores at the time.

This article explains the following:

1. Converting a monolithic subversion repository to a git repository
2. Breaking out the new git repository into a set of discrete repositories.
3. General backup process and scripts to copy the new repositories across to a backup system.

First off, a couple of conventions I keep are…

My server stores a central set of repositories with the name of myproject.git
My workstations work with their local version of repositories called myproject (no .git)
My server exports it’s .git repositories to a backup server using the same .git naming convention.
I have created a git.git user.group on my server to run all git processes. File ownership is given to this user so other users can’t fowl things up at least without thinking about it first.

Step 1 – Migrating to git from Subversion

I store all my git repositories on a server in the directory

/usr/local/share/gitrepos

All of my subversion repositories are stored in

/usr/local/share/svn

Even though each folder is accessible using standard paths and commands, I have to go through the subversion door using the same mechanism I would access it from a remote box, namely http://. Other people may use svn:// if that’s their way of working.

Migration is taken care of with a single command.

sudo git svn clone https://localhost/repository/projects --stdlayout --authors-file=/home/peter/authors.txt -t tags -b branches -T trunk /usr/local/share/gitrepos/projects.git/

An explanation of all the bits follow:

sudo – because I run everything as unprivileged user, sudo gives me the rights i need to create new files and folders.

git svn – this is the git subcommand that manages conversion of subversion repositories.

clone – make a copy of the repositories that I’m pointing at.

https://localhost/repository/projects – I access all my subversion repositories using secure http. This allows me to securely browse the content across the Internet and track things easily using Trac.

Depending on your setup, this parameter will contain the path to your svn repository,however you access it.

–stdlayout option tells git svn that my subversion repository is in the standard layout of trunk/branch/tags directories.

–authors file – this is a file I created by hand containing a list of all the people who committed project material in the past. It’d format is as follows:

peter= Peter Mac Giollafhearga <my email at mydomain.com>
simon= Simon Shagwell <simon\'s email at his domain.com>

-t tags -b branches -T trunk – these values are for completeness and I’m not sure they are necessary given the –stdlayout option, but if you’ve called your branches, tags and trunks anything different, this is how you find it.

Once the command has completed you should find a projects.git folder has been created and navigating into it you will see all your nasty subversion sub-folders which you should have set up as individual repositories in the first place…tut tut!

Step 2 – Breaking out Git into baby gits

The structure of the new git repository is something like this.

gitrepository/
  projects.git/
    project1/
    WorldDomination/
    SomeStuff/
    SomeotherStuff_V2/
    Demos/
    Downloads/
    .git/

The .git folder (you cans see it using ls -a) contains a list of all the git related material such as project history, revisions and tags.
Check you can see your history by typing

git log

The next task is to break the contents of the rather large ‘project’ folder into a git repository per project.
The tool for the job is a combination of the very useful git subtree command and a bit of custom shell scripting specific to this job.

The subtree functionality was written by Avery Pennarun and is hosted by the github site. You can download the git subtree command from the URL http://github.com/apenwarr/git-subtree. Installation instructions are on the same site so I won’t bore you with it here.

Once you have it installed you’re almost ready to roll. The following shell script has comments at the top to explain what it does. Save this to your favourite scripts folder and chmod it so it’s executable (chmod +x git_breakout.sh).

Then cd into the folder you created earlier (in my case /usr/local/share/gitrepository/projects). Run the script and watch the output. Depending on the size of your projects it will take a bit of time to complete; mine took about 20 minutes.

#!/bin/sh

#git_breakout.sh
#This script should be run from within a git repository folder that
#contains many child folders.
#It will create a branch for every subfolder it finds and a new
#top level folder for each.
#It then initialises a git respoitory, copying into the relevant branch

# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
  echo "This script must be run as root" 1>&2
  exit 1
fi

current_dir=`echo ${PWD##*/}`
git_user=git
git_group=git

for project in $( ls . )
do
  if [ -d $project ]; then
    project_lower=`echo $project.git | tr [A-Z] [a-z]`
    branchname=$project_lower."export"
    echo "performing subtree split..."
    git subtree split -P $project -b $branchname
    mkdir ../$project_lower
    cd ../$project_lower
    echo "initialising git"
    git init
    echo "fetching branch"
    git fetch ../$current_dir $branchname
    git checkout -b master FETCH_HEAD 2>&1
    cd ..
    echo "setting appropriate ownership"
    chown $git_user.$git_group -R $project_lower
    cd $current_dir
  fi
done

Once the script has completed, you should be able to see a new directory structure, something along the following lines.

gitrepository/
  projects.git/
  project1.git
  worlddomination.git/
  somestuff.git/
  someotherstuff_v2.git/
  demos.git/
  downloads.git/

Each folder is now its’ own git repository with it’s own internal version history, tags, branches etc.
cd into one of the folders and do a git log just to confirm you still have history.

The original projects.git can be removed (after backing it up for safety’s sake) if you’re satisfied everything is in place. We’re now ready to proceed with the next stage of the game, namely backing up our new git repositories to a remote share.

Step 3 – Backing up Git

I use a 2TB removable disk as a central data server for sharing files throughout my network. My thinking is that should the place ever burn down and I have the opportunity, there’s only one box I need to grab before rushing out the door. Of course, I burn frequent snapshots of this box to DVD.

I have created a ‘backups’ share on the disk which I map to my physical server using NFS.

The entry in my /etc/fstab file is something like this.

192.168.0.20:/DataVolume/backups  /usr/local/share/backups  nfs defaults    0 0

This means I can read/write to my backups folder as if it was a local folder on a local disk.

The job at hand is to be able to backup my git repositories using a cron task scheduled to run when everything is nice and quiet. Below is the script I use to run my git backups.

#!/bin/bash
# git_backup.sh
# Backup git repositories to another folder
 
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

git_command=/usr/bin/git
#Where they're going to be backed up
backup_dir=/usr/local/share/backups/git
#Location of current live repositories
repository_dir=/usr/local/share/gitrepos

cd $repository_dir
for project in $( ls . )
do
    if [ -d $project ]; then
    destination="$backup_dir/$project"

    if [ -d $destination ]; then
      cd $destination
      echo "pulling $project..."
      $git_command pull
      echo "done."
      cd $repository_dir
    else
      echo "mkdir $destination"
      mkdir $destination
      cd "$repository_dir/$project"
      echo "cloning $project..."
      $git_command clone -v -l --no-hardlinks . $destination
      cd ..
      echo "done."
    fi
    fi
done

Once this script is run, you ‘ll have a copy of all your repositories in the backup folder. The next time it’s run should only take a fraction of the time as it won’t have to reestablish the git repository again.

So there you have it, migrating a subversion repository to git is really simple, the fun starts when you try to play with the results. I hope this has been helpful to others faced with the same job. Any improvements please let me know.

Share

Ubuntu 10.04 setup for Ruby on Rails and general use

This extract is from notes I took during the process of setting up both a desktop and laptop for general use as a development environment for Ruby on Rails (RoR) and PHP development.

There were lots of other steps (custom gems and environment stuff I like), but most of what follows is generic enough to get the basics in place. Happy Hacking!!

Installing Skype

sudo apt-get install libqt4-dbus libqt4-network libqt4-xml libasound2
sudo dpkg -i skype-ubuntu-intrepid_2.1.0.81-1_amd64.deb

Installing Amarok

sudo apt-get install xine-ui libxine1-ffmpeg

To get remote shares mapping in fstab.

Firstly, install the smb client package

sudo aptitude install smbfs
sudo apt-get install portmap nfs-common

Then add the following line to /etc/hosts.deny:

echo portmap : ALL>>/etc/hosts.deny

By blocking all clients first, only clients in /etc/hosts.allow below will be allowed to access the server.

Now add the following line to /etc/hosts.allow. User your own IP address.

echo portmap : 192.168.0.20>>hosts.allow

Next create the mounting points on your system

sudo mkdir /mnt/<name of mounting point>
sudo mkdir /mnt/music
sudo mkdir /mnt/backups

Create a file in home folder called .smbcredentials (note the leading dot)

touch ~/.smbcredentials
echo username=[your network username] >> ~/.smbcredentials
echo password=[your network password] >> ~/.smbcredentials
chmod 600 ~/.smbcredentials

Now an entry in /etc/fstab using the ip address of your NAS

#Added a permanent share to shares on NAS box
#Note: everything that follows (up to the 0  0 should appear on the one line)
#and one line is required for each share you want to map

//192.168.0.20/<foldername on NAS> /mnt/<local foldername> cifs
uid=peter,credentials=/home/peter/.smbcredentials,domain=office,dir_mode=0777,file_mode=0777 0       0

Now, for a few of the other standard tools
Setup filezilla and limewire

sudo aptitude install filezilla

#Limewire - download LimeWireLinux.deb from limewire site
# reports missing dependencies
sudo dpkg -i LimeWireLinux.deb
limewire-basic depends on sun-java6-jre | icedtea-java7-jre |
sun-java6-jdk | icedtea-java7-jdk; however:
Package sun-java6-jre is not installed.
Package icedtea-java7-jre is not installed.
Package sun-java6-jdk is not installed.
Package icedtea-java7-jdk is not installed.
dpkg: error processing limewire-basic (--install):
dependency problems - leaving unconfigured

Open synaptic package manager. Go to the Settings/Repositories/Other sources tab.
Check the two default partner sites. Reload the package list and select the following

sun-java6-jre
sun-java6-plugin
sun-java6-fonts packages

Now retry limewire and you’re smoking.

Installing ruby and associated gems

sudo apt-get install ruby
sudo apt-get install rubygems
sudo gem install rake
sudo gem install rails

I tried to install capistrano using sudo but it wasn’t working on laptop however it did work on desktop

gem install capistrano

Then change ~/.bashrc to include path to new gems

emacs ~/.bashrc &
export PATH=$PATH:~/.gem/ruby/1.8/bin:/var/lib/gems/1.8/bin

Now install and configure mysql before trying to install any of the ruby mysql gems

install mysql server
sudo apt-get install mysql-server

Setup prompts you for a password – enter it, remember it and enter it again.

Install the mysql admin tools

sudo apt-get install mysql-admin

NB – must install the libmysqlclient[nn]-dev package
This has mysql_config bundled with it which is needed by the ruby gems

Execute the following to find out what version of libmysqlclient you’re running

dpkg --get-selections | grep mysqlclient

In my case it’s version 16, so execute the following

sudo apt-get install libmysqlclient16-dev

Next, you NEED TO INSTALL the ruby1.8 dev package
If you don’t you’ll get all sorts of mysql gem errors — frustrating!!!

sudo apt-get install ruby1.8-dev
sudo gem install mysql

Installing and configuring git

sudo apt-get install git-core
git config --global user.email

Installing apache

sudo apt-get install apache2

The line above should do it, if you get any glitches, try the following

sudo apt-get install apache2.2-bin
sudo apt-get install apache2.2-common
sudo apt-get install apache2-threaded-dev

Installing php

sudo apt-get install php5
sudo apt-get install php5-mysql

Anything else is stuffing around with particular ruby gems and dev tools so probably too specific for this note.

Share