Hide database queries in Rails console


Most of the time I think it’s useful to see what database queries your commands are generating in the Rails console. The rest of the time the output gets in the way by spamming your console output. It is really easy to turn this logging on and off from inside the console, just run the following command when you want to silence the output:

ActiveRecord::Base.logger.level = 1

This will set the logger’s level to INFO. Then to turn it back on enter:

ActiveRecord::Base.logger.level = 0

This will set the logger’s level back to DEBUG.

Remove the dashboard widget screen from OSX


I don’t know about you but I don’t like the dashboard screen on OSX.

I find having to switch to the dashboard to use a widget more effort than it’s worth. I’d rather a stand alone application that I can launch on demand or if it needs to be persistent an application could add a menu bar icon that I can click on from any desktop.

After accidentally switching to the dashboard one two many times I decided to remove it. I’m not particularly worried about any performance gains I may get from removing the dashboard, I did it purely so i would no longer see it.

First you need to open up a Terminal window (command+space and type terminal and press enter) and enter:

defaults write com.apple.dashboard mcx-disabled -boolean YES

press enter then you need to restart the Dock process to see your changes by typing:

killall Dock

Now you no longer have to be bothered by the dashboard screen.

Of course there’s a possibility at some stage in the future you might want the dashboard back. To get it back you need to repeat the steps above but change boolean YES to boolean NO. Simple!

F5 to reload a tab in Google Chrome


I’ve been wanting to user Chrome more and more lately but one thing standing in my way was F5 to reload a tab. I know on a Mac it’s just Cmd+R but my muscle memory refuses to accept this. Luckily there’s a simple fix – let OSX interpret for you.

  1. Open the keyboard settings window. To do this either type ‘Keyboard‘ into Spotlight (the magnifying glass at the top right of the screen) or open the Apple menu and press System Preferences -> Keyboard.
  2. Click on the Keyboard Shortcuts tab.
  3. Click on Application Shortcuts in the left pane.
  4. Click on the plus (+) at the bottom and enter the following:
    Application: Google Chrome.app
    Menu Title: Reload This Page
    Keyboard Shortcut: press F5 on your keyboard
  5. Click add and close the settings window, you’re finished, time to test!

    Add F5 to reload Chrome Tab to OSX.

    Add F5 to reload Chrome Tab to OSX.

Keeping database passwords for Rails apps out of version control


I started using Capistrano to deploy Rails apps recently and came upon the conundrum of database passwords in version control.

In the past I have deployed Rails apps by hand and have kept database usernames and passwords out of my version control only entering them manually in the final stage of deployment. It is a very bad idea to put database credentials in your version controlled files. This process thwarts the key advantages of using Capistrano, you can’t let Capistrano handle creating your tables if it’s unaware of how to access the database and it’s not really an option to tell Capistrano “wait for a minute here for me to change a couple things then you can continue”.

This leaves us with two options; store the database.yml file outside of the rails root and get Capistrano to symlink it in place or enable Ruby to pull the credentials to be pulled into the database.yml file.

Option 1

Place this code in your config/deploy.rb file and copy the database.yml file you are going to use to shared/config/database.yml.

after "deploy:update_code","deploy:dbyml_symlink"

namespace :deploy do
  task :dbyml_symlink do
    rm #{release_path}/config/database.yml && 
    ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml
  end
end
Option 2

For this method your database.yml file needs to be checked into version control. Secondly you need to put the database password somewhere safe on your server I suggest somewhere like /home/deploy_user/.myapp_db, chown it to the user running your Rails app and chmod it 400. Finally database.yml such that the production section looks something like this:

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: myapp_production
  pool: 5
  username: myapp_db_user
  password: <%= begin IO.read("/home/deploy/.myapp_db") rescue "" end %>
  socket: /var/lib/mysql/mysql.sock

You can be more paranoid and include the username, database name, socket, whatever in this method.

Conclusion

I prefer the second as it works out of the box, i.e. a clean clone of the repository then bundle install && bundle exec rake db:migrate && bundle exec rails s will result in a working application.

Specifying a port for scp to use


This is a trick that gets me every time. To specify a port for ssh you need the argument -p but for scp it is -P. scp -p preserves the modification times of the files you are transferring. I’d love to know why they are different. I can only assume it’s because the need to preserve modification times came before the need to specify a port and they wouldn’t change backward compatibility.

Summary;

ssh -p
scp -P

Creating Splash Images for a Live USB


At work we need to configure lots of linux boxes. In order to stay sane we have a local instance of openSuse Build Service (OBS). OBS manages the building of all of our projects and the creation of our repository. We then have a series of live USBs that query a kickstart file from a local server which not only tells Annaconda which packages to grab from our local repository to create a functioning system but informs our administration packages that a new system has been created and what keys and packages have been installed.

On a whim I decided to alter the boot splash on the USB to theme it with the rest of our system. I learnt some odd facts about the syslinux splash image.

Disclaimer: this is for GRUB (GRUB Legacy) not GRUB2.

  • The image must be 640×480 (or at least one supported by the framebuffer before KMS)
  • The image is actually a PNG even though it’s extension is JPG.
  • The image is 16-bit colour

To scale your image to 640×480, I use Gimp as I would likely have created the image in Gimp so I may as well resize it while I’m there.

To convert your image extension to jpg from png is trivial enough;

mv image.png image.jpg

To convert your image to 16-bit colour is easy if you have ImageMagick installed (don’t change the file extension here though as ImageMagick will actually convert the format!);

convert -depth 16 -colors 65536 image.png image.png

ImageMagick can resize your image as well but I find that it tends to do an ugly job of it. I recommend sticking to Gimp for that task.

OSX Terminal startup


Here I found an answer I’ve been looking for for a while. After reading it I migrated everything from ~/.bash_login and ~/.profile to ~/.bash_profile and now when I open a new Terminal window my environmental variables are setup as I wish.

Specifically:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

# MacPorts Installer addition on 2012-06-21_at_13:10:00: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

# For terminal colour
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

# for custom prompt like `username directory $` in pretty colours
PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;31m\]\$ \[\e[m\]\[\e[0;32m\]'