Friday, August 27, 2010

Forget root password of a Debian Machine & resetting it

I am writing this small post just because

  • i forgot the root password of my debian machine
  • didn’t find a through post that list all steps in one place and had to look at several ones at a time

The steps are as follows

I am quoting these lines from that post

Some Linux distribution, such as Ubuntu for instance, offer a specific boot menu entry where it is stated "Recovery Mode" or "Single-User Mode". If this is your case, selecting this menu entry will boot your machine into single user mode, you can carry on with the next part. If not, you might want to read this part.

Using GRUB, you can manually edit the proposed menu entry at boot time. To do so, when GRUB is presenting the menu list (you might need to press ESC first), follow those instructions:

  • use the arrows to select the boot entry you want to modify.
  • press e to edit the entry
  • use the arrows to go to kernel line
  • press e to edit this entry
  • at the end of the line add the word single
  • press ESC to go back to the parent menu
  • press b to boot this kernel

The kernel should be booting as usual (except for the graphical splash screen you might be used to), and you will finally get a root prompt (sh#).

Here we are, we have gained root access to the filesystem, let's finally change the password.

According to the above words, we should be ok and we have access to the file system. At this state you run the command “passwd” and enter the new password.

If it worked with you then Thanks to the editor. If you got some problems like me, then keep reading.

Problem 1

After editing grub line and add the “single” keyword at the end of that line. I got it loading well until i was prompted for the root password for maintenance and give the ability to skip but by then i will be leaving runlevel 1 and entering runlevel 2 getting login/password prompt i am trying to skip.

To solve this problem do the following:

  • Edit the grub line again and leave the keyword “single” there as before but add at the end as well these words “init=/bin/bash”
  • exit edit mode
  • press the button “b” while you have this modified grub line highlighted to start booting with this modified grub line

Voila, you have now access to shell as a root. run the command “passwd” and you should be fine entering the new desired password. If you got a problem, then continue reading.

Problem 2

Whenever i run the command “passwd” and re-enter the new password i get this error at the end

authentication token lock busy

If so, know that the problem is that you are accessing the system in read-only mode. In order to access it in read-write mode, do the following.

From the shell run this command

mount -o remount,rw /

after that run “passwd” command and this time you should have the ability to enter the new password

and live happily ever after

Sunday, August 15, 2010

Factory Girl in Development

Some many developers use Factory Girl as a replacement to Fixtures and they depend on this gem in Test cases writing.

But Factory Girl can be very useful also in development and can be used in order to generate a bunch of dummy data with different specs for showing your work and reviewing all its details.

The problem we face is that Factory Girl can generate more records in tables that should have their data unchanged such as Countries table.

For example:

Factory.define :user do |f|
  f.association :country

Factory.define :job do |f|
  f.association :user

Now, when we run this piece of code that generates 10 jobs

10.times { Factory.create(:job) }

we will get 10 countries auto-generated
violating the rule we wish to maintain which is having the countries table as it is

To workaround this problem without causing any changes in the code written previously, i came up with that solution

class Factory
  class << self
    alias_method :create_original, :create
    def create(name, overrides = {})
      if name.to_s == 'country'
        country = Country.first
        return country if country

      create_original(name, overrides)

The logic introduced in the above script is simply adding a layer before creation that checks if the created object of certain type and if that type is desired to not be generated, we return the first entry we have in the DB else we do the normal creation

You can add this script in a file and load it in the development environment and use Factory Girl safely without fearing of generating data for tables that should remain as lookup tables

Enjoy :)

Monday, August 9, 2010

Factory Girl & Polymorphic Associations

Suppose you have a case like that

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic => true

class Listing < ActiveRecord::Base
  has_one :address, as => :addressable

class Customer < ActiveRecord::Base
  has_one :address, as => :addressable

Now in your factories you will have something like that

Factory.define :listing do |f|
  f.association :address

since addressable is required, we choose it to be by default related to a customer unless else stated

Factory.define :address do |f|
  f.association :addressable, :factory => :customer

right now if you tried to use the Listing Factory, you will get an address associated with dummy customer and no effect on your side

How can this be fixed ?

Factory.define :listing do |f|
  f.after_build do |listing|
    listing.address = Factory.create(:address, :addressable => listing)

This was the only solution i found after searching for a while
which is a good solution and doesn't need a lot of work to be done