CNK's Blog

Rails Installer on Windows XP

I hear good things about using Rails Installer on Windows but thought I should have some first hand experience before the RailsBridge install fest on Friday. So I fired up the old Windows XP box in the back room and after installing all the Windows updates, I got started. I clicked “Download the Kit” and double clicked it to run. Almost immediately I got a warning about a file that was not accessible AND a popup window from Semantec AntiVirus. So I launched the tool and under “Configure” -> “File System Auto-Protect” turned off the auto-protect while the installer was running.

In the command prompt that was supposed to configure git and ssh, I seem to already have values. It came up with user.name and user.email for me - but I am not sure where it got my name and email address. Is that configured somewhere in Windows? And I should have checked for ssh keys before I ran the installer. It has a key pair with an older date - but it isn’t my usual pair. It might be my key pair from when I was playing with PGP encrypting my mail on ugcs ages ago. But not I don’t see that pair there so I have my doubts.

The stated versions of things are: Git 1.7.9.msygit Ruby 1.9.3p125 Rails 3.2.1

Looks pretty good. Wonder if there would be any problems upgrading the Rails gems to pick up the security patches.

Rails new works and I can start up Webrick in the usual way. Semantec asks me if I want to keep blocking "Ruby" and I answered no - and then I could see the welcome page at http://localhost:3000

Github stuff

The installer creates a task called publickey that copies the generated public key to your clipboard. On XP it complained about ‘clip’ not being a recognized command - but then worked just fine. I could paste my new public key into the appropriate place in Github. I think the path changes that RailsInstaller made only work in the Command Prompt app. I could push to gethub from the terminal but NOT from the shell in emacs. In emacs, my pushes just hung and never timed out or errored. I could Ctr-C out of them - and it always asked me if I wanted to terminate a batch job. I doubt this will bother any of our RailsBridge students so I am not going to investigate any further.

The video on the RailsInstaller site discusses deploying on EngineYard. We are planning to deploy the RailsBridge stuff on free Heroku instances, but I suspect some of the hints may be of use to us. EngineYard uses MySQL instead of Postgres but the “add the database driver for group :production” is the same. The video then says to run “bundle install –without production” so that version information about the database driver gets into the Gemfile.lock. THEN the video told me to edit Gemfile.lock and delete “x86-mingw32” from the version numbering for mysql2 - and anywhere else I see it. It also said to add “ruby$” as a line under PLATFORMS (above the “x86-mingw32” line).

Installing PostgreSQL with homebrew

When I was poking around to see what was installed, I found psql in /usr/bin/psql and I had taken that to mean that I already had PostgreSQL installed. However, looking at the listening ports and in the socket directory, I don’t see any evidence of a running server. Hmmm……

  $ which psql
  /usr/bin/psql

  $ psql --version
  psql (PostgreSQL) 9.0.5
  contains support for command-line editing

  $ ps -ef | grep -i sql
  # .... only see stuff for mysql

  $ lsof -i -P | grep LISTEN
  ruby      16322  cnk   11u  IPv4 0xffffff801910cfa0      0t0  TCP    *:3000 (LISTEN)
  mysqld    29721  cnk   10u  IPv4 0xffffff8018365500      0t0  TCP    *:3306 (LISTEN)

  $ find /usr -name pg_ctl
  # Got nothing

  $ psql
  psql: could not connect to server: Permission denied
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

   $ sudo ls -al /var/pgsql_socket
  total 0
  drwxr-x---   2 _postgres  _postgres   68 Apr 26 16:40 .
  drwxr-xr-x  28 root       wheel      952 Jul  4 12:16 ..

Googling around it appears that the Lion Server may have the full PostgreSQL server running but my laptop seems to only have the psql client installed. OK let’s whip out homebrew. There appear to be formulas for versions 8 and 9 - but the default is now 9 so:

    $ brew install postgresql

That compiled and installed PostgreSQL 9.1.4. Then I followed the instructions given at the end of the install:

    If this is your first install, create a database with:
      initdb /usr/local/var/postgres

The initdb had a couple of interesting messages. First, The files belonging to this database system will be owned by user "cnk". This user must also own the server process. Ok that isn’t a problem - on a laptop anyway. And it also warned me that it was enabling "trust" authentication for local connections. The notes in /usr/local/var/postgres/pg_hba.conf warn me that local “trust” authentication means that any local user can connect as any databases user - including the database superuser. Again more permissive than I generally am, but probably OK - and certainly convenient - as long as I am only accepting connections from localhost.

The next useful piece of information from the brew install script was how to have PostgreSQL start up when I log in:

    If this is your first install, automatically load on login with:
      mkdir -p ~/Library/LaunchAgents
      cp /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
      launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Homebrew also shows it’s Ruby leanings by helpfully suggesting the compile flags I may want when installing the pg ruby gem. Since I may want this available to me in a variety of gemsets, let’s see if I can just install it in the global gemset for Ruby 1.9.3 (the only Ruby I have installed at the moment).

    $ rvm gemset use global
    $ env ARCHFLAGS="-arch x86_64" gem install pg
    Fetching: pg-0.14.0.gem (100%)
    Building native extensions.  This could take a while...
    Successfully installed pg-0.14.0

OK so now that we are installed and running, let’s log in:

  $ psql
  psql: FATAL:  database "cnk" does not exist
  $ createdb cnk
  createdb cnk
  [10:46 PM] (brazen:~) $ psql
  psql (9.1.4)

  cnk=#

Notes for Boulder RailsBridge Workshop

I am going to be teaching at a RailsBridge workshop in 2 weeks and thought I should work through the course at least once before I stand up in front of an audience! I’ll need to work through the install instructions for some other operating systems (at least the Windows instructions since that is where I have the least experience) but thought I would start by making sure my new Mac conformed to their standard setup. I had already set up Xcode, RVM, and Rails as described in a previous blog post.

So I reviewed the install instructions. Things have gotten pretty hairy on the Mac side but mostly looks OK - except they didn’t have anything about the Xcode command line tools killing terminfo files.

Since RailsBridge recommends KomodoEdit, I thought I should try it. One thing to note, it offers you some built-in Rails help - but all of that is for Rails 2 and does not seem to have been updated for Rails 3. Perhaps we should note that in our install or use instructions. I am really tempted to cheat and just tell KomodoEdit to use emacs key bindings - but that would defeat the purpose of my using the same text editor as our beginning students. So I needed some instructions on how to use it. Found these screencasts but am not yet sure how helpful they are going to be.

Outline

OK so time to start working through the curriculum.

  1. In the Ruby/irb section, make sure to emphasize that irb will echo the output from the last command. Make sure you know if the command you just ran changed the variable you are working with - or just handed you back a new value that is NOT associated with the variable, e.g. talk about the difference between flatten and flatten!.

  2. Discuss how web sites work, requests & responses.

  3. Generate topics scaffold. Walk through it in general terms - database, routes, controllers, views.

  4. Play with generated code in the browser. Suggest adding quiet_assets and thin gems in development block so log output is more sensible.

  5. Add votes resource.
    • Resource is like a scaffold but without the full CRUD support.
    • Add associations. Now a good time to play with “rails console”?
  6. Add button. Then go use it. Ooops, error message. Talk about how to read messages. Mention that Rails shows informative error messages in development mode - and opaque ones in production mode.

  7. Add controller. Discuss each part. Find, build, save!, redirect.

  8. Oooops again. I didn’t read carefully and missed the line about showing the votes. Itterate towards it: display topic.votes, topic.votes.size, add ‘votes’ - but “1 votes” looks dorky, use pluralize.

  9. Commit and Push!

  10. Rearrange page flow after adding new topics. While we are in the application layout page, mention we could spruce things up a bit. Also mention the csrf_meta_tags - built in rails security feature. If we are running ahead of schedule, mention the .json format and AJAX

  11. Simplify topic index page. Discuss link_to. Move delete links to topic details page. Discuss instance variables and loop variables. And how about displaying votes on the show page?

Extras

  1. We didn’t discuss validations any where. Add that for the topics - need a title and description.

  2. Mention the generated tests?

  3. Order topics by vote count.
    • First you need to know the difference between inner and outer joins. And then, the ActiveRecord syntax for each. If you just say “Topic.joins(:votes)”, you get an inner join - so you can never vote for a topic that doesn’t already have a vote. Dho!
      Topic.joins(:votes).group(:topic_id).count(:id)
      SELECT COUNT("topics"."id") AS count_id, topic_id AS topic_id
      FROM "topics" INNER JOIN "votes" ON "votes"."topic_id" = "topics"."id"
      GROUP BY topic_id
       => {1=>2, 2=>5}  
    • The query we want is:
      select topics.*, count(votes.id)
        from topics left outer join votes on topics.id = votes.topic_id
       group by topics.id, topics.title, topics.description; 

      To get that from ActiveRecord, we need to use some SQL fragments along with our other ARel methods - in part so that we can explicitly name the column we want to order by.

      Topic.select("topics.*, COUNT(votes.id) AS count_votes")
      .joins("LEFT OUTER JOIN votes ON topics.id = votes.topic_id")
      .group("topics.id")
      .order("count_votes DESC")
    • Another option is to add a counter cache to Topics and then increment_counter on the topic after each vote is saved (discuss ActiveRecord callbacks). Another option is to just move the votes directly into Topics. Should we? why or why not? Discuss.
  4. Making this site look less ugly. Any front end folks? Talk about the asset pipeline.

Diagrams I need:

  1. How a web site works. Click link in browser -> request -> server -> file or script -> response -> renders in your browser

  2. Our development cycle. Decide what we want -> generate/write code -> play with it locally -> git commit -> push to heroku

Things to suggest for the install notes:

  1. The problem with Xcode command tools and terminfo. Find my backup of the terminfo files and post on this site.

  2. Since we are planning to tell folks about ri in class, add the “complile docs” step to the RVM notes: $ rvm docs generate

  3. How about mentioning how to look at your SQLite3 tables in a GUI? A super simple option being: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  4. What about using RailsInstaller on Mac? Or can we avoid the Xcode mess if we use rubyenv? That helps with Ruby, but what about git? and sqlite3? We can get .dmg files for git. Database? We could use MySQL .dmgs if we like.

  5. Typo in http://curriculum.railsbridge.org/curriculum/CRUD_with_scaffolding?back=creating_a_migration%23step2 Second bullet point under model belongs under view. Add a different line explaining attr_accessible - with warning that many books will not tell you to add fields to that list but you may need to.

Rspec output not colorized in emacs

Now that my machine is at least partially set up, I thought I would actually pull down some code from Github and do some programming. I have git, RVM, and sqlite3, so setting up a working environment took ~5 minutes. But there has been another security update since I last touched this code, so I thought I should start by updating everything to Rails 3.2.6 and checking to see that nothing breaks.

Well two things are broken. I now have a number of tests that are not passing. But I’ll have to get back to that later. More importantly, the colorization in my shell within emacs is broken. No pretty green (or red) in my rspec output. Not acceptable.

So first, what is the name for the system that takes the weird ^[[32mAnd "^[[32m^[[1mGe characters and turns them into pretty colors? Apparently that is called Ansi colors: http://en.wikipedia.org/wiki/ANSI_escape_code. On my Linux machines, the emacsen have ansi color interpretation turned on by default. In fact, the docs I can find for the ansi-color eLisp code indicate that colorization has been the default since ~1999. But whatever. Folks on the emacs IRC channel had a number of suggestions about how to get that working (including suggesting not using ansi-color.el but using xterm-color.el instead). The incantation that finally worked on the emacs that shipped with Lion (emacs 22.1.1 ©2007) is:

     (ansi-color-for-comint-mode-on)

While I was customizing my settings, I copied some configurations from some of my other .emacs files into here. I wasn’t sure they were compatible with the installed emacs 22 (I run emacs 23.3.1 on my server) so I tried out the automatic mode setting for Rakefiles. But, to my horror, there isn’t a ruby-mode installed here. Hmmm guess it is time to explore installing a more recent emacs.

Emacs from Homebrew

Braumeister shows an emacs 24.1 formula. Let’s see what that is like.

     ==> Downloading http://ftp.gnu.org/pub/gnu/emacs/emacs-24.1.tar.bz2
     ==> ./configure --prefix=/usr/local/Cellar/emacs/24.1 --without-dbus --enable-locallisppath=/usr/local/share/emacs/site-
     ==> make
     ==> make install
     ==> Summary
     /usr/local/Cellar/emacs/24.1: 3880 files, 102M, built in 2.1 minutes

Ahhhh much better! I have ansi color by default. And more importantly, I have ruby-mode AND my hook for automatically invoking ruby-mode for .rb, .rake, and .spec files works!

And one small tweak for this blog. The .markdown files for this blog have been opening in Fundamental mode without auto-fill on. I think text mode with auto-fill on would be more sensible:

     (add-to-list 'auto-mode-alist '("\\.markdown$" . text-mode))
     (add-hook 'text-mode-hook 'turn-on-auto-fill)

Ok now am I set up to code?

Setting Up My Lion Laptop

In the past 10 years and 2 Macs, I accumulated a lot of stuff that needs to be transferred to my new laptop. So I took advantage of the Migration option that comes up during the first-boot system setup that all Macs have. So I booted my desktop machine into transfer disk mode by restarting while holding down the ‘t’ key, then connected both machines using a Firewire 800 cable. I transferred my user account stuff, the systems Applications, and settings (total: ~240 G). The initial estimate for the transfer was 23 hours but fortunately, once it had been running for a while, it revised the estimate to ~3.5 hours. Once the initial data transfer had completed, I started clearing out stuff I don’t want or won’t work on the new machine.

I had not chosen to transfer what the Setup Assistant referred to as “Other files”. Not sure what all that encompases, but for one thing, it did not transfer the /opt directory containing my MacPorts stuff. To complete the the removal of the MacPorts stuff, I cleared out environment stuff for /opt and my Oracle vars. The Oracle instant client doesn’t run on Lion, so I removed /Library/Oracle. I also used the uninstall section of the MacPorts docs to find out what to ‘rm -rf’ in various Library directories.

The next thing I needed was a compiler, so I installed Xcode 4.3.3 from the App Store. Even with Xcode installed, I still don’t have gcc?! Apparently now one needs to ask Xcode to install an optional command line tools extension. That can be found under Preferences -> Downloads. Unfortunately there appears to be a bug in some part of the Xcode install because after I installed it, my terminal, which had been working just fine, could not run emacs:

    $ emacs
    emacs: Cannot open terminfo database file

I also see a possibly related issue: I can’t use man pages from the command line:

    $ man nmap
    WARNING: terminal is not fully functional

I can’t live with that! Sounds like the fix is to copy in a known good usr/share/terminfo directory. But where can I get one? At the moment, the fastest option appeared to be to reinstall Lion and start again.

Reinstall Lion

First I created a Lion recovery thumb drive using the instructions at http://support.apple.com/kb/HT4848 and the Lion Recovery Disk Assistant v1.0 downloaded from http://support.apple.com/kb/DL1433. Made a 2 G partition on the pink thumb drive (make sure partition table type is GUID under options) Then launched the LRDA and selected the LIONRECOVER partition to receive the data. The recovery stuff copied over in < 15 min and I was able to eject the disk.

To do the actual recovery, I needed to be connected to the internet. Wired network is the fastest - esp as I went to work and used their fast network. I restarted my laptop while holding down Command+R. Because this restart is before my settings kick in I had to use the real Command key - with the apple symbol on it. The laptop booted into recovery mode, checked with Apple to make sure this was a machine that shipped with Lion on it, and then reinstalled. The reinstall took about an hour and left all my user data intact and most of my applications.

Once it had rebooted, I had a nice complete set of /usr/share/terminfo and emacs was happy once more. I made a backup and then checked Xcode. Xcode was still installed in the Applications folder but when I started it, it went through the same ‘first time’ operations as it had the first time. When I looked at the Preferences -> Downloads the 2 iPhone simulator add ons that I had installed the first time were still installed. But the Command Line Tools showed they were not. I reinstalled them and then checked my term info by trying to run emacs again. Nope, same error message about my terminfo database. So clearly the root of the problem is with installing the Xcode command line tools. I untarred my backup terminfo directory and replaced the broken one with the original information from my clean install.

Homebrew

I already had some stuff installed in /usr/local. I don’t think I want most of it, but was concerned that just removing the directory contents would not cleanly uninstall everything. So for my first attempt, I followed the install instructions and ran the install script as:

    /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

Once that had run, it told me to run “brew doctor”. That gave me a lot of warnings about the things that were already installed. So I decided to remove things that were installed by homebrew (.gitignore, Cellar, Library, README.md), tarred up the rest, and cleared everything out of /usr/local. This time after I installed homebrew and ran “brew doctor” I got “system is raring to brew”.

So how do I figure out what formulas are available? One option is from the command line: brew search mysql. That will list all available formulas that have mysql in their name. Another option is searching on http://braumeister.org/

To find out what I have installed, I can look in /usr/local/Cellar/. To find out what version of a package is currently active, I can look at the symlinks in the bin or lib directories. Or I can use the command line: brew list wget; that will list all the files associated with that package.

I may want to override some system packages with ones of my own, so in my .profile, I added /usr/local/bin into path before /usr/bin.

MySQL

Lion has SQLite3 and Postgresql 9 already installed, but I don’t see any sign of MySQL so I installed MySQL 5.5 from brew. After installing, I get a bunch of instructions about creating databases and starting when I boot/login. So first of all, I need to install the main database:

    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" \
                     --datadir=/usr/local/var/mysql --tmpdir=/tmp

The output from mysql_install_db gave me the familiar “be sure to set a root password” warning. It is less clear that you have to start mysql.server first and then use the mysql_secure_installation script.

    mysql.server start
    /usr/local/Cellar/mysql/5.5.25/bin/mysql_secure_installation
    # I set a root password, disabled remote root access,
    # and got rid of the test databases and access

To make the mysql server run when I log in, I followed the instructions that homebrew gave me upon install:

    cp /usr/local/Cellar/mysql/5.5.25/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

RVM

Lion comes with Ruby 1.8.7 installed and the gems from my previous Mac appear to have transferred. But for more recent rubies, I’ll want to use RVM. I had installed RVM on my previous Mac but haven’t really used it, so I think I’ll start over. First I removed the old RVM with “rvm implode”. Then I followed instructions at https://rvm.io/rvm/install/

    curl -L https://get.rvm.io | bash -s stable --ruby

This installed ruby 1.9.2-p194, gem, bundler, rvm, and rake. Then to do a basic rails install:

    rvm gemset create rails-3-2-6
    rvm gem set use rials-3-2-6
    gem install rails mysql pg sqlite3