Tuesday 31 July 2012

Story of Compiling Kernel for Linux VServer

So I've been having a fun week.

This is my first time compiling a kernel and It should be straight forward as there are some helpful websites available on just this topic:

pre-requisites:
Patience. This will take an hour (or more) on a decent quad core machine.
gcc-<version>-plugin-dev
fakeroot
make-kpkg
libncurses5-dev

On compiling the kernel with the VServer patch
http://linux-vserver.org/Installation_on_Linux_2.6
On compiling a general kernel
http://www.howtoforge.com/kernel_compilation_ubuntu
On using your complete kernel
http://www.centos.org/docs/2/rhl-cg-en-7.2/buildkernel-bootloader.html

I was running the commands on a Ubuntu12.04 virtualbox machine, following the Vserver instructions and while running make, it kept running out of disk space.
A quick google tells me the result should be about 500MB at most, and doesn't even tell you what the result should be. My attempts kept running out of space after about 40 minutes and after hitting 8GB used space, of which almost all the room was taken up by vmlinux (2.9GB) and vmlinux.o (4.9GB)
Apparently this is not normal.
Eventually after making a virtual hd with a size of 100GB (of which the result was just short of 9GB itself) it appeared to say a build of bzimage in /arch/boot/x86 folder was completed. This I could never find anywhere in the folder.

I am currently compiling using the howtoforge.com link so we will see how much success I shall have later on.
Currently I am getting GCC error "array subscript is above array bounds" quite a lot but it appears to be ignoring it.

#Edit
Its been 2 hours. I didn't set CONCURENCY_LEVEL= because of fear of it crashing. Starting to wonder if it was a mistake given how long this is taking. It is still compiling and it has just moved from ./drivers to ./fs. My favourite file list, sudo du --max-depth=1 -h | sort -rh , reports file is at 7.9GB and growing.


7.9G .
4.8G ./drivers
689M ./net
602M ./fs
316M ./arch
259M ./sound
126M ./kernel
76M ./security
58M ./crypto
41M ./mm
34M ./lib
31M ./include
19M ./Documentation
17M ./block
13M ./.tmp_versions
6.8M ./firmware
6.2M ./ipc
5.0M ./grsecurity
4.7M ./init
4.1M ./tools
3.8M ./scripts
2.8M ./virt
2.0M ./debian
156K ./samples
92K ./usr

Tuesday 10 July 2012

Foolproof FactoryGirl Sequence

Turns out Sequencing in FactoryGirl isn't as easy as it used to be.
Factory.next has been deprecated and the two obvious ways to make a sequence now throw Trait error or Attribute already defined error "FactoryGirl::AttributeDefinitionError"

The problem with this first approach is name. In a normal sequence you would just say name but I want to use it in the description too. This somehow makes FactoryGirl believe name is a trait.

FactoryGirl.define do
  
  sequence(:code) {|n| "#{n+1000}"}
  
  factory :course do |u|
    name "CS#{ generate :code }"
    description "#{name}, 1st year, 1st semester, Foundation Of #{name}"
  end
end

This next example has a problem with the description line. This causes the description to generate 2 more names and throw an AttributeDefinitionError because you now have more than 1 name generated.
For some reason you cannot get around it by making the name line name = Factory.generate :name because name becomes a Trait again.

FactoryGirl.define do
  
  sequence(:name) {|n| "CS#{n+1000}"}
  
  factory :course do |u|
    name
    description "#{name}, 1st year, 1st semester, Foundation Of #{name}"
  end
end

This is the only thing that works. Static forcing FactoryGirl to use the already defined name again using the |a| block in description. It isn't as pretty as it could be but I have to live with it unless someone can tell me a better way. There is probably a solution where you use an after_build tag but that makes it longer and more complicated unnecessarily and it would still be ugly.

FactoryGirl.define do
  
  sequence(:name) {|n| "CS#{n+1000}"}
  
  factory :course do |u|
    u.name
    u.description {|a| "#{a.name}, 1st year, 1st semester, Foundation Of #{a.name}"}
  end
end

Monday 2 July 2012

Importing a CSV File from Upload in Rails >= 3

For some reason I found the documentation for CSV's really bad!

What I wanted to do is accept a file from a form and parse it into a list of users to be added to the database without saving the CSV file. (Note: I DO save the file via the Paperclip Gem for logging purposes but I do all the processing from the file before it is saved to the database.)

When you have uploaded the file using Form_Tag, the file is usually in params[:name] where :name is tag you gave to file_field_tag :name
If you are using a Form_For, which by the way is strange if you do not intend to save it to the model in question, the file will be in params[:model][:name] from  f.file_field :name
The magic part is to actually get to the file you need to call .read on it.

In the View remember to make it multipart to accept files.


<%= form_for @model, :html => { :multipart => true } do |f| %>
  <%= f.label :file %><br />
  <%= f.file_field :file %><br /> 
  <%= f.submit "Upload" %>
<% end %>



In the Controller require csv, you do not need to install it as a gem but you do need to require it as it is in standard library not core.

require 'csv'
def my_method
@lines = []
CSV.parse(params[:submission][:file].read) do |row|
      @lines << row
end


Replace the yellow bits with whatever you want to do with your CSV file.
Have fun!