Wednesday, August 20, 2014

Ruby on Rails scaffold generated form fields

Rails has a cool feature that generates basic CRUD screens based on command line input. It can generate a model file, a set of views, a controller, tests, and migrations. While officially shunned for production code, scaffolding may produce most of the functionality you need in parts of your application.

It generates views with array style form field names. For example, if your model name is "widget" and it has an attribute of "column1", the form name generated in views will be name="widget[column1]". It may not be immediately clear how to reference this field using params in the controller.

The answer is to reference it as a hash of hashes. A simple name="field" is referenced in the controller as params[:field]. The scaffold generated field in the above example would be referenced as params[:widget][:column1].

Thursday, July 31, 2014

Chromebook, crouton, Ubuntu trusty

I had been in the market for a low cost Linux laptop for a while, but was not overly excited about the choices. There are a small number of Linux laptop vendors, and Dell offers an Ubuntu based ultrabook at a price around $1300. That is not a low cost option.

Then, I found a blog post where someone had installed Linux on a chromebook. I always thought chromebooks were interesting, but limited. The ability to run Linux in a paravirtual mode sold me, but I still had issues with the performance, having used a chromebook loner earlier this year. I was waiting for the new generation of chromebooks to be released late this summer with Intel i3 processors. That would be plenty of horsepower to run Linux and Chromium at a low cost.

I am typing this post on an Acer C720 with the i3 and 4GB RAM ($379), just a few days out of the box. So far, I am happy with the fit and finish of the Acer, the performance, the ports (2 USB, 1 SD, 1 HDMI, Wifi, Bluetooth), and battery life. Following this guide from Lifehacker, using a script from Google called crouton, I installed Ubuntu in a chroot environment and am able to hot key back and forth between Linux and Chromium. It is freaking sweet!

If you run a crouton install with no parameters, you get Ubuntu 12 which is a couple of years old. However, there are 3 versions of Debian, a penetration testing distro called Kali, and 2 versions of Ubuntu LTS available from crouton. After some experimentation, I eventually installed Ubuntu 14 LTS (trusty) with the Unity desktop using this command:

sudo sh -e ~/Downloads/crouton -r trusty -t unity

It doesn't come with a lot of applications, so I need to get busy with apt-get and set up my development environment. This is the low cost Linux machine I've been wanting.

Sunday, April 20, 2014

Blocking and unblocking an IP using iptables

Iptables is the Linux software firewall.

To block an IP (all ports), as root:

iptables -I INPUT -s ip-address -j DROP

To unblock an IP:

There are two steps. The iptables rule must be deleted by line number, so first you need to determine which rule you want to delete.
iptables -L -n --line-numbers

Next, delete the rule for the IP you want to unblock. This will delete rule number 3:
iptables -D INPUT 3

To clear all firewall rules, use the flush switch
iptables -F

Apache processes, process size, IP clients, and status

To help monitor apache performance, here are some useful command line recipes.

Show how many apache processes are running

ps aux | grep [h]ttpd | wc -l
The bracketed [h] prevents the grep process itself from being counted.

Show the average apache process size in MB

ps aux | grep [h]ttpd | awk '{print $6/1024;}' | awk '{avg += ($1 - avg) / NR;} END {print avg " MB";}'

Show the top 10 apache client IPs by number of sockets

/bin/netstat -ntp | /bin/awk '$4 ~/:(80|443)$/ {print $5}' | /bin/sed 's/.*ffff://' | /bin/cut -d: -f 1 | /bin/sort | /usr/bin/uniq -c | /bin/sort -nr | /usr/bin/head

Show full status

apachectl fullstatus

Thursday, April 3, 2014

Adding will_paginate to a rails application

There are times when you have a lot of records to display and want to provide an easy way to navigate through the entire unfiltered list. Various paginate gems offer a solution. The one used most in my shop is will_paginate.

To add it to an application, the first obvious step is to install it if not already installed.
gem install will_paginate

Once available, add it to the config/environment.rb:
config.gem 'will_paginate', :version => '~> 2.3.16' (adjust for your version)

If you are using Bundler, add it to the Gemfile.

Next, add the paginate method to your controller result set for all records to be paginated:
@records = Record.find(:all)
@records = @records.paginate(:page => params[:page], :per_page => 30)

Finally, add code to your view to display the navigation. I sometimes add it to both top and bottom, depending on how many records I am displaying.
<%= will_paginate @records, :style => 'color:blue' %>