Monday, October 04, 2010

Techie Tidbits: Get Going with Cygwin

Installing Cygwin is a pretty straight forward exercise - but here I walk-through a few extra configuration steps that make Cygwin a real productivity catalyst for my day-to-day activities.
1st, Install Cygwin
Download the setup.exe file from here. I usually install to C:\Cygwin and stick all the package stuff in C:\Cygwin\.pkg, where I also stick the setup.exe, once I've initially finished with it.
2nd, relocate /home
Following any install or update, I always start up a shell and remove the /home directory and symlink to C:\Documents and Settings (this some how still works on Win 7, but we could easily use C:\Users):
cd /
rm -fr home
ln -s "C:\Documents and Settings" home
This now relieves you of having to maintain two home directories - after a while of using Cygwin you'll start to wonder why they didn't do this automatically.
3rd, add some Bash Here action
Cygwin has a wealth of command-line tools for text processing; that's great - I find these immensely useful when tweaking configuration files or batch processing server log files, for Apache say. The annoying bit is getting a shell open in the directory where you need to go to work: cue Chere!

In the Select Packages screen, search for and install both chere and mintty packages. If you happen to be reading this through once first, you can go ahead and do this step when you first install Cygwin (in step 1).

Now fire up a shell and enter the following command;

chere -ia1 -t mintty -s bash -e 'Bash Here'
Now you should be able to right click on any directory in Windows Explorer and have a Bash Here option
4th, spin up SSHD (skip this if you're asking 'What's SSHD?')
Errm, actually, I'll save this for another blog...

Friday, April 23, 2010

Darren on Ruby: Adding arrays like vectors in Ruby

In the process of writing some RSpec tests, I found it useful to be able to add arrays of numbers together as you would vectors. Of course Ruby has a Vector class, but I was intrigued as to how I might implement such a thing and what extra utility I could provide; I came up with the following:
module Enumerable
  def **(other)
    zip(other.fill(0, other.size, size - other.size)).collect {|a| a.inject {|sum,e| sum+e}}
  end
  
  def vector_add *others
    sums = nil
    if block_given?
      each do |e|
        sums = sums.nil? ? yield(e) : sums ** yield(e)
      end
    else
      sums = self
    end
    
    others.each do |other|
      sums = sums.nil? ? other : sums ** other
    end if not others.empty?
    
    return sums
  end
end
As you can see I have overridden the ** (exponentiation) operator; not sure this is a great idea, that is perhaps violating some 'behavioral integrity', but it can always be changed or replaced. Try running the above and following code (try pasting into IRB)...
puts [1,2,3] ** [4,5,6]
puts [1,2,3,4] ** [4,5,6]
puts [1,2,3].vector_add([4,5,6])
puts [1,2,3,4].vector_add {|e| [e*1, e*2, e*3, e*4]}
puts [1,2,3,4].vector_add([4,5,6]) {|e| [e*1, e*2, e*3, e*4]}
You should get something like the following:
5
7
9
5
7
9
4
5
7
9
10
20
30
40
14
25
36
40
I'm pretty pleased with that. Now, back to RSpec...

Thursday, March 18, 2010

Techie Tidbits - Making Life a Little Easier

Every now and then I come across a wonderful solution to problems I never really realised I had. I've decided to post them all here so a) I can externalise my memory of them and b) Google can do its thing and share the wonderfulness with those that go looking. I'll try to treat this opening blog as an catalog linking to other posts with the details - enjoy q(^_^)p

Saturday, November 07, 2009

Darren on Flex: Flexins, Drag 'n' Drop Mixin now without sticky cursor and with selectable text

Recap of Version 0.3

If you quickly go back and play with the demo in my previous blog you'll find that if you drag across the TitleWindow i.e. pressing down the mouse button on the background (red canvas), the TitleWindow will stick to the cursor and drag along... not ideal.

Also, while it's not obvious in the previous demo application, clicking buttons, sliding scrollbar thumbs or selecting text in a component that is drag-enabled (by the mixin) will be interpreted as a drag gesture, thus preventing you from doing any of the above. Again, not ideal.

Not to worry, all is fixed

With version 0.3.2 none of that dodgy behaviour exists anymore and drag-enabled components behave as expected: The fixes are truly implementation details, but if you are really interested you can review the commits or each bug here and here at GitHub. That's it really. Visit the DnDMixin's homepage for more details and references to source code. Watch this space! as there's a new mixin in the pipeline: <dr:DataRenderer />

Tuesday, July 14, 2009

HOWTO: Configure Apache+Passenger with HTTPS/SSL

The following BASH commands assume you are in a root shell. It is quite convenient to switch using sudo -s; this should allow for plenty cut and paste action.
apt-get update
apt-get install -y ssh #... if like me you are working on a bare-bones VM
At this point I typically close my VM Server console (cause it's pants... until screw up my network and/or ssh config). I connect using keys from Cygwin; much more productive working this way.
apt-get install -y build-essential wget

Install Apache 2

apt-get -y install apache2 apache2-prefork-dev libreadline5-dev
cat > /etc/apache2/httpd.conf <<EOF
ServerName localhost
EOF

Install Enterprise Ruby (info) & Passenger

wget http://rubyforge.org/frs/download.php/58677/ruby-enterprise-1.8.6-20090610.tar.gz
tar -xzvf ruby-enterprise-1.8.6-20090610.tar.gz
./ruby-enterprise-1.8.6-20090610/installer <<EOF


EOF
/opt/ruby-enterprise-1.8.6-20090610/bin/gem install -y rails # specify version if necessary i.e. -v 2.1.0
cat > /etc/apache2/mods-available/passenger.load <<EOF
LoadModule passenger_module /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/mod_passenger.so
EOF

cat > /etc/apache2/mods-available/passenger.conf <<EOF
PassengerRoot /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4
PassengerRuby /opt/ruby-enterprise-1.8.6-20090610/bin/ruby
EOF
/opt/ruby-enterprise-1.8.6-20090610/bin/passenger-install-apache2-module <<EOF


EOF

Generate Certificate & Private Key

mkdir -p /etc/apache2/ssl/{crt,key}
openssl req -new -x509 -days 365 -keyout /etc/apache2/ssl/key/server.key -out /etc/apache2/ssl/crt/server.crt -nodes -subj '/O=Working With Mr.B, Ltd/OU=WWW Security Team/CN=workingwithmrb.blogspot.com'

Enable Apache Modules

a2enmod rewrite
a2enmod passenger
a2enmod ssl

Define & Install Virtual Host Rails

cat > /etc/apache2/sites-available/blog <<EOF
<VirtualHost *:80>
    ServerName workingwithmrb.blogspot.com
    DocumentRoot /var/www_rails/blog/current/public
    ErrorLog /var/www_rails/blog/current/log/apache.log

    <Location />
        RewriteEngine on
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R]
    </Location>
</VirtualHost>

<VirtualHost *:443>
    ServerName workingwithmrb.blogspot.com
    DocumentRoot /var/www_rails/blog/current/public
    
    ErrorLog /var/www_rails/blog/current/log/apache-ssl.log

    SSLEngine On
    
    SSLProtocol -all +TLSv1 +SSLv3
    SSLCipherSuite HIGH:MEDIUM:!aNULL:+SHA1:+MD5:+HIGH:+MEDIUM
    
    SSLCertificateFile /etc/apache2/ssl/crt/server.crt
    SSLCertificateKeyFile /etc/apache2/ssl/key/server.key
    
    SSLVerifyClient optional
    SSLVerifyDepth 1
    
    SSLOptions +StdEnvVars +StrictRequire
    
    CustomLog /var/log/apache2/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
EOF

a2ensite blog

Distinguish VirtualHosts Listening on Port 80 & 443

sed -i 's,VirtualHost \*,VirtualHost *:80,' /etc/apache2/sites-available/default
sed -i '/NameVirtualHost \*:80/ a NameVirtualHost *:443' /etc/apache2/sites-available/default

Give It A Spin

/etc/init.d/apache2 reload
netstat -napt
The output from netstat should show Apache listening on ports 80 and 443
Remember, if you install all this stuff as root, you must make sure that Apache+Passenger can access the rails application as www-data. Although it's a pretty stupid solution, you can achieve this by simply chmod -R o+rw <rails-app> in the parent directory. A better approach would be to create a user account e.g. called rails that belongs to the www-data group and with a umask of 0002, then anything to do with Rails you should do logged-in as that user.

Hope you find this simple and useful.

Sunday, July 05, 2009

Monday, May 18, 2009

I Broke Google!

... but it seems that the query is working now, lol.

Never seen this error page before... and I'll probably never see it again.

I'm having images of an old desktop in a very large air-craft hanging going, "Pooof"!

Update: Further more, I discovered I can't spell, lol (best of breed)