Statistics
| Branch: | Tag: | Revision:

one / src / cli / oneimage @ 6329d8b3

History | View | Annotate | Download (13.7 KB)

1
#!/usr/bin/env ruby 
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
ONE_LOCATION=ENV["ONE_LOCATION"]
20

    
21
if !ONE_LOCATION
22
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
else
24
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
end
26

    
27
$: << RUBY_LIB_LOCATION
28

    
29

    
30
require 'OpenNebula'
31
require 'CommandManager'
32
require 'client_utilities'
33
require 'command_parse'
34

    
35
ShowTableImage={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for the Image",
39
        :size => 4,
40
        :proc => lambda {|d,e| d.id }
41
    },
42
    :user=> {
43
        :name => "USER",
44
        :desc => "Name of the owner",
45
        :size => 8,
46
        :proc => lambda {|d,e| 
47
            "TODO"
48
        }
49
    },
50
    :uid=> {
51
        :name => "UID",
52
        :desc => "Id of the owner",
53
        :size => 4,
54
        :proc => lambda {|d,e|
55
            d["UID"]
56
        }
57
    },
58
    :gid=> {
59
        :name => "GID",
60
        :desc => "Id of the group",
61
        :size => 4,
62
        :proc => lambda {|d,e|
63
            d.gid
64
        }
65
    },
66
    :name => {
67
        :name => "NAME",
68
        :desc => "Name of the Image",
69
        :size => 20,
70
        :proc => lambda {|d,e| 
71
            d.name
72
        }
73
    },
74
    :type => {
75
        :name => "TYPE",
76
        :desc => "Type of the Image",
77
        :size => 4,
78
        :proc => lambda {|d,e| 
79
            d.short_type_str
80
        }
81
    },
82
    :regtime => {
83
        :name => "REGTIME",
84
        :desc => "Registration time of the Image",
85
        :size => 20,
86
        :proc => lambda {|d,e| 
87
            str_register_time(d)
88
        }
89
    },
90
    :public => {
91
        :name => "PUBLIC",
92
        :desc => "Whether the Image is public or not",
93
        :size => 3,
94
        :proc => lambda {|d,e| 
95
            if d["PUBLIC"].to_i == 1 then "Yes" else "No" end}
96
    },
97
    :persistent => {
98
        :name => "PERSISTENT",
99
        :desc => "Whether the Image is persistent or not",
100
        :size => 3,
101
        :proc => lambda {|d,e| 
102
            if d["PERSISTENT"].to_i == 1 then "Yes" else "No" end}
103
    },
104
    :state => {
105
        :name => "STAT",
106
        :desc => "State of the Image",
107
        :size => 4,
108
        :proc => lambda {|d,e|
109
            d.short_state_str
110
        }
111
    },
112
    :runningvms => {
113
        :name => "#VMS",
114
        :desc => "Number of VMs currently running from this Image",
115
        :size => 5,
116
        :proc => lambda {|d,e| d['RUNNING_VMS'] }
117
    },
118
    
119
    :default => [:id, :user, :name, :type, :regtime, 
120
                 :public, :persistent, :state, :runningvms]
121
}
122

    
123

    
124

    
125
class ImageShow
126
    
127
    def initialize(client, filter_flag="-2")
128
        @imagepool=OpenNebula::ImagePool.new(client, filter_flag.to_i)
129
        @table=ShowTable.new(ShowTableImage)
130
    end
131
    
132
    def header_image_small
133
        scr_bold
134
        scr_underline
135
        print @table.header_str
136
        scr_restore
137
        puts ""
138
    end
139
    
140
    def top(options=nil)
141
        delay=1
142
        delay=options[:delay] if options && options[:delay]
143
        
144
        result=nil
145
        
146
        begin
147
            while true
148
                scr_cls
149
                scr_move(0,0)
150
                result=list_short(options)
151
                sleep delay
152
            end
153
        rescue Exception
154
        end
155
        result
156
    end
157
    
158
    def list_short(options=nil)
159
        res=@imagepool.info()
160

    
161
        if options
162
            @table.columns=options[:columns] if options[:columns]
163
        end
164

    
165
        if OpenNebula.is_error?(res)
166
            result=res
167
            puts res.message
168
            exit -1
169
        else
170

    
171
            if options[:filter_flag]
172
                vms=@imagepool.select{|element|
173
                    element['USERNAME']==options[:filter_flag]  }
174
            else
175
                vms=@imagepool
176
            end
177

    
178
            result=[true, ""]
179
            header_image_small
180

    
181
            if options
182
                puts @table.data_str(vms, options)
183
            else
184
                puts @table.data_str(vms)
185
            end
186
            
187
            result
188
        end
189
    end
190
    
191
    def top(options=nil)
192
        delay=1
193
        delay=options[:delay] if options && options[:delay]
194
        
195
        result=nil
196
        
197
        begin
198
            while true
199
                scr_cls
200
                scr_move(0,0)
201
                result=list_short(options)
202
                sleep delay
203
            end
204
        rescue Exception
205
        end
206
        result
207
    end
208
end
209

    
210

    
211
##########################
212
## COMMAND LINE PARSING ##
213
##########################
214

    
215
class OneImageParse < CommandParse
216

    
217
    COMMANDS_HELP=<<-EOT
218

    
219
Description:
220

    
221
This command enables the user to manage images.
222

    
223

    
224
Commands:
225

    
226
* register (Registers an image, copying it to the repository if it applies)
227
    oneimage register <template>
228
    
229
    template is a file name where the Image description is located
230
    
231
* addattr (Add a new image attribute)
232
    oneimage addattr <image_id> <attribute_name> <attribute_value>
233
    
234
* update (Modifies an image attribute)
235
    oneimage update <image_id> <attribute_name> <attribute_value>
236
    
237
* rmattr (Deletes an Image attribute)
238
    oneimage rmattr <image_id> <attribute_name>
239
    
240
* enable (Enabled an Image)
241
    oneimage enable <image_id>
242
    
243
* disable (Disabled an Image)
244
    oneimage disable <image_id>
245
    
246
* publish (Publish an Image)
247
    oneimage publish <image_id>
248
    
249
* unpublish (Unpublish an Image)
250
    oneimage unpublish <image_id>
251
    
252
* persistent (Makes an Image persistent)
253
    oneimage persistent <image_id>
254

    
255
* nonpersistent (Makes an Image non persistent)
256
    oneimage nonpersistent <image_id>
257
    
258
* list (Shows Images in the pool)
259
    oneimage list <filter_flag>
260
    
261
    where filter_flag can be
262
        a, all   --> all the known Images
263
        m, mine  --> the Images belonging to the user in ONE_AUTH
264
        g, group --> 'mine' plus the Images belonging to the groups
265
                        the user is member of
266
        uid      --> Images of the user identified by this uid
267
        user     --> Images of the user identified by the username
268
        
269
* top (Lists Images continuously)
270
    oneimage top
271

    
272
* show (Gets information about an specific Image)
273
    oneimage show <image_id>
274
   
275
* delete (Deletes an already deployed Image)
276
    oneimage delete <image_id>
277
   
278
EOT
279

    
280
    def text_commands
281
        COMMANDS_HELP
282
    end
283

    
284
    def text_command_name
285
        "oneimage"
286
    end
287
    
288
    def list_options
289
        table=ShowTable.new(ShowTableImage)
290
        table.print_help
291
    end
292
end
293

    
294

    
295
def get_user_flags
296
    ops=Hash.new
297
    if ARGV[0]
298
        case ARGV[0]
299
            when "a", "all"
300
                ops[:filter_user]="-2"
301
            when "m", "mine"
302
                ops[:filter_user]="-3"
303
            when "g", "group"
304
                ops[:filter_user]="-1"
305
            else
306
                if !ARGV[0].match(/^[0123456789]+$/)
307
                    ops[:filter_user]="-2"
308
                    ops[:filter_flag]=ARGV[0]
309
                else
310
                    ops[:filter_user]=ARGV[0]
311
                end
312
        end
313
     else
314
        ops[:filter_user]="-2"
315
     end
316

    
317
     ops
318
end
319

    
320
def get_template(template_path)
321
    begin
322
        template = File.read(ARGV[0])
323
    rescue
324
        result = OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
325
    end
326
    
327
    if !is_successful?(result) 
328
        puts result.message
329
        exit -1
330
    end
331
    
332
    return template
333
end
334

    
335
oneimage_opts=OneImageParse.new
336
oneimage_opts.parse(ARGV)
337
ops=oneimage_opts.options
338

    
339
result=[false, "Unknown error"]
340

    
341
command=ARGV.shift
342

    
343
case command
344
when "register", "create", "add"
345
    check_parameters("register", 1)
346
    template = get_template(ARGV[0])
347
    
348
    image = OpenNebula::Image.new(OpenNebula::Image.build_xml, get_one_client)
349

    
350
    result = image.allocate(template)
351
    if is_successful?(result)
352
        puts "ID: " + image.id.to_s if ops[:verbose]
353
    end
354

    
355
    
356
when "update", "addattr"
357
    check_parameters("update", 3)
358
    image_id=get_image_id(ARGV[0])
359
    
360
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
361
    
362
    result=image.update(ARGV[1],ARGV[2])
363
    if is_successful?(result)
364
        puts "Modified image" if ops[:verbose]
365
    end
366

    
367
when "rmattr"
368
    check_parameters("rmattr", 2)
369
    image_id=get_image_id(ARGV[0])
370

    
371
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
372

    
373
    result=image.remove_attr(ARGV[1])
374
    if is_successful?(result)
375
        puts "Removed image atrribute" if ops[:verbose]
376
    end
377
    
378
when "enable"
379
    check_parameters("enable", 1)
380
    image_id=get_image_id(ARGV[0])
381

    
382
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
383

    
384
    result=image.enable
385
    if is_successful?(result)
386
        puts "Image enabled" if ops[:verbose]
387
    end
388
    
389
when "disable"
390
    check_parameters("disable", 1)
391
    image_id=get_image_id(ARGV[0])
392

    
393
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
394

    
395
    result=image.disable
396
    if is_successful?(result)
397
        puts "Image disabled" if ops[:verbose]
398
    end
399

    
400
when "publish"
401
    check_parameters("publish", 1)
402
    image_id=get_image_id(ARGV[0])
403

    
404
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
405

    
406
    result=image.publish
407
    if is_successful?(result)
408
        puts "Image published" if ops[:verbose]
409
    end
410
    
411
when "unpublish"
412
    check_parameters("unpublish", 1)
413
    image_id=get_image_id(ARGV[0])
414

    
415
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
416

    
417
    result=image.unpublish
418
    if is_successful?(result)
419
        puts "Image unpublished" if ops[:verbose]
420
    end
421
    
422
when "persistent"
423
    check_parameters("publish", 1)
424
    image_id=get_image_id(ARGV[0])
425

    
426
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
427

    
428
    result=image.persistent
429
    if is_successful?(result)
430
        puts "Image made persistent" if ops[:verbose]
431
    end
432
    
433
when "nonpersistent"
434
    check_parameters("nonpersistent", 1)
435
    image_id=get_image_id(ARGV[0])
436

    
437
    image=OpenNebula::Image.new_with_id(image_id, get_one_client)
438

    
439
    result=image.nonpersistent
440
    if is_successful?(result)
441
        puts "Image made nonpersistent" if ops[:verbose]
442
    end
443
    
444
when "list"
445
    ops.merge!(get_user_flags)
446
    if !ops[:xml]
447
        imagelist=ImageShow.new(get_one_client, ops[:filter_user].to_i)
448
        ops[:columns]=ops[:list] if ops[:list]
449
        result=imagelist.list_short(ops) 
450
    else
451
        imagepool=OpenNebula::ImagePool.new(get_one_client,
452
            ops[:filter_user].to_i)
453
        imagepool.info
454
        puts imagepool.to_xml
455
    end
456
    
457
when "top"
458
    ops.merge!(get_user_flags)
459
    imagelist=ImageShow.new(get_one_client, ops[:filter_user].to_i)
460
    ops[:columns]=ops[:list] if ops[:list]
461
    result=imagelist.top(ops)
462
    
463
when "show"
464
    check_parameters("get_info", 1)
465
    args=expand_args(ARGV)
466
    
467
    args.each do |param|
468
        image_id=get_image_id(param)
469

    
470
        image=OpenNebula::Image.new_with_id(image_id, get_one_client)
471
        result = image.info
472
        
473
        if is_successful?(result)
474
            if !ops[:xml]
475
                str="%-15s: %-20s"
476
                str_h1="%-80s"
477

    
478
                print_header(str_h1, "IMAGE #{image[:id]} INFORMATION", true)
479

    
480
                puts str % ["ID", image.id.to_s]
481
                puts str % ["NAME", image.name]
482
                puts str % ["TYPE", image.type_str]            
483
        
484
                value=image['REGTIME'].to_i
485
                if value==0
486
                    value='-'
487
                else
488
                    value=Time.at(value).strftime("%m/%d %H:%M:%S")
489
                end
490
                puts str % ["REGISTER TIME", value]
491
                if image['PUBLIC'].to_i == 1 
492
                    public_str = "Yes" 
493
                else 
494
                    public_str = "No" 
495
                end
496
                puts str % ["PUBLIC", public_str]
497
                
498
                persistent_str=(image["PERSISTENT"].to_i==1 ? "Yes" : "No")
499
                puts str % ["PERSISTENT", persistent_str]
500
                
501
                puts str % ["SOURCE", image['SOURCE']]    
502
                puts str % ["STATE", image.short_state_str]
503
                puts str % ["RUNNING_VMS", image['RUNNING_VMS']]            
504
            
505
                puts
506
        
507
                print_header(str_h1,"IMAGE TEMPLATE",false)
508
        
509
                puts image.template_str
510
            else
511
                puts image.to_xml
512
            end
513
        end
514
     end
515
    
516
when "delete"
517
    check_parameters("delete", 1)
518
    args=expand_args(ARGV)
519

    
520
    args.each do |param|
521
        image_id = get_image_id(param)
522
        image = OpenNebula::Image.new(
523
                        OpenNebula::Image.build_xml(image_id),
524
                        get_one_client)
525

    
526
        result = image.delete
527
        if is_successful?(result)
528
            puts "Image correctly deleted"  if ops[:verbose]
529
        end
530
    end
531

    
532
else
533
    oneimage_opts.print_help
534
    exit -1
535
end
536

    
537
if OpenNebula.is_error?(result)
538
    puts "Error: " + result.message
539
    exit -1
540
end
541

    
542