Class: Puppet::Provider::Scontrol

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

Overview

scontrol 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/scontrol.rb', line 7

def install_prefix
  @install_prefix
end

Class Method Details

.convert_scontrol_key(key) ⇒ Object



95
96
97
98
99
100
# File 'lib/puppet/provider/scontrol.rb', line 95

def self.convert_scontrol_key(key)
  if key =~ %r{^[A-Z]+$}
    return key.downcase
  end
  key.gsub(%r{([a-z])([A-Z])}, '\1_\2').downcase
end

.hash_colon_propertiesObject



65
66
67
# File 'lib/puppet/provider/scontrol.rb', line 65

def self.hash_colon_properties
  []
end

.parse_value(property, raw_value) ⇒ Object



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet/provider/scontrol.rb', line 122

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 raw_value == '(null)'
    return :absent
  end
  if array_properties.include?(property)
    value = if raw_value.include?(',')
              raw_value.split(',')
            elsif raw_value == ''
              :absent
            else
              Array(raw_value)
            end
  elsif hash_colon_properties.include?(property)
    value = {}
    values = raw_value.split(',')
    values.each do |rv|
      kvs = rv.split(':')
      value[kvs[0]] = if kvs.size == 1
                        '1'
                      else
                        kvs[1]
                      end
    end
  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

.scontrol(args, env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/provider/scontrol.rb', line 10

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

.scontrol_listObject



102
103
104
105
106
107
108
109
110
# File 'lib/puppet/provider/scontrol.rb', line 102

def self.scontrol_list
  args = ['show']
  args << scontrol_resource
  args << '--oneliner'
  scontrol(args, {})
rescue Puppet::Error => e
  Puppet.err("Unable to show #{scontrol_resource} resources: #{e}")
  return ''
end

.scontrol_name_keyObject



84
85
86
87
88
89
# File 'lib/puppet/provider/scontrol.rb', line 84

def self.scontrol_name_key
  case resource_type.to_s
  when 'Puppet::Type::Slurm_reservation'
    'Reservation'
  end
end

.scontrol_resourceObject



73
74
75
76
77
78
# File 'lib/puppet/provider/scontrol.rb', line 73

def self.scontrol_resource
  case resource_type.to_s
  when 'Puppet::Type::Slurm_reservation'
    'reservation'
  end
end

.type_paramsObject



53
54
55
# File 'lib/puppet/provider/scontrol.rb', line 53

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

.type_propertiesObject



45
46
47
# File 'lib/puppet/provider/scontrol.rb', line 45

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

Instance Method Details

#createObject



208
209
210
211
# File 'lib/puppet/provider/scontrol.rb', line 208

def create
  scontrol(['create', scontrol_resource, "#{scontrol_name_key}=#{@resource[:name]}", set_values(true)].flatten, custom_env)
  @property_hash[:ensure] = :present
end

#destroyObject



220
221
222
223
# File 'lib/puppet/provider/scontrol.rb', line 220

def destroy
  scontrol(['delete', "#{scontrol_name_key}=#{@resource[:name]}"].flatten, custom_env)
  @property_hash.clear
end

#exists?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/puppet/provider/scontrol.rb', line 204

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

#flushObject



213
214
215
216
217
218
# File 'lib/puppet/provider/scontrol.rb', line 213

def flush
  unless @property_flush.empty?
    scontrol(['update', "#{scontrol_name_key}=#{@resource[:name]}", set_values(false)].flatten, custom_env)
  end
  @property_hash = resource.to_hash
end

#hash_colon_propertiesObject



69
70
71
# File 'lib/puppet/provider/scontrol.rb', line 69

def hash_colon_properties
  self.class.hash_colon_properties
end

#scontrol(*args) ⇒ Object



41
42
43
# File 'lib/puppet/provider/scontrol.rb', line 41

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

#scontrol_name_keyObject



91
92
93
# File 'lib/puppet/provider/scontrol.rb', line 91

def scontrol_name_key
  self.class.scontrol_resource
end

#scontrol_resourceObject



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

def scontrol_resource
  self.class.scontrol_resource
end

#scontrol_show(name) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/puppet/provider/scontrol.rb', line 112

def scontrol_show(name)
  args = ['show']
  args << "#{scontrol_name_key}=#{name}"
  args << '--oneliner'
  scontrol(args, custom_env)
rescue Puppet::Error => e
  Puppet.err("Unable to show #{scontrol_resource} #{name}: #{e}")
  return ''
end

#set_propertiesObject



61
62
63
# File 'lib/puppet/provider/scontrol.rb', line 61

def set_properties
  [type_params, type_properties].flatten
end

#set_values(create) ⇒ Object

rubocop:disable Style/AccessorMethodName



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/puppet/provider/scontrol.rb', line 169

def set_values(create) # rubocop:disable Style/AccessorMethodName
  result = []
  properties = if create
                 set_properties
               else
                 @property_flush.keys
               end
  properties.each do |property|
    value = if create
              resource[property]
            else
              @property_flush[property]
            end
    next if (value == :absent || value == [:absent]) && create
    next if value.nil?
    name = property.to_s.delete('_')
    if !create && (value == :absent || value == [:absent])
      value = set_absent_values[property] || ''
    elsif hash_colon_properties.include?(property)
      value = value.map { |k, _v| "#{k}:#{value}" }.join(',')
    elsif value.is_a?(Array)
      value = value.join(',')
    elsif value.is_a?(Hash)
      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

#type_paramsObject



57
58
59
# File 'lib/puppet/provider/scontrol.rb', line 57

def type_params
  self.class.type_params
end

#type_propertiesObject



49
50
51
# File 'lib/puppet/provider/scontrol.rb', line 49

def type_properties
  self.class.type_properties
end