Class: Puppet::Provider::Gpfs

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

Overview

Shared provider class

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.api_passwordObject

Returns the value of attribute api_password.



16
17
18
# File 'lib/puppet/provider/gpfs.rb', line 16

def api_password
  @api_password
end

.api_userObject

Returns the value of attribute api_user.



15
16
17
# File 'lib/puppet/provider/gpfs.rb', line 15

def api_user
  @api_user
end

.base_urlObject

Returns the value of attribute base_url.



14
15
16
# File 'lib/puppet/provider/gpfs.rb', line 14

def base_url
  @base_url
end

.filesystemsObject

Returns the value of attribute filesystems.



17
18
19
# File 'lib/puppet/provider/gpfs.rb', line 17

def filesystems
  @filesystems
end

Class Method Details

.delete_request(uri_path, wait = true) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/puppet/provider/gpfs.rb', line 209

def self.delete_request(uri_path, wait = true)
  #     uri = URI.join(self.base_url, uri_path)
  #     header = {
  #       'Content-Type' => 'application/json',
  #       'Accept' => 'application/json',
  #     }
  #
  #     http = Net::HTTP.new(uri.host, uri.port)
  #     http.use_ssl = true
  #     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  #     request = Net::HTTP::Delete.new(uri.request_uri, header)
  #     request.basic_auth(self.api_user, self.api_password)
  #     Puppet.debug("Send DELETE to #{uri.to_s}")
  #     response = http.request(request)
  #     unless response.kind_of?(Net::HTTPSuccess)
  #       raise Puppet::Error, "DELETE to #{uri.to_s} failed with code #{response.code}, message=#{self.response_message(response)}"
  #     end
  #     data = self.parse_response_body(response)
  #     Puppet.debug("Response data: #{data}")
  #     return data
  request(uri_path, 'DELETE', wait)
end

.get_request(uri_path, params = nil, wait = false) ⇒ Object



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

def self.get_request(uri_path, params = nil, wait = false)
  #     uri = URI.join(self.base_url, uri_path)
  #     header = {
  #       'Content-Type' => 'application/json',
  #       'Accept' => 'application/json',
  #     }
  #
  #     if params && ! params.empty?
  #       uri.query = URI.encode_www_form(params)
  #     end
  #     http = Net::HTTP.new(uri.host, uri.port)
  #     http.use_ssl = true
  #     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  #     request = Net::HTTP::Get.new(uri.request_uri, header)
  #     request.basic_auth(self.api_user, self.api_password)
  #     Puppet.debug("Send GET to #{uri.to_s}")
  #     response = http.request(request)
  #     unless response.kind_of?(Net::HTTPSuccess)
  #       raise Puppet::Error, "GET to #{uri.to_s} failed with code #{response.code}, message=#{self.response_message(response)}"
  #     end
  #     data = self.parse_response_body(response)
  #     Puppet.debug("Response data: #{data}")
  #     return data
  request(uri_path, 'GET', params, wait)
end

.human_readable_kilobytes(value) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/puppet/provider/gpfs.rb', line 236

def self.human_readable_kilobytes(value)
  return '0' if value.to_i.zero?
  {
    'K' => 1024,
    'M' => 1024**2,
    'G' => 1024**3,
    'T' => 1024**4,
  }.each_pair do |suffix, factor|
    next unless value < factor
    factored_value = (value.to_f / (factor / 1024))
    # Check if integer value is same as float rounded to one decimal place
    return "#{Integer(factored_value)}#{suffix}" if Integer(factored_value) == factored_value.round(1)
    return "#{factored_value.round(1)}#{suffix}"
  end
  value
end

.mmlsfs_filesystemsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet/provider/gpfs.rb', line 22

def self.mmlsfs_filesystems
  filesystems = []
  mmlsfs_output = mmlsfs('all', '-T', '-Y')
  mmlsfs_output.each_line do |line|
    l = line.strip.split(':')
    next if l[2] == 'HEADER'
    fs = l[6]
    if @filesystems && !@filesystems.include?(fs)
      next
    end
    filesystems << fs unless filesystems.include?(fs)
  end
  filesystems
end

.parse_response_body(response) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/puppet/provider/gpfs.rb', line 43

def self.parse_response_body(response)
  data = JSON.parse(response.body)
rescue JSON::ParseError
  Puppet.debug('Unable to parse response body')
  data = nil
ensure
  data
end

.post_request(uri_path, data, wait = true) ⇒ 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/gpfs.rb', line 155

def self.post_request(uri_path, data, wait = true)
  #     uri = URI.join(self.base_url, uri_path)
  #     header = {
  #       'Content-Type' => 'application/json',
  #       'Accept' => 'application/json',
  #     }
  #     http = Net::HTTP.new(uri.host, uri.port)
  #     http.use_ssl = true
  #     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  #     request = Net::HTTP::Post.new(uri.request_uri, header)
  #     request.basic_auth(self.api_user, self.api_password)
  #     request.body = data.to_json
  #     Puppet.debug("Send POST to #{uri.to_s} with data #{data}")
  #     response = http.request(request)
  #     unless response.kind_of?(Net::HTTPSuccess)
  #       raise Puppet::Error, "POST to #{uri.to_s} failed with code #{response.code}, message=#{self.response_message(response)}"
  #     end
  #     data = self.parse_response_body(response)
  #     Puppet.debug("Response data: #{data}")
  #     return data
  request(uri_path, 'POST', data, wait)
end

.put_request(uri_path, data, wait = true) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/puppet/provider/gpfs.rb', line 182

def self.put_request(uri_path, data, wait = true)
  #     uri = URI.join(self.base_url, uri_path)
  #     header = {
  #       'Content-Type' => 'application/json',
  #       'Accept' => 'application/json',
  #     }
  #     http = Net::HTTP.new(uri.host, uri.port)
  #     http.use_ssl = true
  #     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  #     request = Net::HTTP::Put.new(uri.request_uri, header)
  #     request.basic_auth(self.api_user, self.api_password)
  #     request.body = data.to_json
  #     Puppet.debug("Send PUT to #{uri.to_s} with data #{data}")
  #     response = http.request(request)
  #     unless response.kind_of?(Net::HTTPSuccess)
  #       raise Puppet::Error, "PUT to #{uri.to_s} failed with code #{response.code}, message=#{self.response_message(response)}"
  #     end
  #     data = self.parse_response_body(response)
  #     Puppet.debug("Response data: #{data}")
  #     return data
  request(uri_path, 'PUT', data, wait)
end

.request(uri_path, type, params = nil, wait = false) ⇒ Object

Raises:

  • (Puppet::Error)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet/provider/gpfs.rb', line 82

def self.request(uri_path, type, params = nil, wait = false)
  uri = URI.join(base_url, uri_path)
  header = {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
  }
  body = nil

  case type
  when 'GET'
    if params && !params.empty?
      uri.query = URI.encode_www_form(params)
    end
    request = Net::HTTP::Get.new(uri.request_uri, header)
  when 'POST'
    request = Net::HTTP::Post.new(uri.request_uri, header)
    body = params.to_json
  when 'PUT'
    request = Net::HTTP::Put.new(uri.request_uri, header)
    body = params.to_json
  when 'DELETE'
    request = Net::HTTP::Delete.new(uri.request_uri, header)
  end
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request.basic_auth(api_user, api_password)
  Puppet.debug("Send #{type} to #{uri}")
  if body
    request.body = body
    Puppet.debug("DATA: #{body}")
  end
  response = http.request(request)
  unless response.is_a?(Net::HTTPSuccess)
    raise Puppet::Error, "#{type} to #{uri} failed with code #{response.code}, message=#{response_message(response)}"
  end
  data = parse_response_body(response)
  Puppet.debug("Response data:\n#{JSON.pretty_generate(data)}")
  return data unless wait
  job_success, job_output = wait_for_job(data)
  raise Puppet::Error, "#{type} to #{uri} job failed: #{job_output}" unless job_success
end

.response_message(response) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/puppet/provider/gpfs.rb', line 52

def self.response_message(response)
  data = parse_response_body(response)
  return nil if data.nil?
  message = nil
  if data.key?('status')
    message = data['status']['message']
  end
  message
end

.set_scalemgmt_defaultsObject



37
38
39
40
41
# File 'lib/puppet/provider/gpfs.rb', line 37

def self.set_scalemgmt_defaults
  @base_url = 'https://localhost:443/scalemgmt/' if @base_url.nil?
  @api_user = 'admin' if @api_user.nil?
  @api_password = 'admin001' if @api_password.nil?
end

.to_kb(value) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/puppet/provider/gpfs.rb', line 253

def self.to_kb(value)
  factors = {
    'M' => 1024,
    'G' => 1024**2,
    'T' => 1024**3,
  }
  if value =~ %r{^([0-9\.]+)(T|G|M)$} # rubocop:disable Style/GuardClause
    v = Regexp.last_match(1).to_f
    f = Regexp.last_match(2)
    factor = factors[f]
    return "#{Integer(v * factor)}K"
  else
    return value
  end
end

.wait_for_job(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/provider/gpfs.rb', line 62

def self.wait_for_job(data)
  jobid = data['jobs'][0]['jobId']
  wait = true
  while wait
    data = request("v2/jobs/#{jobid}", 'GET')
    status = data['jobs'][0]['status']
    if status == 'RUNNING'
      Puppet.debug("Job #{jobid} still running, sleeping 1 second...")
      sleep(1)
    elsif status == 'COMPLETED'
      stdout = data['jobs'][0]['result']['stdout']
      Puppet.debug("Job completed successfully: #{stdout.join(', ')}")
      return true, stdout.join(', ')
    else
      stderr = data['jobs'][0]['result']['stderr']
      return false, stderr.join(', ')
    end
  end
end

Instance Method Details

#delete_request(*args) ⇒ Object



232
233
234
# File 'lib/puppet/provider/gpfs.rb', line 232

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

#get_request(*args) ⇒ Object



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

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

#post_request(*args) ⇒ Object



178
179
180
# File 'lib/puppet/provider/gpfs.rb', line 178

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

#put_request(*args) ⇒ Object



205
206
207
# File 'lib/puppet/provider/gpfs.rb', line 205

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