Skip to content
  • About
  • Friends
  • About
  • Friends
The Blog of Philippmy personal Site of Things
  • About
  • Friends
Written by Philipp on 2008-07-05

my very first (ehm… useful ?!?!) ruby script.

NAS . nexenta . ruby

So concerning my first try of a web based zfs managment interface i will restart the project with some new conditions:

  • 1. i will switch from the former language PHP to the – like everywhere told – dev-friendly language ruby.
  • 2. i will first concentrate on just the basic functions.
  • 3. i have to use the system build in security realms (so either PAM or an equal interface)
  • 4. i will use dynamic JS-Functions to enhance the GUI. But the system should also work with simple browser (e.g. lynx/links)
  • 5. i will offer a RESTful interface. So it should be very easy for 3rd party apps to connect to them and use alle the functionality.

The Beginning ^^:
So my first task is to bring an overview of all system-disks (other devices are not necessary at the moment) into my ruby context.
So basic system command is:

root@sunny:~# format < /dev/null
Searching for disks…

The device does not support mode page 3 or page 4,
or the reported geometry info is invalid.
WARNING: Disk geometry is based on capacity data.

The current rpm value 0 is invalid, adjusting it to 3600
done

c3t0d0: configured with capacity of 18.61GB

AVAILABLE DISK SELECTIONS:
0. c0d0 <DEFAULT cyl 3734 alt 2 hd 255 sec 63>
/pci@0,0/pci-ide@11,1/ide@0/cmdk@0,0
1. c3t0d0 <ST92011A–3.04 cyl 2430 alt 2 hd 255 sec 63>
/pci@0,0/pci925,1234@11,3/storage@2/disk@0,0
Specify disk (enter its number):

Beside of the interesting warning, this will do the job. The format command will normally lead to a prompt. To avoid this, you just pipe the output to /dev/null.

The really valuable stuff is in the upper part of the output.
After consulting the ruby API about strings and arrays i ended up with the following (dirty) lines of code:


#!ruby
#!/usr/local/bin/ruby -rubygems

s = %x{format < /dev/null}
sa = s.split(“\n”)
counter = 0
dsk = false
puts “Array: “+sa.length().to_s()
sa.map do |i|
if i.strip().length() > 0
print counter.to_s()+” ”
if counter == (sa.length()-1)
dsk = false
end
if dsk == true
puts “DSK: “+i.strip()+”\n”
else
puts i.strip()+”\n”
end
if i.eql?(“AVAILABLE DISK SELECTIONS:”)
dsk = true
end

end
counter += 1
end

I found a really nice post about the ruby command line interface. That post gave me a good start in experimenting with the produced string output. You just always remember the irb to test parts of your code. Of course there is _A_Lot_ of debugging code. But the end-result is correct. Of course there should no hard-coded string in there. A by the way: it was a little bit inconvenient to quarrel again with the integer<->string conversion after some relaxing and luxurious years coding Java ^^. Okay so no implicit casting in ruby (?). Oh wait a minute – no typed language, so no casting?.

Oh yes… i don’t want to hide the output:

root@sunny:~$ ruby cmd.rb

The device does not support mode page 3 or page 4,
or the reported geometry info is invalid.
WARNING: Disk geometry is based on capacity data.

The current rpm value 0 is invalid, adjusting it to 3600
Array: 12
0 Searching for disks…
1 done
3 c3t0d0: configured with capacity of 18.61GB
6 AVAILABLE DISK SELECTIONS:
7 DSK: 0. c0d0 <DEFAULT cyl 3734 alt 2 hd 255 sec 63>
8 DSK: /pci@0,0/pci-ide@11,1/ide@0/cmdk@0,0
9 DSK: 1. c3t0d0 <ST92011A–3.04 cyl 2430 alt 2 hd 255 sec 63>
10 DSK: /pci@0,0/pci925,1234@11,3/storage@2/disk@0,0
11 Specify disk (enter its number):


So i guess i found my disks. At the end, i can just iterate about the even number of entries :-).
Okay ruby-gurus. How can i optimize my code. I am beginner level and “slurred” during years, coding Java and anciently PHP.
I am sure there is a lot of potential to cleanup this mess ^^.

Blogged with the Flock Browser

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook

Like this:

Like Loading...

Related

1 comment

  • Willem van Kerkhof has written: 2008-07-05 at 11:32

    looks like you could significantly shorten that implementation and make it more human-readable at the same time:

    #!ruby
    #!/usr/local/bin/ruby -rubygems

    return_value = `format < /dev/null`
    what_i_want = return_value.split(‘AVAILABLE DISK SELECTIONS:’)[1].strip
    disks = []
    what_i_want.each_line do |line|
    line = line.strip
    if /^(\d+\.\s[a-z0-9]+\s\)$/.match line
    disks.push {:device => line}
    puts “DSK: #{line}”
    elsif line[0] == ‘/’
    disks.last[:location] = line
    puts ” line”
    end
    end
    puts “Specify disk (enter its number):”

    the disks array now looks something like this:
    [{:device => ‘c0d0 ‘, :location => ‘/pci@0,0/pci-ide@11,1/ide@0/cmdk@0,0’},
    {:device => ‘c3t0d0 ‘, :location => ‘ /pci@0,0/pci925,1234@11,3/storage@2/disk@0,0’}]

    I guess, that is what you intended to get 😉

    btw: beware of bugs in the above code, I have not run nor tested it whatsoever…

Leave a ReplyCancel reply

Archives

  • August 2025
  • November 2023
  • February 2023
  • January 2023
  • April 2020
  • January 2018
  • December 2017
  • May 2017
  • February 2016
  • September 2015
  • December 2014
  • August 2014
  • June 2014
  • March 2014
  • February 2014
  • September 2013
  • August 2013
  • July 2013
  • November 2012
  • October 2012
  • September 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • January 2011
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • January 2010
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • September 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006

Calendar

July 2008
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  
« Jun   Aug »

Categories

  • Bash
  • Bochum
  • Build
  • CCC
  • CLI
  • Coderwall
  • Coventry
  • DB
  • Edu
  • Freenas
  • Gitlab
  • Graphics
  • Hacking
  • iOS
  • Java
  • Javascript
  • Mac
  • NAS
  • Network
  • nexenta
  • Perl
  • Personal
  • PHP
  • Play! Framework
  • Proxmox
  • ruby
  • Ruby on Rails
  • Security
  • SmartOS
  • Snippets
  • Sound
  • Tech
  • Testing
  • Tooling
  • Twitter
  • UI
  • Uncategorized
  • Video
  • Virtualisierung
  • ZFS

Copyright The Blog of Philipp 2026 | Theme by ThemeinProgress | Proudly powered by WordPress

%d