Revision 45bfe9aa src/onedb/onedb

View differences:

src/onedb/onedb
46 46
$: << RUBY_LIB_LOCATION+'/onedb'
47 47

  
48 48
require 'cli/command_parser'
49
require 'optparse/time'
49 50
require 'onedb'
51
require 'onedb_live'
50 52
require 'opennebula'
51 53

  
52 54
FORCE={
......
224 226
    :format => Array
225 227
}
226 228

  
229
###############################################################################
230
# Live operation options
231
###############################################################################
232

  
233
START_TIME = {
234
    :name   => "start_time",
235
    :short  => "-s TIME",
236
    :large  => "--start TIME" ,
237
    :description => "First time to process",
238
    :format => Time
239
}
240

  
241
END_TIME = {
242
    :name   => "end_time",
243
    :short  => "-e TIME",
244
    :large  => "--end TIME" ,
245
    :description => "Last time to process",
246
    :format => Time
247
}
248

  
249
ID = {
250
    :name   => "id",
251
    :short  => "-i ID",
252
    :large  => "--id ID" ,
253
    :description => "Filter by ID",
254
    :format => Numeric
255
}
256

  
257
XPATH = {
258
    :name   => "xpath",
259
    :short  => "-x ID",
260
    :large  => "--xpath ID" ,
261
    :description => "Filter by xpath",
262
    :format => String
263
}
264

  
265
EXPR= {
266
    :name   => "expr",
267
    :short  => "-e ID",
268
    :large  => "--expr ID" ,
269
    :description => "Filter by expression (UNAME=oneadmin)",
270
    :format => String
271
}
272

  
273
DRY= {
274
    :name   => "dry",
275
    :large  => "--dry" ,
276
    :description => "Do not write in the database, output xml"
277
}
278

  
279
DELETE= {
280
    :name   => "delete",
281
    :short  => "-d",
282
    :large  => "--delete" ,
283
    :description => "Delete all matched xpaths"
284
}
227 285

  
228 286
cmd=CommandParser::CmdParser.new(ARGV) do
229 287
    description <<-EOT.unindent
......
414 472
            [-1, e.message]
415 473
        end
416 474
    end
475

  
476
    LIVE_ACTION_HELP = <<-EOT.unindent
477
        **WARNING**: This action is done while OpenNebula is running. Make
478
        a backup of the datasbase before executing.
479
    EOT
480

  
481
    ###########################################################################
482
    # Purge history
483
    ###########################################################################
484
    purge_history_desc = <<-EOT.unindent
485
        Deletes all but the last history records from non DONE VMs
486

  
487
        #{LIVE_ACTION_HELP}
488
    EOT
489

  
490
    command :'purge-history', purge_history_desc,
491
            :options => [START_TIME, END_TIME] do
492
        begin
493
            action = OneDBLive.new
494
            action.purge_history(options)
495
        rescue Exception => e
496
            puts e.message
497
            pp e.backtrace
498
            [-1, e.message]
499
        end
500

  
501
        0
502
    end
503

  
504
    ###########################################################################
505
    # Purge VMs in DONE state
506
    ###########################################################################
507
    purge_done_desc = <<-EOT.unindent
508
        Deletes all VMs in DONE state
509

  
510
        #{LIVE_ACTION_HELP}
511
    EOT
512

  
513
    command :'purge-done', purge_done_desc,
514
            :options => [START_TIME, END_TIME] do
515
        begin
516
            action = OneDBLive.new
517
            action.purge_done_vm(options)
518
        rescue Exception => e
519
            puts e.name
520
            pp e.backtrace
521
            [-1, e.message]
522
        end
523

  
524
        0 # exit code
525
    end
526

  
527
    ###########################################################################
528
    # Change value in object body
529
    ###########################################################################
530
    change_body_desc = <<-EOT.unindent
531
        Changes a value from the body of an object. The possible objects are:
532
            vm, host, vnet, image, cluster, document, group, marketplace,
533
            marketplaceapp, secgroup, template, vrouter or zone
534

  
535
        You can filter the objects to modify using one of these options:
536

  
537
            * --id: object id, example: 156
538
            * --xpath: xpath expression, example: TEMPLATE[count(NIC)>1]
539
            * --expr: xpath expression, can use operators =, !=, <, >, <= or >=
540
                examples: UNAME=oneadmin, TEMPLATE/NIC/NIC_ID>0
541

  
542
        If you want to change a value use a third parameter. In case you want
543
        to delete it use --delete option.
544

  
545
        Change the second network of VMs that belong to "user":
546

  
547
            onedb change-body vm --expr UNAME=user \\
548
                '/VM/TEMPLATE/NIC[NETWORK="service"]/NETWORK' new_network
549

  
550
        Delete cache attribute in all disks, write xml, do not modify DB:
551

  
552
            onedb change-body vm '/VM/TEMPLATE/DISK/CACHE' --delete --dry
553

  
554
        Delete cache attribute in all disks in poweroff:
555

  
556
            onedb change-body vm --expr LCM_STATE=8 \\
557
                '/VM/TEMPLATE/DISK/CACHE' --delete
558

  
559
        #{LIVE_ACTION_HELP}
560
    EOT
561

  
562
    command :'change-body', change_body_desc, :object, :xpath, [:value, nil],
563
            :options => [ID, XPATH, EXPR, DRY, DELETE] do
564
        begin
565
            action = OneDBLive.new
566
            action.change_body(args[0], args[1], args[2], options)
567
        rescue Exception => e
568
            puts e.message
569
            pp e.backtrace
570
            [-1, e.message]
571
        end
572

  
573
        0 # exit code
574
    end
417 575
end

Also available in: Unified diff