myGrid/t2-server-gem

View on GitHub
bin/t2-delete-runs

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# Copyright (c) 2010-2012 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 = {}
conn_params, creds = register_options("Usage: t2-delete-runs [options] "\
    "server-address [run-ids...]") do |opt|
  opt.separator "  Where server-address is the full URI of the server to "\
    "connect to,"
  opt.separator "  e.g.: http://example.com:8080/taverna, run-ids are the "\
    "id numbers"
  opt.separator "  of the runs you want to delete and [options] can be:"
  opt.on("--all", "Delete all runs on the server") do
    options[:all] = true
  end
  opt.on("-f", "--finished", "Delete only finished runs. Combine with --all "\
    "to delete all finished runs") do
      options[:finished] = true
  end
end

# get runs and server address from the arguments
runs = []
address = ""
for arg in ARGV
  if arg.match(/https?:\/\//) == nil
    runs << arg
  else
    address = arg
  end
end

uri, creds = parse_address(address, creds)

# connect...
begin
  server = T2Server::Server.new(uri, conn_params)
rescue RuntimeError => e
  puts e
  exit 1
end

# ...get the runs...
server_runs = server.runs(creds)

# ...and delete them!
for run in server_runs
  begin
    if options[:all] || runs.include?(run.id)
      if options[:finished]
        run.delete if run.finished?
      else
        run.delete
      end
    end
  rescue T2Server::AuthorizationError => ae
    puts "You are not authorized to delete run '#{run.id}' - skipping."
    next
  rescue T2Server::T2ServerError => e
    puts "There was a problem while deleting run '#{run.id}' - skipping."
    next
  end
end