Statistics
| Branch: | Tag: | Revision:

one / src / cli / onevnet @ d4a7ff51

History | View | Annotate | Download (11.5 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs        #
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
$: << RUBY_LIB_LOCATION+"/cli"
29

    
30
require 'command_parser'
31
require 'one_helper/onevnet_helper'
32
require 'one_helper/onecluster_helper'
33

    
34
cmd=CommandParser::CmdParser.new(ARGV) do
35
    usage "`onevnet` <command> [<args>] [<options>]"
36
    version OpenNebulaHelper::ONE_VERSION
37

    
38
    helper = OneVNetHelper.new
39

    
40
    before_proc do
41
        helper.set_client(options)
42
    end
43

    
44
    ########################################################################
45
    # Global Options
46
    ########################################################################
47
    set :option, CommandParser::OPTIONS+OpenNebulaHelper::CLIENT_OPTIONS
48

    
49
    CREATE_OPTIONS = [OneClusterHelper::CLUSTER]
50
    STD_OPTIONS = CommandParser::OPTIONS + OpenNebulaHelper::CLIENT_OPTIONS
51

    
52
    ########################################################################
53
    # Formatters for arguments
54
    ########################################################################
55
    set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg|
56
        OpenNebulaHelper.rname_to_id(arg, "GROUP")
57
    end
58

    
59
    set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg|
60
        OpenNebulaHelper.rname_to_id(arg, "USER")
61
    end
62

    
63
    set :format, :vnetid, OneVNetHelper.to_id_desc do |arg|
64
        helper.to_id(arg)
65
    end
66

    
67
    set :format, :vnetid_list, OneVNetHelper.list_to_id_desc do |arg|
68
        helper.list_to_id(arg)
69
    end
70

    
71
    set :format, :filterflag, OneVNetHelper.filterflag_to_i_desc do |arg|
72
        helper.filterflag_to_i(arg)
73
    end
74

    
75
    set :format, :ar_id, "Integer" do |arg|
76
        format_int(arg)
77
    end
78

    
79
    ########################################################################
80
    # Commands
81
    ########################################################################
82

    
83
    create_desc = <<-EOT.unindent
84
        Creates a new Virtual Network from the given template file
85
    EOT
86

    
87
    command :create, create_desc, :file, :options=>CREATE_OPTIONS do
88
        cid = options[:cluster] || ClusterPool::NONE_CLUSTER_ID
89

    
90
        helper.create_resource(options) do |vn|
91
            begin
92
                template=File.read(args[0])
93
                vn.allocate(template, cid)
94
            rescue => e
95
                STDERR.puts e.message
96
                exit -1
97
            end
98
        end
99
    end
100

    
101
    delete_desc = <<-EOT.unindent
102
        Deletes the given Virtual Network
103
    EOT
104

    
105
    command :delete, delete_desc, [:range, :vnetid_list] do
106
        helper.perform_actions(args[0],options,"deleted") do |vn|
107
            vn.delete
108
        end
109
    end
110

    
111
    addar_desc = <<-EOT.unindent
112
        Adds an address range to the Virtual Network
113
    EOT
114

    
115
    command :addar, addar_desc, :vnetid, :options=>STD_OPTIONS + [OneVNetHelper::SIZE,
116
        OneVNetHelper::MAC, OneVNetHelper::IP, OneVNetHelper::IP6_GLOBAL,
117
        OneVNetHelper::IP6_ULA ] do
118
        helper.perform_action(args[0],options,"lease added") do |vn|
119
            ar = "AR = [ "
120

    
121
            if options[:ip]
122
                if options[:ip6_global] || options[:ip6_ula]
123
                    ar << "TYPE=\"IP4_6\""
124
                else
125
                    ar << "TYPE=\"IP4\""
126
                end
127
            else
128
                if options[:ip6_global] || options[:ip6_ula]
129
                    ar << "TYPE=\"IP6\""
130
                else
131
                    ar << "TYPE=\"ETHER\""
132
                end
133
            end
134

    
135
            if options[:size]
136
                ar << ", SIZE = " << options[:size]
137
            else
138
                STDERR.puts "Address range needs to specify size (-s size)"
139
                exit -1
140
            end
141

    
142
            ar << ", IP = " << options[:ip] if options[:ip]
143
            ar << ", MAC = " << options[:mac] if options[:mac]
144
            ar << ", GLOBAL_PREFIX = " << options[:ip6_global] if options[:ip6_global]
145
            ar << ", ULA_PREFIX = " << options[:ip6_ula] if options[:ip6_ula]
146

    
147
            ar << "]"
148

    
149
            vn.add_ar(ar)
150
        end
151
    end
152

    
153
    addleases_desc = <<-EOT.unindent
154
        (DEPRECATED, use addar) Adds a lease to the Virtual Network
155
    EOT
156

    
157
    command :addleases, addleases_desc, :vnetid, :ip, [:mac, nil] do
158
        helper.perform_action(args[0],options,"lease added") do |vn|
159
            vn.addleases(args[1], args[2])
160
        end
161
    end
162

    
163
    rmar_desc = <<-EOT.unindent
164
        Removes an address range from the Virtual Network
165
    EOT
166

    
167
    command :rmar, rmar_desc, :vnetid, :ar_id do
168
        helper.perform_action(args[0],options,"address range removed") do |vn|
169
            vn.rm_ar(args[1])
170
        end
171
    end
172

    
173
    rmleases_desc = <<-EOT.unindent
174
        (DEPRECATED, use rmar) Removes a lease from the Virtual Network
175
    EOT
176

    
177
    command :rmleases, rmleases_desc, :vnetid, :ip do
178
        helper.perform_action(args[0],options,"lease removed") do |vn|
179
            vn.rmleases(args[1])
180
        end
181
    end
182

    
183
    free_desc = <<-EOT.unindent
184
        Frees a reserved address range from the Virtual Network
185
    EOT
186

    
187
    command :free, free_desc, :vnetid, :ar_id do
188
        helper.perform_action(args[0],options,"address range freed") do |vn|
189
            vn.free(args[1])
190
        end
191
    end
192

    
193
    hold_desc = <<-EOT.unindent
194
        Holds a Virtual Network lease, marking it as used
195
    EOT
196

    
197
    command :hold, hold_desc, :vnetid, :ip,
198
        :options=>STD_OPTIONS + [OneVNetHelper::AR] do
199
        helper.perform_action(args[0],options,"lease on hold") do |vn|
200
            ar  = options[:ar_id] || -1
201

    
202
            vn.hold(args[1], ar)
203
        end
204
    end
205

    
206
    release_desc = <<-EOT.unindent
207
        Releases a Virtual Network lease on hold
208
    EOT
209

    
210
    command :release, release_desc, :vnetid, :ip,
211
        :options=>STD_OPTIONS + [OneVNetHelper::AR] do
212
        helper.perform_action(args[0],options,"lease released") do |vn|
213
            ar  = options[:ar_id] || -1
214

    
215
            vn.release(args[1], ar)
216
        end
217
    end
218

    
219
    reserve_desc = <<-EOT.unindent
220
        Reserve addresses from the Virtual Network. A new virtual network will
221
        be created to hold the reservation. Optionally the reservation can be
222
        put on an exisiting VNET, as long as it contains a valid reservation
223
        from the same VNET
224
    EOT
225

    
226
    command :reserve, reserve_desc, :vnetid, [:vnetid, nil],
227
        :options=>STD_OPTIONS + [OneVNetHelper::AR, OneVNetHelper::NAME,
228
            OneVNetHelper::SIZE, OneVNetHelper::MAC, OneVNetHelper::IP] do
229
        helper.perform_action(args[0],options,"reservation made") do |vn|
230
            rsize = options[:rsize] || -1
231
            rname = options[:rname] || -1
232

    
233
            addr = nil
234
            addr = options[:mac] if options[:mac]
235
            addr = options[:ip] if options[:ip]
236

    
237
            if rsize == -1
238
                STDERR.puts "Specify a size (-s size) for the reservation"
239
                exit -1
240
            end
241

    
242
            if rname == -1 && args[1].nil?
243
                STDERR.puts "Specify a name (-n name) for the reservation"
244
                exit -1
245
            end
246

    
247
            vn.reserve(rname, rsize, options[:ar_id], addr, args[1])
248
        end
249
    end
250

    
251

    
252
    chgrp_desc = <<-EOT.unindent
253
        Changes the Virtual Network group
254
    EOT
255

    
256
    command :chgrp, chgrp_desc,[:range, :vnetid_list], :groupid do
257
        helper.perform_actions(args[0],options,"Group changed") do |vn|
258
            vn.chown(-1, args[1].to_i)
259
        end
260
    end
261

    
262
    chown_desc = <<-EOT.unindent
263
        Changes the Virtual Network owner and group
264
    EOT
265

    
266
    command :chown, chown_desc, [:range, :vnetid_list], :userid,
267
            [:groupid,nil] do
268
        gid = args[2].nil? ? -1 : args[2].to_i
269
        helper.perform_actions(args[0],options,"Owner/Group changed") do |vn|
270
            vn.chown(args[1].to_i, gid)
271
        end
272
    end
273

    
274
    chmod_desc = <<-EOT.unindent
275
        Changes the Virtual Network permissions
276
    EOT
277

    
278
    command :chmod, chmod_desc, [:range, :vnetid_list], :octet do
279
        helper.perform_actions(args[0],options, "Permissions changed") do |vn|
280
            vn.chmod_octet(args[1])
281
        end
282
    end
283

    
284
    list_desc = <<-EOT.unindent
285
        Lists Virtual Networks in the pool
286
    EOT
287

    
288
    command :list, list_desc, [:filterflag, nil],
289
            :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS+
290
            [OpenNebulaHelper::DESCRIBE] do
291
        helper.list_pool(options, false, args[0])
292
    end
293

    
294
    show_desc = <<-EOT.unindent
295
        Shows information for the given Virtual Network
296
    EOT
297

    
298
    command :show, show_desc, :vnetid,
299
            :options=>OpenNebulaHelper::XML do
300
        helper.show_resource(args[0],options)
301
    end
302

    
303
    update_desc = <<-EOT.unindent
304
        Update the template contents. If a path is not provided the editor will
305
        be launched to modify the current content.
306
    EOT
307

    
308
    command :update, update_desc, :vnetid, [:file, nil],
309
    :options=>OpenNebulaHelper::APPEND do
310
        helper.perform_action(args[0],options,"modified") do |obj|
311
            if options[:append]
312
                str = OpenNebulaHelper.append_template(args[0], obj, args[1])
313
            else
314
                str = OpenNebulaHelper.update_template(args[0], obj, args[1])
315
            end
316

    
317
            obj.update(str, options[:append])
318
        end
319
    end
320

    
321
    update_ar_desc = <<-EOT.unindent
322
        Update Address Range variables. SIZE, IP, MAC and TYPE cannot be updated
323
    EOT
324

    
325
    command :updatear, update_ar_desc, :vnetid, :ar_id, [:file, nil] do
326
        helper.perform_action(args[0],options,"modified") do |obj|
327
            rc = obj.info
328

    
329
            if OpenNebula.is_error?(rc)
330
                puts rc.message
331
                exit -1
332
            end
333

    
334
            obj.delete_element("AR_POOL/AR[AR_ID!=#{args[1]}]")
335
            obj.delete_element("AR_POOL/AR/LEASES")
336
            obj.delete_element("AR_POOL/AR/USED_LEASES")
337

    
338
            if obj.template_like_str("AR_POOL").empty?
339
                puts "Address Range #{args[1]} does not exit for "<<
340
                    "Virtual Network #{args[0]}"
341
                exit -1
342
            end
343

    
344
            str = OpenNebulaHelper.update_template_helper(false, args[0], obj,
345
                args[2], "AR_POOL", false)
346

    
347
            obj.update_ar(str)
348
        end
349
    end
350

    
351
    rename_desc = <<-EOT.unindent
352
        Renames the Virtual Network
353
    EOT
354

    
355
    command :rename, rename_desc, :vnetid, :name do
356
        helper.perform_action(args[0],options,"renamed") do |o|
357
            o.rename(args[1])
358
        end
359
    end
360
end