Posts Tagged ruby

Building Clouds

I've spent this year building networks using Amazon Web Services and teaching people how to do it. So I'd like to share the code that I've used as teaching examples and as seeds for the creation of some pretty cool environments.

  • AWS PY was my first published attempt at interacting with AWS in python & Puppet to instantiate, provison and control EC2 instances, as well as the seed for an incredibly cool project at the start of this year.
  • AWS RB followed on to duplicate the instantiation and provisioning of EC2 instances using Amazon's Ruby APIs and Chef Solo. This was initially done as an itch that I had to scratch but since then it has been used as the seed for some of my paid AWS work.
  • AWS VPC once again uses Amazon's Ruby APIs and Chef Solo, this time to provision a Virtual Private Cloud. Amazon provides excellent documentation on what a VPC is and how to provision one using its web-based admin console, but I wanted to create a cloud from the ground up using repeatable scripts with no admin console interaction. The only pre-requisite is that you have an AWS account and have API keys (and have provided all the necessary details to Amazon so that they will allow you to create EC2 instances).
  • AWS VPC PY is my latest example and still a work in progress. It is designed to showcase how to create an AWS VPC using python, Boto and Fabric rather than Amazon's Ruby APIs. I'm not wedded to either approach so it is interesting to try out which one works & feels better in different contexts.

May you find these as useful as I have, and still do.

Tags: , , , , , , , ,

Kramdown and Webby

A number of the sites that I manage for fun are simply static web pages. The dynamic nature is handled by javascript classes and plugins. This means that I really don’t need or want an application server to serve these sites, but I do want to still use some of the practices that I apply to web application development. Enter webby – it works by combining the contents of a page with a layout to produce HTML. Awesomely simple and powerful. It can use the rdiscount gem to process files in markdown format into html pages, but I prefer to use the kramdown library. No problem, here is how you can add kramdown support to your webby project.

if try_require('kramdown', 'kramdown')
  Webby::Filters.register :kramdown do |input|
    Kramdown::Document.new(input, :parse_block_html => true).to_html
  end
else
  Webby::Filters.register :kramdown do |input|
    raise Webby::Error, "'kramdown' must be installed to use the kramdown filter"
  end
end

Simply paste the above into a file called kramdown.rb in your project’s lib directory and then you can specify filter: kramdown in your webby templates.

Tags: , , ,

RubyGems in a JAR

On a few projects now I’ve used jruby with rake. I know that I can use rvm and just fetch the gems per project, but for developers stuck on windows that way is a little more than difficult. So here’s how I package up rubygems in a JAR.

#!/bin/bash
mkdir gemjar
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar haml --version 3.1.2 --no-rdoc --no-ri
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar net-scp --version 1.0.4 --no-rdoc --no-ri
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar jruby-openssl --version 0.7.4 --no-rdoc --no-ri
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar fpm --version 0.3.7 --no-rdoc --no-ri
jar cf ruby-gems.jar -C gemjar .
rm -rf gemjar

You then invoke jruby rake via:

#!/bin/bash
java -jar jruby-complete-1.6.3.jar -rruby-gems.jar -S rake build.rb $@

This means that you can then invoke the following useful little nuggets:

task :list_gems do
  require "rubygems/gem_runner"
  Gem::GemRunner.new.run ["list"]
end

task :sass_watch do
  require 'haml'
  require 'haml/exec'
  opts = Haml::Exec::Sass.new ['--watch', 'src/main/sass:src/main/webapp/static/css']
  opts.parse!
end

task :ssh_copy, :file do |t, args|
  require "net/ssh"
  require "net/scp"
  Net::SSH.start("repository.remote", "user") do |ssh|
    ssh.scp.upload!(args.file, "/tmp/files/")
  end
end

task :make_rpm => :make_war do
  fail "Please install rpm-build to make RPMs!" unless system("rpmbuild --version")

  require "fpm"
  require "fpm/program"
  exit_code = FPM::Program.new.run([
    "-s", "dir", "-t", "rpm", "-n", APPLICATION_NAME, "-v", RPM_VERSION_NUMBER, "-a", "all",
    "--post-install", "target/rpmbuild/opt/application/bin/post_install.sh",
    "--pre-uninstall", "target/rpmbuild/opt/application/bin/pre_uninstall.sh",
    "-C", "target/rpmbuild", "opt"
  ])

  fail "Build failed" if exit_code != 0
end

How awesome is this?

Tags: , , , , , , , , ,