class OodCore::Clusters

An enumerable that contains a list of {Cluster} objects

Constants

CONFIG_VERSION

The format version of the configuration file

Public Class Methods

load_file(path) click to toggle source

Parse a configuration file or a set of configuration files in a directory @param path [#to_s] configuration file or directory path @raise [ConfigurationNotFound] if path does not exist @return [Clusters] the clusters parsed from config

# File lib/ood_core/clusters.rb, line 17
def load_file(path)
  config = Pathname.new(path.to_s).expand_path

  clusters = []
  if config.file?
    if config.readable?
      CONFIG_VERSION.any? do |version|
        begin
          YAML.safe_load(config.read)&.fetch(version, {}).each do |k, v|
            clusters << Cluster.new(send("parse_#{version}", id: k, cluster: v))
          end
        rescue Psych::SyntaxError => e
          clusters << InvalidCluster.new(
            id: config.basename(config.extname).to_s,
            errors: [ e.message.to_s ]
          )
        end
      end
    end
  elsif config.directory?
    Pathname.glob([config.join("*.yml"), config.join("*.yaml")]).select(&:file?).select(&:readable?).each do |p|
      CONFIG_VERSION.any? do |version|
        begin
          if cluster = YAML.safe_load(p.read)&.fetch(version, nil)
            clusters << Cluster.new(send("parse_#{version}", id: p.basename(p.extname()).to_s, cluster: cluster))
          end
        rescue Psych::SyntaxError => e
          clusters << InvalidCluster.new(
            id: p.basename(p.extname).to_s,
            errors: [ e.message.to_s ]
          )
        end
      end
    end
  else
    raise ConfigurationNotFound, "configuration file '#{config}' does not exist"
  end

  new clusters
end
new(clusters = []) click to toggle source

@param clusters [Array<Cluster>] list of cluster objects

# File lib/ood_core/clusters.rb, line 109
def initialize(clusters = [])
  @clusters = clusters
end

Private Class Methods

parse_v1(id:, cluster:) click to toggle source

Parse a list of clusters from a 'v1' config NB: Makes minimum assumptions about config

# File lib/ood_core/clusters.rb, line 61
def parse_v1(id:, cluster:)
  c = {
    id: id,
    metadata: {},
    login: {},
    job: {},
    acls: [],
    custom: {}
  }

  c[:metadata][:title]   = cluster["title"] if cluster.key?("title")
  c[:metadata][:url]     = cluster["url"]   if cluster.key?("url")
  c[:metadata][:private] = true             if cluster["cluster"]["data"]["hpc_cluster"] == false

  if l = cluster["cluster"]["data"]["servers"]["login"]
    c[:login][:host] = l["data"]["host"]
  end

  if rm = cluster["cluster"]["data"]["servers"]["resource_mgr"]
    c[:job][:adapter] = "torque"
    c[:job][:host]    = rm["data"]["host"]
    c[:job][:lib]     = rm["data"]["lib"]
    c[:job][:bin]     = rm["data"]["bin"]
    c[:job][:acls]    = []
  end

  if v = cluster["validators"]
    if vc = v["cluster"]
      c[:acls] = vc.map do |h|
        {
          adapter: "group",
          groups: h["data"]["groups"],
          type: h["data"]["allow"] ? "whitelist" : "blacklist"
        }
      end
    end
  end

  c
end
parse_v2(id:, cluster:) click to toggle source

Parse a list of clusters from a 'v2' config

# File lib/ood_core/clusters.rb, line 103
def parse_v2(id:, cluster:)
  cluster.merge(id: id)
end

Public Instance Methods

[](id) click to toggle source

Find cluster in list using the id of the cluster @param id [Object] id of cluster object @return [Cluster, nil] cluster object if found

# File lib/ood_core/clusters.rb, line 116
def [](id)
  @clusters.detect { |cluster| cluster == id }
end
each(&block) click to toggle source

For a block {|cluster| …} @yield [cluster] Gives the next cluster object in the list

# File lib/ood_core/clusters.rb, line 122
def each(&block)
  @clusters.each(&block)
end