Nick Sieger: Blog Setupdo what you lovetag:blog.nicksieger.com,2005:TypoTypo2008-07-23T20:20:59+00:00Nickurn:uuid:962842f5-8d1a-4671-966b-fb59b6e7b9832008-07-23T20:20:59+00:002008-07-23T20:20:59+00:00Comment on Blog Setup by Nick<p>ivar: we typically just include the <code>db</code> directory and rakefile in our war file and do these tasks on the server with a jruby installation. I haven’t really seen any best practices in this area yet. There’s been talk of having the application run its own migrations at application startup, but that seems a little dangerous...</p>ivarurn:uuid:e87181c8-34a6-434a-b72d-fff4b92cc7a22008-07-23T17:45:39+00:002008-07-23T17:45:40+00:00Comment on Blog Setup by ivar<p>Having a simple recipe like this is very handy - thanks ! My next question is if you have a suggested method for handling migrations ? (or rake tasks in general)</p>Songurn:uuid:3b6c2da4-4cc6-4397-b5c7-5bd52e6dd85a2008-07-14T07:39:44+00:002008-07-14T07:39:45+00:00Comment on Blog Setup by Song<p>Thanks for sharing. I am waiting for this. </p>Ivan Vinturn:uuid:bbb7092c-fbc6-4113-8d6c-076f52df4fe82008-07-10T09:27:40+00:002008-07-10T09:27:40+00:00Comment on Blog Setup by Ivan Vint<p>Thanks, will try to setup a test blog on my gentoo linux desktop this weekend.</p>Alexander Mikhailianurn:uuid:993cfd62-2c1e-4b8a-b79b-df24bccf1f092008-07-10T08:37:14+00:002008-07-10T08:37:14+00:00Comment on Blog Setup by Alexander Mikhailian<p>As you mention using activerecord-jdbcmysql-adapter, may I point you to a recent bug at <a href='http://jira.codehaus.org/browse/JRUBY-2768' rel="nofollow">codehaus</a> that I introduced asking not to force the <tt>utf8_bin</tt> collation in the driver. </p>
<p>As a next step and probably as a TODO for the 1.0 version of the ActiveRecord-JDBC, it would be nice to allow multiple encodings, and have <tt>utf-8</tt> as a fallback enciding and not as the hard-wired rule.</p>
<p>This is probably more in line with the attitude of MRI 1.8.6 towards the encodings. </p>Nick Siegerurn:uuid:e7399075-a1ca-4af7-b122-ede5647c4fcc2008-07-10T05:10:27+00:002008-07-10T05:10:27+00:00Blog Setup<p>The other day <a href="http://blog.nicksieger.com/articles/2008/07/06/this-blog-powered-by-glassfish-jruby-and-jruby-rack#comments">several people chimed in</a> wondering how I set up this blog with JRuby and Glassfish. One of the reasons I didn’t include the details in the post is that it’s not really much different than any JRuby/Glassfish/Warbler deployment, but in case you don’t know what that looks like, here are the basics.</p>
<h3>Preconditions (Java)</h3>
<p>I’m running on a <a href="http://joyent.com/accelerator">Joyent Accelerator</a>, which runs <a href="http://opensolaris.org/">OpenSolaris</a>, which has JDK 6 installed by default. If you’re running on some flavor of Linux, hopefully <a href="https://jdk-distros.dev.java.net/">there’s a package available for you to install</a>, otherwise you may have to <a href="http://www.java.com/en/download/help/5000010500.xml">download a self-extracting binary</a>.</p>
<h3><a href="https://glassfish.dev.java.net/downloads/v2ur2-b04.html">Install Glassfish</a></h3>
<p>This step is actually straightforward; not at all as problematic as you might expect of a piece of Java technology! In the parent directory where you want Glassfish to be installed (substituting the name of the Glassfish jar you downloaded as appropriate):</p>
<pre><code>java -Xmx256m -jar glassfish-installer-v2ur2-b04-sunos_x86.jar
cd glassfish
chmod -R +x lib/ant/bin
./lib/ant/bin/ant -f setup.xml
</code></pre>
<p>Start Glassfish.</p>
<pre><code>./bin/asadmin start-domain
</code></pre>
<p>You may want to add <code>GLASSFISH/bin</code> to your path so that you can run the Glassfish <code>asadmin</code> command from anywhere.</p>
<p>On Solaris, <a href="http://www.sun.com/bigadmin/content/selfheal/smf-quickstart.jsp">SMF</a> is the subsystem that is used to ensure services are started at boot time (among other things). Glassfish <a href="http://blogs.sun.com/bloggerkedar/entry/app_server_and_solaris_10">works nicely with SMF</a>. On other systems, there may be <code>/etc/rc.d</code> init scripts out there, or you can roll your own (<code>asadmin start-domain</code> and <code>asadmin stop-domain</code>).</p>
<h3>Install JRuby</h3>
<p><a href="http://dist.codehaus.org/jruby/jruby-bin-1.1.2.tar.gz">Download JRuby</a> and <a href="http://wiki.jruby.org/wiki/Getting_Started">unpack it somewhere</a>. I recommend adding <code>JRUBY_HOME/bin</code> to the end of your path, so it doesn’t clash with Matz-Ruby.</p>
<h3>Install Warbler and activerecord-jdbcmysql-adapter</h3>
<p>In addition to Warbler, I’m using the activerecord-jdbcmysql-adapter to connect to the blog’s database. Both can be installed with Rubygems:</p>
<pre><code>jruby -S gem install warbler activerecord-jdbcmysql-adapter
</code></pre>
<p>With Rails 2 and up, the application’s <code>config/database.yml</code> file should be updated for <code>adapter: jdbcmysql</code>:</p>
<pre><code><% jdbc = defined?(JRUBY_VERSION) ? 'jdbc' : '' %>
development:
adapter: <%= jdbc %>mysql
encoding: utf8
database: testapp_development
username: root
password:
socket: /tmp/mysql.sock
# same for test/production...
</code></pre>
<p>Otherwise, you need to <a href="http://wiki.jruby.org/wiki/Running_Rails_with_ActiveRecord-JDBC">jump through some extra environment.rb configuration hoops</a>.</p>
<h3>Configure Warbler</h3>
<p>Warbler needs to be told about any gems that your application uses. To generate a Warbler configuration file:</p>
<pre><code>jruby -S warble config
</code></pre>
<p>The file is generated at <code>config/warble.rb</code>. In it, modify the following sections:</p>
<pre><code>config.gems = ["activerecord-jdbcmysql-adapter"]
...
config.webxml.jruby.min.runtimes = 2
config.webxml.jruby.max.runtimes = 4
</code></pre>
<h3>Build and deploy the .war</h3>
<pre><code>jruby -S warble
asadmin deploy --contextroot / blog.war
</code></pre>
<p>(<code>--contextroot /</code> makes the application rooted at <code>/</code> in the server, rather than at <code>/blog</code> which would be the default.)</p>
<p>At this point, the blog application is up and running on port 8080. I had previously been running the blog with an Apache/.htaccess-based setup reverse-proxying to mongrel, so all I had to do was change the port. I haven’t touched it since.</p>
<h3>But is this right for you?</h3>
<p>Chances are, this setup is overkill for a simple blog. If you’re going to try it, I’d recommend at minimum running on a VPS with at least 1G of memory. But once you get the core pieces in place, updating and re-deploying the application is really just as simple as the last two commands. It’s mundane and boring in its simplicity. But boring is good when you don’t want to worry about having to keep Mongrel running, or max out the memory in your server and make it unstable.</p>