tag:blogger.com,1999:blog-88711698634837017562024-03-13T21:22:35.179-07:00Log of Day-to-day Findings in CodingWe used to stuck with many small issues in day-to-day coding. Again there can be small tips & tricks on how to get savvy, especially for less popular platforms like django & Ruby on Rails.
The target of this blog is list the solutions & tips a developer found in his day-to-day coding.Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-8871169863483701756.post-52575213292040282272011-02-19T21:49:00.000-08:002011-02-19T21:50:19.097-08:00Let facebook connect to your pc using reverse ssh tunnel from free Amazon AWS EC2Developing facebook app (or facebook connect/login/register) on developing workstation requires a callback URL where facebook will forward a user after user approval. Amazon.com is offering a cloud EC2 micro instance to any new user free for one year. And since each EC2 instance has a real IPv4 IP, we can easily utilize that for facebook's required callback url. Well, its not that easy to upload development code to EC2 instance every time that I need to test. The solution is <span style="font-weight: bold;">reverse ssh tunnel</span> from EC2 instance to my local pc behind NAT or firewall.<br /><br />Reverse ssh tunnel allows you use an encrypted tunnel for forwarding a connection to a remote host to your local workstation. To use reverse ssh tunnel one need to open a port from EC2 firewall configuration. I am running Ubuntu 10.04 LTS ami but this should work on all Linux instances.<br /><br />Say I want to open 9000 port on my EC2 instance. I assume that I am using the 'default' security group (each security group consists of set of firewall rules). To do so, run the following from <a href="http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/">ec2 command line tool</a> -<br /><pre class="brush:shell"><br />ec2-authorize default -p 9000<br /></pre><br /><br />Also authorize ssl port 22 if done yet.<br /><pre class="brush:shell"><br />ec2-authorize default -p 22<br /></pre><br /><br />Now log in to your ec2 instance and check if the following options exist in /etc/sshd_config file or not. If not, append the the lines to file.<br /><br />AllowTcpForwarding yes<br />GatewayPorts yes<br /><br />Now make the reverse ssh tunnel -<br /><pre class="brush:shell"><br />ssh -nNT -R0.0.0.0:9000:localhost:3000 <username>@<ec2_public_dns_or_ip> -i <your_private_key_file_that_ends_with_pem><br /></pre><br /><br />(Thanks to Vincent Danen for his tutorial <a href="http://www.techrepublic.com/article/setting-up-a-reverse-ssh-tunnel/5779944"><span style="font-size:100%;">Setting up a reverse SSH tunnel</span></a> where he shows how to do reverse ssh without opening a terminal). Here is description of the parameters:<br /><br />-n prevents reading from standard in<br />-N just set up a tunnel, without opening a console for executing command<br />-T disable pseudo tty allocation<br />-R reverse ssh tunnel. This parameter option must be followed by [bind_address:]port:host:hostport<br />-i your identity file (private key)<br /><br /><br />Thats it! Now point your browser to<br />http://<ec2_public_dns_or_ip>:9000<br /><br />enjoy :)Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-47037632363317187312008-12-06T07:05:00.000-08:002008-12-06T07:30:20.270-08:00Some months with RSpec<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_1Qaj3RGyCcY/STqXv3n3hQI/AAAAAAAAAKA/lVS_DfgWvus/s1600-h/rcov.png"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 320px; height: 129px;" src="http://3.bp.blogspot.com/_1Qaj3RGyCcY/STqXv3n3hQI/AAAAAAAAAKA/lVS_DfgWvus/s320/rcov.png" alt="" id="BLOGGER_PHOTO_ID_5276696762058769666" border="0" /></a><br />For several months, I have been writing test-case ahead of any implementation. But the joy multiplied when we started thinking BDD. To me using <a href="http://rspec.info/">RSpec</a> as BDD tool is not much different than Rails default test mechanism other than the representation, unless you try the <a href="http://github.com/aslakhellesoy/cucumber/wikis">Story</a>.<br /><br />Test first development has a big advantage that you consider all possible scenarios before writing code. Yet sometimes you may miss some lines covered. An wonderful gem you require to check test coverage in Ruby is rcov. And no wonder RSpec has built-in support for rcov :) To test you code coverage, run<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>rake spec:rcov<br /></code></pre><br />This will create a folder named "coverage" on your rails root. Open the index.html from that folder in your browser to see how much your code is covered by test case. The screenshot is a polling script I just started.Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com1tag:blogger.com,1999:blog-8871169863483701756.post-56601069096056697462008-12-01T00:48:00.000-08:002008-12-01T02:20:41.036-08:00Show numeric digit in your languageMy previous post was about a Ruby on Rails plugin I wrote to convert any numeric digit to other languages' representations. Here is the internals; how that works.<br /><br />That plugin converts a whole string character by character. To add any language, just add that language's digits to "NUMERIC_LOCALIZE_MAP" hash.<br /><br /><div class="code"><br /><pre><span class="constant">NUMERIC_LOCALIZE_MAP</span> <span class="punct">=</span> <span class="punct">{</span><br /> <span class="punct">'</span><span class="string">bn</span><span class="punct">'</span> <span class="punct">=></span> <span class="punct">["</span><span class="string">০</span><span class="punct">","</span><span class="string">১</span><span class="punct">","</span><span class="string">২</span><span class="punct">","</span><span class="string">৩</span><span class="punct">","</span><span class="string">৪</span><span class="punct">","</span><span class="string">৫</span><span class="punct">","</span><span class="string">৬</span><span class="punct">","</span><span class="string">৭</span><span class="punct">","</span><span class="string">৮</span><span class="punct">","</span><span class="string">৯</span><span class="punct">"],</span><br /> <span class="punct">'</span><span class="string">hi</span><span class="punct">'</span> <span class="punct">=></span> <span class="punct">["</span><span class="string">०</span><span class="punct">","</span><span class="string">१</span><span class="punct">","</span><span class="string">२</span><span class="punct">","</span><span class="string">३</span><span class="punct">","</span><span class="string">४</span><span class="punct">","</span><span class="string">५</span><span class="punct">","</span><span class="string">६</span><span class="punct">","</span><span class="string">७</span><span class="punct">","</span><span class="string">८</span><span class="punct">","</span><span class="string">९</span><span class="punct">"],</span><br /> <span class="punct">'</span><span class="string">ar</span><span class="punct">'</span> <span class="punct">=></span> <span class="punct">["</span><span class="string">٠</span><span class="punct">",</span> <span class="punct">"</span><span class="string">١</span><span class="punct">","</span><span class="string">٢</span><span class="punct">","</span><span class="string">٣</span><span class="punct">","</span><span class="string">٤</span><span class="punct">","</span><span class="string">٥</span><span class="punct">","</span><span class="string">٦</span><span class="punct">","</span><span class="string">٧</span><span class="punct">","</span><span class="string">٨</span><span class="punct">","</span><span class="string">٩</span><span class="punct">"]</span><br /><span class="punct">}</span><br /><br /><span class="keyword">def </span><span class="method">localize_numeric</span><span class="punct">(</span><span class="ident">source</span><span class="punct">,</span> <span class="ident">locale_code</span> <span class="punct">=</span> <span class="constant">nil</span><span class="punct">)</span><br /><span class="ident"> string_representation</span> <span class="punct">=</span> <span class="ident">source</span><br /><span class="ident">result_string</span> <span class="punct">=</span> <span class="constant">nil</span><br /><br /><span class="keyword">if</span> <span class="ident">locale_code</span> <span class="punct">&&</span> <span class="constant">NUMERIC_LOCALIZE_MAP</span><span class="punct">.</span><span class="ident">include?</span><span class="punct">(</span><span class="ident">locale_code</span><span class="punct">)</span><br /><span class="ident"> result_string</span> <span class="punct">=</span> <span class="ident">string_representation</span><span class="punct">.</span><span class="ident">gsub</span><span class="punct">(/</span><span class="regex"><span class="escape">\d</span></span><span class="punct">/){|</span><span class="ident">digit</span><span class="punct">|</span><br /> <span class="constant">NUMERIC_LOCALIZE_MAP</span><span class="punct">[</span><span class="ident">locale_code</span><span class="punct">][</span><span class="ident">digit</span><span class="punct">.</span><span class="ident">to_i</span><span class="punct">]</span><br /> <span class="punct">}</span><br /><span class="keyword">else</span><br /> <span class="ident">result_string</span> <span class="punct">=</span> <span class="ident">string_representation</span><br /><span class="keyword">end</span><br /><span class="ident">result_string</span><br /><span class="keyword">end</span><br /></pre><br /></div>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-32978744432009676222008-11-25T01:58:00.000-08:002008-12-01T00:50:13.321-08:00Ruby on Rails plugin to localize numeric digits (translate to any language)Probably <a href="http://www.somewherein.net/">somewhere in...</a> is the first company to build Bangla web2.0 solutions using rails. The first problem we faced is the lack of support for Unicode in Ruby. But fortunately we rarely needed to use string funtions and thus avoid the consequence. Secondly, we needed a stable and efficient il8n library(plugin) for Ruby on Rails to translate the interface (we had planned to roll out with support for two language initially). The rescue came from <a href="http://www.globalize-rails.org/">globalize plugin</a>. Its pretty old and no update for quite a while, yet this robust plugin suits the best to our need. The translation list is backed by database. So, you can easily create admin panel to manage translation list.<br /><br />Then we felt the need for another feature; representing numeric digits to local language. And its better if that can work seamlessly with our existing code written using globalize plugin. The plugin <a href="http://github.com/yeameen/globalize_numeric/tree">globalize_numeric</a>, with globalize, can translate your digits to any language (currently have Banga, Hindi, and Arabic) with minimal change in code that uses globalize for localization.<br /><br />To add other language, add the language digits to NUMERIC_LOCALIZE_MAP in 'lib/core_ext.rb' file.<br /><br />Plugin's <a href="http://github.com/yeameen/globalize_numeric/tree">github repository</a>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com1tag:blogger.com,1999:blog-8871169863483701756.post-21130981347455071432008-11-24T19:54:00.000-08:002008-11-25T01:29:02.108-08:00Converting Bangla (Bengali) digits to ascii onesruby 1.8 doesn't support unicode. This creates problem when I need to parse Bangla digits in a text where each Bangla digit is represented as three bytes in utf-8.<br /><br />I used a simple approach to satisfiy my need - 1. take the the last byte of each bangla digit, and 2. subtract by last byte of bangla digit '0'. Converting the whole number is even simpler. Here is my code -<br /><div class="code"><br /><pre> <span class="keyword">def </span><span class="method">self.translate_number_from_bangla</span><span class="punct">(</span><span class="ident">p_bn_number</span><span class="punct">)</span><br /> <span class="ident">en_number</span> <span class="punct">=</span> <span class="number">0</span><br /> <span class="ident">length</span> <span class="punct">=</span> <span class="ident">p_bn_number</span><span class="punct">.</span><span class="ident">length</span><br /> <span class="keyword">for</span> <span class="ident">i</span> <span class="keyword">in</span> <span class="number">1</span><span class="punct">..</span><span class="ident">length</span><span class="punct">/</span><span class="number">3</span> <span class="keyword">do</span><br /> <span class="ident">cur_digit</span> <span class="punct">=</span> <span class="ident">p_bn_number</span><span class="punct">[</span><span class="number">3</span><span class="punct">*</span><span class="ident">i</span><span class="punct">-</span><span class="number">1</span><span class="punct">].</span><span class="ident">to_i</span> <span class="punct">-</span> <span class="number">166</span><br /> <span class="keyword">if</span> <span class="ident">cur_digit</span> <span class="punct">>=</span> <span class="number">0</span> <span class="punct">&&</span> <span class="ident">cur_digit</span> <span class="punct"><=</span> <span class="number">9</span><br /> <span class="ident">en_number</span> <span class="punct">*=</span> <span class="number">10</span><br /> <span class="ident">en_number</span> <span class="punct">+=</span> <span class="ident">cur_digit</span><br /> <span class="keyword">end</span><br /> <span class="keyword">end</span><br /> <span class="keyword">return</span> <span class="ident">en_number</span><br /> <span class="keyword">end</span><br /></pre><br /></div>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com1tag:blogger.com,1999:blog-8871169863483701756.post-17394437865002905742008-11-24T19:39:00.000-08:002008-11-25T00:42:07.595-08:00Tips: Update your rubygems (with special case)The most suggested updating technique is<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ sudo gem update --system<br /></code></pre><br /><br />But this technique may not work with rubygems version 1.1 and 1.2. run<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$gem -v<br /></code></pre><br />to know your rubygems version.<br /><br />For those having these two versions, install rubygems-update gem and then run update_rubygems command (as sudo if necessary)<br /><br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ sudo gem install rubygems-update<br />$ sudo update_rubygems<br /></code></pre>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-20364480538900821112008-04-15T09:23:00.001-07:002008-11-25T00:42:07.595-08:00View offline documentation of installed ruby gemsFrom command line, just run this -<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ gem server<br /></code></pre><br />This will start ruby documentation server on port 8088<br /><br />previously it was<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ gem_server<br /></code></pre><br />which is now deprecated and may not work in your systemYeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com2tag:blogger.com,1999:blog-8871169863483701756.post-16783320807804544342008-04-03T23:53:00.000-07:002008-11-25T00:42:34.199-08:00Installing Ruby On Rails on Ubuntu with rmagickMy laptop hard disk drive (it was a fujitsu drive on an HP Pavilion nobebook pc) was gone withing just two months of purchase. Later I got the replacement but, I had to spent another to prepare rails stack again :(<br />Anyway, here is the recipe for Ubuntu 7.10<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ sudo apt-get install ruby irb ri rdoc ruby1.8-dev libopenssl-ruby1.8 build-essential<br />$ wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz<br />$ tar xzvf rubygems-1.0.1.tgz<br />$ cd rubygems-1.0.1<br />$ sudo ruby setup.rb<br />$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem<br />$ sudo gem update --system<br />$ sudo gem install rails<br />$ sudo gem install mongrel<br /></code></pre><br />I couldn't install rmagick on the first place. Because building rmagick (v2.3.0) requires ImageMagick version 3.0 or greater but current Ubuntu repository provides only v2.6 (by the time I wrote this). So, I had to install from the source. Thanks to <a href="http://emmanueloga.wordpress.com/2008/01/25/setting-up-rmagick-2-needs-imagemagick-63x-on-ubuntu/">Emmannual</a> for this suggestion.<br />Remove ImageMagick, if you have already installed<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>sudo apt-get remove imagemagick<br /></code></pre><br />Now download, compile and install from the ImageMagick source<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ tar xvvzf ImageMagick.tar.gz<br />$ cd ImageMagick<br />$ ./configure –prefix=/usr<br />$ make<br />$ sudo make install<br />$ sudo gem install rmagick<br /></code></pre><br />Or alternatively, you can download binary of ImageMagick-dev from debian repo<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code><br />$ sudo apt-get install libmagick9-dev<br />$ sudo gem install rmagick<br /></code></pre><br />One more step. Install all necessary gems that are required by the project you are working. Like, our current rails project uses the gem 'memcache-client'. So, I ran the following<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>$ sudo apt-get install memcached<br />$ sudo gem install memcache-client<br /></code></pre>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-3578867635646854702008-04-03T00:29:00.000-07:002008-11-25T00:42:57.441-08:00Enable mod_rewrite in Apache under Ubuntu'mod_rewrite' is disabled in default apache2 installation. To enable this, I followed following procedure. Thanks to <a href="http://www.lavluda.com/">Lavlu</a> for showing me this.<br />At first, load mod_rewrite using Apache's mod enable command<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code># a2enmod rewrite<br /><br /></code></pre><br />Then enable mod_rewrite from Apache2's configuration file. In default installation, there should be a 000-dafult file under /etc/apache2/sites-enabled directory. You should modify that. Just make the value 'All' (it should be 'None' by default) to AllowOverride key for '/' and '/var/www/' directory. So, the line will look like -<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>AllowOverride All<br /><br /></code></pre><br /><br />Now force reload Apache configuration<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code># sudo /etc/init.d/apache2 force-reload<br /><br /></code></pre>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-68707413648119708802008-04-03T00:17:00.000-07:002008-11-25T00:43:11.051-08:00Backing up mysql database and restoreThis is also needed when you need to move your database elsewhere.<br />To backup your database -<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>mysqldump --user <mysql_username> --password=<mysql_password> <database> [<tablename>] > <output_filename><br /></code></pre><br />To restore/initiate from the backup<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>mysql --user <db_username> --password=<db_password> <dbname> < <dump_file><br /></code></pre>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-34631953432384404032008-03-20T02:13:00.000-07:002008-04-07T09:24:44.655-07:00Running a schedule task for a rails applicationThere are <a href="http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails">several ways</a> described in ruby on rails <a href="http://wiki.rubyonrails.org/rails">wiki</a>. But for our need, I took the simplest one - create a cron entry that executes <a href="http://wiki.rubyonrails.org/rails/pages/RunnerScript">RunnerScript</a>.<br /><br />At first create a static Model method. RunnerScript can only access model and run its class methods, it cannot access controllers. For example, we needed a script that periodically check for expired items and mark. So, I wrote a class method like follows -<br /><br /><div class="code"><pre><span class="keyword">def </span><span class="method">self.mark_expired_items</span><span class="punct">()</span><br /> <span class="ident">update_query</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">status = <span class="expr">#{Constant::Item::STATUS_EXPIRED}</span></span><span class="punct">"</span><br /> <span class="comment"># find expired items</span><br /> <span class="ident">update_conditions</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">status = <span class="expr">#{Constant::Item::STATUS_PUBLISHED}</span><br /> AND created_at < '<span class="expr">#{(-Constant::Item::EXPRITY_TIME).days.from_now.to_s(:db)}</span>'</span><span class="punct">"</span><br /> <span class="ident">update_all</span><span class="punct">(</span><span class="ident">update_query</span><span class="punct">,</span> <span class="ident">update_conditions</span><span class="punct">)</span><br /> <span class="keyword">end</span><br /><br /></pre><br /></div><br />Then create a cron entry that runs this periodically. For our purpose (we wanted to expire item 4 days after publishing)<br />0 * * * * <project_path>/script/runner Item.mark_expired_items<br /><br />Thats it. This script checks every hour for expired items and mark all of those as expiredYeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com1tag:blogger.com,1999:blog-8871169863483701756.post-22219427725502539132008-03-18T09:29:00.000-07:002008-11-25T00:43:28.071-08:00ssh login without re-entering passwordAt my work, i have to enter our hosting server. Sometimes, several times a day. And obviously I have to enter password to get through.<br /><br />I knew the solution before. We used to do this in our distributed systems lab at college. Because, sometimes it becomes necessary to enter a remote machine without password by the automated scripts.<br /><br />And today I had to rediscover this again ;)<br /><br />Enter the remote machine and check there exists .ssh folder under `~` (home) folder. if it doesn't exist, create one.<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>mkdir ~/.ssh<br /></code></pre><br />then 'exit' to come back to your own console.<br />2. create your own key. For simplicity, just press enter at all prompts<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>ssh-keygen -t dsa<br /></code></pre><br />3. Copy your ssh key to remote host<br /><pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>cat ~/.ssh/id_dsa.pub | ssh <remote_host_address> "cat - >> ~/.ssh/authorized_keys"<br /></code></pre><br />Voilà! You're done. Now try to ssh again to the remote host. You'll get the remote shell without any password :)Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0tag:blogger.com,1999:blog-8871169863483701756.post-47385191912288651372008-03-17T02:58:00.000-07:002008-11-25T00:43:47.785-08:00Migrating subversion repository - forking new projectThis far in our company we were using single repository project to maintain all our projects. That is, we create a new subdirectory in the root project directory when we need to start another project. It requires less server side maintenance at the cost of less security. It was just fine for a small developer team, until recently.<br /><br />Now I need to create projects out from each directory under root (i.e. each project), with revision number starting from 1 for each project.<br /><br />The steps I followed -<br />1. Created svk mirror of each projects root directory.<br />2. Dump svk's local mirror. This creates a plain svn dump maintaining projects revision number<br />3. Manually removing projects root directory<br />4. Load svn dump to a new svn repository<br /><br />That's it :)<br /><br /><span style="font-weight: bold;">1. Create svk mirror for each project<br /></span>a. initialize local mirror<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>svk mirror <svn url>/<project_directory> //<project_name><br /></code></pre><br />b. sync, copy project directory to local svk mirror<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>svk sync //<project_name><br /></code></pre><span style="font-weight: bold;">2. Dump local svk mirror</span><br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>svnadmin dump ~/.svk/local > <dumpfile><br /></code></pre>3. Manually removing project's root directory<br /><br /><br />4. Create a new project and load the dump<br /><br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>sudo svnadmin create <project_name><br /><br />sudo svnadmin load <project_name> < <dumpfile><br /></code></pre>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com1tag:blogger.com,1999:blog-8871169863483701756.post-50180085575079356492008-02-25T01:33:00.000-08:002008-02-25T03:05:21.042-08:00Rails Time and DatabaseLast few days I improved rails code related to time function in several places.<br /><span style="font-weight: bold;"><br />1.</span><br />Rails extended basic Time class to give output in several format. So that you can output in database datetime format by passing :db symbol as argument to Time's to_s() method. Previously I produced database format manually<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>Time.now.strftime("%Y-%m-%d %H:%M:%S").to_s<br /></code></pre>now, it simplifies to<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>Time.now.to_s(:db)<br /></code></pre>Detail documentation can be found <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Conversions.html#M000569">here</a><br /><br /><span style="font-weight: bold;">2.</span><br />To parse database date to ruby Time, I used this.<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>Time.parse(token.created_at.to_s)<br /></code></pre>where created_at is DateTime type field in Token model<br /><br /><span style="font-weight: bold;">3.</span><br />To get back-and-forth between Unix Timestamp and ruby Time, you can simply use -<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>Time.now.to_i<br /></code></pre>and to read -<br /><pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"><code>Time.at(unix_timestamp)<br /></code></pre>Yeameenhttp://www.blogger.com/profile/17132774104653769691noreply@blogger.com0