Class: OneVNetHelper

Inherits:
OpenNebulaHelper::OneHelper show all
Defined in:
lib/one_helper/onevnet_helper.rb

Overview

OneVnet Command Helper

Constant Summary collapse

AR =
{
    :name => 'address_range',
    :short => '-a ar_id',
    :large => '--address_range ar_id',
    :format => Integer,
    :description => 'ID of the address range'
}
SHOW_AR =
{
    :name => 'show_ar',
    :large => '--show-ar',
    :description => 'Show also AR templates'
}
MAC =
{
    :name => 'mac',
    :short => '-m mac',
    :large => '--mac mac',
    :format => String,
    :description => 'First MAC address in : notation'
}
IP =
{
    :name => 'ip',
    :short => '-i ip',
    :large => '--ip ip',
    :format => String,
    :description => 'First IP address in . notation'
}
IP6 =
{
    :name => 'ip6',
    :short => '-6 ip6',
    :large => '--ip6 ip6',
    :format => String,
    :description => 'First IPv6 address, in CIDR notation e.g. 2001::1/48'
}
SIZE =
{
    :name => 'size',
    :short => '-s size',
    :large => '--size size',
    :format => String,
    :description => 'Number of addresses'
}
IP6_GLOBAL =
{
    :name => 'ip6_global',
    :short => '-g ip6_pref',
    :large => '--ip6_global ip6_pref',
    :format => String,
    :description => 'IP6 global prefix'
}
IP6_ULA =
{
    :name => 'ip6_ula',
    :short => '-u ip6_pref',
    :large => '--ip6_ula ip6_pref',
    :format => String,
    :description => 'IP6 ula prefix'
}
NAME =
{
    :name => 'name',
    :short => '-n reservation name',
    :large => '--name reservation name',
    :format => String,
    :description => 'Name of the address reservation'
}
GATEWAY =

R_SIZE = {

    :name => "rsize",
    :short => "-s reservation size",
    :large => "--size reservation size",
    :format => String,
    :description => "Number of addresses to reserve"
}
{
    :name       => 'gateway',
    :large      => '--gateway ip',
    :format     => String,
    :description=> 'IP of the gateway'
}
NETMASK =
{
    :name       => 'netmask',
    :large      => '--netmask mask',
    :format     => String,
    :description=> 'Netmask in dot notation'
}
VN_MAD =
{
    :name       => 'vn_mad',
    :large      => '--vn_mad mad',
    :format     => String,
    :description=> 'Use this driver for the network'
}
VLAN_ID =
{
    :name       => 'vlanid',
    :large      => '--vlanid id',
    :format     => String,
    :description=> 'VLAN ID assigned'
}
ADDAR_OPTIONS =
[
    SIZE, MAC, IP, IP6, IP6_GLOBAL, IP6_ULA, GATEWAY, NETMASK, VN_MAD,
    VLAN_ID
]

Instance Attribute Summary

Attributes inherited from OpenNebulaHelper::OneHelper

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebulaHelper::OneHelper

#check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, #to_id, to_id_desc, #user_name

Constructor Details

This class inherits a constructor from OpenNebulaHelper::OneHelper

Class Method Details

.conf_fileObject



137
138
139
# File 'lib/one_helper/onevnet_helper.rb', line 137

def self.conf_file
    'onevnet.yaml'
end

.rnameObject



133
134
135
# File 'lib/one_helper/onevnet_helper.rb', line 133

def self.rname
    'VNET'
end

.state_to_str(id) ⇒ Object



141
142
143
144
145
# File 'lib/one_helper/onevnet_helper.rb', line 141

def self.state_to_str(id)
    id = id.to_i
    state_str = VirtualNetwork::VN_STATES[id]
    VirtualNetwork::SHORT_VN_STATES[state_str]
end

Instance Method Details

#check_orphansObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/one_helper/onevnet_helper.rb', line 232

def check_orphans
    orphans = []
    xpath = '/VMTEMPLATE_POOL/VMTEMPLATE/TEMPLATE/NIC'

    pool = factory_pool
    tmpl_pool = OpenNebula::TemplatePool.new(@client, -2)

    pool.info
    tmpl_pool.info

    pool.each do |img|
        attrs = { :id    => img['ID'],
                  :name  => img['NAME'],
                  :uname => img['UNAME'] }

        orphans << img['ID'] if check_orphan(tmpl_pool,
                                             xpath,
                                             'NETWORK', attrs)
    end

    orphans
end

#format_pool(options) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/one_helper/onevnet_helper.rb', line 147

def format_pool(options)
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :ID, 'ONE identifier for Virtual Network', :size=>4 do |d|
            d['ID']
        end

        column :USER, 'Username of the Virtual Network owner', :left,
               :size=>15 do |d|
            helper.user_name(d, options)
        end

        column :GROUP, 'Group of the Virtual Network', :left,
               :size=>12 do |d|
            helper.group_name(d, options)
        end

        column :NAME, 'Name of the Virtual Network', :left,
               :size=>19 do |d|
            d['NAME']
        end

        column :CLUSTERS, 'Cluster IDs', :left, :size=>10 do |d|
            OpenNebulaHelper.clusters_str(d['CLUSTERS']['ID']) rescue '-'
        end

        column :BRIDGE, 'Bridge associated to the Virtual Network', :left,
               :size=>8 do |d|
            d['BRIDGE']
        end

        column :STATE, 'State of the Virtual Network', :left,
               :size=>6 do |d|
            OneVNetHelper.state_to_str(d['STATE'])
        end

        column :LEASES, "Number of this Virtual Network's given leases",
               :size=>6 do |d|
            d['USED_LEASES']
        end

        column :UPDATED, 'Number of VMs with updated VN attributes', :size=>4 do |d|
            if d['UPDATED_VMS']['ID'].nil?
                '0'
            else
                [d['UPDATED_VMS']['ID']].flatten.size
            end
        end

        column :OUTDATED, 'Number of VMs with outdated VN attributes', :size=>4 do |d|
            if d['OUTDATED_VMS']['ID'].nil?
                '0'
            else
                [d['OUTDATED_VMS']['ID']].flatten.size
            end
        end

        column :ERROR, 'Number of VMs that failed to update VN attributes', :size=>4 do |d|
            if d['ERROR_VMS']['ID'].nil?
                '0'
            else
                [d['ERROR_VMS']['ID']].flatten.size
            end
        end

        default :ID, :USER, :GROUP, :NAME, :CLUSTERS, :BRIDGE, :STATE,
                :LEASES, :OUTDATED, :ERROR
    end
end

#show_ar(vn, ar_id) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/one_helper/onevnet_helper.rb', line 218

def show_ar(vn, ar_id)
    CLIHelper.print_header(format('%-80s', "TEMPLATE FOR AR #{ar_id}"),
                           false)

    begin
        template = vn.template_like_str("AR_POOL/AR[AR_ID=#{ar_id}]")
    rescue StandardError
        STDERR.puts "Can not get template for AR #{ar_id}"
        return
    end

    puts template
end

#update_ar(vnet_id, ar_id, file, options) ⇒ Object

Update VNet address range

Parameters:

  • vnet_id (Intenger)

    Virtual Network ID

  • ar_id (Intenger)

    Address Range ID

  • file (String)

    Path to file to read

  • options (Hash)

    User CLI options



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/one_helper/onevnet_helper.rb', line 261

def update_ar(vnet_id, ar_id, file, options)
    perform_action(vnet_id, options, 'AR updated') do |obj|
        rc = obj.info

        if OpenNebula.is_error?(rc)
            STDERR.puts rc.message
            exit(-1)
        end

        obj.delete_element("AR_POOL/AR[AR_ID!=#{ar_id}]")
        obj.delete_element('AR_POOL/AR/LEASES')
        obj.delete_element('AR_POOL/AR/USED_LEASES')
        obj.delete_element('AR_POOL/AR/MAC_END')
        obj.delete_element('AR_POOL/AR/IP_END')
        obj.delete_element('AR_POOL/AR/IP6_ULA')
        obj.delete_element('AR_POOL/AR/IP6_ULA_END')
        obj.delete_element('AR_POOL/AR/IP6_GLOBAL')
        obj.delete_element('AR_POOL/AR/IP6_GLOBAL_END')

        if obj.template_like_str('AR_POOL').empty?
            STDERR.puts "Address Range #{ar_id} does not exist for " \
                        "Virtual Network #{vnet_id}"
            exit(-1)
        end

        xpath = "AR_POOL/AR[AR_ID=#{ar_id}]"

        if options[:append]
            str = OpenNebulaHelper.append_template(vnet_id,
                                                   obj,
                                                   file,
                                                   xpath)
        else
            str = OpenNebulaHelper.update_template(vnet_id,
                                                   obj,
                                                   file,
                                                   xpath)
        end

        if options[:append]
            # Insert element in current template
            parts = obj.template_like_str('AR_POOL').split("\n")

            # Insert it in second position, OpenNebula will sort it
            parts.insert(1, "#{str.strip},")

            parts = parts.join("\n")
            str   = parts
        else
            # Use the information from user
            unless str.gsub(' ', '').match(/AR=\[/)
                str = "AR=[\n#{str.split("\n").join(",\n")}]"
            end
        end

        obj.update_ar(str)
    end
end