Revision c03fcc70

View differences:

src/onedb/onedb
226 226
    :format => Array
227 227
}
228 228

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

  
229 233
START_TIME = {
230 234
    :name   => "start_time",
231 235
    :short  => "-s TIME",
......
242 246
    :format => Time
243 247
}
244 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
}
285

  
245 286
cmd=CommandParser::CmdParser.new(ARGV) do
246 287
    description <<-EOT.unindent
247 288
        This command enables the user to manage the OpenNebula database. It
......
439 480
        Deletes all but the last history records from non DONE VMs
440 481
    EOT
441 482

  
442
    command :'purge-history' , purge_history_desc,
483
    command :'purge-history', purge_history_desc,
443 484
            :options => [BACKUP, START_TIME, END_TIME] do
444 485
        begin
445 486
            action = OneDBLive.new
......
459 500
        Deletes all VMs in DONE state
460 501
    EOT
461 502

  
462
    command :'purge-done' , purge_done_desc,
503
    command :'purge-done', purge_done_desc,
463 504
            :options => [BACKUP, START_TIME, END_TIME] do
464 505
        begin
465 506
            action = OneDBLive.new
......
472 513

  
473 514
        0 # exit code
474 515
    end
516

  
517
    ###########################################################################
518
    # Change value in object body
519
    ###########################################################################
520
    change_body_desc = <<-EOT.unindent
521
        Changes a value from the body of an object. The possible objects are:
522
            vm, host, vnet, image, cluster, document, group, marketplace,
523
            marketplaceapp, secgroup, template, vrouter or zone
524

  
525
        You can filter the objects to modify using one of these options:
526

  
527
            * --id: object id, example: 156
528
            * --xpath: xpath expression, example: TEMPLATE[count(NIC)>1]
529
            * --expr: xpath expression, can use operators =, !=, <, >, <= or >=
530
                example: TEMPLATE/NIC/NIC_ID>0
531

  
532
        If you want to change a value use a third parameter. In case you want
533
        to delete it use --delete option.
534
    EOT
535

  
536
    command :'change-body', change_body_desc, :object, :xpath, [:value, nil],
537
            :options => [BACKUP, ID, XPATH, EXPR, DRY, DELETE] do
538
        begin
539
            action = OneDBLive.new
540
            action.change_body(args[0], args[1], args[2], options)
541
        rescue Exception => e
542
            puts e.message
543
            pp e.backtrace
544
            [-1, e.message]
545
        end
546

  
547
        0 # exit code
548
    end
475 549
end
src/onedb/onedb_live.rb
183 183
            delete("history", "vid = #{vm.id}", false)
184 184
        end
185 185
    end
186

  
187
    def check_expr(object, expr)
188
        reg = /^(?<xpath>.+?)(?<operator>=|!=|>=|<=|>|<)(?<value>.*?)$/
189
        parsed = expr.match(reg)
190

  
191
        raise "Expression malformed: '#{expr}'" unless parsed
192

  
193
        val = object[parsed[:xpath]]
194
        return false if !val
195

  
196
        p_val = parsed[:value].strip
197
        val.strip!
198

  
199
        res = false
200

  
201
        res = case parsed[:operator]
202
        when '='
203
            val == p_val
204
        when '!='
205
            val != p_val
206
        when '<'
207
            val.to_i < p_val.to_i
208
        when '>'
209
            val.to_i > p_val.to_i
210
        when '<='
211
            val.to_i <= p_val.to_i
212
        when '>='
213
            val.to_i >= p_val.to_i
214
        end
215

  
216
        res
217
    end
218

  
219
    def change_body(object, xpath, value, options = {})
220
        case (object||'').downcase.strip.to_sym
221
        when :vm
222
            table = 'vm_pool'
223
            object = OpenNebula::VirtualMachinePool.new(client)
224
            federate = false
225

  
226
        when :host
227
            table = 'host_pool'
228
            object = OpenNebula::HostPool.new(client)
229
            federate = false
230

  
231
        when :vnet
232
            table = 'network_pool'
233
            object = OpenNebula::VirtualNetworkPool.new(client)
234
            federate = false
235

  
236
        when :image
237
            table = 'image_pool'
238
            object = OpenNebula::ImagePool.new(client)
239
            federate = false
240

  
241
        when :cluster
242
            table = 'cluster_pool'
243
            object = OpenNebula::ClusterPool.new(client)
244
            federate = false
245

  
246
        when :document
247
            table = 'document_pool'
248
            object = OpenNebula::DocumentPool.new(client)
249
            federate = false
250

  
251
        when :group
252
            table = 'group_pool'
253
            object = OpenNebula::GroupPool.new(client)
254
            federate = true
255

  
256
        when :marketplace
257
            table = 'marketplace_pool'
258
            object = OpenNebula::MarketPlacePool.new(client)
259
            federate = true
260

  
261
        when :marketplaceapp
262
            table = 'marketplaceapp_pool'
263
            object = OpenNebula::MarketPlaceAppPool.new(client)
264
            federate = true
265

  
266
        when :secgroup
267
            table = 'secgroup_pool'
268
            object = OpenNebula::SecurityGroupPool.new(client)
269
            federate = false
270

  
271
        when :template
272
            table = 'template_pool'
273
            object = OpenNebula::TemplatePool.new(client)
274
            federate = false
275

  
276
        when :vrouter
277
            table = 'vrouter_pool'
278
            object = OpenNebula::VirtualRouterPool.new(client)
279
            federate = false
280

  
281
        when :zone
282
            table = 'zone_pool'
283
            object = OpenNebula::ZonePool.new(client)
284
            federate = true
285

  
286
        else
287
            raise "Object type '#{object}' not supported"
288
        end
289

  
290
        if !value && !options[:delete]
291
            raise "A value or --delete should specified"
292
        end
293

  
294
        object.info
295

  
296
        object.each do |o|
297
            if options[:id]
298
                next unless o.id.to_s.strip == options[:id].to_s
299
            elsif options[:xpath]
300
                next unless o[options[:xpath]]
301
            elsif options[:expr]
302
                next unless check_expr(o, options[:expr])
303
            end
304

  
305
            o.info
306
            doc = Nokogiri::XML(o.to_xml, nil, NOKOGIRI_ENCODING) do |c|
307
                c.default_xml.noblanks
308
            end
309

  
310
            doc.xpath(xpath).each do |e|
311
                if options[:delete]
312
                    e.remove
313
                else
314
                    e.content = value
315
                end
316
            end
317

  
318
            xml = doc.root.to_xml
319

  
320
            if options[:dry]
321
                puts xml
322
            else
323
                update_body(table, xml, "oid = #{o.id}", federate)
324
            end
325
        end
326
    end
186 327
end
328

  

Also available in: Unified diff