Class: OneQuotaHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/one_helper/onequota_helper.rb

Constant Summary collapse

LIMIT_DEFAULT =
"-1"
LIMIT_UNLIMITED =
"-2"
EDITOR_PATH =
'/usr/bin/vi'
HELP_QUOTA =
"    #-----------------------------------------------------------------------\n    # Supported quota limits:\n    #\n    #  DATASTORE = [\n    #    ID     = <ID of the datastore>\n    #    IMAGES = <Max. number of images in the datastore>\n    #    SIZE   = <Max. storage capacity (Mb) used in the datastore>\n    #  ]\n    #\n    #  VM = [\n    #    VMS              = <Max. number of VMs>\n    #    RUNNING_VMS      = <Max. number of running VMs>\n    #    MEMORY           = <Max. allocated memory (MB)>\n    #    RUNNING_MEMORY   = <Max. running memory (MB)>\n    #    CPU              = <Max. allocated CPU>\n    #    RUNNING_CPU      = <Max. running CPU>\n    #    SYSTEM_DISK_SIZE = <Max. allocated system disk (MB)>\n    #  ]\n    #\n    #  NETWORK = [\n    #    ID     = <ID of the network>\n    #    LEASES = <Max. number of IP leases from the network>\n    #  ]\n    #\n    #  IMAGE = [\n    #    ID     = <ID of the image>\n    #    RVMS   = <Max. number of VMs using the image>\n    #  ]\n".unindent
"    #\n    #  In any quota:\n    #    -1 means use the default limit (set with the 'defaultquota' command)\n    #    -2 means unlimited.\n    #\n    #  The usage counters \"*_USED\" are shown for information\n    #  purposes and will NOT be modified.\n    #-----------------------------------------------------------------------\n".unindent
"    #\n    #  In any quota:\n    #    -2 means unlimited.\n    #\n    #  The usage counters \"*_USED\" will always be 0 for the default\n    #  quotas, and can be ignored.\n    #-----------------------------------------------------------------------\n".unindent

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_batch_quota(path) ⇒ Object

Retrieves a clean quota template, without any existing resource

information
@param path [String] path to the new contents. If nil a editor will be
       used
@return [String] contents of the new quotas


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
# File 'lib/one_helper/onequota_helper.rb', line 131

def self.get_batch_quota(path)
    str = ""

    if path.nil?
        require 'tempfile'

        tmp  = Tempfile.new('one-cli')
        path = tmp.path

        tmp << HELP_QUOTA << "\n"

        tmp.close

        editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH
        system("#{editor_path} #{path}")

        unless $?.exitstatus == 0
            puts "Editor not defined"
            exit -1
        end

        str = File.read(path)

        File.unlink(path)
    else
        str = File.read(path)
    end

    str
end

.merge_quota(resource, str) ⇒ Object

Edits the quota template of a resource, adding the quotas set in str

@param resource [PoolElement] to get the current info from
@param str [String] quota template, created by get_batch_quota()
@return [String, OpenNebula::Error] merged contents of the new quotas on
  success, Error if the user info could not be retrieved


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/one_helper/onequota_helper.rb', line 167

def self.merge_quota(resource, str)
    rc = resource.info

    if OpenNebula.is_error?(rc)
        return rc
    end

    # Instead of parsing the existing quotas, and deleting the ones that
    # conflict with the batch quota string, the new quotas are placed at
    # the end of the template sent to opennebula. This relies on the core
    # reading them in order and replacing the quotas with each new
    # appearance

    tmp_str = ""

    tmp_str << resource.template_like_str("DATASTORE_QUOTA") << "\n"
    tmp_str << resource.template_like_str("VM_QUOTA") << "\n"
    tmp_str << resource.template_like_str("NETWORK_QUOTA") << "\n"
    tmp_str << resource.template_like_str("IMAGE_QUOTA") << "\n"

    tmp_str << str

    return tmp_str
end

.set_quota(resource, path, is_default = false) ⇒ Object

Edits the quota template of a resource

@param [XMLElement] resource to get the current info from
@param [String] path to the new contents. If nil a editor will be
       used
@param [True|False] is_default To change the help text
@return [String] contents of the new quotas


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
124
# File 'lib/one_helper/onequota_helper.rb', line 84

def self.set_quota(resource, path, is_default=false)
    str = ""

    if path.nil?
        require 'tempfile'

        tmp  = Tempfile.new('one-cli')
        path = tmp.path

        tmp << HELP_QUOTA

        if (is_default)
            tmp << HELP_DEFAULT_QUOTA_FOOTER
        else
            tmp << HELP_QUOTA_FOOTER
        end

        tmp << resource.template_like_str("DATASTORE_QUOTA") << "\n"
        tmp << resource.template_like_str("VM_QUOTA") << "\n"
        tmp << resource.template_like_str("NETWORK_QUOTA") << "\n"
        tmp << resource.template_like_str("IMAGE_QUOTA") << "\n"

        tmp.close

        editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH
        system("#{editor_path} #{path}")

        unless $?.exitstatus == 0
            puts "Editor not defined"
            exit -1
        end

        str = File.read(path)

        File.unlink(path)
    else
        str = File.read(path)
    end

    str
end

Instance Method Details

#format_quota(qh, default_quotas, resource_id) ⇒ Object

Outputs formated quota information to stdout

@param qh [Hash] with the quotas for a given resource
@param default_quotas_hash [XMLElement] with the default quota limits
@param resource_id [Integer] user/group ID


197
198
199
200
201
202
203
204
205
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
242
243
244
245
246
247
248
249
250
251
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/one_helper/onequota_helper.rb', line 197

def format_quota(qh, default_quotas, resource_id)
    str_h1="%-80s"

    puts

    CLIHelper.print_header(str_h1 % "VMS USAGE & QUOTAS",false)

    puts

    @default_quotas = default_quotas

    vm_quotas = [qh['VM_QUOTA']['VM']].flatten

    # This initializes the VM quotas for users/groups that don't have any
    # resource usage yet. It not applied to oneamdin
    if vm_quotas[0].nil? && resource_id.to_i != 0
        limit = LIMIT_DEFAULT

        vm_quotas = [{
            "VMS"                   => limit,
            "VMS_USED"              => "0",
            "CPU"                   => limit,
            "CPU_USED"              => "0",
            "MEMORY"                => limit,
            "MEMORY_USED"           => "0",
            "RUNNING_VMS"           => limit,
            "RUNNING_VMS_USED"      => "0",
            "RUNNING_CPU"           => limit,
            "RUNNING_CPU_USED"      => "0",
            "RUNNING_MEMORY"        => limit,
            "RUNNING_MEMORY_USED"   => "0",
            "SYSTEM_DISK_SIZE"      => limit,
            "SYSTEM_DISK_SIZE_USED" => "0"
        }]
    end

    if !vm_quotas[0].nil?
        CLIHelper::ShowTable.new(nil, self) do
            column :"VMS", "", :right, :size=>17 do |d|
                if !d.nil?
                    elem = 'VMS'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%7d /       -" % [d["VMS_USED"]]
                    else
                        "%7d / %7d" % [d["VMS_USED"], limit]
                    end
                end
            end

            column :"MEMORY", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'MEMORY'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%8s /        -" % [
                            OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M")
                        ]
                    else
                        "%8s / %8s" % [
                            OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"),
                            OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
                        ]
                    end
                end
            end

            column :"CPU", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'CPU'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%8.2f /        -" % [d["CPU_USED"]]
                    else
                        "%8.2f / %8.2f" % [d["CPU_USED"], limit]
                    end
                end
            end

            column :"SYSTEM_DISK_SIZE", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'SYSTEM_DISK_SIZE'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%8s /        -" % [
                            OpenNebulaHelper.unit_to_str(d["SYSTEM_DISK_SIZE_USED"].to_i,{},"M")
                        ]
                    else
                        "%8s / %8s" % [
                            OpenNebulaHelper.unit_to_str(d["SYSTEM_DISK_SIZE_USED"].to_i,{},"M"),
                            OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
                        ]
                    end
                end
            end
        end.show(vm_quotas, {})

        puts
    end

    CLIHelper.print_header(str_h1 % "VMS USAGE & QUOTAS - RUNNING",false)

    puts

    if !vm_quotas[0].nil?
        CLIHelper::ShowTable.new(nil, self) do
            column :"RUNNING VMS", "", :right, :size=>17 do |d|
                if !d.nil?
                    elem = 'RUNNING_VMS'
                    limit = d[elem] || LIMIT_UNLIMITED
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if d["RUNNING_VMS_USED"].nil?
                        d["RUNNING_VMS_USED"] = 0
                    end

                    if limit == LIMIT_UNLIMITED
                        "%7d /       -" % [d["RUNNING_VMS_USED"]]
                    else
                        "%7d / %7d" % [d["RUNNING_VMS_USED"], limit]
                    end
                end
            end

            column :"RUNNING MEMORY", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'RUNNING_MEMORY'
                    limit = d[elem] || LIMIT_UNLIMITED
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if d["RUNNING_MEMORY_USED"].nil?
                        d["RUNNING_MEMORY_USED"] = 0
                    end

                    if limit == LIMIT_UNLIMITED
                        "%8s /        -" % [
                            OpenNebulaHelper.unit_to_str(d["RUNNING_MEMORY_USED"].to_i,{},"M")
                        ]
                    else
                        "%8s / %8s" % [
                            OpenNebulaHelper.unit_to_str(d["RUNNING_MEMORY_USED"].to_i,{},"M"),
                            OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
                        ]
                    end
                end
            end

            column :"RUNNING CPU", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'RUNNING_CPU'
                    limit = d[elem] || LIMIT_UNLIMITED
                    limit = helper.get_default_limit(
                        limit, "VM_QUOTA/VM/#{elem}")

                    if d["RUNNING_CPU_USED"].nil?
                        d["RUNNING_CPU_USED"] = 0
                    end

                    if limit == LIMIT_UNLIMITED
                        "%8.2f /        -" % [d["RUNNING_CPU_USED"]]
                    else
                        "%8.2f / %8.2f" % [d["RUNNING_CPU_USED"], limit]
                    end
                end
            end
        end.show(vm_quotas, {})

        puts
    end

    CLIHelper.print_header(str_h1 % "DATASTORE USAGE & QUOTAS",false)

    puts

    ds_quotas = [qh['DATASTORE_QUOTA']['DATASTORE']].flatten

    if !ds_quotas[0].nil?
        CLIHelper::ShowTable.new(nil, self) do
            column :"ID", "", :size=>12 do |d|
                d["ID"] if !d.nil?
            end

            column :"IMAGES", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'IMAGES'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%8d /        -" % [d["IMAGES_USED"]]
                    else
                        "%8d / %8d" % [d["IMAGES_USED"], limit]
                    end
                end
            end

            column :"SIZE", "", :right, :size=>19 do |d|
                if !d.nil?
                    elem = 'SIZE'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%8s /        -" % [
                            OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M")
                        ]
                    else
                        "%8s / %8s" % [
                            OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"),
                            OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
                        ]
                    end
                end
            end
        end.show(ds_quotas, {})

        puts
    end

    CLIHelper.print_header(str_h1 % "NETWORK USAGE & QUOTAS",false)

    puts

    net_quotas = [qh['NETWORK_QUOTA']['NETWORK']].flatten

    if !net_quotas[0].nil?
        CLIHelper::ShowTable.new(nil, self) do
            column :"ID", "", :size=>12 do |d|
                d["ID"] if !d.nil?
            end

            column :"LEASES", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'LEASES'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "NETWORK_QUOTA/NETWORK[ID=#{d['ID']}]/#{elem}")

                    if limit == LIMIT_UNLIMITED
                        "%8d /        -" % [d["LEASES_USED"]]
                    else
                        "%8d / %8d" % [d["LEASES_USED"], limit]
                    end
                end
            end
        end.show(net_quotas, {})

        puts
    end

    CLIHelper.print_header(str_h1 % "IMAGE USAGE & QUOTAS",false)

    puts

    image_quotas = [qh['IMAGE_QUOTA']['IMAGE']].flatten

    if !image_quotas[0].nil?
        CLIHelper::ShowTable.new(nil, self) do
            column :"ID", "", :size=>12 do |d|
                d["ID"] if !d.nil?
            end

            column :"RUNNING VMS", "", :right, :size=>20 do |d|
                if !d.nil?
                    elem = 'RVMS'
                    limit = d[elem]
                    limit = helper.get_default_limit(
                        limit, "IMAGE_QUOTA/IMAGE[ID=#{d['ID']}]/RVMS")

                    if limit == LIMIT_UNLIMITED
                        "%8d /        -" % [d["RVMS_USED"]]
                    else
                        "%8d / %8d" % [d["RVMS_USED"], limit]
                    end
                end
            end
        end.show(image_quotas, {})
    end
end

#get_default_limit(limit, xpath) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/one_helper/onequota_helper.rb', line 493

def get_default_limit(limit, xpath)
    if limit == LIMIT_DEFAULT
        if !@default_quotas.nil?
            limit = @default_quotas[xpath]

            limit = LIMIT_UNLIMITED if limit.nil? || limit == ""
        else
            limit = LIMIT_UNLIMITED
        end
    end

    return limit
end