myGrid/t2-server-gem

View on GitHub
bin/t2-server-info

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# Copyright (c) 2010-2013 The University of Manchester, UK.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#  * Neither the names of The University of Manchester nor the names of its
#    contributors may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Robert Haines

require 'rubygems'
require 't2-server-cli'

include T2Server::CLI

# set up options
options = { :list => 0 }
conn_params, creds = register_options("Usage: t2-server-info [options] " +
  "server-address") do |opt|
  opt.separator "  Where server-address is the full URI of the server to"
  opt.separator "  connect to, e.g.: http://example.com:8080/taverna"
  opt.separator "  and [options] can be:"

  opt.on("-l", "--list", "List details for the runs on this server. Repeat "\
    "(e.g. -ll or -lll) to increase the amount of detail shown.") do
    options[:list] += 1
  end
end

if options[:list] > 0
  begin
    require 'hirb'
  rescue LoadError
    puts "\n****\nTo tabulate run information in your terminal the hirb gem "\
      "must be installed:\n\n$ gem install hirb\n\nRun information will not "\
      "be shown this time, sorry.\n****\n\n"
    options[:list] = 0
  end
end

# read and check server address and credentials
uri, creds = parse_address(ARGV.shift, creds)

# connect to server and output information
begin
  T2Server::Server.new(uri, conn_params) do |server|
    puts "     Server: #{server.uri}"
    puts "    Version: #{server.version}"
    puts "  Run limit: #{server.run_limit(creds)}"
    runs = server.runs(creds)
    puts "No. of runs: #{runs.length}"
    if options[:list] > 0 && runs.length > 0
      if options[:list] == 1
        fields = [:identifier]
        headers = {:identifier => 'Run ID'}
        lengths = {}
      elsif options[:list] == 2
        fields = [:identifier, :status, :expiry]
        headers = {:identifier => 'Run ID', :status => 'Status',
          :expiry => 'Expiry time (local)'}
        lengths = {:status => 11, :expiry => 19}
      else
        fields = [:identifier, :name, :status, :expiry]
        headers = {:identifier => 'Run ID', :name => "Name",
          :status => 'Status', :expiry => 'Expiry time (local)'}
        lengths = {:identifier => 11, :name => 25, :status => 11,
          :expiry => 19}
      end

      puts Hirb::Helpers::ObjectTable.render(runs,
        :fields => fields,
        :headers => headers,
        :filters => {:expiry => [:strftime, "%H:%M:%S %d/%m/%Y"]},
        :max_fields => lengths,
        :description => false)
    end
  end
rescue RuntimeError => e
  puts e
  exit 1
end