Git-style binaries
16 December 2008
In this version of PoolParty internals, we’ll check out the git-style binaries used in PoolParty. Note, for this episode, I may move a little fast as there is a lot to cover.
Git, if you don’t already know is a super sleek SCM (Source control management system). It gives you hundreds of binaries using the idiom: git action options command… One big benefit to this is that all the binaries are self-contained, but function together.
With PoolParty, it makes sense to contain the binaries, both to enable quick development and to target the action the command-line method will be doing. This enables us to do cloud action, such as cloud start and cloud ssh. How do we do this? Let’s dive in!
Opening poolparty/bin/cloud, we see the following
#!/usr/bin/env ruby
require "poolparty"
require "poolpartycl"
name = ARGV.select {|arg| arg
if Binary.available_binaries_for("cloud").include?(arg) }.first
# Hiding methods here that strip out actions that aren't included
# in the PoolParty list of available command-line actions
# for brevity
# If no command is passed in, show help
new_args.push("-h") unless name
o = PoolParty::Optioner.new(new_args,
{:extra_help => "Cloud actions\n#{Binary.list_binaries_for("cloud")}",
:abstract => true}) do |opts, optioner|
opts.on('-n cloudname', '--name name', 'Address this cloud')
{ |c| optioner.cloudname c }
end
program_name = "#{File.basename($0)}-#{name}"
program_location = File.join(Binary.binary_directory, program_name)
command_line = "#{program_location}"
# Run it from the command-line first
if Binary.available_binaries_for("cloud").include?(name)
system command_line, *ARGV
else
puts "Unknown poolparty binary: #{name}"
end
Phew, that’s a lot. It’s super simple, the first few lines are ensuring that the action is contained in the list of available actions.
These are gathered just by looking in the directory with the binaries that start with “cloud,” in this case:
Dir["#{binary_directory}/#{ty}-*"].map {|a| File.basename(a.gsub(/#{ty}-/, '')) }.sort
Moving on, the Optioner is a custom optsparser class that sits inside of PoolParty that gives PoolParty the ability to extend it’s own option parser. Not much to the class really, but I urge you to check out the class if you are interested on knowing how it works.
Finally, we are going to call the command! We have to rebuild the arguments sent in such that the arguments are parsed properly by the next binary that will consume them. We can use the splat operator so that when the optsparser reads them again, it’s as though the arguments were entered on the command-line.
Phew, that was quite a bit! As always, I urge you to check out PoolParty if you haven’t already. It’s pretty spiffy and super fun!
Comments
blog comments powered by Disqus