It's Not Personal

... well, maybe just a little bit.

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

How can I set up an editor to work with Git on Windows?

If you've ever asked yourself this question, checkout my answer on StackOverflow.com

Cheers

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)

Wednesday, April 29, 2009

Online Equation Builder

Hey, this is just a quick one to point out this cool on-line tool to build equations:

Roger's Online Equation Editor

You simply enter you LaTeX into the form, submit and save the image it generates. If like me, any clue you had on LaTeX markup is long-gone, there is a nice concise guide to the Mathsy stuff.

Nice one Roger.

Cheers

Friday, April 17, 2009

Darren on Ruby: Migrating Rails to Tomcat / JEE, Part 3: Using Warbler (Rack) to Deploy to Tomcat, Jetty or GlassFish

The Series So Far

Part 1: Switching to JRuby & Apache Derby showed you how to migrate from the Ruby+SQLite3 technology stack to JRuby+Derby.
Part 2: Migrating Your Data From SQLite3 to Derby showed you how to export your data out of your SQLite3 database and import that data into your new Apache Derby database.

Now I will take you through using Warbler to package your Rails app into a Web ARchive, ready for running on Tomcat, Jetty, Glassfish or whatever Servlet container you like.

Packaging Rails as a WAR

  1. Install Warbler:
    • jgem install warbler -y
  2. Configure Production Database:
    • Replace the production spec in RAILS_ROOT\config\database.yml with
      production:
        adapter: jdbc
        url: jdbc:derby:db\blog_ production;create=true
        driver: org.apache.derby.jdbc.EmbeddedDriver
        username: app
        password: app
      
  3. Satisfy Ruby Dependencies:
    • We need to make sure that all the Rails app's Ruby runtime dependencies are taken care of. We only need to worry about the dependencies that are required explicitly i.e. not provided transitively by Rails or Warbler. In our case it boils down to the JDBC/Derby stuff.
    • To specify the inclusion of these deps, we need to generate and modify the Warbler configuration; first run warble config, then edit the RAILS_ROOT\config\warble.rb file, replacing (or inserting after) this line
        # config.gems += ["activerecord-jdbcmysql-adapter", "jruby-openssl"]
      
      ...with this line
        config.gems += ["activerecord-jdbc-adapter", "activerecord-jdbcderby-adapter", "jdbc-derby", "jruby-openssl"]
      
  4. Satisfy Java Dependencies:
    • We need to make sure that all the Rails app's Java runtime dependencies are taken care of, that is, we must ensure the Derby jars will wind-up on the deployed applications CLASSPATH. Here are 3 ways to achieve this, in increasing scope:
      1. Copy derby.jar, derbyclient.jar and derbynet.jar in DERBY_HOME\lib to RAILS_ROOT\lib
      2. For Tomcat, copy these jars into TOMCAT_HOME\lib
      3. Copy them to JAVA_HOME\jre\lib\ext
      Taking the first option, Warbler will by default put these jars into WEB-INF\lib of the built WAR file.
  5. Rails 2.3.2 Session Management Issue:
    • We need to make few extra configuration changes due to an incompatibility with Warbler.
    • Uncomment the following line in RAILS_ROOT\config\initializers\session_store.rb
      # ActionController::Base.session_store = :active_record_store
      
    • Run the following commands to bring the database upto spec:
      jrake db:sessions:create RAILS_ENV=production
      jrake db:migrate RAILS_ENV=production
      
    • Now add the following line of code to the RAILS_ROOT\config\warble.rb file:
        config.webxml.jruby.session_store = 'db'
      
    • In RAILS_ROOT\config\environment.rb, just before
      Rails::Initializer.run do |config|
      
      ... add the following
      if defined?(JRUBY_VERSION)
        # hack to fix jruby-rack's incompatibility with rails edge
        module ActionController
          module Session
            class JavaServletStore
              def initialize(app, options={}); end
              def call(env); end
            end
          end
        end
      end
      
Now we should be good to go. Run warble which should produce blog.war. Now take this and drop it into TOMCAT_HOME\webapps. Restart Tomcat if it does not auto-deploy the application then navigate to your blog. This concludes this 3 part series on migrating from Ruby on Rails+SQLite3+WEBrick to JRuby on Rails+Derby+Tomcat. I hope through this blog I can spare you the pain I have gone through in figuring out how to get all this to work.

Epilogue

Next step for me is to get this procedure (of constructing the WAR file) mavenized. Maven handles WAR artifacts seemlessly; furthermore it knows how to 'overlay' WAR archives, reducing multiple WARs to one. This obviously simplifies the deployment and maintenance effort, a benefit I hope to reap in my current project that (now) produces three WAR achives. Good luck in your next steps. Cheers

Tuesday, April 14, 2009

Darren on Ruby: Migrating Rails to Tomcat / JEE, Part 2: Migrating Your Data From SQLite3 to Derby

The Series So Far

Part 1: Switching to JRuby & Apache Derby showed you how to migrate from the Ruby+SQLite3 technology stack to JRuby+Derby.

Now we will take a look at migrating the important stuff: your data. I found two ways to do a data migration: the easy way and the hard way. And unfortunately for me, I found the hard way first.

Update, 01/05/2009: You Don't Really Need to Migrate to Derby

You can continue to use SQLite3 if it so pleases you. You just need to install the necessary gems i.e. jdbc-adapter:
jgem install activerecord-jdbcsqlite3-adapter -y
The other thing is that it seems that SQLite3 allows you to use names for entities that are keywords in Derby e.g. min or max. Check here for a list of keywords used by Derby.

Do It The Easy Way, Do It The Ruby Way

To facilitate the easy way, we need to install the AR_Fixtures Rails plugin (not quite sure why this isn't a Gem, thus making it available to all Rails apps).
ruby script\plugin install http://topfunky.net/svn/plugins/ar_fixtures
This plugin gives us the db:fixtures:dump Rake task. As you'll see next it understands the MODEL and RAILS_ENV commandline/environment variables. It can't dump the entire database, strangely enough.

For this first step you need to go back to RAILS_ROOT\config\database.yml, disable (prepend an underscore) the jdbc development spec and re-enable the sqlite3 development spec. Then run the following commands:

rake db:fixtures:dump MODEL=Post
rake db:fixtures:dump MODEL=Comment
rake db:fixtures:dump MODEL=Tag
Take a look in RAILS_ROOT\test\fixtures; you should see posts.yml, comments.yml and tags.yml.

Now change you RAILS_ROOT\config\database.yml file to point to the jdbc development spec again, then run:

jrake db:migrate
jrake db:fixtures:load
You should now be good to go - start up your app using JRuby and test the blog data was migrated by going here.

Do It The Hard Way, Do It The Way I Did It When I Didn't Know The Easy Way

...Also The Way To Do It If Your Data Isn't Entirely Managed By ActiveRecord
  1. Export SQLite3 Data:
    • Run sqlite3 development.sqlite3 .dump >> development_dump.sql in RAILS_ROOT\db
  2. Tidy-up SQL Dump:
    • Open development_dump.sql in a text editor and make the following changes:
    • Remove any DDL or DML commands to do with the sqlite_sequence table
    • Remove any DDL or DML commands to do with the schema_migrations table, including the index
    • Unquote all table names e.g. change CREATE TABLE "posts" ... to CREATE TABLE posts ...
    • Replace AUTOINCREMENT NOT NULL with NOT NULL GENERATED BY DEFAULT AS IDENTITY
    • Replace datetime with timestamp
    • Replace integer\([0-9]*\) with integer
    • Replace text with long varchar
    • Replace DEFAULT ([0-9]*|NULL) NULL with DEFAULT NULL
    • Also be sure that any varchar(255) table columns only have that many characters, or else the import will complain and truncate your data (in this case, your precious blogs).
      If varchar(255) is not big enough, just switch it to long varchar
  3. Import into Derby:
    • Run ij (the Derby command-line client) in RAILS_ROOT\db
    • Run the following commands in ij:
      connect 'jdbc:derby:blog_development;create=true' as dev;
      run 'development_dump.sql';
      exit;
      

That should be it; you should have seen each model-table being created followed by the corresponding blog data going into Derby. You might see some 'Table already exists' errors when the DDL statements execute; these will occur if you previously ran jrake db:migrate, which creates the tables for you - just ignore them. Run the server again using JRuby and you should be able to access your blogs again.

What's Next?

Next I will step through the procedure to package your Rails application to a Web ARchive file, ready to deploy to Tomcat, Jetty or any JEE/Servlet container of your choice. You can catch that blog here

Cheers