Class: OodCore::Cluster

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

Overview

An object that describes a cluster and its given features that third-party code can take advantage of.

Direct Known Subclasses

InvalidCluster

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster) ⇒ Cluster

Returns a new instance of Cluster.

Parameters:

  • cluster (#to_h)

    the cluster object

Options Hash (cluster):

  • :id (#to_sym)

    The cluster id

  • :metadata (#to_h) — default: {}

    The cluster’s metadata

  • :login (#to_h) — default: {}

    The cluster’s SSH host

  • :job (#to_h) — default: {}

    The job adapter for this cluster

  • :custom (#to_h) — default: {}

    Any custom resources for this cluster

  • :acls (Array<#to_h>) — default: []

    List of ACLs to validate against

  • :batch_connect (#to_h) — default: {}

    Configuration for batch connect templates

  • :errors (#to_a) — default: []

    List of configuration errors



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ood_core/cluster.rb', line 48

def initialize(cluster)
  c = cluster.to_h.symbolize_keys

  # Required options
  @id = c.fetch(:id) { raise ArgumentError, "No id specified. Missing argument: id" }.to_sym

  # General options
  @metadata_config = c.fetch(:metadata, {}).to_h.symbolize_keys
  @login_config    = c.fetch(:login, {})   .to_h.symbolize_keys
  @job_config      = c.fetch(:job, {})     .to_h.symbolize_keys
  @custom_config   = c.fetch(:custom, {})  .to_h.symbolize_keys
  @acls_config     = c.fetch(:acls, [])    .map(&:to_h)
  @batch_connect_config = c.fetch(:batch_connect, {}).to_h.symbolize_keys

  # side affects from object creation and validation
  @errors          = c.fetch(:errors, [])  .to_a
end

Instance Attribute Details

#acls_configHash (readonly)

The acls configuration describing the permissions for this cluster

Returns:

  • (Hash)

    the acls configuration



29
30
31
# File 'lib/ood_core/cluster.rb', line 29

def acls_config
  @acls_config
end

#errorsObject (readonly)

The errors encountered with configuring this cluster



33
34
35
# File 'lib/ood_core/cluster.rb', line 33

def errors
  @errors
end

#idSymbol (readonly)

The unique identifier for a given cluster

Returns:

  • (Symbol)

    the cluster id



13
14
15
# File 'lib/ood_core/cluster.rb', line 13

def id
  @id
end

#job_configHash (readonly)

The job adapter configuration used for this cluster

Returns:

  • (Hash)

    the job configuration



25
26
27
# File 'lib/ood_core/cluster.rb', line 25

def job_config
  @job_config
end

#login_configHash (readonly)

The login configuration used for this cluster

Returns:

  • (Hash)

    the login configuration



21
22
23
# File 'lib/ood_core/cluster.rb', line 21

def 
  @login_config
end

#metadata_configHash (readonly)

Metadata configuration providing descriptive information about cluster

Returns:

  • (Hash)

    the metadata configuration



17
18
19
# File 'lib/ood_core/cluster.rb', line 17

def 
  @metadata_config
end

Instance Method Details

#==(other) ⇒ Boolean

The comparison operator

Parameters:

  • other (#to_sym)

    object to compare against

Returns:

  • (Boolean)

    whether objects are equivalent



176
177
178
# File 'lib/ood_core/cluster.rb', line 176

def ==(other)
  (other) ? id == other.to_sym : false
end

#aclsArray<Acl::Adapter>

Build the ACL adapters from the ACL list configuration

Returns:



152
153
154
# File 'lib/ood_core/cluster.rb', line 152

def acls
  build_acls acls_config
end

#allow?Boolean

Whether this cluster is allowed to be used

Returns:

  • (Boolean)

    whether cluster is allowed



158
159
160
161
162
# File 'lib/ood_core/cluster.rb', line 158

def allow?
  return @allow if defined?(@allow)

  @allow = acls.all?(&:allow?)
end

#batch_connect_config(template = nil) ⇒ Hash

The batch connect template configuration used for this cluster

Parameters:

  • template (#to_sym, nil) (defaults to: nil)

    the template type

Returns:

  • (Hash)

    the batch connect configuration



119
120
121
122
123
124
125
# File 'lib/ood_core/cluster.rb', line 119

def batch_connect_config(template = nil)
  if template
    @batch_connect_config.fetch(template.to_sym, {}).to_h.symbolize_keys.merge(template: template.to_sym)
  else
    @batch_connect_config
  end
end

#batch_connect_ssh_allow?Boolean?

Whether this cluster supports SSH to batch connect nodes

Returns:

  • (Boolean, nil)

    whether cluster supports SSH to batch connect node



166
167
168
169
170
171
# File 'lib/ood_core/cluster.rb', line 166

def batch_connect_ssh_allow?
  return @batch_connect_ssh_allow if defined?(@batch_connect_ssh_allow)
  return @batch_connect_ssh_allow = nil if batch_connect_config.nil?

  @batch_connect_ssh_allow = batch_connect_config.fetch(:ssh_allow, nil)
end

#batch_connect_template(context = {}) ⇒ BatchConnect::Template

Build a batch connect template from the respective configuration

Parameters:

  • context (#to_h) (defaults to: {})

    the context used for rendering the template

Returns:



130
131
132
133
# File 'lib/ood_core/cluster.rb', line 130

def batch_connect_template(context = {})
  context = context.to_h.symbolize_keys
  BatchConnect::Factory.build batch_connect_config(context[:template] || :basic).merge(context)
end

#custom_allow?(feature) ⇒ Boolean

Whether the custom feature is allowed based on the ACLs

Returns:

  • (Boolean)

    is this custom feature allowed



144
145
146
147
148
# File 'lib/ood_core/cluster.rb', line 144

def custom_allow?(feature)
  allow? &&
    !custom_config(feature).empty? &&
    build_acls(custom_config(feature).fetch(:acls, []).map(&:to_h)).all?(&:allow?)
end

#custom_config(feature = nil) ⇒ Hash

The configuration for any custom features or resources for this cluster

Parameters:

  • feature (#to_sym, nil) (defaults to: nil)

    the feature or resource

Returns:

  • (Hash)

    configuration for custom feature or resource



138
139
140
# File 'lib/ood_core/cluster.rb', line 138

def custom_config(feature = nil)
  feature ? @custom_config.fetch(feature.to_sym, {}).to_h.symbolize_keys : @custom_config
end

#job_adapterJob::Adapter

Build a job adapter from the job configuration

Returns:



102
103
104
# File 'lib/ood_core/cluster.rb', line 102

def job_adapter
  Job::Factory.build(job_config)
end

#job_allow?Boolean

Whether the job feature is allowed based on the ACLs

Returns:

  • (Boolean)

    is the job feature allowed



108
109
110
111
112
113
114
# File 'lib/ood_core/cluster.rb', line 108

def job_allow?
  return @job_allow if defined?(@job_allow)

  @job_allow = (allow? && ! job_config.empty? && build_acls(
    job_config.fetch(:acls, []).map(&:to_h)
  ).all?(&:allow?))
end

#loginOpenStruct

The login used for this cluster

Returns:

  • (OpenStruct)

    the login



88
89
90
# File 'lib/ood_core/cluster.rb', line 88

def 
  OpenStruct.new()
end

#login_allow?Boolean

Whether the login feature is allowed

Returns:

  • (Boolean)

    is login allowed



94
95
96
97
98
# File 'lib/ood_core/cluster.rb', line 94

def 
  return @login_allow if defined?(@login_allow)

  @login_allow = (allow? && !.empty?)
end

#metadataOpenStruct

Metadata that provides extra information about this cluster

Returns:

  • (OpenStruct)

    the metadata



82
83
84
# File 'lib/ood_core/cluster.rb', line 82

def 
  OpenStruct.new 
end

#to_hHash

Convert object to hash

Returns:

  • (Hash)

    the hash describing this object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ood_core/cluster.rb', line 188

def to_h
  {
    id: id,
    metadata: ,
    login: ,
    job: job_config,
    custom: custom_config,
    acls: acls_config,
    batch_connect:  batch_connect_config
  }
end

#to_symSymbol

Convert object to symbol

Returns:

  • (Symbol)

    the symbol describing this object



182
183
184
# File 'lib/ood_core/cluster.rb', line 182

def to_sym
  id
end

#valid?Boolean

This cluster is always valid

Returns:

  • (Boolean)

    true



202
203
204
# File 'lib/ood_core/cluster.rb', line 202

def valid?
  return true
end