google “chef-solo rails deploy git submodule checkout branch sha”

Working with chef resource deploy, faced several issues with Git provider, for examples

– enable_submodule does not work properly
– it does not get the latest code from submodules
– it does not allow you to choose specific branch and sha of any submodule
– in fact for main repo it does not allow you to switch to any specific branch and sha
– for some reason it says branch=version, who knows why ….

I tried correcting all above, and below is one of my examples, its working for me

Advertisements

DSL approach made it really simple

How much watts we do consume per cycle? Have we joined any of the green innovations like for instance green button. It would be great to know how much watts my house consumed this cycle, will I be able to reduce it, any one out there to help me reduce it. I have so many appliances running at home and I do have a smart meter measuring how much has been consumed. Would you also want to know how does meter measure your watts consumed, and how smart meters provide data for further calculation of your electricity bills.




There are several initiatives taking place in US, PlotWatt, FirstFuel and more, and they help you know your own detailed energy usage, along with other features like calculations, helping you out choosing right pricing plan, giving you several suggestions on how could you reduce your consumption and electricity bills.

Okeh, so this was a quick about Green Button.

Working on one of such initiatives, I came across to define a solution that will enable users to see what electricity bill they would end up paying if they apply some different pricing plan, hence ability to apply different pricing plans against the data consumed for the current or past cycles and being able to compare plans. Rather this would actually help utilities defining their trial plans.

Being rubiest for now more than 5 years, it immediately struck me about having a nice ruby DSL for this one, which could be easier to extend for new rules or sub-requirements. And this was a too much of excitement for me. Writing DSL gives you opportunities to play around ruby mixins, better modeling your classes, modules, beautifully engaging them with each other to work for us and present a nice output.

form

But there was a bad news for me, team decided to do this in python :(  … though still I am to design and develop it.

Not having much exposure to python, having my ruby design plan ready on my mind, I started looking similar python capabilities, parsing, meta-programming, DSL models and more, team as well decided an approach that no need admin to define plans on the fly( like one above), but we could have some fixed plans defined, that actually saved my efforts.

Following is one example of one of those plans we have,

And we do have several such plans and we could define more, simply focusing on plan rules. The guts have been encapsulated using classes Plan and Calc. Calc helps to collect, compute usage for the specified period, while Plan provides semantics  around.

I did enjoy python scripting a lot, python looks to me as a raw scripting platform where ruby as a very nice, user-friendly scripting platform.

dealing with concerns

Taking care of concerns, my views are

They are meant not to have large/GOD classes

They are not specific to ActiveRecord

They are there to help us out find out dependencies easier

They should not be dependent on each other, if module A has concern :b, then there should exist paths “root/a.rb” and “/root/a/b.rb“. Though this we ourself have to make sure about concerns path by writing additional code at initialization

I always willing to write clean and neat code, and have end up maintaining my concerns the following ways

  1. Reopening Class
  2. Include Module
  3. Extend ActiveSupport::Concern

I do have one doubt though, out of 3 approaches, which one is good for faster execution?

mailto:jenkins,subject:test case1, case2 on http://staging.site.com

mailto:jenkins,subject:test case1, case2 on http://staging.site.com

So this is something we thought of

– email Jenkins
– to run one or more jobs
– against one of our deployed staging apps(given base_url)

We wrote a mail_watcher.rb, which periodically reads such emails, and rewrite <JENKINS_CHECKS_DIR>/<test_case_name>.checks files adding parameters to be passed on to the test case. In this case it is BASE_URL=. And all we know that we could make Jenkins

1. watch files getting modified
2. use file contents(key=val), as paramters further to be used or passed on to the job exec

Ohh, wait, I did not know how could Jenkins read parameters from a given key=val file and pass it on further. We actaully wrote another shell script which runs as the first step of every job and reads the testcase_name.checks file and set ENV variables like for example BASE_URL.

So in case of subject:test testcase1 on http://mysite.com

– mail watcher will write testcase1.checks file with BASE_URL=http://mysite.com
– shell script will set ENV variable BASE_URL=http://mysite.com


Following is the mail watcher that we simply run to be able to get Jenkins job triggered.

Now any of us teammates, can anytime send an email and get a test case executed. In addition we are as well generating a detailed report email for each job-run that gets published on a Google group. We do have our own custom, half-baked, reporting mechanism(ruby code), which we want to compile the job-execution-report email for each testcase/job executed.

Example reports

subject: testcase1 failed.
body: <Jenkins console output>

subject: rspec 231 passed, 2 failed.
body: <Jenkins console output>

reading huge xml in ruby

Dealing with huge xml? say of size 400 MB or more than a GB. No worries, Nokogiri::XML::Reader is there for your help. But let me tell you it takes a while before you understand it and start using it. Needs to understand types of elements, and when to read value and and when to read attributes. However I have went over it and tried defining my own module which now simplifies it.

Your large xml doc must be having thousands of records, and you may or may not know the structure of record. This module will help you easily read your records, its elements, something like this below …

hey_my_module
  take_this_xml
  and look_for_these_elements
  let_me_know_if_you_find_any_of_them

…hence

HugeXML.read xml, elements_lookup do |element|
  # => element{ :name, :value, :attributes}
end

so simple right? wanna check it out… here it is  => github.com/amolpujari/reading-huge-xml

 

DRYing if @current_user.nil?

While working on one of rails applications, as I usually tend to check whether user is logged in, specially in views as follows

<% if @current_user.nil? %>
  <a href='url to login page'>
<% else %>
  <a href='some url where logged in user have access'>
<% end %>

And over some period I found this pattern repeating everywhere.

I thought of DRYing it, I wanted to have a kind of before_filter for every link that appear on the page, every link should go for login if that page or feature require user entity. and this should be handled by a common thing, similar to before_filter: login_require, on server side. I intend to achieve similar in views. A while back when I read up about html5 custom data attribute, and unobtrusive javascript etc, referring to them, I got one idea as described below, though it is not something that I am making use of custom data attribute exactly, but I found this idea useful and working. May be someone would have good suggestions here for me.

I will put any link which require user login as follows

<a href="/feature/page/" login-require >Feature</a>

And then I will have my after_load.js which will have the following event binding

jQuery('a[login-require]').bind('click', function() {
  <% if @current_user.nil? %>
    openOverlay('overlay-login');
    return false;
  <% else %>
    return true;
  <% end %>
});

to_wordpress_html ruby_file_path: Code highlighter for wordpress

Started tech blogging and found it is necessary to have a nice code highlighter, and I was actually looking out for a blogging tool that would appear very simpler, nice, less time to post, and still have good control over minor aspects.  Trying to achieve all this using wordpress, and found a good friend coderay (when looked inside html from m.onkey.org).

Now.. to have any code snippet here on the blog I just have to say “to_wordpress_html” as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def to_wordpress_html file_path
  return unless file_path

  require 'coderay'
  html = CodeRay.scan_file( file_path, :ruby).div(:line_numbers => :table)

  html = "<div style=\"overflow:auto;margin-top: 5px;\">#{html}</div>"

  html.sub!('class="code"><pre>', ' class="code"><pre style="padding-left: 2px;margin-bottom: 0;padding-bottom: 0;"> ')
  html.sub!('<pre><a href="#n1" ',' <pre style="border-right: 1px solid gray;padding-left: 2px;padding-right: 2px;margin-bottom: 0;padding-bottom: 0;"><a href="#n1" ')

  html.sub!('class="CodeRay"',      ' class="CodeRay" style="margin-bottom:0;" ')
  html.sub!('class="line-numbers"', ' class="line-numbers" style="padding:0;" ')
  html.sub!('class="code"',         ' class="code" style="padding:0;" ')
end  

if not ARGV[0]
  puts "Require ruby file."
else
  File.open('code.html','w'){ |file| file.write to_wordpress_html(ARGV[0]) }
end

# ruby coderay_for_wordpress.rb coderay_for_wordpress.rb

May be I will later add support for lines from and to, and more…

adding to my migration helper

Recently I added few methods to my migration helpers for the reasons

  1. Rails support for many databases causes not to have foreign key support, which I wanted to have
  2. Why to use default SIGNED column for id or for other column where it is not needed, so getting more numbers store
  3. One may not want to have unicode support for system managed tables(with no user generated content), unicode table requires more space.

I tested below units against mysql 5.1 only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# migration_helpers.rb
module MigrationHelpers

  def add_foreign_key(from_table, from_column, to_table, to_column='id')
    constraint_name = "fk_#{from_table}_#{from_column}"
    #execute %{alter table #{from_table} add index (#{from_column})}
    add_index from_table, from_column
    execute %{alter table #{from_table} add constraint #{constraint_name} foreign key (#{from_column}) references #{to_table}(#{to_column}) ON DELETE NO ACTION ON UPDATE NO ACTION}
  end

  def remove_foreign_key(from_table, from_column)
    constraint_name = "fk_#{from_table}_#{from_column}"
    execute %{alter table #{from_table} drop foreign key #{constraint_name}}
    #execute %{alter table #{from_table} drop index index_on_#{from_table}_on_#{from_column}} # column needs to be dropped first
  end

  def innodb_latin
    'ENGINE = InnoDB DEFAULT CHARACTER SET = latin1'
  end

  def id_column
    'INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)'
  end

  def foreign_key_column
    'INT UNSIGNED'
  end

  def count_coulmn
    'INT UNSIGNED DEFAULT 0'
  end

  def number_column
    'INT UNSIGNED'
  end
end

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CreateAuthentications < ActiveRecord::Migration
  def self.up
    create_table :authentications, :id => false, :options => innodb_latin do |t|
      t.column      :id, id_column
      t.column      :user_id, foreign_key_column
      t.string      :provider
      t.string      :uid
      t.text        :user_data
      t.timestamps
    end
  end

  def self.down
    drop_table :authentications
  end
end