Class: OodAppkit::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/ood_appkit/cluster.rb

Overview

An object that describes a given cluster of nodes used by an HPC center

Constant Summary collapse

VERSION =

YAML configuration version

:v1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, title:, validators: {}, servers: {}, hpc_cluster: true) ⇒ Cluster

Returns a new instance of Cluster.

Parameters:

  • id (Symbol)

    id of cluster

  • title (String)

    title of cluster

  • validators (Hash) (defaults to: {})

    hash of validations that describe the validators

  • servers (Hash) (defaults to: {})

    hash of servers with corresponding server info

  • hpc_cluster (Boolean) (defaults to: true)

    whether this is an hpc-style cluster



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ood_appkit/cluster.rb', line 41

def initialize(id:, title:, validators: {}, servers: {}, hpc_cluster: true)
  # Set id & title of cluster
  @id = id
  @title = title

  # Generate hash of validations
  @validators = validators.each_with_object({}) do |(k, v), h|
    h[k] = v[:type].constantize.new(v)
  end

  # Generate hash of servers
  @servers = servers.each_with_object({}) do |(k, v), h|
    h[k] = v[:type].constantize.new(v)
  end

  # Is this an hpc-style cluster?
  @hpc_cluster = hpc_cluster
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Grab object from @servers hash or check if it exists

Parameters:

  • method_name

    the method name called

  • arguments

    the arguments to the call

  • block

    an optional block for the call



79
80
81
82
83
84
85
86
87
# File 'lib/ood_appkit/cluster.rb', line 79

def method_missing(method_name, *arguments, &block)
  if /^(.+)_server$/ =~ method_name.to_s
    @servers.fetch($1.to_sym, nil)
  elsif /^(.+)_server\?$/ =~ method_name.to_s
    @servers.has_key? $1.to_sym
  else
    super
  end
end

Instance Attribute Details

#idSymbol (readonly)

Unique ID of the cluster

Returns:

  • (Symbol)

    id of cluster



11
12
13
# File 'lib/ood_appkit/cluster.rb', line 11

def id
  @id
end

#serversHash<Server> (readonly)

Hash of servers this cluster supports

Returns:

  • (Hash<Server>)

    hash of servers



23
24
25
# File 'lib/ood_appkit/cluster.rb', line 23

def servers
  @servers
end

#titleString (readonly)

Title of the cluster

Returns:

  • (String)

    title of cluster



15
16
17
# File 'lib/ood_appkit/cluster.rb', line 15

def title
  @title
end

#validatorsHash<#valid?> (readonly)

Hash of validators this cluster validates against

Returns:

  • (Hash<#valid?>)

    hash of validators



19
20
21
# File 'lib/ood_appkit/cluster.rb', line 19

def validators
  @validators
end

Class Method Details

.all(file: File.expand_path('../../../config/clusters.yml', __FILE__), force: false) ⇒ Hash<Cluster>

A list of accessible clusters for the currently running user

Parameters:

  • file (String) (defaults to: File.expand_path('../../../config/clusters.yml', __FILE__))

    yaml file with cluster configurations

  • force (Boolean) (defaults to: false)

    whether we force invalid clusters to be included as well

Returns:

  • (Hash<Cluster>)

    list of clusters user has access to



29
30
31
32
33
34
# File 'lib/ood_appkit/cluster.rb', line 29

def self.all(file: File.expand_path('../../../config/clusters.yml', __FILE__), force: false)
  parse_config(file).each_with_object({}) do |(k, v), h|
    c = Cluster.new v.merge(id: k)
    h[k] = c if c.valid? || force
  end
end

.parse_config(file) ⇒ Object

Parse the config file



98
99
100
# File 'lib/ood_appkit/cluster.rb', line 98

def self.parse_config(file)
  YAML.load(File.read(file)).deep_symbolize_keys.fetch(VERSION, {})
end

Instance Method Details

#hpc_cluster?Boolean

Whether this is an hpc-style cluster (i.e., meant for heavy computation)

Returns:

  • (Boolean)

    whether this an hpc-style cluster



71
72
73
# File 'lib/ood_appkit/cluster.rb', line 71

def hpc_cluster?
  @hpc_cluster
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Check if method ends with custom *_server or *_server?

Parameters:

  • method_name

    the method name to check

Returns:

  • (Boolean)


92
93
94
# File 'lib/ood_appkit/cluster.rb', line 92

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('_server', '_server?') || super
end

#valid?Boolean

Whether this is a valid cluster

Examples:

Whether I have access to this cluster

my_cluster.valid?
#=> true

Returns:

  • (Boolean)

    whether user has access to this cluster



65
66
67
# File 'lib/ood_appkit/cluster.rb', line 65

def valid?
  !@validators.any? {|name, validator| !validator.valid?}
end