for all mac ruby devloper outs…
for all mac ruby devloper outside: http://www.macruby.org any opinions about it so far?
for all mac ruby devloper outside: http://www.macruby.org any opinions about it so far?
So concerning my first try of a web based zfs managment interface i will restart the project with some new conditions:
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 ^^.
I tried The Camping Short, Short Example for the ruby camping framework.
#!ruby
#!/usr/local/bin/ruby -rubygems
require 'camping'
Camping.goes :HomePage
module HomePage::Controllers
# The root slash shows the `index' view.
class Index < R '/'
def get
render :index
end
end
# Any other page name gets sent to the view
# of the same name.
#
# /index -> Views#index
# /sample -> Views#sample
#
class Page < R '/(\w+)'
def get(page_name)
render page_name
end
end
end
module HomePage::Views
# If you have a `layout' method like this, it
# will wrap the HTML in the other methods. The
# `self << yield' is where the HTML is inserted.
def layout
html do
title { 'My HomePage' }
body { self << yield }
end
end
# The `index' view. Inside your views, you express
# the HTML in Ruby. See http://code.whytheluckystiff.net/markaby/.
def index
p 'Hi my name is Charles.'
p 'Here are some links:'
ul do
li { a 'Google', :href => 'http://google.com' }
li { a 'A sample page', :href => '/sample' }
end
end
# The `sample' view.
def sample
p 'A sample page'
end
end
It seems to be very easy to create the separat pages.
The example above generates one entry page. (the index method in Views) and one additional page (sample method).
As far as i should know it from rails, it uses the same MVC pattern and theres is a similar syntax. Just the Views are in native ruby without mixing up with html.
But maybe that will appear later on.
The next step is to see howto connect ruby with some inner sys-functions of nexenta ^^.
Just a short one:
After succeeding in installing ruby gems, i finally was able to install the ruby micro-framework camping.
So ruby should just be used as a small administration web interface. In my eyes a rails aproach seems to be like breaking a fly on the wheel.
Just easy as this:
root@sunny:~# gem install camping –source http://code.whytheluckystiff.net
…
Installing ri documentation for markaby-0.5…
Installing ri documentation for camping-1.5.180…
Installing RDoc documentation for activesupport-1.4.2…
Installing RDoc documentation for builder-2.1.1…
Installing RDoc documentation for markaby-0.5…
Installing RDoc documentation for camping-1.5.180…
Smooth and easy. Ready to rumble
.
Next in the row is the installation of the ruby gems package.
So it should be easy… just following the instructions.
So…..
root@sunny:~# wget http://rubyforge.org/frs/download.php/38647/rubygems-1.2.0.zip
root@sunny:~# unzip rubygems-1.2.0.zip
root@sunny:~# cd rubygems-1.2.0
root@sunny:~# ruby setup.rb config
./lib/rubygems/spec_fetcher.rb:1:in `require’: no such file to load — zlib (LoadError)
from ./lib/rubygems/spec_fetcher.rb:1
from ./lib/rubygems/source_index.rb:10:in `require’
from ./lib/rubygems/source_index.rb:10
from ./lib/rubygems.rb:767:in `require’
from ./lib/rubygems.rb:767
from setup.rb:22:in `require’
from setup.rb:22
Welcom to error world
.
Okay…. should be easy to solve:
root@sunny:~# apt-get install libzlib-ruby zlib1g-dev
or for 64bit Plattforms:
root@sunny:~# apt-get install libzlib-ruby lib64z1-dev
But still the same Error!
Finally i found something in the Weblog of Lucas Chan.
Hm its about readhat/centos…. okay why not.
Just go to the ruby sources:
root@sunny:~# cd /usr/local/src/ruby-1.8.6-p110/
root@sunny:~# cd ext/zlib
root@sunny:~# ruby ruby extconf.rb –with-zlib-include=/usr/include –with-zlib-lib=/usr/lib
checking for deflateReset() in -lz… yes
checking for zlib.h… yes
checking for kind of operating system… Unix
creating Makefile
root@sunny:~# make
root@sunny:~# make install
and finally:
root@sunny:~# cd /usr/local/src/rubygems-1.2.0/
root@sunny:~# ruby setup.rb config
….
== Thanks
Keep those gems coming!
— Jim & Chad & Eric (for the RubyGems team)
Again: something learned!
If you want to build your own small ruby script for administrating you Nexenta-Box, you normally just have to do a:
root@sunny:~# apt-get install ruby
Unfortunetally this Package seems to be broken:
root@sunny:~# ruby -v
ld.so.1: ruby1.8: fatal: libruby1.8.so.1: open failed: No such file or directory
The needed lib libruby1.8.so.1 is not on its desired place.
So you need to compile yourself a version of ruby:
root@sunny:~# apt-get remove ruby
root@sunny:~# apt-get autoremove
root@sunny:~# apt-get install build-essential
root@sunny:~# cd /tmp
root@sunny:~# wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p110.tar.gz
root@sunny:~# tar xvfz ruby-1.8.6-p110.tar.gz
root@sunny:~# cd ruby-1.8.6-p110
root@sunny:~# ./configure
root@sunny:~# make
root@sunny:~# make test-all
root@sunny:~# make install
root@sunny:~# make install-doc
/usr/local/bin should be in your path!
root@sunny:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
root@sunny:~# make install-doc
root@sunny:~# which ruby
/usr/local/bin/ruby
root@sunny:~# ruby -v
ruby 1.8.6 (2007-09-23 patchlevel 110) [i386-solaris2.11]
DONE!
Powered by WordPress