<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dan.thoughts &#187; Linux</title>
	<atom:link href="http://blog.sosedoff.com/category/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sosedoff.com</link>
	<description>Web-development, PHP, Ruby, Sinatra, Merb, Rails, MySQL, SQLite, Web Services.</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:54:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Processing emails with Postfix and Rails</title>
		<link>http://blog.sosedoff.com/2011/08/10/processing-emails-with-postfix-and-rails/</link>
		<comments>http://blog.sosedoff.com/2011/08/10/processing-emails-with-postfix-and-rails/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 00:28:44 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby Gems]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[mail processing]]></category>
		<category><![CDATA[postfix]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=391</guid>
		<description><![CDATA[This is a short manual on how to setup postfix and rails application to receive and process email messages.
Stack:

Debian / Ubuntu Server
Postix
Ruby 1.9
Rails 3.0

Overview
You have an application where users get email notifications. And you want to allow them to reply directly to the email.
In order to do so, each email should have an unique (depends [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short manual on how to setup postfix and rails application to receive and process email messages.</p>
<p>Stack:</p>
<ul>
<li>Debian / Ubuntu Server</li>
<li>Postix</li>
<li>Ruby 1.9</li>
<li>Rails 3.0</li>
</ul>
<h2>Overview</h2>
<p>You have an application where users get email notifications. And you want to allow them to reply directly to the email.<br />
In order to do so, each email should have an unique (depends on situation) reply-to address. Usually its something like that:</p>
<pre><code>P946d272cf7da4dd6b0cb613605bced65@yourdomain.com
</code></pre>
<p>This means that the mailserver you use should support dynamic/virtual email addresses and forwarding.</p>
<h2>Postfix Configuration</h2>
<p>First, you&#8217;ll need to install postfix (in not installed):</p>
<pre><code>apt-get install postfix
</code></pre>
<p>Configuration should look like this:</p>
<pre><code># See /usr/share/postfix/main.cf.dist for a commented, more complete version

default_privs = apps

# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = ap

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=no
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = YOUR_APP_HOSTNAME
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydomain = YOUR_APP_DOMAIN
mydestination = YOUR_APP_DOMAIN, localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
relay_domain = localhost
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical
</code></pre>
<p>Last option <strong>recipient_canonical_maps</strong> allows you to define a dynamic email addresses and forward them to the specific system mailbox for processing.</p>
<p>Create a file /etc/postfix/recipient_canonical:</p>
<pre><code>/^P[0-9abcdef]{1,}(M[0-9]{1,})?/ apps
</code></pre>
<p>This will add a virtual recipient addresses and forward messages to user apps.</p>
<p><strong>NOTE:</strong> <em>Regular expressions should be in POSIX format</em>. For test you can use <a href="http://www.regextester.com/">regextester.com</a></p>
<h2>Email Aliases</h2>
<p>After you added support for virtual addresses all mail will be delivered to the system user mailbox (apps). But, we need to drive all that traffic into our app. In order to do so we will have to setup mail piping directly into your application script.</p>
<p>Edit /etc/aliases:</p>
<pre><code>apps: "| /home/apps/APP_NAME/current/script/email_receiver_script"
</code></pre>
<p>And rebuild the aliases db by running:</p>
<pre><code>newaliases
</code></pre>
<p>Do not forget to restart postfix:</p>
<pre><code>/etc/init.d/postfix restart
</code></pre>
<p>You can test out the email delivery. For errors check /var/log/mail.info</p>
<h2>Mail Receiver Script</h2>
<p>Since all mail will be forwarded directly to our mail receiver script via piping there are few things to consider:</p>
<ul>
<li>Email receiver should consume as less memory as possible.</li>
<li>Email receiver should not load the whole application (because of item above).</li>
<li>Email receiver should only validate and preprocess incoming messages and leave actual processing to another subsystem via queue.</li>
</ul>
<h3>Configuration</h3>
<p>There are few ruby libraries that are well suited for this case:</p>
<ul>
<li><a href="https://github.com/mikel/mail">mail</a> &#8211; Email processing, ruby 1.9.2 compatible (comparing to tmail which is not)</li>
<li><a href="https://github.com/antirez/redis">redis</a> &#8211; Simple key-value in-memory database.</li>
<li><a href="https://github.com/defunkt/resque">resque</a> &#8211; Redis-backed library for creating background jobs, placing those jobs on multiple queues, and processing them.</li>
</ul>
<p>Install gems:</p>
<pre><code>gem install mail redis resque
</code></pre>
<p>Here is an example email receiver script:</p>
<div class="highlight">
<pre><span class="c1">#!/usr/bin/env ruby</span> 

<span class="nb">require</span> <span class="s1">&#39;rubygems&#39;</span>
<span class="nb">require</span> <span class="s1">&#39;mail&#39;</span>
<span class="nb">require</span> <span class="s1">&#39;redis&#39;</span>
<span class="nb">require</span> <span class="s1">&#39;resque&#39;</span> 

<span class="k">class</span> <span class="nc">EmailReply</span>
  <span class="vi">@queue</span> <span class="o">=</span> <span class="ss">:email_replies</span> 

  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
    <span class="n">mail</span>    <span class="o">=</span> <span class="no">Mail</span><span class="o">.</span><span class="n">read_from_string</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
    <span class="n">from</span>    <span class="o">=</span> <span class="n">mail</span><span class="o">.</span><span class="n">from</span><span class="o">.</span><span class="n">first</span>
    <span class="n">to</span>      <span class="o">=</span> <span class="n">mail</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">first</span> 

    <span class="k">if</span> <span class="n">mail</span><span class="o">.</span><span class="n">multipart?</span>
      <span class="n">part</span> <span class="o">=</span> <span class="n">mail</span><span class="o">.</span><span class="n">parts</span><span class="o">.</span><span class="n">select</span> <span class="p">{</span> <span class="o">|</span><span class="nb">p</span><span class="o">|</span> <span class="nb">p</span><span class="o">.</span><span class="n">content_type</span> <span class="o">=~</span> <span class="sr">/text\/plain/</span> <span class="p">}</span><span class="o">.</span><span class="n">first</span> <span class="k">rescue</span> <span class="kp">nil</span>
      <span class="k">unless</span> <span class="n">part</span><span class="o">.</span><span class="n">nil?</span>
        <span class="n">message</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">body</span><span class="o">.</span><span class="n">decoded</span>
      <span class="k">end</span>
    <span class="k">else</span>
      <span class="n">message</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">body</span><span class="o">.</span><span class="n">decoded</span>
    <span class="k">end</span> 

    <span class="k">unless</span> <span class="n">message</span><span class="o">.</span><span class="n">nil?</span>
      <span class="no">Resque</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="no">EmailReply</span><span class="p">,</span> <span class="n">from</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span> 

<span class="no">EmailReply</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="vg">$stdin</span><span class="o">.</span><span class="n">read</span><span class="p">)</span>
</pre>
</div>
<p>This script receives the mail message then tries to extract the plaintext body. If the email message is valid it adds it to the queue for future processing.</p>
<h2>Mail Queue processing</h2>
<p>After we put emails into the queue we&#8217;ll need to create a worker.</p>
<p>If you need to extract a reply from the body, use (mail_extract)[https://github.com/sosedoff/mail_extract]:</p>
<pre><code>gem install mail_extract
</code></pre>
<p>Simple worker (resque job worker), extracted from one of the projects. (RAILS_ROOT/lib/email_reply.rb):</p>
<div class="highlight">
<pre><span class="k">class</span> <span class="nc">InvalidReplyUUID</span>    <span class="o">&lt;</span> <span class="no">StandardError</span> <span class="p">;</span> <span class="k">end</span>
<span class="k">class</span> <span class="nc">InvalidReplyUser</span>    <span class="o">&lt;</span> <span class="no">StandardError</span> <span class="p">;</span> <span class="k">end</span>
<span class="k">class</span> <span class="nc">InvalidReplyProject</span> <span class="o">&lt;</span> <span class="no">StandardError</span> <span class="p">;</span> <span class="k">end</span>
<span class="k">class</span> <span class="nc">InvalidReplyMessage</span> <span class="o">&lt;</span> <span class="no">StandardError</span> <span class="p">;</span> <span class="k">end</span> 

<span class="k">class</span> <span class="nc">EmailReply</span>
  <span class="vi">@queue</span> <span class="o">=</span> <span class="ss">:email_replies</span> 

  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">parse_email_uuid</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">str</span> <span class="o">=~</span> <span class="sr">/^P[0-9abcdef]+(M[\d]+)?@/i</span>
      <span class="n">parts</span> <span class="o">=</span> <span class="n">str</span><span class="o">.</span><span class="n">scan</span><span class="p">(</span><span class="sr">/^P([0-9abcdef]+)(M([\d]+))?/</span><span class="p">)</span><span class="o">.</span><span class="n">flatten</span>
      <span class="n">project_uuid</span> <span class="o">=</span> <span class="n">parts</span><span class="o">.</span><span class="n">first</span>
      <span class="n">message_id</span> <span class="o">=</span> <span class="n">parts</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">3</span> <span class="o">?</span> <span class="n">parts</span><span class="o">.</span><span class="n">last</span> <span class="p">:</span> <span class="kp">nil</span> 

      <span class="n">result</span> <span class="o">=</span> <span class="p">{</span><span class="ss">:project_uuid</span> <span class="o">=&gt;</span> <span class="n">project_uuid</span><span class="p">}</span>
      <span class="n">result</span><span class="o">[</span><span class="ss">:message_id</span><span class="o">]</span> <span class="o">=</span> <span class="n">message_id</span> <span class="k">unless</span> <span class="n">message_id</span><span class="o">.</span><span class="n">nil?</span>
      <span class="n">result</span>
    <span class="k">else</span>
      <span class="k">raise</span> <span class="no">InvalidReplyUUID</span><span class="p">,</span> <span class="s2">&quot;Invalid UUID: </span><span class="si">#{</span><span class="n">str</span><span class="si">}</span><span class="s2">&quot;</span>
    <span class="k">end</span>
  <span class="k">end</span> 

  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">perform</span><span class="p">(</span><span class="n">from</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">body</span><span class="p">)</span>
    <span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find_by_email</span><span class="p">(</span><span class="n">from</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">user</span><span class="o">.</span><span class="n">nil?</span>
      <span class="k">raise</span> <span class="no">InvalidReplyUser</span><span class="p">,</span> <span class="s2">&quot;User with email = </span><span class="si">#{</span><span class="n">from</span><span class="si">}</span><span class="s2"> is not a member of the app.&quot;</span>
    <span class="k">end</span> 

    <span class="n">info</span> <span class="o">=</span> <span class="n">parse_email_uuid</span><span class="p">(</span><span class="n">to</span><span class="p">)</span> 

    <span class="n">project</span> <span class="o">=</span> <span class="no">Project</span><span class="o">.</span><span class="n">find_by_uuid</span><span class="p">(</span><span class="n">info</span><span class="o">[</span><span class="ss">:project_uuid</span><span class="o">]</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">project</span><span class="o">.</span><span class="n">nil?</span>
      <span class="k">raise</span> <span class="no">InvalidReplyProject</span><span class="p">,</span> <span class="s2">&quot;Project with UUID = </span><span class="si">#{</span><span class="n">info</span><span class="o">[</span><span class="ss">:project_uuid</span><span class="o">]</span><span class="si">}</span><span class="s2"> was not found.&quot;</span>
    <span class="k">end</span> 

    <span class="k">if</span> <span class="n">info</span><span class="o">.</span><span class="n">key?</span><span class="p">(</span><span class="ss">:message_id</span><span class="p">)</span>
      <span class="n">message</span> <span class="o">=</span> <span class="n">project</span><span class="o">.</span><span class="n">messages</span><span class="o">.</span><span class="n">find_by_id</span><span class="p">(</span><span class="n">info</span><span class="o">[</span><span class="ss">:message_id</span><span class="o">]</span><span class="p">)</span>
      <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">nil?</span>
        <span class="k">raise</span> <span class="no">InvalidReplyMessage</span><span class="p">,</span> <span class="s2">&quot;Message with ID = </span><span class="si">#{</span><span class="n">info</span><span class="o">[</span><span class="ss">:message_id</span><span class="o">]</span><span class="si">}</span><span class="s2"> was not found on project &#39;</span><span class="si">#{</span><span class="n">project</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">&#39;&quot;</span>
      <span class="k">end</span>
    <span class="k">end</span> 

    <span class="n">params</span> <span class="o">=</span> <span class="p">{</span>
      <span class="ss">:project</span>  <span class="o">=&gt;</span> <span class="n">project</span><span class="p">,</span>
      <span class="ss">:body</span>     <span class="o">=&gt;</span> <span class="no">MailExtract</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">body</span><span class="p">)</span><span class="o">.</span><span class="n">body</span><span class="p">,</span>
      <span class="ss">:markup</span>   <span class="o">=&gt;</span> <span class="s1">&#39;plain&#39;</span><span class="p">,</span>
      <span class="ss">:sent_via</span> <span class="o">=&gt;</span> <span class="s1">&#39;email&#39;</span>
    <span class="p">}</span>
    <span class="n">params</span><span class="o">[</span><span class="ss">:message</span><span class="o">]</span> <span class="o">=</span> <span class="n">message</span> <span class="k">unless</span> <span class="n">message</span><span class="o">.</span><span class="n">nil?</span> 

    <span class="n">message</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">messages</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
    <span class="k">unless</span> <span class="n">message</span><span class="o">.</span><span class="n">save</span>
      <span class="k">raise</span> <span class="no">RuntimeError</span><span class="p">,</span> <span class="s2">&quot;Unable to save message. Errors: </span><span class="si">#{</span><span class="n">message</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">inspect</span><span class="si">}</span><span class="s2">&quot;</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre>
</div>
<p><strong>NOTE:</strong> <em>Its important that both mail receiver and worker are using the same queue.</em></p>
<p>Create a resque.rake in RAILS_ROOT/lib/tasks:</p>
<div class="highlight">
<pre><span class="nb">require</span> <span class="s1">&#39;resque/tasks&#39;</span>
<span class="n">task</span> <span class="s2">&quot;resque:setup&quot;</span> <span class="o">=&gt;</span> <span class="ss">:environment</span>
</pre>
</div>
<p>And fire it up:</p>
<pre><code>rake resque:work QUEUE=email_replies
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2011/08/10/processing-emails-with-postfix-and-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fast and easy password generation</title>
		<link>http://blog.sosedoff.com/2011/01/22/fast-and-easy-password-generation/</link>
		<comments>http://blog.sosedoff.com/2011/01/22/fast-and-easy-password-generation/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 01:21:15 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=316</guid>
		<description><![CDATA[I used to generate a lot of passwords and usually it would be an online service found by googling &#8220;generate password online&#8221;. It worked fine until i got tired of it and decided to find something else, much easier and faster. Something that will give me the results right away from terminal while doing server [...]]]></description>
			<content:encoded><![CDATA[<p>I used to generate a lot of passwords and usually it would be an online service found by googling &#8220;generate password online&#8221;. It worked fine until i got tired of it and decided to find something else, much easier and faster. Something that will give me the results right away from terminal while doing server setup. </p>
<p>Here we go, bash script (found online and modified by my needs):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">charspool</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'a'</span> <span style="color: #ff0000;">'b'</span> <span style="color: #ff0000;">'c'</span> <span style="color: #ff0000;">'d'</span> <span style="color: #ff0000;">'e'</span> <span style="color: #ff0000;">'f'</span> <span style="color: #ff0000;">'g'</span> <span style="color: #ff0000;">'h'</span> <span style="color: #ff0000;">'i'</span> <span style="color: #ff0000;">'j'</span> <span style="color: #ff0000;">'k'</span> <span style="color: #ff0000;">'l'</span> <span style="color: #ff0000;">'m'</span> <span style="color: #ff0000;">'n'</span> <span style="color: #ff0000;">'o'</span> <span style="color: #ff0000;">'p'</span>
<span style="color: #ff0000;">'q'</span> <span style="color: #ff0000;">'r'</span> <span style="color: #ff0000;">'s'</span> <span style="color: #ff0000;">'t'</span> <span style="color: #ff0000;">'u'</span> <span style="color: #ff0000;">'v'</span> <span style="color: #ff0000;">'w'</span> <span style="color: #ff0000;">'x'</span> <span style="color: #ff0000;">'y'</span> <span style="color: #ff0000;">'z'</span> <span style="color: #ff0000;">'0'</span> <span style="color: #ff0000;">'1'</span> <span style="color: #ff0000;">'2'</span> <span style="color: #ff0000;">'3'</span> <span style="color: #ff0000;">'4'</span> <span style="color: #ff0000;">'5'</span> <span style="color: #ff0000;">'6'</span> <span style="color: #ff0000;">'7'</span>
<span style="color: #ff0000;">'8'</span> <span style="color: #ff0000;">'9'</span> <span style="color: #ff0000;">'0'</span> <span style="color: #ff0000;">'A'</span> <span style="color: #ff0000;">'B'</span> <span style="color: #ff0000;">'C'</span> <span style="color: #ff0000;">'D'</span> <span style="color: #ff0000;">'E'</span> <span style="color: #ff0000;">'F'</span> <span style="color: #ff0000;">'G'</span> <span style="color: #ff0000;">'H'</span> <span style="color: #ff0000;">'I'</span> <span style="color: #ff0000;">'J'</span> <span style="color: #ff0000;">'K'</span> <span style="color: #ff0000;">'L'</span> <span style="color: #ff0000;">'M'</span> <span style="color: #ff0000;">'N'</span> <span style="color: #ff0000;">'O'</span>
<span style="color: #ff0000;">'P'</span> <span style="color: #ff0000;">'Q'</span> <span style="color: #ff0000;">'R'</span> <span style="color: #ff0000;">'S'</span> <span style="color: #ff0000;">'T'</span> <span style="color: #ff0000;">'U'</span> <span style="color: #ff0000;">'V'</span> <span style="color: #ff0000;">'W'</span> <span style="color: #ff0000;">'X'</span> <span style="color: #ff0000;">'Y'</span> <span style="color: #ff0000;">'Z'</span> <span style="color: #ff0000;">'-'</span> <span style="color: #ff0000;">'_'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
&nbsp;
<span style="color: #007800;">len</span>=<span style="color: #800000;">${#charspool[*]}</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-lt</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">num</span>=<span style="color: #000000;">20</span>;
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #007800;">num</span>=$<span style="color: #000000;">1</span>;
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">randomnumbers</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-c</span> <span style="color: #007800;">$num</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>urandom <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">od</span> <span style="color: #660033;">-t</span> u1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{for (i = 2; i &lt;= NF; i++) print $i}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;password: &quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> c <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$randomnumbers</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #800000;">${charspool[$((c % len))]}</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">echo</span></pre></div></div>

<h3>Installation</h3>
<p>Just create a script called something like &#8220;genpassword&#8221; in your bin dir and make it executable.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>genpassword
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>genpassword</pre></div></div>

<p>Works on any unix-like machine with bash installed.</p>
<h3>Usage</h3>
<p>Usage is pretty straight-forward. Just type &#8220;genpassword&#8221; in terminal and you&#8217;ll get the password.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">genpassword <span style="color: #666666; font-style: italic;"># This will give a default length (20 chars) password</span>
genpassword <span style="color: #000000;">64</span> <span style="color: #666666; font-style: italic;"># This will give you a 64 chars long password</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2011/01/22/fast-and-easy-password-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging email output in Rails</title>
		<link>http://blog.sosedoff.com/2010/11/05/debugging-email-output-in-rails/</link>
		<comments>http://blog.sosedoff.com/2010/11/05/debugging-email-output-in-rails/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 20:32:24 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[email templates]]></category>
		<category><![CDATA[sendmail]]></category>
		<category><![CDATA[troubleshooting]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=289</guid>
		<description><![CDATA[Need to troubleshoot email output? No problem. Just need to configure your development environment with the following lines:

# RAILS_ROOT/config/environments/development.rb
config.action_mailer.delivery_method = :sendmail 
config.action_mailer.sendmail_settings = &#123;:location =&#62; &#34;/usr/bin/fake-sendmail.sh&#34;&#125;
config.action_mailer.default_url_options = &#123; :host =&#62; &#34;YOUR HOST&#34; &#125;

And here is the fake-sendmail utility i wrote: (save as &#8220;/usr/bin/fake-sendmail.sh&#8221;)

#!/usr/bin/ruby
&#160;
dir = &#34;/tmp/fake-sendmail&#34;
headers = ''; $stdin.each_line &#123; &#124;l&#124; headers &#60;&#60; l ; break [...]]]></description>
			<content:encoded><![CDATA[<p>Need to troubleshoot email output? No problem. Just need to configure your development environment with the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># RAILS_ROOT/config/environments/development.rb</span>
config.<span style="color:#9900CC;">action_mailer</span>.<span style="color:#9900CC;">delivery_method</span> = <span style="color:#ff3333; font-weight:bold;">:sendmail</span> 
config.<span style="color:#9900CC;">action_mailer</span>.<span style="color:#9900CC;">sendmail_settings</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>:location <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/usr/bin/fake-sendmail.sh&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
config.<span style="color:#9900CC;">action_mailer</span>.<span style="color:#9900CC;">default_url_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;YOUR HOST&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>And here is the fake-sendmail utility i wrote: (save as &#8220;/usr/bin/fake-sendmail.sh&#8221;)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
dir = <span style="color:#996600;">&quot;/tmp/fake-sendmail&quot;</span>
headers = <span style="color:#996600;">''</span>; <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">each_line</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span> headers <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> l ; <span style="color:#9966CC; font-weight:bold;">break</span> <span style="color:#9966CC; font-weight:bold;">if</span> l.<span style="color:#9900CC;">strip</span>.<span style="color:#9900CC;">length</span> == <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
body = <span style="color:#996600;">''</span> ; <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">each_line</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span> body <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> l <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">format</span> = body.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>html<span style="color:#006600; font-weight:bold;">/</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#996600;">&quot;html&quot;</span> : <span style="color:#996600;">&quot;txt&quot;</span>
filename = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">to_i</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">mkdir</span><span style="color:#006600; font-weight:bold;">&#40;</span>dir<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>dir<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{dir}/#{filename}_headers.txt&quot;</span>, <span style="color:#996600;">&quot;w&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> f.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>headers<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{dir}/#{filename}.#{format}&quot;</span>, <span style="color:#996600;">&quot;w&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> f.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>body<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Paste: <a href="http://pastie.org/private/er61gafxb6fzowjo4chjvq">http://pastie.org/private/er61gafxb6fzowjo4chjvq</a></p>
<p>As the result you&#8217;ll see 2 files created for each outgoing email. One has all email headers and another one has the body. </p>
<p>Also, it works for Sinatra and Merb. Woot!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/11/05/debugging-email-output-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maintenance page for Rails applications with nginx</title>
		<link>http://blog.sosedoff.com/2010/09/22/maintenance-page-for-rails-applications-with-nginx/</link>
		<comments>http://blog.sosedoff.com/2010/09/22/maintenance-page-for-rails-applications-with-nginx/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 16:34:14 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[web servers]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=281</guid>
		<description><![CDATA[Here is a small tooltip how to setup maintenance page for Rails applications under nginx.  I usually put offline.txt into public directory while making any changes or testing.
Solution works with any type of backend server (passenger/php-fpm/upstream)
Sample vhost config:

server {
  listen 80;
  server_name HOSTNAME;

  location / {
    root /path/to/your/public/dir;
 [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a small tooltip how to setup maintenance page for Rails applications under nginx.  I usually put offline.txt into public directory while making any changes or testing.<br />
Solution works with any type of backend server (passenger/php-fpm/upstream)</p>
<p>Sample vhost config:</p>
<pre>
server {
  listen 80;
  server_name HOSTNAME;

  location / {
    root /path/to/your/public/dir;
    passenger_enabled on;
    rails_env production;

    if (-f $document_root/offline.txt) {
      return 503;
    }
  }

  location /maintenance.html {
    root /path/to/your/public/dir;
  }

  error_page 503 /maintenance.html;
}
</pre>
<p>Pastie: <a href="http://pastie.org/private/pgnhvnkj4uxe4mkgnhvqw">http://pastie.org/private/pgnhvnkj4uxe4mkgnhvqw</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/09/22/maintenance-page-for-rails-applications-with-nginx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom field aggregations in Sphinx using SphinxQL</title>
		<link>http://blog.sosedoff.com/2010/09/06/custom-field-aggregations-in-sphinx-using-sphinxql/</link>
		<comments>http://blog.sosedoff.com/2010/09/06/custom-field-aggregations-in-sphinx-using-sphinxql/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 01:52:23 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[aggregations]]></category>
		<category><![CDATA[indexing]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query language]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[sphinx ql]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=270</guid>
		<description><![CDATA[Sphinx is a really powerful tool for a full-text database search. It is the perfect option as a search engine on your website&#8217;s data.
In default mode it works as a regular tcp server and has multiple native language bindings for php, ruby, c, etc. But its another outstanding feature is MySQL Protocol Connectoin and SphinxQL, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sphinxsearch.com/">Sphinx</a> is a really powerful tool for a full-text database search. It is the perfect option as a search engine on your website&#8217;s data.<br />
In default mode it works as a regular tcp server and has multiple native language bindings for php, ruby, c, etc. But its another outstanding feature is MySQL Protocol Connectoin and SphinxQL, which is similar to native mysql query language. </p>
<p>So, ok. Lets say we have N documents with M attributes. Attributes could be different: string, integer, double, boolean. Out objective is to perform attribute aggregation based on specified search term (user-defined, etc). That will give us full information on data selected only by search term. Its only use-case when you really need to get these aggregate fields.  Next part is tricky and not really efficient. </p>
<p>First of all, you have to setup Sphinx search daemon instance using different configuration file (it could not run both). Another problem &#8211; you have to setup another data sources and index files, Sphinx puts a lock on all used-right-now files. </p>
<p>Lets assume we have a database of books. We need to build a form with sliders which could be used as user-friendly search filter. All we need is to get a list of min and max attributes values. But there is a problem: sometimes, while working with sphinx you might find yourself trying to use it like you usually do with regular RDMS. Unfortunately, sphinx has a different design. Basically, sphinx has one primary field which presents in each search request &#8211; DocumentID. Its an unique id that represents your data ID, which makes it harder to product aggregate data. And there is no way to get rid of that field.<br />
The whole idea of our aggregation &#8211; using boolean match mode with no weighting performed at all. In that case all results will have weight field = 1. That will give us ability to group all the results by weight field, rejecting the DocumentID field. </p>
<p>Here is the sample query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
  MIN<span style="color: #66cc66;">&#40;</span>reviews<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> min_reviews<span style="color: #66cc66;">,</span> MAX<span style="color: #66cc66;">&#40;</span>reviews<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_reviews<span style="color: #66cc66;">,</span>
  MIN<span style="color: #66cc66;">&#40;</span>pages<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> min_pages<span style="color: #66cc66;">,</span> MAX<span style="color: #66cc66;">&#40;</span>pages<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_pages<span style="color: #66cc66;">,</span>
  MIN<span style="color: #66cc66;">&#40;</span>pub_year<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> min_date<span style="color: #66cc66;">,</span> MAX<span style="color: #66cc66;">&#40;</span>pub_year<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_date<span style="color: #66cc66;">,</span>
  @weight <span style="color: #993333; font-weight: bold;">AS</span> w
<span style="color: #993333; font-weight: bold;">FROM</span> 
  INDEX_NAME
<span style="color: #993333; font-weight: bold;">WHERE</span>
  MATCH<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SEARCH_TERM'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span> pages <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">30</span>
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> w <span style="color: #993333; font-weight: bold;">OPTION</span> ranker <span style="color: #66cc66;">=</span> none</pre></div></div>

<p>The result of this query will be one row with field alias names. Thats&#8217;s it. </p>
<p>All statements are fully customizable. Just check <a href="http://www.sphinxsearch.com/docs/current.html#sphinxql-reference">full SphinxQL reference</a> for details.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/09/06/custom-field-aggregations-in-sphinx-using-sphinxql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippet: Cleanup your Git repository</title>
		<link>http://blog.sosedoff.com/2010/06/15/snippet-cleanup-your-git-repository/</link>
		<comments>http://blog.sosedoff.com/2010/06/15/snippet-cleanup-your-git-repository/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 06:35:56 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cleanup]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=240</guid>
		<description><![CDATA[Snippet (found on net) for removing files from repository that are no longer present under your project.

$ git rm $&#40;git ls-files -d&#41;

For best use add it to bash alias file: ~/.bashrc or ~/.bash-aliases (under ubuntu):

alias gitclean='git rm $(git ls-files -d)'

]]></description>
			<content:encoded><![CDATA[<p>Snippet (found on net) for removing files from repository that are no longer present under your project.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git <span style="color: #c20cb9; font-weight: bold;">rm</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>git ls-files -d<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>For best use add it to bash alias file: ~/.bashrc or ~/.bash-aliases (under ubuntu):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gitclean</span>=<span style="color: #ff0000;">'git rm $(git ls-files -d)'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/06/15/snippet-cleanup-your-git-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handy HTTP requests with Curb and Ruby</title>
		<link>http://blog.sosedoff.com/2010/06/13/handy-http-requests-with-curb-and-ruby/</link>
		<comments>http://blog.sosedoff.com/2010/06/13/handy-http-requests-with-curb-and-ruby/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 05:43:49 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[API's]]></category>
		<category><![CDATA[crawlers]]></category>
		<category><![CDATA[curb]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[requests]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=232</guid>
		<description><![CDATA[While working on one of the projects, i tried to find multi-purpose HTTP request class that can use different network interfaces/ip addresses with retry option (if connection slow or server not responding for some reason). 
Here is a small class wrapper build on top of Ruby Curb implemented as a module:

module ApiRequest
  USER_AGENTS = [...]]]></description>
			<content:encoded><![CDATA[<p>While working on one of the projects, i tried to find multi-purpose HTTP request class that can use different network interfaces/ip addresses with retry option (if connection slow or server not responding for some reason). </p>
<p>Here is a small class wrapper build on top of Ruby <a href="http://curb.rubyforge.org/">Curb</a> implemented as a module:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ApiRequest
  USER_AGENTS = <span style="color:#006600; font-weight:bold;">&#91;</span>
    <span style="color:#996600;">'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'</span>,
    <span style="color:#996600;">'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)'</span>,
    <span style="color:#996600;">'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'</span>,
    <span style="color:#996600;">'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4'</span>,
    <span style="color:#996600;">'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2) Gecko/20100323 Namoroka/3.6.2'</span>,
    <span style="color:#996600;">'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100401 Ubuntu/9.10 (karmic) Firefox/3.5.9'</span>
  <span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  CONNECTION_TIMEOUT = <span style="color:#006666;">10</span>
&nbsp;
  @@interfaces = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># get random user-agent string for usage</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> random_agent
    USER_AGENTS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span>USER_AGENTS.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># get random IP/network interface specified in @@interfaces</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> random_interface
    size = @@interfaces.<span style="color:#9900CC;">size</span>
    size <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? @@interfaces<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span>size<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># perform request, assign_to - specify network interface/ip</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> perform<span style="color:#006600; font-weight:bold;">&#40;</span>url, assign_to=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> url
    interface = assign_to.<span style="color:#0000FF; font-weight:bold;">nil</span>? ? <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">random_interface</span> : assign_to
    req = <span style="color:#6666ff; font-weight:bold;">Curl::Easy</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span>
    req.<span style="color:#9900CC;">timeout</span> = CONNECTION_TIMEOUT
    req.<span style="color:#9900CC;">interface</span> = interface <span style="color:#9966CC; font-weight:bold;">unless</span> interface.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    req.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'User-Agent'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">random_agent</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      req.<span style="color:#9900CC;">perform</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> req.<span style="color:#9900CC;">response_code</span> == <span style="color:#006666;">200</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> req.<span style="color:#9900CC;">downloaded_bytes</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? req.<span style="color:#9900CC;">body_str</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#0000FF; font-weight:bold;">nil</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># perform request by number of attempts</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> fetch<span style="color:#006600; font-weight:bold;">&#40;</span>url, attempts=<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    result = <span style="color:#0000FF; font-weight:bold;">nil</span>
    1.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>attempts<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
      result = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">perform</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">break</span> <span style="color:#9966CC; font-weight:bold;">unless</span> result.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> result
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And sample usage:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> TestRequest
  <span style="color:#9966CC; font-weight:bold;">include</span> ApiRequest
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> foo
     body = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">fetch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://google.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If module variable &#8220;<em>@@interfaces</em>&#8221; is array of ip addresses or network interfaces then one of them (randomly selected) will be used to perform request. Also, function &#8220;<em>fetch</em>&#8221; has parameter &#8220;<em>attempts</em>&#8221; which set to 3 by default. It means that operation will be invoked n times until result is downloaded from url. Otherwise &#8211; it returns nil.<br />
Function perform has a parameter &#8220;<em>assign_to</em>&#8221; (which it not used in &#8220;<em>fetch</em>&#8221; function) that allows to bind request to specified interface.  It is useful if you have situation when you might use different workers that bound to exact interface or just one that uses random ip`s. Also, class <em>ApiRequest</em> has a list of user agents which it uses randomly for each performed request.</p>
<p>Pastie: <a href="http://pastie.org/private/j19j3hbebte9bjqaydslmg">http://pastie.org/private/j19j3hbebte9bjqaydslmg</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/06/13/handy-http-requests-with-curb-and-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting processor affinity for a certain task or process in Linux</title>
		<link>http://blog.sosedoff.com/2010/06/06/setting-processor-affinity-for-a-certain-task-or-process-in-linux/</link>
		<comments>http://blog.sosedoff.com/2010/06/06/setting-processor-affinity-for-a-certain-task-or-process-in-linux/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 01:43:37 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[cores]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[smp]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=218</guid>
		<description><![CDATA[When you are using SMP you might want to override the kernel&#8217;s process scheduling and bind a certain process to a specific CPU(s).
What is this?
CPU affinity is nothing but a scheduler property that &#8220;bonds&#8221; a process to a given set of CPUs on the SMP system. The Linux scheduler will honor the given CPU affinity [...]]]></description>
			<content:encoded><![CDATA[<p>When you are using <a href="http://en.wikipedia.org/wiki/Symmetric_multiprocessing">SMP</a> you might want to override the kernel&#8217;s process scheduling and bind a certain process to a specific CPU(s).</p>
<h4>What is this?</h4>
<p>CPU affinity is nothing but a scheduler property that &#8220;bonds&#8221; a process to a given set of CPUs on the SMP system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity:</p>
<blockquote><p>The scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications. For example, application such as Oracle (ERP apps) use # of cpus per instance licensed. You can bound Oracle to specific CPU to avoid license problem. This is a really useful on large server having 4 or 8 CPUS</p></blockquote>
<h4>Setting processor affinity for a certain task or process using taskset command</h4>
<p>taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity. However taskset is not installed by default. You need to install schedutils (Linux scheduler utilities) package.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> shedutils</pre></div></div>

<p>Under latest version of Debian / Ubuntu Linux taskset is installed by default using util-linux package.</p>
<p>The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. For example:</p>
<ul>
<li>0&#215;00000001 is processor #0 (1st processor)</li>
<li>0&#215;00000003 is processors #0 and #1</li>
<li>0&#215;00000004 is processors #2 (3rd processor)</li>
</ul>
<p>To set the processor affinity of process 13545 to processor #0 (1st processor) type following command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ taskset 0x00000001 <span style="color: #660033;">-p</span> <span style="color: #000000;">13545</span></pre></div></div>

<p>If you find a bitmask hard to use, then you can specify a numerical list of processors instead of a bitmask using -c flag:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ taskset <span style="color: #660033;">-c</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-p</span> <span style="color: #000000;">13545</span>
$ taskset <span style="color: #660033;">-c</span> <span style="color: #000000;">3</span>,<span style="color: #000000;">4</span> <span style="color: #660033;">-p</span> <span style="color: #000000;">13545</span></pre></div></div>

<p>where  -p : Operate on an existing PID and not launch a new task (default is to launch a new task)</p>
<p><small>via <a href="http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html">http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/06/06/setting-processor-affinity-for-a-certain-task-or-process-in-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Git on CentOS 5.2</title>
		<link>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/</link>
		<comments>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 03:28:19 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[centos]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=151</guid>
		<description><![CDATA[First, we need to install all dependencies:

# yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel

Next, get the git 1.6.x sources:

# wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz

Then, unpack and cd into git sources folder and install it:

# make &#38;&#38; make install &#38; make clean

That`s it, now you`ll have git system ready to go.
]]></description>
			<content:encoded><![CDATA[<p>First, we need to install all dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel</span></pre></div></div>

<p>Next, get the git 1.6.x sources:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz</span></pre></div></div>

<p>Then, unpack and cd into git sources folder and install it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># make &amp;&amp; make install &amp; make clean</span></pre></div></div>

<p>That`s it, now you`ll have git system ready to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useless filesystem over MySQL</title>
		<link>http://blog.sosedoff.com/2009/04/06/useless-filesystem-over-mysql/</link>
		<comments>http://blog.sosedoff.com/2009/04/06/useless-filesystem-over-mysql/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 04:10:28 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[fuse]]></category>
		<category><![CDATA[mysqlfuse]]></category>
		<category><![CDATA[over]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=121</guid>
		<description><![CDATA[Here is a completely useless filesystem based on MySQL database storage &#8211; mysqlfuse, implemented with Fuse.
I didnt find any way how i can use it, but meanwhile, this fs working. Not perfect of course, in that case its not maintained for a long time. Doesnt support information about free drive space, so any filemanager keeps [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a completely useless filesystem based on MySQL database storage &#8211; mysqlfuse, implemented with Fuse.<br />
I didnt find any way how i can use it, but meanwhile, this fs working. Not perfect of course, in that case its not maintained for a long time. Doesnt support information about free drive space, so any filemanager keeps saying &#8216;Error: No space left on device&#8217;. Such case making it more useless. </p>
<p>It`s really easy to set it up. </p>
<p>First, we need to install developer headers for fuse:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libfuse-dev</pre></div></div>

<p>Next, getting sources (32bit only, not working in 64bit):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>voxel.dl.sourceforge.net<span style="color: #000000; font-weight: bold;">/</span>sourceforge<span style="color: #000000; font-weight: bold;">/</span>mysqlfs<span style="color: #000000; font-weight: bold;">/</span>mysqlfs-0.4.0-rc1.tar.bz2</pre></div></div>

<p>Unpack it, and compile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xjvf</span> mysqlfs-0.4.0-rc1.tar.bz2
$ <span style="color: #7a0874; font-weight: bold;">cd</span> mysqlfs-0.4.0-rc1
$ .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Next, we need to setup the database</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> mysqlfs<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">SELECT</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">INSERT</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">UPDATE</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">DELETE</span> <span style="color: #990099; font-weight: bold;">ON</span> mysqlfs.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> mysqlfs@<span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">%</span>&quot;</span> IDENTIFIED BY <span style="color: #008000;">'password'</span><span style="color: #000033;">;</span>
FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span></pre></div></div>

<p>And create database schema. SQL file located in root folder of the sources</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span> mysqlfs <span style="color: #000000; font-weight: bold;">&lt;</span> schema.sql</pre></div></div>

<p>And finally, mount filesystem to some folder:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqlfs <span style="color: #660033;">-ohost</span>=MYSQLHOST <span style="color: #660033;">-ouser</span>=MYSQLUSER <span style="color: #660033;">-opassword</span>=MYSQLPASS <span style="color: #660033;">-odatabase</span>=mysqlfs MOUNT_DIR</pre></div></div>

<p>Now, its gonna be working. To use automatic configuration parameters you can create section [mysqlfs] in your mysql configuration file (my.cnf)</p>
<p>Parameters:</p>
<pre>
-ohost=<hostname>
    MySQL server host

  -ouser=<username>
    MySQL username

  -opassword=
<password>
    MySQL password

  -odatabase=<db>
    MySQL database name
</pre>
<p>That`s it. Anyway, using FUSE there is a way to create so weird filesystems proxy. For example, there is SQLite over FUSE. And it is too old. Next time i`ll write about Amazon S3 over FUSE projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/04/06/useless-filesystem-over-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

