module OpenNebula

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

————————————————————————– # Copyright 2002-2023, 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. #

Constants

DEFAULT_POOL_PAGE_SIZE
NOKOGIRI
OX
REXML_FORMATTERS
VERSION

OpenNebula version

Public Class Methods

arg_host(arg) click to toggle source

Gets the host from an argument

# File lib/scripts_common.rb, line 61
def self.arg_host(arg)
    result = arg.match("^\([^:]*\):.*$")

    return result[1] if result
end
arg_path(arg) click to toggle source
# File lib/scripts_common.rb, line 67
def self.arg_path(arg)
    result = arg.match('^[^:]*:(.*)$')

    return result[1] if result
end
decrypt(res, token) click to toggle source

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

# 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) click to toggle source

receive a object key => value format returns hashed values

# 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) click to toggle source

This function is used to pass error message to the mad

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source
# File lib/scripts_common.rb, line 56
def self.is_disk?(arg)
    arg.match("disk\.[0-9]+$")
end
is_error?(value) click to toggle source

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

# File lib/opennebula/error.rb, line 59
def self.is_error?(value)
    value.class==OpenNebula::Error
end
log(message)
Alias for: log_info
log_debug(message) click to toggle source

Logs a debug message

# File lib/scripts_common.rb, line 42
def self.log_debug(message)
    log_function("DEBUG", message)
end
log_error(message) click to toggle source

Logs an error message

# File lib/scripts_common.rb, line 37
def self.log_error(message)
    log_function("ERROR", message)
end
log_function(severity, message) click to toggle source

Generic log function

# File lib/scripts_common.rb, line 22
def self.log_function(severity, message)
    STDERR.puts "#{severity}: #{File.basename $0}: #{message}"
end
log_info(message) click to toggle source

Logs an info message

# File lib/scripts_common.rb, line 27
def self.log_info(message)
    log_function("INFO", message)
end
Also aliased as: log
log_warning(message) click to toggle source

Logs an info message

# File lib/scripts_common.rb, line 32
def self.log_warning(message)
    log_function('WARNING', message)
end
pool_page_size() click to toggle source
# File lib/opennebula/client.rb, line 24
def self.pool_page_size
    @@pool_page_size
end
process_monitoring(xmldoc, oid, xpath_expressions) click to toggle source

Processes the monitoring data in XML returned by OpenNebula

@param [XMLElement] xmldoc monitoring data returned by OpenNebula

monitorization timestamp

@param [Integer] oid Id of the object to process @param [Array<String>] xpath_expressions Elements to retrieve.

@return [Hash<String, Array<Array<int>>, OpenNebula::Error] Hash with

the requested xpath expressions, and an Array of [timestamp, value].
# File lib/opennebula/pool_element.rb, line 265
def self.process_monitoring(xmldoc, oid, xpath_expressions)
    hash = {}
    timestamps = xmldoc.retrieve_elements(
        "/MONITORING_DATA/MONITORING[ID=#{oid}]/TIMESTAMP"
    )

    xpath_expressions.each do |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
    end

    hash
end
send_to_monitor(msg_type, result, oid, data) click to toggle source
# 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