Module: OpenNebula

Included in:
MarketPlaceAppExt
Defined in:
lib/models/service.rb,
lib/opennebula.rb,
lib/models/role.rb,
lib/opennebula/acl.rb,
lib/opennebula/vdc.rb,
lib/scripts_common.rb,
lib/opennebula/hook.rb,
lib/opennebula/host.rb,
lib/opennebula/pool.rb,
lib/opennebula/user.rb,
lib/opennebula/zone.rb,
lib/opennebula/error.rb,
lib/opennebula/group.rb,
lib/opennebula/image.rb,
lib/opennebula/utils.rb,
lib/opennebula/client.rb,
lib/opennebula/system.rb,
lib/opennebula/cluster.rb,
lib/opennebula/acl_pool.rb,
lib/opennebula/document.rb,
lib/opennebula/hook_log.rb,
lib/opennebula/ssh_auth.rb,
lib/opennebula/template.rb,
lib/opennebula/vdc_pool.rb,
lib/opennebula/vm_group.rb,
lib/opennebula/wait_ext.rb,
lib/opennebula/wait_ext.rb,
lib/opennebula/xml_pool.rb,
lib/opennebula/datastore.rb,
lib/opennebula/hook_pool.rb,
lib/opennebula/host_pool.rb,
lib/opennebula/ldap_auth.rb,
lib/opennebula/user_pool.rb,
lib/opennebula/x509_auth.rb,
lib/opennebula/xml_utils.rb,
lib/opennebula/zone_pool.rb,
lib/opennebula/group_pool.rb,
lib/opennebula/image_pool.rb,
lib/opennebula/vntemplate.rb,
lib/opennebula/marketplace.rb,
lib/opennebula/xml_element.rb,
lib/opennebula/cluster_pool.rb,
lib/opennebula/pool_element.rb,
lib/opennebula/document_json.rb,
lib/opennebula/document_pool.rb,
lib/opennebula/template_pool.rb,
lib/opennebula/vm_group_pool.rb,
lib/opennebula/datastore_pool.rb,
lib/opennebula/marketplaceapp.rb,
lib/opennebula/security_group.rb,
lib/opennebula/virtual_router.rb,
lib/opennebula/virtual_machine.rb,
lib/opennebula/virtual_network.rb,
lib/opennebula/vntemplate_pool.rb,
lib/opennebula/marketplace_pool.rb,
lib/opennebula/server_x509_auth.rb,
lib/opennebula/flow/service_pool.rb,
lib/opennebula/document_pool_json.rb,
lib/opennebula/server_cipher_auth.rb,
lib/opennebula/marketplaceapp_pool.rb,
lib/opennebula/security_group_pool.rb,
lib/opennebula/virtual_router_pool.rb,
lib/opennebula/virtual_machine_pool.rb,
lib/opennebula/virtual_network_pool.rb,
lib/opennebula/flow/service_template.rb,
lib/opennebula/flow/service_template_pool.rb

Overview

————————————————————————– # Copyright 2002-2022, OpenNebula Project, OpenNebula Systems #

#

Licensed under the Apache License, Version 2.0 (the “License”); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at #

#

www.apache.org/licenses/LICENSE-2.0 #

#

Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an “AS IS” BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ————————————————————————— #

Defined Under Namespace

Modules: LockableExt, MarketPlaceAppExt, ParsePoolBase, ServiceTemplateExt, TemplateExt, VirtualMachineExt, WaitExt, WaitExtEvent, WaitExtPolling Classes: Acl, AclPool, Client, Cluster, ClusterPool, Datastore, DatastorePool, Document, DocumentJSON, DocumentPool, DocumentPoolJSON, Error, Group, GroupPool, Hook, HookLog, HookPool, Host, HostPool, Image, ImagePool, LdapAuth, MarketPlace, MarketPlaceApp, MarketPlaceAppPool, MarketPlacePool, NokogiriStreamParser, OpenNebulaServicePool, OxStreamParser, ParsePoolSax, ParsePoolSaxBase, Pool, PoolElement, Role, SecurityGroup, SecurityGroupPool, ServerCipherAuth, ServerX509Auth, Service, ServicePool, ServiceTemplate, ServiceTemplatePool, SshAuth, System, Template, TemplatePool, User, UserPool, VMGroup, VMGroupPool, VNTemplate, VNTemplatePool, Vdc, VdcPool, VirtualMachine, VirtualMachinePool, VirtualNetwork, VirtualNetworkPool, VirtualRouter, VirtualRouterPool, X509Auth, XMLElement, XMLPool, Zone, ZonePool

Constant Summary collapse

VERSION =

OpenNebula version

'6.4.0'
DEFAULT_POOL_PAGE_SIZE =
200
@@pool_page_size =
DEFAULT_POOL_PAGE_SIZE

Class Method Summary collapse

Class Method Details

.arg_host(arg) ⇒ Object

Gets the host from an argument



61
62
63
64
65
# File 'lib/scripts_common.rb', line 61

def self.arg_host(arg)
    result = arg.match("^\([^:]*\):.*$")

    return result[1] if result
end

.arg_path(arg) ⇒ Object



67
68
69
70
71
# File 'lib/scripts_common.rb', line 67

def self.arg_path(arg)
    result = arg.match('^[^:]*:(.*)$')

    return result[1] if result
end

.decrypt(res, token) ⇒ Object

receive hashed values (res) with a token returns original values



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/opennebula/utils.rb', line 37

def self.decrypt(res, token)
    opts = {}

    res.each do |key, encrypted_value|
        decipher = OpenSSL::Cipher::AES.new(256,:CBC)
        decipher.decrypt
        decipher.key = token[0..31]
        plain = decipher.update(Base64::decode64(encrypted_value)) + decipher.final
        opts[key] = plain
    end
    return opts
end

.encrypt(opts, token) ⇒ Object

receive a object key => value format returns hashed values



24
25
26
27
28
29
30
31
32
33
# File 'lib/opennebula/utils.rb', line 24

def self.encrypt(opts, token)
    res = {}
    opts.each do |key, value|
        cipher = OpenSSL::Cipher::AES.new(256,:CBC)
        cipher.encrypt.key = token[0..31]
        encrypted = cipher.update(value) + cipher.final
        res[key] = Base64::encode64(encrypted).gsub("\n", "")
    end
    return res
end

.error_message(message) ⇒ Object

This function is used to pass error message to the mad



52
53
54
# File 'lib/scripts_common.rb', line 52

def self.error_message(message)
    STDERR.puts message
end

.exec_and_log(command, message = nil, allowed_return_code = 0) ⇒ Object

Executes a command, if it fails returns error message and exits If a second parameter is present it is used as the error message when the command fails



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scripts_common.rb', line 76

def self.exec_and_log(command, message=nil, allowed_return_code=0)
    command = command.shellsplit.shelljoin # escape
    output=`#{command} 2>&1 1>/dev/null`
    code=$?.exitstatus

    if code!=0 && code!=allowed_return_code
        log_error "Command \"#{command}\" failed."
        log_error output
        if !message
            error_message output
        else
            error_message message
        end
        exit code
    end
    log "Executed \"#{command}\"."
end

.handle_driver_exception(action, ex, host, did = nil, id = nil, file = nil) ⇒ Object

>> /var/log/one/oned.log



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opennebula/utils.rb', line 51

def self.handle_driver_exception(action, ex, host, did = nil, id = nil, file = nil)

    file    ||= ""
    id      ||= ""
    did     ||= ""
    OpenNebula::log_error(action + " of VM #{id} #{did} on host #{host} #{file} "+
                "due to \"#{ex.message}\"" +
                "\n********* STACK TRACE *********\n" +
                "\t#{ex.backtrace.join("\n\t")}" +
                "\n*******************************\n")

    OpenNebula.error_message("There is a problem: #{ex.message}")
    exit (-1)
end

.is_disk?(arg) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/scripts_common.rb', line 56

def self.is_disk?(arg)
    arg.match("disk\.[0-9]+$")
end

.is_error?(value) ⇒ Boolean

Returns true if the object returned by a method of the OpenNebula library is an Error

Returns:

  • (Boolean)


59
60
61
# File 'lib/opennebula/error.rb', line 59

def self.is_error?(value)
    value.class==OpenNebula::Error
end

.log_debug(message) ⇒ Object

Logs a debug message



42
43
44
# File 'lib/scripts_common.rb', line 42

def self.log_debug(message)
    log_function("DEBUG", message)
end

.log_error(message) ⇒ Object

Logs an error message



37
38
39
# File 'lib/scripts_common.rb', line 37

def self.log_error(message)
    log_function("ERROR", message)
end

.log_function(severity, message) ⇒ Object

Generic log function



22
23
24
# File 'lib/scripts_common.rb', line 22

def self.log_function(severity, message)
    STDERR.puts "#{severity}: #{File.basename $0}: #{message}"
end

.log_info(message) ⇒ Object Also known as: log

Logs an info message



27
28
29
# File 'lib/scripts_common.rb', line 27

def self.log_info(message)
    log_function("INFO", message)
end

.log_warning(message) ⇒ Object

Logs an info message



32
33
34
# File 'lib/scripts_common.rb', line 32

def self.log_warning(message)
    log_function('WARNING', message)
end

.pool_page_sizeObject



24
25
26
# File 'lib/opennebula/client.rb', line 24

def self.pool_page_size
    @@pool_page_size
end

.process_monitoring(xmldoc, oid, xpath_expressions) ⇒ Hash<String, Array<Array<int>>, OpenNebula::Error] Hash with the requested xpath expressions, and an Array of [timestamp, value].

Processes the monitoring data in XML returned by OpenNebula

Parameters:

  • xmldoc (XMLElement)

    monitoring data returned by OpenNebula monitorization timestamp

  • oid (Integer)

    Id of the object to process

  • xpath_expressions (Array<String>)

    Elements to retrieve.

Returns:

  • (Hash<String, Array<Array<int>>, OpenNebula::Error] Hash with the requested xpath expressions, and an Array of [timestamp, value].)

    Hash<String, Array<Array<int>>, OpenNebula::Error] Hash with the requested xpath expressions, and an Array of [timestamp, value].



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/opennebula/pool_element.rb', line 266

def self.process_monitoring(xmldoc, oid, xpath_expressions)
    hash = {}
    timestamps = xmldoc.retrieve_elements(
        "/MONITORING_DATA/MONITORING[ID=#{oid}]/TIMESTAMP")

    xpath_expressions.each { |xpath|
        xpath_values = xmldoc.retrieve_elements(
            "/MONITORING_DATA/MONITORING[ID=#{oid}]/#{xpath}")

        if ( xpath_values.nil? )
            hash[xpath] = []
        else
            hash[xpath] = timestamps.zip(xpath_values)
        end
    }

    return hash
end

.send_to_monitor(msg_type, result, oid, data) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
# File 'lib/scripts_common.rb', line 94

def self.send_to_monitor(msg_type, result, oid, data)
    # Read monitord.conf
    one_location = ENV['ONE_LOCATION']

    if !one_location
        file_dir = '/etc/one/'
    else
        file_dir = one_location + '/etc/'
    end

    file_name = 'monitord.conf'

    begin
        # Suppress augeas require warning message
        $VERBOSE = nil

        require 'augeas'

        aug = Augeas.create(:no_modl_autoload => true,
            :no_load          => true,
            :root             => file_dir,
            :loadpath         => file_name)

        aug.clear_transforms
        aug.transform(:lens => 'Oned.lns', :incl => file_name)
        aug.context = "/files/#{file_name}"
        aug.load

        mon_address = aug.get('NETWORK/MONITOR_ADDRESS')
        mon_port    = aug.get('NETWORK/PORT')
        mon_key     = aug.get('NETWORK/PUBKEY').tr('"', '')

        mon_address = "127.0.0.1" if mon_address.include? "auto"
    rescue LoadError
        mon_address = "127.0.0.1"
        mon_port    = 4124
    end

    # Encrypt
    if mon_key && !mon_key.empty?
        block_size = mon_key.n.num_bytes - 11

        edata = ''
        index = 0

        loop do
            break if index >= data.length

            edata << mon_key.public_encrypt(data[index, block_size])

            index += block_size
        end

        data = edata
    end

    # Send data
    begin
        require 'base64'
        require 'zlib'
        require 'socket'

        zdata  = Zlib::Deflate.deflate(data, Zlib::BEST_COMPRESSION)
        data64 = Base64.strict_encode64(zdata)

        if (result == "SUCCESS" || result == "0")
            result = "SUCCESS"
        else
            result = "FAILURE"
        end

        if Integer(oid) == -1
            ts = 0
        else
            ts = Time.now.to_i
        end

        msg = "#{msg_type} #{result} #{oid} #{ts} #{data64}"

        socket_udp = UDPSocket.new()
        socket_udp.send(msg, 0, mon_address, mon_port)
    rescue LoadError
        STDERR.puts('Unable to send data to Monitor Daemon')
    end
end