Friday, September 16, 2011

Ubuntu: Install from USB

Apparently, you can install Ubuntu through a USB stick if your BIOS can boot from USB. I though this was pretty neat and wouldn't have to deal with throwaway media like CDs and DVDs. Unfortunately, each time I tried, the computer would not boot from the USB. It just kept booting from the harddrive no matter what I did in the BIOS.

I finally managed to figure this one out. The first 2 computers just didn't boot from USB. No hints. Nothing. Googling this problem brought up nothing. The solutions were to re-download Ubuntu, check your USB, use a CD, try on another computer, etc.

Fortunately, the 3rd computer I used gave me "Missing Operating System". Bingo! I found something searchable. This brought up a nice blog article. A solution! And it works! Basically, the problem was in the creation of the bootable USB. I was using Ubuntu's Startup Disk Creator. Apparently, it doesn't properly wipe out the USB and repartition appropriately. This caused havoc. The idea is to wipe out the USB stick and repartition before using Startup Disk Creator. This solved my problem.

Check out the solution here: http://ubuntuliving.blogspot.com/2008/11/missing-operating-system-step-by-step.html

Friday, September 2, 2011

Ubuntu + iPhone: Upgrade/Restore

It's no secret. Ubuntu + iPhone is a pain in the ass! However, it just got a little less painful for me.

You see, the way I've been approaching this is to never plug my iPhone into my computer. If the two don't meet, there can't be any problems. Don't solve your problems. Get rid of them!

Unfortunately, I've been losing reception the last two weeks and it's been annoying me to no end. I called my service provider and they determined it's probably because I'm still on 3.1.3. Great. I need to upgrade my iPhone. iPhone, meet Ubuntu. Ubuntu, meet iPhone. Now PLEASE play nice.

Of course, that was too much to ask for. I installed VirtualBox and got Windows XP installed. I then added the iPhone as a USB connection in the settings. I then proceeded to plug in my iPhone. iTunes detects my iPhone. Yay! A small triumph. I decided to restore instead of upgrade. I wanted to start from scratch anyways.

After iTunes wiped out my data, it started to prepare for the restore. At which it hung. After a few minutes, it gave me a 1602 error. I tried everything I could think of: reboot iPhone, reboot VirtualBox, reboot Ubuntu, re-plug in iPhone, etc. The best I got out of all this was a different error: 1604. At this point, my iPhone is permanently in Recovery Mode and now, I couldn't even select the iPhone USB connection in VirtualBox.

I thought to myself, "Screw this. I'm fixing you even if it takes all night!" And so, I did. Thanks to this post.

Follow it. It works.

Here are the keypoints and some added tips from the comments.
  • Add yourself to both the vboxusers and audio groups.
  • Change the USB filter settings so that only Name and Vendor ID have values. DELETE EVERYTHING ELSE. It's not recommended. It's mandatory!

After all this, I was able to restore my iPhone without problems. Booyah!

Monday, August 29, 2011

Rails 3.0: RSpec 2 + Machinist + ActiveRecord

RSpec 2 usually cleans up after itself if you set config.use_transactional_fixtures to true. However, if you use Machinist 2, this isn't the case. Apparently Machinist likes to keep around a cached version to speed things up. This is bad when trying to run independent tests.

To prevent this from happening, add this to config/environments/test.rb.
Machinist.configure do |config|
  config.cache_objects = false
end

References

http://blog.angelbob.com/posts/315-RSpec-not-clearing-database-when-you-use-machinist---published-rails

Sunday, August 28, 2011

Rails 3.0: PostgreSQL

If you're starting fresh, getting a Rails application running on PostgreSQL can be rather involved.

Install PostgreSQL on Ubuntu.
sudo apt-get install postgresql libpq-dev pgadmin3 -y

Create a new Rails application with PostgreSQL.
rails new <project name> -d postgresql

If this is an old project, install the gem in Gemfile.
gem 'pg'

Create the PostgreSQL user. The username will be the same name as your project which can be found in config/database.yml.
sudo su postgres -c psql
# Enters the psql console.
create user "<username>" with [superuser] password "<password>";
# Outputs CREATE ROLE.
\q
The superuser keyword is optional but makes things easier in development.

If you get the error "FATAL: ident authentication failed", edit /etc/postgresql/8.4/main/pg_hba.conf.
local all all ident
to
local all all md5

Restart PostgreSQL.
sudo service postgresql restart

Create the development and test databases through the terminal.
createdb <project name>_development -U <username> -W
createdb <project name>_test -U <username> -W

References

http://olmonrails.wordpress.com/2008/08/12/switching-rails-to-postgresql/

Thursday, July 21, 2011

Ubuntu 11.04: Window Docking Shortcuts

I stumbled upon some new shortcuts in Natty Narwhal to dock a window in a certain position.

Try out Ctrl+Alt+<Number Pad Key>. This will dock your window according the position of the number in the number pad. Note that it doesn't work with the numbers along the top of the keyboard.

1: Bottom left corner
2: Bottom
3: Bottom right corner
4: Left
5: Middle (but not maximized)
6: Right
7: Top left corner
8: Top
9: Top right corner
0: Maximize

If you press the same number several times, it will change the size of the window.

I found this super handy when I needed to fit things inside my monitor, such as having 2 terminals side-by-side.

Try it out and let me know what you think down below in the comments!

Wednesday, July 13, 2011

Ubuntu 11.04: Redshift

Redshift is a program that changes the temperature of your monitor according to the time of day. It basically emulates sunset and encourages your body to sleep earlier. Something that I desperately need to do.

I recently upgraded to Ubuntu 11.04 Natty Narwhal, and was happy to find that Redshift had made it into the repositories! Unfortunately, after installing both the application and its GUI counterpart, it did not start.

I tried it in the terminal and to my dismay, I got the following:
> redshift
No clock applet was found.
Initialization of gnome-clock failed.
Trying next provider...
Latitude and longitude must be set.

So what's going on? Well, as of Ubuntu 11.04, Gnome is no longer the default. It's Unity. Hence, there is no gnome-clock.

Luckily, Ubuntu uses Redshift v1.6. In this version, a configuration file can be added! Just create the file ~/.config/redshift.conf with the following:
[redshift]
location-provider=manual

[manual]
lat=49.3
lon=123.06

After saving the file, you can now restart Redshift from the GUI.

Enjoy and sleep early!

Thursday, June 9, 2011

Logging Bootup/Startup Scripts

I was writing some startup scripts in /etc/init.d/ to be run during bootup. However, there was a problem and the scripts weren't running properly. Unfortunately, there were no logs anywhere.

System Wide Logging

I found a solution in Ubuntu which was to open up /etc/default/bootlogd and set the option to yes. You supposedly see the boot logs in /var/log/boot. However, I found it was writing to /var/log/boot.log. Sort of. I got partial output (about 6 lines). The file was never fully written to.

Script Specific Logging

I found a workaround. Instead of using a system wide log, use a script specific log. Add this to your script. It will log all output.
exec > /tmp/debug-my-script.txt 2>&1

References

Tuesday, June 7, 2011

Automatic Server Bootup on Power Failure

Imagine a power failure where your servers are running. Of course, they should be plugged into a UPS, but what if that goes down too? Your server will power off.

When the power comes back, your server will remain quiet unless you tell your servers to come back to life by themselves.

There are 2 things you must do in the BIOS to make this happen.

  1. Tell the server to bootup after a power failure. This is the AC Power Loss Restart option in the BIOS. This should be set to On.
  2. Ignore a missing keyboard. There is an area in the BIOS that handles halting on errors. It should be set so that it ignores keyboard errors. If this is not ignored, the server will wait for the user to press F1.

Saturday, March 5, 2011

Rails 3.0: Installing Haml

Installing Haml is easy. Open up Gemfile.
gem 'haml'

Install.
bundle install

Now start naming your HTML files with a .haml extension, such as index.html.haml.

Cheat Sheet

Here's a quick cheat sheet to get you started.
#id
.class
%tag
%a{ :href => 'http://wesleymwwong.blogspot.com' }
/ HTML comment
= output
&= escaped output
!= not escaped output
- ruby
-# ruby comment
#{ inline ruby }
:javascript
!!! strict
!!! 5

References

Sunday, February 27, 2011

Why Haml?

When things are modular, simplicity and understanding have a chance to precipitate. Working with something that deals with a multitude of responsibilities is mentally tiring.

When using markup like HTML, there is a distinction between structure and layout. HTML provides the structure and CSS provides the layout. You should be able to separate out the two. The HTML should still make sense and be organized even without the CSS. This idea is very well demonstrated with CSS Zen Garden.

Inline CSS, I Banish Thee!

So where does Haml come into all of this? With Haml, I find it very hard to use inline CSS. Some may think this is a disadvantage because you can't do quick tweaks (read hacks). Hacks are usually done in the moment with the thought of fixing it up afterwards. Unfortunately, these usually end up sticking around permanently since it works and there is always something more important to do. You end up with HTML that does more than structure. With Haml, you are more inclined to let CSS handle the layout.

My Poor Fingers

Haml saves you a lot of keystrokes. It is much less verbose than HTML and ends up saving you a lot of time. With less characters required, it is easier to see the structure of the entire page at a glance, which helps once you move onto styling the page with CSS.

This Looks Familiar

Once you start converting your HTML to Haml, you'll notice something. It kind of looks like CSS! Because of this, it makes the CSS easier to write and understand.

Haml

If you have time, check out Haml and let me know what you think.

Thursday, February 10, 2011

Rails 3.0: Cucumber + Mocking

I know. Cucumber is used for integration tests. Integration tests and mocking shouldn't even be in the same sentence. However, there are times when practicality trumps theory.

For instance, emails. How do you ensure that emails have been sent/received?

Another example is MailChimp. When a user signs up, you add them to a MailChimp list. Now, whenever you need to create a user, your app hits MailChimp. Since you are suppose to start with a clean slate, you now need to clean out the MailChimp list. This gets tedious and time consuming.

To speed things up, you may want to mock certain tasks like these.

Cucumber supports RSpec mocking (aka doubles) pretty easily.

Open up features/support/local_env.rb (or your custom config file for Cucumber)
require 'cucumber/rspec/doubles'

Done! Enjoy!

(Just as a note, I would test MailChimp without mocking when testing signup. I would mock it out in all other cases. Unfortunately, I haven't figured out how to test emails for real. If you have any suggestions, please leave a comment. I would love to hear it!)

Friday, February 4, 2011

Rails 3.0: Devise + Mongoid + RSpec

Devise removes the mundane job of writing all the code required to handle a user account. Plus, it supports Mongoid.

The following is a non-exhaustive list of tasks Devise takes care of for you:
  • signup
  • forgot/reset password (including emails)
  • confirmation emails
  • password encryption
  • login
  • logout
  • editing account info
  • locking accounts
  • session expiration

All this is available out of the box. Along with the goodies that are pre-packaged, Devise is fully customizable.

Installation

Open Gemfile
gem 'devise'

Install
bundle install
rails generate devise:install

You will need to setup your mailer if it isn't already setup. Devise depends on the default host being set, so ensure the following is set
# ... other mailer settings
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Don't worry! Devise is smart and has already detected that you are using Mongoid at this point and not ActiveRecord. It has also created config/initializers/devise.rb. This is where you can configure Devise. Take some time to familiarize yourself with all the options and uncomment any that you would like to use. I'll wait.

Test Helpers

If you are using RSpec, create spec/support/devise.rb with
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

Now you have access to things like
sign_in @user
sign_out @user

Create a User model

It is time to create a User!
rails generate devise user

This generates a user model at app/models/user.rb with some Devise specific lines of code. Include the modules that you want and remove the ones you don't.

Believe it or not, Devise is now properly hooked up and ready!

Routes

Devise automagically generates all the user routes required for handling the user account. If you look in config/routes.rb, you'll notice
devise_for :users

To see all the routes
rake routes

You will notice a bunch of user routes. Fire up your server and navigate to localhost:3000/users/sign_up. Voila!

As a quick reference, here are some useful routes:
  • signup: new_user_registration_url
  • edit account: edit_user_registration_url
  • login: new_user_session_url
  • logout: destroy_new_user_session_url

References

I encourage you to take a look at the Devise home page as there is a wealth of information there.

https://github.com/plataformatec/devise

Wednesday, January 12, 2011

Ubuntu: Installing Fonts

It's very easy to install fonts in Ubuntu. Fonts usually come in a zip file. There may be several different versions of the same font with different extensions. I've currently tried this with ttf and otf fonts.

System-wide fonts are located at /usr/share/fonts. However, I prefer to keep things to myself, so I will install it in my home directory.

Create a .fonts directory in your home if it doesn't exist already.
mkdir ~/.fonts

Extract your font to the .fonts directory. You only need to extract one of the font versions. For example, if the zip file contains both myfont.ttf and myfont.otf, you only need to pick one of them, like myfont.ttf.

Refresh the font cache.
sudo fc-cache -f -v

If you had an application open, now would be a great time to restart it. You should now see the new font.