Statistics
| Branch: | Tag: | Revision:

one / src / cli / onetemplate @ 6329d8b3

History | View | Annotate | Download (10.9 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
ShowTableTemplate={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for the Template",
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 Template",
69
        :size => 20,
70
        :proc => lambda {|d,e|
71
            d.name
72
        }
73
    },
74
    :regtime => {
75
        :name => "REGTIME",
76
        :desc => "Registration time of the Template",
77
        :size => 20,
78
        :proc => lambda {|d,e|
79
            str_register_time(d)
80
        }
81
    },
82
    :public => {
83
        :name => "PUBLIC",
84
        :desc => "Whether the Template is public or not",
85
        :size => 3,
86
        :proc => lambda {|d,e|
87
            if d["PUBLIC"].to_i == 1 then "Yes" else "No" end}
88
    },
89

    
90
    :default => [:id, :user, :name, :regtime, :public]
91
}
92

    
93

    
94

    
95
class TemplateShow
96

    
97
    def initialize(client, filter_flag="-2")
98
        @templatepool=OpenNebula::TemplatePool.new(client, filter_flag.to_i)
99
        @table=ShowTable.new(ShowTableTemplate)
100
    end
101

    
102
    def header_template_small
103
        scr_bold
104
        scr_underline
105
        print @table.header_str
106
        scr_restore
107
        puts ""
108
    end
109

    
110
    def top(options=nil)
111
        delay=1
112
        delay=options[:delay] if options && options[:delay]
113

    
114
        result=nil
115

    
116
        begin
117
            while true
118
                scr_cls
119
                scr_move(0,0)
120
                result=list_short(options)
121
                sleep delay
122
            end
123
        rescue Exception
124
        end
125
        result
126
    end
127

    
128
    def list_short(options=nil)
129
        res=@templatepool.info()
130

    
131
        if options
132
            @table.columns=options[:columns] if options[:columns]
133
        end
134

    
135
        if OpenNebula.is_error?(res)
136
            result=res
137
            puts res.message
138
            exit -1
139
        else
140

    
141
            if options[:filter_flag]
142
                objs=@templatepool.select{|element|
143
                    element['USERNAME']==options[:filter_flag]  }
144
            else
145
                objs=@templatepool
146
            end
147

    
148
            result=[true, ""]
149
            header_template_small
150

    
151
            if options
152
                puts @table.data_str(objs, options)
153
            else
154
                puts @table.data_str(objs)
155
            end
156

    
157
            result
158
        end
159
    end
160
end
161

    
162

    
163
##########################
164
## COMMAND LINE PARSING ##
165
##########################
166

    
167
class OneTemplateParse < CommandParse
168

    
169
    COMMANDS_HELP=<<-EOT
170

    
171
Description:
172

    
173
This command enables the user to manage templates.
174

    
175

    
176
Commands:
177

    
178
* create (Registers a Template from a template file)
179
    onetemplate create <file>
180

    
181
    file is a file name where the Template description is located
182

    
183
* addattr (Add a new Template attribute)
184
    onetemplate addattr <template_id> <attribute_name> <attribute_value>
185

    
186
* update (Modifies a Template attribute)
187
    onetemplate update <template_id> <attribute_name> <attribute_value>
188

    
189
* rmattr (Deletes a Template attribute)
190
    onetemplate rmattr <template_id> <attribute_name>
191

    
192
* publish (Publish a Template)
193
    onetemplate publish <template_id>
194

    
195
* unpublish (Unpublish an Template)
196
    onetemplate unpublish <template_id>
197

    
198
* list (Shows Templates in the pool)
199
    onetemplate list <filter_flag>
200

    
201
    where filter_flag can be
202
        a, all   --> all the known Templates
203
        m, mine  --> the Templates belonging to the user in ONE_AUTH
204
        g, group --> 'mine' plus the Templates belonging to the groups
205
                        the user is member of
206
        uid      --> Templates of the user identified by this uid
207
        user     --> Templates of the user identified by the username
208

    
209
* top (Lists Templates continuously)
210
    onetemplate top
211

    
212
* show (Gets information about an specific Template)
213
    onetemplate show <template_id>
214

    
215
* delete (Deletes a Template)
216
    onetemplate delete <template_id>
217

    
218
EOT
219

    
220
    def text_commands
221
        COMMANDS_HELP
222
    end
223

    
224
    def text_command_name
225
        "onetemplate"
226
    end
227

    
228
    def list_options
229
        table=ShowTable.new(ShowTableTemplate)
230
        table.print_help
231
    end
232
end
233

    
234

    
235
def get_user_flags
236
    ops=Hash.new
237
    if ARGV[0]
238
        case ARGV[0]
239
            when "a", "all"
240
                ops[:filter_user]="-2"
241
            when "m", "mine"
242
                ops[:filter_user]="-3"
243
            when "g", "group"
244
                ops[:filter_user]="-1"
245
            else
246
                if !ARGV[0].match(/^[0123456789]+$/)
247
                    ops[:filter_user]="-2"
248
                    ops[:filter_flag]=ARGV[0]
249
                else
250
                    ops[:filter_user]=ARGV[0]
251
                end
252
        end
253
     else
254
        ops[:filter_user]="-2"
255
     end
256

    
257
     ops
258
end
259

    
260
def get_template(template_path)
261
    begin
262
        template = File.read(ARGV[0])
263
    rescue
264
        result = OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
265
    end
266

    
267
    if !is_successful?(result)
268
        puts result.message
269
        exit -1
270
    end
271

    
272
    return template
273
end
274

    
275
onetemplate_opts=OneTemplateParse.new
276
onetemplate_opts.parse(ARGV)
277
ops=onetemplate_opts.options
278

    
279
result=[false, "Unknown error"]
280

    
281
command=ARGV.shift
282

    
283
case command
284
when "create", "register", "add"
285
    check_parameters("create", 1)
286
    template_contents = get_template(ARGV[0])
287

    
288
    template = OpenNebula::Template.new(OpenNebula::Template.build_xml, get_one_client)
289

    
290
    result=template.allocate(template_contents)
291
    if !OpenNebula.is_error?(result)
292
        puts "ID: " + template.id.to_s if ops[:verbose]
293
        exit 0
294
    end
295

    
296
when "update", "addattr"
297
    check_parameters("update", 3)
298
    template_id = get_template_id(ARGV[0])
299

    
300
    template = OpenNebula::Template.new_with_id(template_id, get_one_client)
301

    
302
    result = template.update(ARGV[1],ARGV[2])
303
    if is_successful?(result)
304
        puts "Modified template" if ops[:verbose]
305
    end
306

    
307
when "rmattr"
308
    check_parameters("rmattr", 2)
309
    template_id = get_template_id(ARGV[0])
310

    
311
    template = OpenNebula::Template.new_with_id(template_id, get_one_client)
312

    
313
    result = template.remove_attr(ARGV[1])
314
    if is_successful?(result)
315
        puts "Removed template attribute" if ops[:verbose]
316
    end
317

    
318
when "publish"
319
    check_parameters("publish", 1)
320
    template_id = get_template_id(ARGV[0])
321

    
322
    template = OpenNebula::Template.new_with_id(template_id, get_one_client)
323

    
324
    result = template.publish
325
    if is_successful?(result)
326
        puts "Template published" if ops[:verbose]
327
    end
328

    
329
when "unpublish"
330
    check_parameters("unpublish", 1)
331
    template_id = get_template_id(ARGV[0])
332

    
333
    template = OpenNebula::Template.new_with_id(template_id, get_one_client)
334

    
335
    result = template.unpublish
336
    if is_successful?(result)
337
        puts "Template unpublished" if ops[:verbose]
338
    end
339

    
340
when "list"
341
    ops.merge!(get_user_flags)
342
    if !ops[:xml]
343
        templatelist = TemplateShow.new(get_one_client, ops[:filter_user].to_i)
344
        ops[:columns] = ops[:list] if ops[:list]
345
        result = templatelist.list_short(ops)
346
    else
347
        templatepool = OpenNebula::TemplatePool.new(get_one_client,
348
            ops[:filter_user].to_i)
349
        templatepool.info
350
        puts templatepool.to_xml
351
    end
352

    
353
when "top"
354
    ops.merge!(get_user_flags)
355
    templatelist = TemplateShow.new(get_one_client, ops[:filter_user].to_i)
356
    ops[:columns] = ops[:list] if ops[:list]
357
    result = templatelist.top(ops)
358

    
359
when "show"
360
    check_parameters("get_info", 1)
361
    args = expand_args(ARGV)
362

    
363
    args.each do |param|
364
        template_id = get_template_id(param)
365

    
366
        template = OpenNebula::Template.new_with_id(template_id, get_one_client)
367
        result = template.info
368

    
369
        if is_successful?(result)
370
            if !ops[:xml]
371
                str="%-15s: %-20s"
372
                str_h1="%-80s"
373

    
374
                print_header(str_h1, "TEMPLATE #{template[:id]} INFORMATION", true)
375

    
376
                puts str % ["ID", template.id.to_s]
377
                puts str % ["NAME", template.name]
378

    
379
                value = template['REGTIME'].to_i
380
                if value==0
381
                    value='-'
382
                else
383
                    value=Time.at(value).strftime("%m/%d %H:%M:%S")
384
                end
385
                puts str % ["REGISTER TIME", value]
386
                if template['PUBLIC'].to_i == 1
387
                    public_str = "Yes"
388
                else
389
                    public_str = "No"
390
                end
391
                puts str % ["PUBLIC", public_str]
392

    
393
                puts
394

    
395
                print_header(str_h1,"TEMPLATE CONTENTS",false)
396

    
397
                puts template.template_str
398
            else
399
                puts template.to_xml
400
            end
401
        end
402
     end
403

    
404
when "delete"
405
    check_parameters("delete", 1)
406
    args = expand_args(ARGV)
407

    
408
    args.each do |param|
409
        template_id = get_template_id(param)
410
        template = OpenNebula::Template.new(
411
                        OpenNebula::Template.build_xml(template_id),
412
                        get_one_client)
413

    
414
        result = template.delete
415
        if is_successful?(result)
416
            puts "Template correctly deleted"  if ops[:verbose]
417
        end
418
    end
419

    
420
else
421
    onetemplate_opts.print_help
422
    exit -1
423
end
424

    
425
if OpenNebula.is_error?(result)
426
    puts "Error: " + result.message
427
    exit -1
428
end
429

    
430