Class: Puppet::Provider::Sacctmgr

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/sacctmgr.rb

Overview

sacctmgr provider parent class

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.install_prefixObject

Returns the value of attribute install_prefix.



7
8
9
# File 'lib/puppet/provider/sacctmgr.rb', line 7

def install_prefix
  @install_prefix
end

Class Method Details

.all_propertiesObject



51
52
53
# File 'lib/puppet/provider/sacctmgr.rb', line 51

def self.all_properties
  [name_attribute, (type_params - [sacctmgr_name_attribute]), type_properties].flatten
end

.fields_name_overridesObject



76
77
78
# File 'lib/puppet/provider/sacctmgr.rb', line 76

def self.fields_name_overrides
  {}
end

.format_fieldsObject



68
69
70
# File 'lib/puppet/provider/sacctmgr.rb', line 68

def self.format_fields
  sacctmgr_properties.map { |r| r.to_s.delete('_') }.join(',')
end

.name_attributeObject



10
11
12
# File 'lib/puppet/provider/sacctmgr.rb', line 10

def self.name_attribute
  :name
end

.parse_time(t) ⇒ Object



200
201
202
203
204
# File 'lib/puppet/provider/sacctmgr.rb', line 200

def self.parse_time(t)
  time = PuppetX::SLURM::Util.parse_time(t)
  return t if time.nil?
  time[3].to_i + (time[2].to_i * 60) + (time[1].to_i * 3600) + (time[0].to_i * 86_400)
end

.parse_value(property, raw_value) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/puppet/provider/sacctmgr.rb', line 206

def self.parse_value(property, raw_value)
  Puppet.debug("parse_value: property=#{property} raw_value(#{raw_value.class})=#{raw_value}")
  if absent_values.key?(property)
    Puppet.debug("parse_value: absent_values found: #{(raw_value == absent_values[property])}")
    if raw_value == absent_values[property]
      return :absent
    end
  end
  if array_properties.include?(property)
    value = if raw_value.include?(',')
              raw_value.split(',')
            elsif raw_value == ''
              :absent
            else
              Array(raw_value)
            end
  elsif time_to_seconds.include?(property)
    value = parse_time(raw_value).to_s
  elsif raw_value == ''
    value = :absent
  elsif raw_value.include?('=')
    value = {}
    raw_value.split(',').each do |i|
      k, v = i.split('=')
      if v.to_s[-1] == 'M'
        v.chop!
      end
      value[k] = v.to_s
    end
  elsif raw_value.include?(',')
    value = raw_value.split(',')
  else
    value = raw_value
  end
  value
end

.sacctmgr(args, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet/provider/sacctmgr.rb', line 119

def self.sacctmgr(args, options = {})
  sacctmgr_path = nil
  unless @install_prefix.nil?
    sacctmgr_path = File.join(@install_prefix, 'bin', 'sacctmgr')
  end
  if sacctmgr_path.nil?
    sacctmgr_path = which('sacctmgr')
    Puppet.debug("Used which to find sacctmgr: path=#{sacctmgr_path}")
  end
  if sacctmgr_path.nil?
    [
      '/bin',
      '/usr/bin',
      '/usr/local/bin',
    ].each do |dir|
      path = File.join(dir, 'sacctmgr')
      next unless File.exist?(path)
      sacctmgr_path = path
      Puppet.debug("Used static search to find sacctmgr: path=#{sacctmgr_path}")
      break
    end
  end
  raise Puppet::Error, 'Unable to find sacctmgr executable' if sacctmgr_path.nil?
  cmd = [sacctmgr_path] + args
  default_options = { failonfail: true, combine: true }
  ret = execute(cmd, default_options.merge(options))
  return ret
rescue Puppet::Error => e
  Puppet.err("Failed to run sacctmgr command: #{e}")
  raise
end

.sacctmgr_list(withassoc = false, filter = {}, flags = []) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppet/provider/sacctmgr.rb', line 155

def self.sacctmgr_list(withassoc = false, filter = {}, flags = [])
  args = ['list']
  args << sacctmgr_resource
  args << "format=#{format_fields}"
  args += ['--noheader', '--parsable2']
  if withassoc
    args << 'withassoc'
  end
  unless filter.empty?
    args << 'where'
  end
  filter.each do |k, v|
    args << "#{k}=#{v}"
  end
  flags.each do |f|
    args << f
  end
  sacctmgr(args)
rescue Puppet::Error => e
  Puppet.err("Unable to list #{sacctmgr_resource} resources: #{e}")
  return ''
end

.sacctmgr_list_assoc(format = [], filter = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/provider/sacctmgr.rb', line 178

def self.sacctmgr_list_assoc(format = [], filter = {})
  args = ['list']
  args << sacctmgr_resource
  args << "format=#{format.join(',')}"
  args += ['--noheader', '--parsable2']
  args << 'withassoc'
  unless filter.empty?
    args << 'where'
  end
  filter.each do |k, v|
    args << "#{k}=#{v}"
  end
  sacctmgr(args)
rescue Puppet::Error => e
  Puppet.err("Unable to list assoc #{sacctmgr_resource} resources: #{e}")
  return ''
end

.sacctmgr_name_attributeObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet/provider/sacctmgr.rb', line 14

def self.sacctmgr_name_attribute
  case resource_type.to_s
  when 'Puppet::Type::Slurm_cluster'
    :cluster
  when 'Puppet::Type::Slurm_account'
    :account
  when 'Puppet::Type::Slurm_user'
    :user
  else
    :name
  end
end

.sacctmgr_propertiesObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/provider/sacctmgr.rb', line 55

def self.sacctmgr_properties
  values = [sacctmgr_name_attribute, (type_params - [sacctmgr_name_attribute]), type_properties].flatten
  properties = []
  values.each do |v|
    properties << if fields_name_overrides.key?(v)
                    fields_name_overrides[v]
                  else
                    v
                  end
  end
  properties
end

.sacctmgr_resourceObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/puppet/provider/sacctmgr.rb', line 84

def self.sacctmgr_resource
  case resource_type.to_s
  when 'Puppet::Type::Slurm_cluster'
    'cluster'
  when 'Puppet::Type::Slurm_qos'
    'qos'
  when 'Puppet::Type::Slurm_account'
    'account'
  when 'Puppet::Type::Slurm_user'
    'user'
  when 'Puppet::Type::Slurm_license'
    'resource'
  end
end

.time_to_secondsObject



27
28
29
# File 'lib/puppet/provider/sacctmgr.rb', line 27

def self.time_to_seconds
  []
end

.tresObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/puppet/provider/sacctmgr.rb', line 103

def self.tres
  values = []
  args = ['show', 'tres', 'format=type,name,id', '--noheader', '--parsable2']
  output = sacctmgr(args)
  output.each_line do |line|
    data = line.chomp.split('|')
    value = if data[1] != ''
              "#{data[0]}/#{data[1]}"
            else
              data[0]
            end
    values << value
  end
  values
end

.type_paramsObject



43
44
45
# File 'lib/puppet/provider/sacctmgr.rb', line 43

def self.type_params
  resource_type.parameters.reject { |p| [:name, :resource_name, :provider].include?(p) }.sort
end

.type_propertiesObject



35
36
37
# File 'lib/puppet/provider/sacctmgr.rb', line 35

def self.type_properties
  resource_type.validproperties.reject { |p| p == :ensure }.sort
end

Instance Method Details

#createObject



311
312
313
314
# File 'lib/puppet/provider/sacctmgr.rb', line 311

def create
  sacctmgr(['-i', 'create', sacctmgr_resource, @resource[self.class.name_attribute], set_values(true)].flatten)
  @property_hash[:ensure] = :present
end

#destroyObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/puppet/provider/sacctmgr.rb', line 341

def destroy
  cmd = ['-i', 'delete', sacctmgr_resource, 'where', "name=#{@resource[self.class.name_attribute]}"]
  # Resource specific behavior
  # If cluster is 'absent' then delete the entire account without cluster filter
  if sacctmgr_resource == 'account'
    if @resource[:cluster] && @resource[:cluster].to_s == 'absent'
      Puppet.notice("Slurm_account[#{@resource[:name]}] Removing all accounts by name #{@resource[:name]}")
    else
      cmd << "cluster=#{@resource[:cluster]}"
    end
  # If cluster and account are 'absent' then delete the entire user without cluster and account filter
  elsif sacctmgr_resource == 'user'
    if (@resource[:account] && @resource[:account].to_s == 'absent') &&
       (@resource[:cluster] && @resource[:cluster].to_s == 'absent')
      Puppet.notice("Slurm_user[#{@resource[:name]}] Removing all users by name #{@resource[:name]}")
    else
      cmd << "account=#{@resource[:account]}"
      cmd << "cluster=#{@resource[:cluster]}"
      if @resource[:partition].to_s != 'absent'
        cmd << "partition=#{@resource[:partition]}"
      end
    end
  elsif sacctmgr_resource == 'resource'
    cmd << "server=#{@resource[:server]}"
    cmd << "cluster=#{@resource[:cluster]}" if @resource[:cluster]
  end
  sacctmgr(cmd.flatten)
  @property_hash.clear
end

#exists?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/puppet/provider/sacctmgr.rb', line 307

def exists?
  @property_hash[:ensure] == :present
end

#flushObject



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/puppet/provider/sacctmgr.rb', line 316

def flush
  unless @property_flush.empty?
    cmd = ['-i', 'modify', sacctmgr_resource, 'where', "name=#{@resource[self.class.name_attribute]}"]
    if sacctmgr_resource == 'account'
      cmd << "cluster=#{@resource[:cluster]}"
    elsif sacctmgr_resource == 'user'
      cmd << "account=#{@resource[:account]}"
      cmd << "cluster=#{@resource[:cluster]}"
    elsif sacctmgr_resource == 'resource'
      cmd << "server=#{@resource[:server]}"
      cmd << "cluster=#{@resource[:cluster]}" if @resource[:cluster]
    end
    cmd << 'set'
    values = set_values(false)
    cmd << values
    unless values.empty?
      sacctmgr(cmd.flatten)
    end
    property_skip_set_values.each do |p|
      send("set_#{p}")
    end
  end
  @property_hash = resource.to_hash
end

#parse_tres(value) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/puppet/provider/sacctmgr.rb', line 243

def parse_tres(value)
  tres = {}
  value.split(',').each do |val|
    k, v = val.split('=')
    tres[k] = v
  end
  tres
end

#property_name_overridesObject



72
73
74
# File 'lib/puppet/provider/sacctmgr.rb', line 72

def property_name_overrides
  {}
end

#property_skip_set_valuesObject



80
81
82
# File 'lib/puppet/provider/sacctmgr.rb', line 80

def property_skip_set_values
  []
end

#sacctmgr(*args) ⇒ Object



151
152
153
# File 'lib/puppet/provider/sacctmgr.rb', line 151

def sacctmgr(*args)
  self.class.sacctmgr(*args)
end

#sacctmgr_list_assoc(*args) ⇒ Object



196
197
198
# File 'lib/puppet/provider/sacctmgr.rb', line 196

def sacctmgr_list_assoc(*args)
  self.class.sacctmgr_list_assoc(*args)
end

#sacctmgr_resourceObject



99
100
101
# File 'lib/puppet/provider/sacctmgr.rb', line 99

def sacctmgr_resource
  self.class.sacctmgr_resource
end

#set_values(create) ⇒ Object

rubocop:disable Style/AccessorMethodName



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/puppet/provider/sacctmgr.rb', line 252

def set_values(create) # rubocop:disable Style/AccessorMethodName
  result = []
  properties = if create
                 type_params - [self.class.name_attribute] + type_properties
               else
                 @property_flush.keys
               end
  properties.each do |property|
    next if property_skip_set_values.include?(property.to_sym) && !create
    value = if create
              resource[property]
            else
              @property_flush[property]
            end
    next if (value.to_s == 'absent' || value == [:absent] || value == ['absent']) && create
    next if value.nil?
    if property_name_overrides.key?(property.to_sym)
      property = property_name_overrides[property]
    end
    name = property.to_s.delete('_')
    if !create && (value.to_s == 'absent' || value == [:absent] || value == ['absent'])
      if property.to_s.include?('tres')
        current_value = @property_hash[property]
        next if current_value.nil?
        next if current_value.to_s == 'absent'
        new_tres = {}
        current_value.each_pair do |k, _v|
          new_tres[k] = '-1'
        end
        value = new_tres.map { |k, v| "#{k}=#{v}" }.join(',')
      else
        value = set_absent_values[property] || '-1'
      end
    elsif value.is_a?(Array)
      value = value.join(',')
    elsif value.is_a?(Hash)
      current_value = @property_hash[property] || {}
      current_value = {} if current_value.to_s == 'absent'
      current_value.each_pair do |k, _v|
        unless value.key?(k)
          value[k] = '-1'
        end
      end
      value = value.map { |k, v| "#{k}=#{v}" }.join(',')
    elsif value.is_a?(String)
      if value =~ %r{\s}
        value = "'#{value}'"
      end
    end
    result << "#{name}=#{value}"
  end

  result
end

#time_to_secondsObject



31
32
33
# File 'lib/puppet/provider/sacctmgr.rb', line 31

def time_to_seconds
  self.class.time_to_seconds
end

#type_paramsObject



47
48
49
# File 'lib/puppet/provider/sacctmgr.rb', line 47

def type_params
  self.class.type_params
end

#type_propertiesObject



39
40
41
# File 'lib/puppet/provider/sacctmgr.rb', line 39

def type_properties
  self.class.type_properties
end