Post navigation

Recently in Nettigo I’m busy with learning Chef. Oh, we are small company (in terms of number of servers), so, when finally I had some practice witch Chef I stated I don’t need it :) At least, as full-blown tool, with knife, Chef server (both self hosted and as service).

What I find useful is Chef in local mode, Capistrano and Kitchen. At least this is what I found interesting for me. My goal is not to get fully automated server provisioning, deployment and configuration. I need reliable way to do deployments in repeatable way. I’m moving my application to new server and planning for some microservices inside. This is meant for me to be more reliable deployment way than old – write down all steps when I did created production environment.

I’m deploying my web application once to VirtualBox and sometimes to real server. When creating my Rails application, I need to restore all downaloadable content like images, datasheets, etc. If I do deploy to VirutalBox instance fastest way is to copy it from backup on my laptop. When I do deploy to host in Internet to use data from backup stored on other server. To speed up downloading I have tested different network options (doesn’t matter what one). I needed way to compare how fast are data downloaded. Data downloaded are different files copied from different locations in background. So I did simple ‘oneliner’ shown above to calculate how much data was downloaded and saved to disk in given time and calculate speed in kB/s. Looks ugly, but anybody who remember Perl can not say that ;)

To be honest I didn’t expect I will write again here at this blog. But good news I did manage to find some time to write!

Since 2011 I’m not freelancing anymore, but all the time I use Rails in Nettigo. Codebase used there has it’s roots in Rails 1.2 or even earlier release – first migrations have not timestamps at beginning of filename but numbers, this change was introduced at Rails 2.1. In other words there was a few upgrades in live of this application, and there will be some in future ;) Some upgrades were easy but sometimes You can encounter problems which looks strange and after few hours debugging You have still no idea what is going on :)

Right now I’m in upgrade process from Rails 3.1 to 3.2. It went quite smooth (or at least I think so, since I have not deployed to production environment :) it yet) with one exception – will_paginate have started generating wrong URLs. For example on main page instead of just adding page parameter it have changed path to /about?page=2&controller=Public/products. It looked strange since root was defined, links with link_to were generated for it, page was displayed, but when will_paginate called url_for, it returned garbage – redirecting to About page instead of keeping original URL.

To keep long story short (as usual best solution is to leave code, get some sleep and see at problem again with fresh mind), this diff illustrates what was a cause:

Root was served from controller in namespace (module Public), and with change in routing engine (Rails 3.2 introduced new one) capital letter did the difference.

Long time since last post, isn’t it? Well, I’m completely busy with my work in Nettigo, selling Arduinos and other nice gears :) I do not do gigs anymore, so there is a lot less reasons to write about Rails.

But I still use this framework, since my backend software is written in Rails, so here is one thing which I think can be useful for You.

Often we want to pass ID of current user to Rails model internals. Most answers are don’t do that, this is controller work to do authorization stuff, MVC is to prevent such thins, etc.

here was not related picture (as usual on this blog). Some readers were complaining it was not only unrelated but even offending. Well, maybe they were right, so, I have removed that picture.

Yeah, right, but what if we want to keep some kind of audit trail what is happening with given model? This is not authorization, just who done what.

Since in my application case audit records are created in observers simple passing user id as some additional parameter is no go for me – I don’t want to change interface just to pass user info, that breaks too much things.

Thread.current acts as a hash accessible in current thread and allow us to pass some info skipping MVC isolation (You wanted that, right? :) )

In observers I can access user ID and store it with record. If it is not present, audit trails is being marked as created by System. That means probably cron job, or console action (well on console I can run code inside with_user block when needed).

This is safe as long each request is being processed in single thread and we make sure that user ID will be cleared after our operation. When ID will stay in Thread.current, next request being processed by this Rails app instance will have access to that value and wrong audit records will be created.

Code has to be updated, since any exception will occur inside block, code after yield will be skipped and user ID won’t be erased from Thread.current. Here it is, final version:

When You are about to use Ruby to connect to some SOAP-like API You will probably use SOAP4r gem. First step would be to generate client code from WSDL provided by API. If it is served via HTTPS and server has SSL certificate not signed by some common CA (like self signed certs) You will experience following error running wsdl2ruby.rb:

HTTPClient (this is gem used by SOAP4r to do all HTTP communication) tries to validate SSL certificates by default. Good, this is what SSL was thought for :) HTTPClient allows to use own CA (which is better solution than to lower security, especially on production :) ), however I don’t know how to pass this via SOPA4r abstraction (ssl-config.set-trust-ca('path') in HTTPClient (UPDATE – OK I think here You can find info how to specify CA, client key and cert in SOAP client). We can tell HTTPClient not to check SSL certs via options of SOAP::RPC::Driver when using client code:

This may be not very wise, so use that if You know what You are doing.

And what about wsdl2ruby.rb

Above example relates to scenario when You have generated client code, which should connect to HTTPS. But how to recover from certificate verify failed when running wsdl2ruby.rb (in other words – when generating client code and WSDL is on HTTPS server with self signed cert)?

First solution is to download WSDL and generate client code from local file. Will do the trick unless WSDL imports some additional XSDs via HTTPS URL…

This gist gave me idea how to solve that (BTW – very useful trick to debug HTTP traffic if You can not use proxy like Charles Proxy). I did checked out SOAP4r gem code and updated wsdl2ruby.rb adding at begin:

That’s it. Well, almost. I was unlucky – due to some configuration twists WSDL and other referenced resources were hosted in some dev environment with wrong network configuration. Fixing that was totally out of my reach.

What was wrong? WSDL was accessible via HTTPS. All resources imported inside were referenced via HTTPS, too. But. When accessing these HTTPS URLs (other than WSDL) from outside network (where I was) plain HTTP error message was displayed, so SOAP code could not be generated. When URL was changed to HTTP – proper XML file was available.

Now all requests to HTTPS are converted to plain HTTP. Again – do it when You understand what You are doing :)

Where Ruby brought me

When I started this blog (Apr 2006) I’ve just have discovered Ruby on Rails. During this almost five years many things have changed. And finally Ruby brought me to this place:

Massimo and Arduino Logo at Maker Faire 2010 in NYCCC by http://www.flickr.com/photos/mattrichardson/

And now small announcement. As You can see I was publishing on this blog very seldom in recent months. And reason is very simple. Almost two years ago I have started side business (do freelancer have side businesses? :)) ) selling Arduinos in Poland (and whole Europe). After slow start, it have took off, and between my work as freelancer and running shop there were no time to write on this blog.

Recently I have made decision to stop freelancing and devote all my time to Arduino (and electronics) related topics. Will I stop writing about Ruby? I guess not. Shop is on some hosted platform, but I do run some custom RoR software as backend. I plan to migrate at some point to own e-commerce solution, so I’m not leaving Ruby world.