1000fires

The brain drivel of Hank…

About

Giving back to 'Open Source' and in particular Ruby on Rails ( RoR ). This site will serve as a place where I put my own lessons learned.

Goals:

  • To auto start with init.d, upon westhost restart, etc.
  • Use Apache to front end, via mod_proxy.

You’ll have to follow other sites on installing gems, etc. This Site has some very useful info, namely you need to symlink mongrel_cluster_ctl and ruby to /usr/bin/ (you’ll have to do a find / -name mongrel_cluster_ctl). Mine was here: /usr/local/ruby/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/bin/mongrel_cluster_ctl

So I linked into /etc/init.d/ like so:

ln -s /usr/local/ruby/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/bin/mongrel_cluster_ctl /etc/init.d/

chkconfig is not installed on westhost, so you have to do the init.d link/scrips the old fashioned way:

ln -s /etc/init.d/mongrel_cluster_ctl /etc/rc.d/rc3.d/S81mongrel_cluster

ln -s /etc/init.d/mongrel_cluster_ctl /etc/rc.d/rc3.d/K19mongrel_cluster

K = Kill, the number means the order

S= Start, “”

This all came up b/c I wanted to write a shared mixin module to have attachment_fu ready actions for all my controllers that have attachments. No need to put that crap in every controller. So in a mixin module when you want to do some fun reflection/introspection like so:

    @this_obj = self.controller_name.classify.constantize.find_safe
(id,logged_in_user.id)

In Dev this worked great. Tests were good, my mostly view/template developer partner, approved. Deploy to test and WHAMMO!

    "NameError ("Blogs | ListAll" is not a valid constant name!):

…”

What gives? Mmm, seems when you include a mixin, that is included in a controller. The self.controller_name will report incorrectly in Test and Prod. So I had to adjust the code a little using a new method:

        @this_obj = self.this_controller_name.classify.constantize.find_safe(
id,logged_in_user.id)

Below is the method(which is basically David’s code for controller_name).

###############################
#This method is here b/c it is apparent that Rails
#ActionController.controller_name
# is unreliable in Test and Production for some reason.
# attachment_symbol_name: Blogs | List_all_attachments
# attachment_model ERROR: "Blogs | ListAllAttachment"
#is not a valid constant name
###############################
def this_controller_name
    self_class = self.class.to_s.sub(/Controller$/, '').underscore
end

Cheap man’s SMS Gateway

March 10th, 2008
  1. Parse number to determine the telecom provider (example: http://www.notepage.net/smtp.htm)
  2. Create an email message to the respective email @ address for that provider.

Issues: SMTP is not as time sensitive. But for a small web app, is this ok?

Simplified Internet and its interaction, enjoy

A) USER LAYER :: normal usage, browsers
1.Computer
2.Operating System (Windows OSX)
3.Browser (Internet Explorer / Safari)
4.Internet (Comcast, Verizon)

B) The network :: NETWORK LAYER
Probably the area we control the LEAST. We can’t MAKE routers talk faster OR make Sprint love us!!!

C) Website/Hosting/Servers :: SERVER LAYER
1.Server(s) (which is a fancy name for computer) ie. Dell
2.Database (where information is stored) ie Oracle, DB2, SQL Server, MySQL
3.Basic HTML files and images( STATIC CONTENT)
-> HTML, CSS, JPG, Javascript, Flash

D) Website base language/framework (does the heavy lifting, most of work***) :: APPLICATION_LAYER
This requires (B)
-> From Microsoft, Open Source, 3rd party.

E) Our code. :: CODE_LAYER
Our programs: (this is where I/we work most of the time, you MUST understand A-D though)
*we write code in the Base/fundamental programming languages from D
*we implement Extra modules/libraries other people write

DONE with A,B,C,D
***************************
***************************
***************************

Web Pages are really akin yelling across canyon!!!!

Internet Applications in the real world(web pages)
——————–
STEPS FOR ONE WEB REQUEST:
1. User A) Users  —> give me a page |NETWORK B) —> |SERVER C) Joe user want to look at ‘XYZ web page’.
2. SERVER C) Send ‘XYZ web page’ to user |NETWORK B) —-> traverse network |USER A) User receives response. Views ‘XYZ web page.’

The above is TRUE even for just an image or buying a widget!

*** In the old days of the internet. All web pages were just layer C. They were simple files served up by the server. This harkens back to the days of the home page internet, when everyone had an internet home page like so: “www.mindspring.com~hbeaver” which I DID!.

This error is extremely frustrating. It occurs when the .svn info is missing in a directory. In my example this happens when running ‘rake doc:app’  and I have found only one sequence that will fix this. In my case, I am removing doc from subversion and going to manage outside of source control.

To fix:

  1. cd /doc/app
  2. mv ‘app’ ‘app-todelete’
  3. do a new checkout from svn just on the directory you renamed. Like so:
    svn co svn+ssh://usery@mysecre.server.com/var/www/apps/repos/trunk/doc/app
  4. The newest version in source should be in the proper location.
  5. You will unfortunately have to merge any changes from the old directory, as in my case ‘app-todelete’

Enjoy!

Installing Subversion on Westhost

February 20th, 2008

Because of the versions of Westhost apache and other files, the easiest way is not to install the latest and greatest. I had success installing this version: subversion-1.2.0-rc4

I also make my own apps directory that I put in my path.

./configure –prefix=/apps/subversion

Port Knocking + OTP

February 15th, 2008

The problem is how to securely lock down your firewalls for very privileged services like SSH or a intranet server?

Is adding port knocking together with OTP a good,solid solution for perimeter defense? I’m going to play with this. The idea:

  • Port knock sequence numbers are based on a secret key and a cipher to create an 16 bit sequence or port numbers. ex. “7111 31189 40922″.
    1. That is UDP knock: 7111, 31189, and 40922.
    2. These numbers change approx. every 1-2 minutes. based on secret key and cipher and date/time seed.
  • The payload of each packet is a sequence number + 2 digit OTP number.
    ex: packet 1: “1|3dz”, packet 2: “2|js9″, packet 3: “3|34m”
  • There is a REQUEST packet that goes on another port based on cipher, ex. 5433 (changes daily?)
    The request packet provides the next sequence number,asks for what port to open: 22, ssh in this example that is ENCRYPTED also with IP Address. like so: “4|22|67.100.40.23″ => encrypted as “f321a0a5eb48f2ef2f89f52124bcc11a”
  • The entire sequence is started by a START port number and END port number that changes every day with a simple string as payload. ex: START port: 8765,END port: 64100.So the entire OTP +port knock sequence from my OTP+port knock client:
    • UDP:8765=>”START_PK_SEQ”
    • UDP: 7111 -> “1|3dz”
    • UDP: 31189 => “2|js9″
    • UDP: 40922 => “3|34m”
    • UDP: 5433 => ‘f321a0a5eb48f2ef2f89f52124bcc11a”
    • UDP:64100=>”END_PK_SEQ”Port 22 is now open on the host firewall for the requesting ip based on successfully decrypting packet #5.
  • In theory this would be resistant against man-in-the-middle and reply attacks.

Soon the rest of this blog will deal with a basic overview of has_many associations and a practical use. For now look at this simple tag that uses has_many of a Blog model to show the Interests (or topics) for a given blog. You would use this helper_tag like so in your view, given that you have a single instance of a blog object called ‘blog’

<%= more_ugly_interests_tag(blog) %>

I hope this small article might help others:

#########################################
#Tag to show usage of has_many for a model.
#Ruby on Rails
#*this_model* is the actual instance of a blog or photo
#which has a list of interests.
#
#Call tag like this:
# <%= more_ugly_interests_tag(blog) %>
#the syntax below uses what Ruby calls a ‘block’
#this is the format:
# some_variable_that_has_a_list.each { |list_item
# #do something with each item
# puts list_item + ” !!!!”
# }
#########################################
def more_ugly_interests_tag(this_model)

to_return = “INTERESTS”

this_model.interests.each { |each_interest|

to_return =  << each_interest << "
"

}

#now we have end the whole function/method with what we want to return/print
to_return

end

All the Agile folks would say if you run/create your tests you might not even get this far. However, that’s not always reality for me.

1. ‘tail -f /development.log’

If you are on OSX, the above is easy.
On Windows, I’d suggest looking at Cygwin and getting tail working. It’s worth it.

a) open terminal.
b) Clear the screen before you reattempt what is broken. (Use Apple + K on OSX)
c) Stop tail right after getting the error (CTRL + C)
d) Look for the first line that is not normal Rails and debug output. See next area.

2. Skip all the fluff and go for Ruby errors. You will learn to see “stacktraces” and errors in Ruby/Rails after a little practice.

They will look something akin to this:

1)An error
2)The file that the error occurred in…
3)the line number/and problem code…
4)A sample of the code at and around the error.

5) A bunch of code that was run prior to this happening.

3. Did you recently change the file in question? If so, revert what you did?

4. Use logger.debug to print out something right before the error to see if your code is making it that far.

First as you read Rails and learn more you will discover the DRY (Don’t Repeat Yourself) principle. This means do something once and then don’t dig another ditch.

So here’s my opinion, both partials and tags are for DRY.

One usage difference in my opion: Is if the output is only a few lines, then a tag is simpler and cleaner. However, if the output is many, many lines of HTML like a menu then I start to lean toward partial. However,

There is one thing that a tag can do nicely that partials can’t as cleanly. Arguments can be passed and code run away from the view. Example:

<%=  commentor_pretty_name(blog.login_id) %>

here is the actual tag in application_helper.rb:

##############################
# takes a login_id alone and converts to pretty name for comment/tag
##############################
def commentor_pretty_name(login_id)

def_return = "Nil Name"
begin
logger.debug("app helper commenter_pretty_name")
#the "||" operand below means that we will use the def_return of "Nil Name" if Login.find returns nil
Login.find(login_id).unique_name || def_return
#end
rescue
def_return
end

end

#### END CODE

To do the same in a partial you would have to have this:

<%= render :partial => ‘commenter’, :locals => { :login_id => user.id} %>

user.id would be passed as argument AND then on the partial you would need some of the ruby code in the actual partial to lookup the user.

<%   #note no '=' tag this is purely to run ruby code and set variables

@def_return = "Nil Name"
begin
logger.debug("app helper commenter_pretty_name")
@def_return  = Login.find(login_id).unique_name
#end
rescue
@def_return
end
%>
    <%= @def_return  %>

Best of both worlds? Use a tag to run the ruby code and then a partial to render? When the tag gets riddled with html/view then this presents a good blend of keeping view and model seperate by using a tag to setup/do ruby work and simple partial to present.