You have fallen into a pit trap

Zargon

Using Xz With Ruby

LZMA2 / xz are the hottest new compression algorithms on the block. When I found out Katello would have to decrypt certain data payloads compressed with xz, I was excited to work on it.

I found the excellent ruby-xz package and got to work. My little bit of code (not quite ready for a pull request yet, otherwise I’d link it) would simply have to check a file on a remote server, figure out if the file was newer than the last check, and if so decrypt it and process it.

This gem makes it so simple! You need to require ‘open-uri’ to open URLs as file streams first.

The data I’m parsing is JSON, so I can’t load the data in chunks. Random chunks of decompressed data would not be valid JSON.

1
2
3
4
5
6
7
8
require 'open-uri'
require 'ruby-xz'

...

open(@url) do |f|
  JSON.load(XZ.decompress_stream(f))
end

Fast & easy!

Hero Sheets - the Pathfinder Character Generator

I’ve started working on some software to improve management of Pathfinder character sheets. I play Pathfinder weekly, and while the game is quite nice, managing your character is an enormous pain in the ass. I constantly have to erase and rewrite stuff, people forget to bring their sheets and nobody knows how to level up. Surprisingly, nobody has really solved this problem with software. WOTC provides a pretty nice tool for electronic character management for D&D 4e, but it is surprisingly lacking in mobile support.

Hero Sheets logo

There isn’t even a section of the Pathfinder rulebook that explicitly states how leveling up works. The rules can be inferred by understanding multiple sections of the book, but surprisingly there is no flow chart or even simple paragraph explanation. I hope to fix that! The first goal of Hero Sheets is to make it simple to create and level up characters. A clear, wizard(hah!)-like interface and the ability to print a nicely formatted PDF will be a great start and should hopefully please players everywhere.

I plan on releasing the core rules module as an open source rubygem. I’ve mostly been working on the sign up page + blog right now, to help generate interest, but once the gem is a little more polished I’ll toss it up on github. Even if Hero Sheets isn’t successful, maybe the community can make something interesting!

Modular LDAP Auth for Ruby

ldap fluff provides an easy way for your ruby apps to query regular LDAP, FreeIPA and Active Directory systems for authentication and authorization.

It’s currently being used in the Katello Project to provide the auth system with authentication and group membership info

You can easily add ldap fluff to your system, especially if you’re already using devise or something similar, to add additional auth backend support. Here’s how to get started:

1
gem install ldap_fluff

You need /etc/ldap_fluff.yml to configure your LDAP connection. An example is provided in the source repo

Here’s an example to connect to my FreeIPA server, named AMERICA

config example
1
2
3
4
5
6
7
8
host:  america
port: 389
encryption: start_tls
base_dn: dc=jomara,dc=redhat,dc=com
group_base: dc=jomara,dc=redhat,dc=com
server_type: free_ipa
service_user: admin
service_pass: reallylookingforwardtotheweekend

The service_user doesn’t have to be admin (and probably shouldn’t be - but I’m no LDAP admin), but just any user that has R/O access to user & group data

Next, fire up irb or your rails console

irb fluff example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
irb(main):001:0> require 'ldap_fluff'
irb(main):002:0> ldap = LdapFluff.new
=> #<LdapFluff:0x7f7e9ca37358>...
irb(main):003:0> ldap.group_list('rstantz')
=> ["ipausers", "ghostbusters", "broskies", "eighties"]
irb(main):005:0> ldap.is_in_groups?('rstantz',['ghostbusters'])
=> true
irb(main):006:0> ldap.is_in_groups?('rstantz',['transformers','ghostbusters'])
=> false
irb(main):007:0> ldap.is_in_groups?('rstantz',['transformers'])
=> false
irb(main):009:0> ldap.authenticate?('rstantz','bustin')
=> false
irb(main):010:0> ldap.authenticate?('rstantz','ghostbustin')
=> true

Note that the “is in groups?” method is an AND operation. These methods should provide all you need to support read only authorization and authentication based on LDAP and LDAP groups.

Cron With Rails on Openshift

Openshift has a cron cartridge that’s very easy to install and configure. You simply install the cron cartridge:

1
rhc-ctl-app -a $appname -e add-cron-1.4

And populate the directory tree in your app’s git repository:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
openshift
├── cron
│   ├── daily
│   │   └── my_script.rb
│   ├── hourly
│   ├── minutely
│   ├── monthly
│   ├── README.cron
│   └── weekly
│       ├── chrono.dat
│       ├── chronograph
│       ├── jobs.allow
│       ├── jobs.deny
│       └── README

The trick is that unlike Heroku, the Openshift cron system is basically just like regular linux cron. They do not execute (by default) in your application’s environment. Thankfully, it’s very easy to set up your scripts to run this way.

ruby cron example
1
2
3
4
5
6
7
8
#!/usr/bin/env ruby
# this script will default to DEVELOPMENT if you dont force it
ENV['RAILS_ENV'] ||= 'production'

require File.expand_path("./config/environment", ENV['OPENSHIFT_REPO_DIR'])

Rails.logger.debug 'daily cron job!'
MyHelper.do_something_daily('internet')

It should be pretty obvious where to put your script files based on when you want them to run!