Statistics
| Branch: | Tag: | Revision:

one / src / cli / onevnet @ 6329d8b3

History | View | Annotate | Download (10.4 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 'client_utilities'
32
require 'command_parse'
33

    
34

    
35
ShowTableVN={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for virtual network",
39
        :size => 4,
40
        :proc => lambda {|d,e| d.id }
41
    },
42
    :name => {
43
        :name => "NAME",
44
        :desc => "name of the virtual network",
45
        :size => 15,
46
        :left => true,
47
        :proc => lambda {|d,e| d.name }
48
    },
49
    :user => {
50
        :name => "USER",
51
        :desc => "Username of the virtual network owner",
52
        :size => 8,
53
        :left => true,
54
        :proc => lambda {|d,e| "TODO" }
55
    },
56
    :uid=> {
57
        :name => "UID",
58
        :desc => "Id of the owner",
59
        :size => 4,
60
        :proc => lambda {|d,e|
61
            d["UID"]
62
        }
63
    },
64
    :gid=> {
65
        :name => "GID",
66
        :desc => "Id of the group",
67
        :size => 4,
68
        :proc => lambda {|d,e|
69
            d.gid
70
        }
71
    },
72
    :type => {
73
        :name => "TYPE",
74
        :desc => "NType of virtual network",
75
        :size => 6,
76
        :proc => lambda {|d,e| 
77
                          if(d["TYPE"] == "0")
78
                              return "Ranged"
79
                          else
80
                              if (d["TYPE"] == "1")
81
                                  return "Fixed"
82
                              end
83
                          end
84
                      }
85
    },
86
    :size => {
87
        :name => "SIZE",
88
        :desc => "Number of hosts (free + used) in the virtual network",
89
        :size => 6,
90
        :proc => lambda {|d,e| d["SIZE"] }
91
    },
92
    :bridge => {
93
        :name => "BRIDGE",
94
        :desc => "Bridge associated to the virtual network",
95
        :size => 6,
96
        :proc => lambda {|d,e| d["BRIDGE"] }
97
    },
98
    :public => {
99
        :name => "PUBLIC",
100
        :desc => "Whether the Image is public or not",
101
        :size => 1,
102
        :proc => lambda {|d,e| if d["PUBLIC"].to_i == 1 then "Y" else "N" end}
103
    },
104
    :totalleases => {
105
        :name => "#LEASES",
106
        :desc => "Number of this virtual network's given leases",
107
        :size => 7,
108
        :proc => lambda {|d,e| d["TOTAL_LEASES"] }
109
    },
110

    
111
    :default => [:id, :user, :name, :type, :bridge, :public, :totalleases]
112
}
113

    
114
class VNShow
115
    def initialize(filter_flag="-2")
116
        @vnpool=OpenNebula::VirtualNetworkPool.new(get_one_client,
117
            filter_flag.to_i)
118
        @table=ShowTable.new(ShowTableVN)
119
    end
120
    
121
    def header_vn_small
122
        scr_bold
123
        scr_underline
124
        print @table.header_str
125
        scr_restore
126
        puts ""
127
    end
128
    
129
    def list_short(options=nil)
130
        res=@vnpool.info
131
        if options
132
            @table.columns=options[:columns] if options[:columns]
133
        end
134

    
135
        if OpenNebula.is_error?(res)
136
            result=res
137
        else
138
            result=res
139
            header_vn_small
140
            if options[:filter_flag]
141
                 vns=@vnpool.select{|element|
142
                         element["USERNAME"]==options[:filter_flag]
143
                         }
144
            else
145
                 vns=@vnpool
146
            end
147
            puts @table.data_str(vns, options)
148
            result
149
        end
150
    end
151
end
152

    
153
class OneVNParse < CommandParse
154
    
155
    COMMANDS_HELP=<<-EOT
156

    
157
Description:
158

    
159
This command enables the user to manage virtual networks in the OpenNebula
160
server. It provides functionality to create, get information and delete a
161
particular network or to list available and used IP's.
162

    
163

    
164
Commands:
165

    
166
* create (Creates a new virtual network)
167
    onevnet create <template>
168
    
169
    template is a filename where the virtual network is described
170
    
171
* show (Gets info from a virtual network)
172
    onevnet show <network_id>
173
    
174
* publish (Publish a virtual network)
175
    onevnet publish <network_id>
176

    
177
* unpublish (Unpublish a virtual network)
178
    onevnet unpublish <network_id>
179
    
180
* delete (Removes a virtual network)
181
    onevnet delete <network_id>
182

    
183
* addleases (Adds a lease to the virtual network)
184
    onevnet addleases <network_id> <IP> [<MAC>]
185

    
186
* rmleases (Removes a lease fom the virtual network)
187
    onevnet rmleases <network_id> <IP>
188

    
189
* list (Lists virtual networks in the pool)
190
    onevnet list <filter_flag>
191

    
192
    where filter_flag can be
193
        a, all   --> all the known VNets
194
        m, mine  --> the VNets belonging to the user in ONE_AUTH
195
        g, group --> 'mine' plus the VNets belonging to the groups
196
                        the user is member of
197
        uid      --> VNets of the user identified by this uid
198
        user     --> VNets of the user identified by the username
199

    
200
Information columns:
201

    
202
* NID       Network ID
203
* NAME      Name of the virtual network
204
* TYPE      Type of virtual network (0=ranged, 1=fixed)
205
* BRIDGE    Bridge associated to the virtual network
206
* LEASES    Number of leases used from this virtual network
207

    
208
EOT
209

    
210
    def text_commands
211
        COMMANDS_HELP
212
    end
213

    
214
    def text_command_name
215
        "onevnet"
216
    end
217

    
218
    def list_options
219
        table=ShowTable.new(ShowTableVN)
220
        table.print_help
221
    end
222

    
223
end
224

    
225
onevn_opts=OneVNParse.new([:list, :xml])
226
onevn_opts.parse(ARGV)
227
ops=onevn_opts.options
228

    
229
result=[false, "Unknown error"]
230

    
231
command=ARGV.shift
232

    
233
case command
234
when "create"
235
    check_parameters("create", 1)
236
    vn=OpenNebula::VirtualNetwork.new(
237
        OpenNebula::VirtualNetwork.build_xml, get_one_client)
238
    template=File.read(ARGV[0])
239
    result=vn.allocate(template)
240
    if !OpenNebula.is_error?(result)
241
        puts "ID: " + vn.id.to_s if ops[:verbose]
242
        exit 0
243
    end
244
    
245
when "show"
246
    check_parameters("show", 1)
247
    args=expand_args(ARGV)
248

    
249
    args.each do |param|
250
        vn_id=get_vn_id(param)
251
        vn=OpenNebula::VirtualNetwork.new_with_id(vn_id, get_one_client)
252
        result=vn.info
253
        if is_successful?(result)
254
            if !ops[:xml]
255
                str_h1="%-80s"
256
                str="%-10s: %-20s"
257
                print_header(str_h1,
258
                    "VIRTUAL NETWORK #{vn.id.to_s} INFORMATION",true)
259
        
260
                puts str % ["ID: ",vn.id.to_s]
261
                puts str % ["UID: ",vn["UID"]]
262
                if vn['PUBLIC'].to_i == 1 
263
                    public_str = "Y" 
264
                else 
265
                    public_str = "N" 
266
                end
267
                puts str % ["PUBLIC", public_str]
268
                puts
269
                print_header(str_h1,"VIRTUAL NETWORK TEMPLATE",false)
270
        
271
                puts vn.template_str(false)
272
        
273
                leases_str = vn.template_like_str('/VNET/LEASES', false)
274
  
275
                if !leases_str.empty?
276
                    puts
277
                    print_header(str_h1,"LEASES INFORMATION",false)
278
                    puts leases_str
279
                end
280
            else
281
                puts vn.to_xml(true)
282
            end
283
        end
284
    end
285
    
286
when "publish"
287
    check_parameters("publish", 1)
288
    vn_id=get_vn_id(ARGV[0])
289

    
290
    vn=OpenNebula::VirtualNetwork.new_with_id(vn_id, get_one_client)
291

    
292
    result=vn.publish
293
    if is_successful?(result)
294
        puts "VirtualNetwork published" if ops[:verbose]
295
    end
296
    
297
when "unpublish"
298
    check_parameters("unpublish", 1)
299
    vn_id=get_vn_id(ARGV[0])
300

    
301
    vn=OpenNebula::VirtualNetwork.new_with_id(vn_id, get_one_client)
302

    
303
    result=vn.unpublish
304
    if is_successful?(result)
305
        puts "VirtualNetwork unpublished" if ops[:verbose]
306
    end
307
    
308
when "delete"
309
    check_parameters("delete", 1)
310
    args=expand_args(ARGV)
311

    
312
    args.each do |param|
313
        vn_id=get_vn_id(param)
314
        vn=OpenNebula::VirtualNetwork.new(
315
            OpenNebula::VirtualNetwork.build_xml(vn_id), get_one_client)
316
        result=vn.delete
317
        if !OpenNebula.is_error?(result)
318
            puts "Virtual Network deleted" if ops[:verbose]
319
            break
320
        end
321
    end
322

    
323
when "addleases"
324
    check_parameters("addleases", 2)
325
    vn_id = get_vn_id(ARGV[0])
326

    
327
    vn = OpenNebula::VirtualNetwork.new_with_id(vn_id, get_one_client)
328

    
329
    result = vn.addleases(ARGV[1], ARGV[2])
330
    if is_successful?(result)
331
        puts "Leases added" if ops[:verbose]
332
    end
333

    
334
when "rmleases"
335
    check_parameters("rmleases", 2)
336
    vn_id = get_vn_id(ARGV[0])
337

    
338
    vn = OpenNebula::VirtualNetwork.new_with_id(vn_id, get_one_client)
339

    
340
    result = vn.rmleases(ARGV[1])
341
    if is_successful?(result)
342
        puts "Leases removed" if ops[:verbose]
343
    end
344

    
345
when "list"
346
    if ARGV[0]
347
        case ARGV[0]
348
        when "a", "all"
349
            filter_flag="-2"
350
        when "m", "mine"
351
            filter_flag="-3"
352
        when "g", "group"
353
            filter_flag="-1"
354
        else
355
            if !ARGV[0].match(/^[0123456789]+$/)
356
                filter_flag="-2"
357
                ops[:filter_flag]=ARGV[0]
358
            else
359
                filter_flag=ARGV[0]
360
            end
361
        end
362
    else
363
        filter_flag="-2"
364
    end
365

    
366
    if !ops[:xml]
367
        vnlist=VNShow.new(filter_flag)
368
        ops[:columns]=ops[:list] if ops[:list]
369
        result=vnlist.list_short(ops)
370
    else
371
        vnpool=OpenNebula::VirtualNetworkPool.new(get_one_client,
372
            filter_flag.to_i)
373
        vnpool.info
374
        puts vnpool.to_xml(true)
375
    end
376
    
377
else
378
    onevn_opts.print_help
379
    exit -1
380
end
381

    
382
if OpenNebula.is_error?(result)
383
    puts "Error: " + result.message
384
    exit -1
385
end