Class: Puppet::Provider::Gpfs
- Inherits:
-
Puppet::Provider
- Object
- Puppet::Provider
- Puppet::Provider::Gpfs
- Defined in:
- lib/puppet/provider/gpfs.rb
Overview
Shared provider class
Class Attribute Summary collapse
-
.api_password ⇒ Object
Returns the value of attribute api_password.
-
.api_user ⇒ Object
Returns the value of attribute api_user.
-
.base_url ⇒ Object
Returns the value of attribute base_url.
-
.filesystems ⇒ Object
Returns the value of attribute filesystems.
Class Method Summary collapse
- .delete_request(uri_path, wait = true) ⇒ Object
- .get_request(uri_path, params = nil, wait = false) ⇒ Object
- .human_readable_kilobytes(value) ⇒ Object
- .mmlsfs_filesystems ⇒ Object
- .parse_response_body(response) ⇒ Object
- .post_request(uri_path, data, wait = true) ⇒ Object
- .put_request(uri_path, data, wait = true) ⇒ Object
- .request(uri_path, type, params = nil, wait = false) ⇒ Object
- .response_message(response) ⇒ Object
- .set_scalemgmt_defaults ⇒ Object
- .to_kb(value) ⇒ Object
- .wait_for_job(data) ⇒ Object
Instance Method Summary collapse
- #delete_request(*args) ⇒ Object
- #get_request(*args) ⇒ Object
- #post_request(*args) ⇒ Object
- #put_request(*args) ⇒ Object
Class Attribute Details
.api_password ⇒ Object
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_user ⇒ Object
Returns the value of attribute api_user.
16 17 18 |
# File 'lib/puppet/provider/gpfs.rb', line 16 def api_user @api_user end |
.base_url ⇒ Object
Returns the value of attribute base_url.
16 17 18 |
# File 'lib/puppet/provider/gpfs.rb', line 16 def base_url @base_url end |
.filesystems ⇒ Object
Returns the value of attribute filesystems.
16 17 18 |
# File 'lib/puppet/provider/gpfs.rb', line 16 def filesystems @filesystems end |
Class Method Details
.delete_request(uri_path, wait = true) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/puppet/provider/gpfs.rb', line 214 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
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 |
# File 'lib/puppet/provider/gpfs.rb', line 130 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
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/puppet/provider/gpfs.rb', line 241 def self.human_readable_kilobytes(value) return '0' if value.to_i.zero? { 'K' => 1024, 'M' => 1024**2, 'G' => 1024**3, 'T' => 1024**4, 'P' => 1024**5 }.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_filesystems ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/puppet/provider/gpfs.rb', line 21 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
44 45 46 47 48 49 50 51 |
# File 'lib/puppet/provider/gpfs.rb', line 44 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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/puppet/provider/gpfs.rb', line 160 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
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/puppet/provider/gpfs.rb', line 187 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
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 124 125 126 127 128 |
# File 'lib/puppet/provider/gpfs.rb', line 85 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)}" 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
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/puppet/provider/gpfs.rb', line 53 def self.(response) data = parse_response_body(response) return nil if data.nil? = nil if data.key?('status') = data['status']['message'] end end |
.set_scalemgmt_defaults ⇒ Object
38 39 40 41 42 |
# File 'lib/puppet/provider/gpfs.rb', line 38 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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/puppet/provider/gpfs.rb', line 262 def self.to_kb(value) factors = { 'M' => 1024, 'G' => 1024**2, 'T' => 1024**3, 'P' => 1024**4 } if value =~ %r{^([0-9.]+)(P|T|G|M)$} v = Regexp.last_match(1).to_f f = Regexp.last_match(2) factor = factors[f] "#{Integer(v * factor)}K" else value end end |
.wait_for_job(data) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/puppet/provider/gpfs.rb', line 64 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'] case status when 'RUNNING' Puppet.debug("Job #{jobid} still running, sleeping 1 second...") sleep(1) when '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
237 238 239 |
# File 'lib/puppet/provider/gpfs.rb', line 237 def delete_request(*args) self.class.delete_request(*args) end |
#get_request(*args) ⇒ Object
156 157 158 |
# File 'lib/puppet/provider/gpfs.rb', line 156 def get_request(*args) self.class.get_request(*args) end |
#post_request(*args) ⇒ Object
183 184 185 |
# File 'lib/puppet/provider/gpfs.rb', line 183 def post_request(*args) self.class.post_request(*args) end |
#put_request(*args) ⇒ Object
210 211 212 |
# File 'lib/puppet/provider/gpfs.rb', line 210 def put_request(*args) self.class.put_request(*args) end |