class T2Server::Run

An interface for easily running jobs on a Taverna 2 Server with minimal setup and configuration required.

A run can be in one of three states:

Attributes

id[R]

The identifier of this run on the server.

identifier[R]

The identifier of this run on the server.

server[R]

The server instance that this run is hosted on.

Public Class Methods

create(server, workflow) → run click to toggle source
create(server, workflow, connection_parameters) → run
create(server, workflow, user_credentials) → run
create(server, workflow, ...) {|run| ...}

Create a new run in the :initialized state. The run will be created on the server with address supplied by server. This can either be a String of the form http://example.com:8888/blah or an already created instance of T2Server::Server. The workflow must also be supplied as a string in t2flow or scufl format. User credentials and connection parameters can be supplied if required but are both optional. If server is an instance of T2Server::Server then connection_parameters will be ignored.

This method will yield the newly created Run if a block is given.

# File lib/t2-server/run.rb, line 141
def Run.create(server, workflow, *rest)
  credentials = nil
  uri = nil
  conn_params = nil

  rest.each do |param|
    case param
    when URI
      uri = param
    when ConnectionParameters
      conn_params = param
    when HttpCredentials
      credentials = param
    end
  end

  if server.class != Server
    server = Server.new(server, conn_params)
  end

  if uri.nil?
    uri = server.initialize_run(workflow, credentials)
  end

  run = new(server, uri, credentials)
  yield(run) if block_given?
  run
end

Public Instance Methods

add_keypair_credential(service_uri, filename, password, click to toggle source
alias = "Imported Certificate", type = :pkcs12) → URI

Provide a client certificate credential for the secure service at the specified URI. You will need to provide the password to unlock the private key. You will also need to provide the ‘alias’ or ‘friendlyName’ of the key you wish to use if it differs from the default. The URI of the credential on the server is returned. Only the owner of a run may supply credentials for it. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 714
def add_keypair_credential(uri, filename, password,
                           name = "Imported Certificate", type = :pkcs12)
  return unless owner?

  type = type.to_s.upcase
  contents = Base64.encode64(IO.read(filename))

  # basic uri checks
  uri = _check_cred_uri(uri)

  cred = XML::Fragments::KEYPAIR_CRED % [uri, name, contents,
    type, password]
  value = XML::Fragments::CREDENTIAL % cred

  @server.create(links[:sec_creds], value, "application/xml", @credentials)
end
add_password_credential(service_uri, username, password) → URI click to toggle source

Provide a username and password credential for the secure service at the specified URI. The URI of the credential on the server is returned. Only the owner of a run may supply credentials for it. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 683
def add_password_credential(uri, username, password)
  return unless owner?

  # Is this a new credential, or an update?
  cred_uri = credential(uri)

  # basic uri checks
  uri = _check_cred_uri(uri)

  cred = XML::Fragments::USERPASS_CRED % [uri, username, password]
  value = XML::Fragments::CREDENTIAL % cred

  if cred_uri.nil?
    @server.create(links[:sec_creds], value, "application/xml",
      @credentials)
  else
    @server.update(cred_uri, value, "application/xml", @credentials)
  end
end
add_trust(filename, type = :x509) → URI click to toggle source

Add a trusted identity (server public key) to verify peers when using https connections to Web Services. The URI of the trust on the server is returned. Only the owner of a run may add a trust. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 796
def add_trust(filename, type = :x509)
  return unless owner?

  type = type.to_s.upcase

  contents = Base64.encode64(IO.read(filename))

  value = XML::Fragments::TRUST % [contents, type]
  @server.create(links[:sec_trusts], value, "application/xml", @credentials)
end
baclava_input=(filename) → bool click to toggle source

Use a baclava file for the workflow inputs.

# File lib/t2-server/run.rb, line 454
def baclava_input=(filename)
  state = status
  raise RunStateError.new(state, :initialized) if state != :initialized

  file = upload_file(filename)
  result = @server.update(links[:baclava], file, "text/plain", @credentials)

  @baclava_in = true if result

  result
end
baclava_input? → bool click to toggle source

Have the inputs to this run been set by a baclava document?

# File lib/t2-server/run.rb, line 506
def baclava_input?
  @baclava_in
end
baclava_output → string click to toggle source

Get the outputs of this run in baclava format. This can only be done if the output has been requested in baclava format by set_baclava_output before starting the run.

# File lib/t2-server/run.rb, line 524
def baclava_output
  state = status
  raise RunStateError.new(state, :finished) if state != :finished

  raise AccessForbiddenError.new("baclava output") if !@baclava_out

  baclava_uri = Util.append_to_uri_path(links[:wdir], BACLAVA_FILE)
  @server.read(baclava_uri, "*/*", @credentials)
end
baclava_output? → bool click to toggle source

Has this run been set to return results in baclava format?

# File lib/t2-server/run.rb, line 514
def baclava_output?
  @baclava_out
end
create_time → string click to toggle source

Get the creation time of this run as an instance of class Time.

# File lib/t2-server/run.rb, line 583
def create_time
  Time.parse(@server.read(links[:createtime], "text/plain", @credentials))
end
credential(service_uri) → URI click to toggle source

Return the URI of the credential set for the supplied service, if any. Only the owner of a run may query its credentials. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 759
def credential(uri)
  return unless owner?

  credentials[uri]
end
credentials → Hash click to toggle source

Return a hash (service_uri => credential_uri) of all the credentials provided for this run. Only the owner of a run may query its credentials. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 737
def credentials
  return unless owner?

  creds = {}
  doc = xml_document(@server.read(links[:sec_creds], "application/xml",
    @credentials))

  xpath_find(doc, XPaths[:sec_cred]).each do |c|
    uri = URI.parse(xml_node_content(xpath_first(c, XPaths[:sec_suri])))
    cred_uri = URI.parse(xml_node_attribute(c, "href"))
    creds[uri] = cred_uri
  end

  creds
end
delete click to toggle source

Delete this run from the server.

# File lib/t2-server/run.rb, line 193
def delete
  @server.delete(@uri, @credentials)
end
delete_all_credentials → bool click to toggle source

Delete all credentials associated with this workflow run. Only the owner of a run may delete its credentials. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 783
def delete_all_credentials
  return unless owner?

  @server.delete(links[:sec_creds], @credentials)
end
delete_all_trusts → bool click to toggle source

Delete all trusted identities associated with this workflow run. Only the owner of a run may delete its trusts. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 847
def delete_all_trusts
  return unless owner?

  @server.delete(links[:sec_trusts], @credentials)
end
delete_credential(service_uri) → bool click to toggle source

Delete the credential that has been provided for the specified service. Only the owner of a run may delete its credentials. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 771
def delete_credential(uri)
  return unless owner?

  @server.delete(credentials[uri], @credentials)
end
delete_trust(URI) → bool click to toggle source

Delete the trust with the provided URI. Only the owner of a run may delete its trusts. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 835
def delete_trust(uri)
  return unless owner?

  @server.delete(uri, @credentials)
end
exitcode → integer click to toggle source

Get the return code of the run. Zero indicates success.

# File lib/t2-server/run.rb, line 379
def exitcode
  @server.read(links[:exitcode], "text/plain", @credentials).to_i
end
expiry → string click to toggle source

Return the expiry time of this run as an instance of class Time.

# File lib/t2-server/run.rb, line 285
def expiry
  Time.parse(@server.read(links[:expiry], "text/plain", @credentials))
end
expiry=(time) → bool click to toggle source

Set the expiry time of this run to time. time should either be a Time object or something that the Time class can parse. If the value given does not specify a date then today’s date will be assumed. If a time/date in the past is specified, the expiry time will not be changed.

# File lib/t2-server/run.rb, line 296
def expiry=(time)
  unless time.instance_of? Time
    time = Time.parse(time)
  end

  # need to massage the xmlschema format slightly as the server cannot
  # parse timezone offsets with a colon (eg +00:00)
  date_str = time.xmlschema(2)
  date_str = date_str[0..-4] + date_str[-2..-1]
  @server.update(links[:expiry], date_str, "text/plain", @credentials)
end
finish_time → string click to toggle source

Get the finish time of this run as an instance of class Time.

# File lib/t2-server/run.rb, line 599
def finish_time
  Time.parse(@server.read(links[:finishtime], "text/plain", @credentials))
end
finished? → bool click to toggle source

Is this run in the :finished state?

# File lib/t2-server/run.rb, line 575
def finished?
  status == :finished
end
grant_permission(username, permission) → username click to toggle source

Grant the user the stated permission. A permission can be one of :none, :read, :update or :destroy. Only the owner of a run may grant permissions on it. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 622
def grant_permission(username, permission)
  return unless owner?

  value = XML::Fragments::PERM_UPDATE % [username, permission.to_s]
  @server.create(links[:sec_perms], value, "application/xml", @credentials)
end
initialized? → bool click to toggle source

Is this run in the :initialized state?

# File lib/t2-server/run.rb, line 559
def initialized?
  status == :initialized
end
input_port(port) → Port click to toggle source

Get port.

# File lib/t2-server/run.rb, line 235
def input_port(port)
  input_ports[port]
end
input_ports → Hash click to toggle source

Return a hash (name, port) of all the input ports this run expects.

# File lib/t2-server/run.rb, line 225
def input_ports
  @input_ports = _get_input_port_info if @input_ports.nil?

  @input_ports
end
mkdir(dir) → bool click to toggle source

Create a directory in the run’s working directory on the server. This could be used to store input data.

# File lib/t2-server/run.rb, line 404
def mkdir(dir)
  dir = Util.strip_path_slashes(dir)

  @server.mkdir(links[:wdir], dir, @credentials)
end
output_port(port) → Port click to toggle source

Get output port port.

# File lib/t2-server/run.rb, line 256
def output_port(port)
  output_ports[port] if finished?
end
output_ports → Hash click to toggle source

Return a hash (name, port) of all the output ports this run has. Until the run is finished this method will return nil.

# File lib/t2-server/run.rb, line 244
def output_ports
  if finished? and @output_ports.nil?
    @output_ports = _get_output_port_info
  end

  @output_ports
end
owner → String click to toggle source

Get the username of the owner of this run. The owner is the user who created the run on the server.

# File lib/t2-server/run.rb, line 183
def owner
  @owner = _get_run_owner if @owner.nil?

  @owner
end
owner? → bool click to toggle source

Are the credentials being used to access this run those of the owner? The owner of the run can give other users certain access rights to their runs but only the owner can change these rights - or even see what they are. Sometimes it is useful to know if the user accessing the run is actually the owner of it or not.

# File lib/t2-server/run.rb, line 611
def owner?
  @credentials.username == owner
end
permission(username) → permission click to toggle source

Return the permission granted to the supplied username, if any. Only the owner of a run may query its permissions. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 657
def permission(username)
  return unless owner?

  permissions[username]
end
permissions → hash click to toggle source

Return a hash (username => permission) of all the permissions set for this run. Only the owner of a run may query its permissions. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 635
def permissions
  return unless owner?

  perms = {}
  doc = xml_document(@server.read(links[:sec_perms], "application/xml",
    @credentials))

  xpath_find(doc, XPaths[:sec_perm]).each do |p|
    user = xml_node_content(xpath_first(p, XPaths[:sec_uname]))
    perm = xml_node_content(xpath_first(p, XPaths[:sec_uperm])).to_sym
    perms[user] = perm
  end

  perms
end
request_baclava_output → bool click to toggle source

Set the server to save the outputs of this run in baclava format. This must be done before the run is started.

# File lib/t2-server/run.rb, line 485
def request_baclava_output
  return if @baclava_out
  state = status
  raise RunStateError.new(state, :initialized) if state != :initialized

  @baclava_out = @server.update(links[:output], BACLAVA_FILE, "text/plain",
    @credentials)
end
revoke_permission(username) → bool click to toggle source

Revoke whatever permissions that have been granted to the user. Only the owner of a run may revoke permissions on it. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 669
def revoke_permission(username)
  return unless owner?

  uri = Util.append_to_uri_path(links[:sec_perms], username)
  @server.delete(uri, @credentials)
end
running? → bool click to toggle source

Is this run in the :running state?

# File lib/t2-server/run.rb, line 567
def running?
  status == :running
end
start click to toggle source

Start this run on the server.

Raises RunStateError if the run is not in the :initialized state.

# File lib/t2-server/run.rb, line 335
def start
  state = status
  raise RunStateError.new(state, :initialized) if state != :initialized

  # set all the inputs
  _check_and_set_inputs unless baclava_input?

  @server.update(links[:status], state_to_text(:running), "text/plain",
    @credentials)
end
start_time → string click to toggle source

Get the start time of this run as an instance of class Time.

# File lib/t2-server/run.rb, line 591
def start_time
  Time.parse(@server.read(links[:starttime], "text/plain", @credentials))
end
status → string click to toggle source

Get the status of this run. Status can be one of :initialized, :running or :finished.

# File lib/t2-server/run.rb, line 325
def status
  text_to_state(@server.read(links[:status], "text/plain", @credentials))
end
stderr → string click to toggle source

Get anything that the run printed to the standard error stream.

# File lib/t2-server/run.rb, line 395
def stderr
  @server.read(links[:stderr], "text/plain", @credentials)
end
stdout → string click to toggle source

Get anything that the run printed to the standard out stream.

# File lib/t2-server/run.rb, line 387
def stdout
  @server.read(links[:stdout], "text/plain", @credentials)
end
trusts → Array click to toggle source

Return a list of all the URIs of trusts that have been registered for this run. At present there is no way to differentiate between trusts without noting the URI returned when originally uploaded. Only the owner of a run may query its trusts. nil is returned if a user other than the owner uses this method.

# File lib/t2-server/run.rb, line 815
def trusts
  return unless owner?

  t_uris = []
  doc = xml_document(@server.read(links[:sec_trusts], "application/xml",
    @credentials))

  xpath_find(doc, XPaths[:sec_trust]). each do |t|
    t_uris << URI.parse(xml_node_attribute(t, "href"))
  end

  t_uris
end
upload_data(data, remote_name, remote_directory = "") → bool click to toggle source

Upload data to the server and store it in remote_file. The remote directory to put this file in can also be specified, but if it is it must first have been created by a call to #mkdir.

# File lib/t2-server/run.rb, line 434
def upload_data(data, remote_name, remote_directory = "")
  location_uri = Util.append_to_uri_path(links[:wdir], remote_directory)
  @server.upload_data(data, remote_name, location_uri, @credentials)
end
upload_file(filename, params={}) → string click to toggle source

Upload a file, with name filename, to the server. Possible values that can be passed in via params are:

  • :dir - The directory to upload to. If this is not left blank the corresponding directory will need to have been created by #mkdir.

  • :rename - Save the file on the server with a different name.

The name of the file on the server is returned.

# File lib/t2-server/run.rb, line 420
def upload_file(filename, params={})
  location = params[:dir] || ""
  uri = Util.append_to_uri_path(links[:wdir], location)
  rename = params[:rename] || ""
  file_uri = @server.upload_file(filename, uri, rename, @credentials)
  Util.get_path_leaf_from_uri(file_uri)
end
wait(check_interval = 1) click to toggle source

Wait (block) for this run to finish. How often (in seconds) the run is tested for completion can be specified with check_interval.

Raises RunStateError if the run is still in the :initialised state.

# File lib/t2-server/run.rb, line 353
def wait(*params)
  state = status
  raise RunStateError.new(state, :running) if state == :initialized

  interval = 1
  params.each do |param|
    case param
    when Hash
      warn "[DEPRECATION] 'Run#wait(params={})' is deprecated and will " +
        "be removed in 1.0. Please use Run#wait(check_interval) instead."
      interval = param[:interval] || 1
    when Integer
      interval = param
    end
  end

  # wait
  until finished?
    sleep(interval)
  end
end
workflow → string click to toggle source

Get the workflow that this run represents.

# File lib/t2-server/run.rb, line 312
def workflow
  if @workflow == ""
    @workflow = @server.read(links[:workflow], "application/xml",
      @credentials)
  end
  @workflow
end
zip_output → binary blob click to toggle source

Get the working directory of this run directly from the server in zip format.

# File lib/t2-server/run.rb, line 547
def zip_output
  state = status
  raise RunStateError.new(state, :finished) if state != :finished

  output_uri = Util.append_to_uri_path(links[:wdir], "out")
  @server.read(output_uri, "application/zip", @credentials)
end