Class: OpenNebulaDriver

Inherits:
ActionManager show all
Includes:
DriverExecHelper
Defined in:
lib/OpenNebulaDriver.rb

Overview

This class provides basic messaging and logging functionality to implement OpenNebula Drivers. A driver is a program that specialize the OpenNebula behavior by interfacing with specific infrastructure functionalities.

A Driver inherits this class and only has to provide methods for each action it wants to receive. The method must be associated with the action name through the register_action function

Direct Known Subclasses

VirtualMachineDriver

Constant Summary

Constants included from DriverExecHelper

DriverExecHelper::RESULT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DriverExecHelper

#action_command_line, #action_is_local?, #action_script_name, failed?, #get_info_from_execution, #initialize_helper, #log, #log_method, #read_configuration, #send_message

Methods inherited from ActionManager

#cancel_action, #register_action, #start_listener, #trigger_action

Constructor Details

#initialize(directory, options = {}) ⇒ OpenNebulaDriver

Initialize OpenNebulaDriver object

Parameters:

  • directory (String)

    path inside the remotes directory where the scripts are located

  • options (Hash) (defaults to: {})

    named options to change the object’s behaviour

Options Hash (options):

  • :concurrency (Number) — default: 10

    max number of threads

  • :threaded (Boolean) — default: true

    enables or disables threads

  • :retries (Number) — default: 0

    number of retries to copy scripts to the remote host

  • :local_actions (Hash) — default: {}

    hash with the actions executed locally and the name of the script if it differs from the default one. This hash can be constructed using parse_actions_list



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/OpenNebulaDriver.rb', line 52

def initialize(directory, options = {})
    @options={
        :concurrency => 10,
        :threaded => true,
        :retries => 0,
        :local_actions => {},
        :timeout => nil
    }.merge!(options)

    super(@options[:concurrency], @options[:threaded])

    @retries = @options[:retries]
    @timeout = @options[:timeout]

    # Set default values
    initialize_helper(directory, @options)

    register_action(:INIT, method('init'))
end

Instance Attribute Details

#local_scripts_base_pathString (readonly)

Returns Base path for scripts.

Returns:

  • (String)

    Base path for scripts



36
37
38
# File 'lib/OpenNebulaDriver.rb', line 36

def local_scripts_base_path
  @local_scripts_base_path
end

#local_scripts_pathString (readonly)

Returns Path for scripts.

Returns:

  • (String)

    Path for scripts



38
39
40
# File 'lib/OpenNebulaDriver.rb', line 38

def local_scripts_path
  @local_scripts_path
end

#remote_scripts_base_pathString (readonly)

Returns Base path for scripts.

Returns:

  • (String)

    Base path for scripts



36
37
38
# File 'lib/OpenNebulaDriver.rb', line 36

def remote_scripts_base_path
  @remote_scripts_base_path
end

#remote_scripts_pathString (readonly)

Returns Path for scripts.

Returns:

  • (String)

    Path for scripts



38
39
40
# File 'lib/OpenNebulaDriver.rb', line 38

def remote_scripts_path
  @remote_scripts_path
end

Class Method Details

.parse_actions_list(str) ⇒ Hash

This function parses a string with this form:

'deploy,shutdown,poll=poll_ganglia, cancel '

and returns a hash:

{"POLL"=>"poll_ganglia", "DEPLOY"=>nil, "SHUTDOWN"=>nil,
  "CANCEL"=>nil}

Parameters:

  • str (String)

    imput string to parse

Returns:

  • (Hash)

    parsed actions



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/OpenNebulaDriver.rb', line 162

def self.parse_actions_list(str)
    actions = {}
    str_splitted = str.split(/\s*,\s*/).map {|s| s.strip }

    str_splitted.each do |a|
        m=a.match(/([^=]+)(=(.*))?/)
        next unless m

        action=m[1].upcase

        if m[2]
            script=m[3]
            script.strip! if script
        else
            script=nil
        end

        actions[action]=script
    end

    actions
end

Instance Method Details

#do_action(parameters, id, host, aname, ops = {}) ⇒ Object

Calls remotes or local action checking the action name and

Parameters:

  • parameters (String)

    arguments passed to the script

  • id (Number, String)

    action identifier

  • host (String)

    hostname where the action is going to be executed

  • aname (String, Symbol)

    name of the action

  • ops (Hash) (defaults to: {})

    extra options for the command

Options Hash (ops):

  • :stdin (String)

    text to be writen to stdin

  • :script_name (String)

    default script name for the action, action name is used by defaults

  • :respond (Bool)

    if defined will send result to ONE core

  • :base64 (Bool)

    encode the information sent to ONE core

  • :zip (Bool)

    compress the information sent to ONE core



86
87
88
89
90
91
92
93
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
# File 'lib/OpenNebulaDriver.rb', line 86

def do_action(parameters, id, host, aname, ops = {})
    options = {
        :stdin => nil,
        :script_name => nil,
        :respond => true,
        :ssh_stream => nil,
        :base64 => false,
        :zip => false,
        :no_extra_params => false
    }.merge(ops)

    params = parameters
    params = "#{params} #{id} #{host}" unless options[:no_extra_params]
    command = action_command_line(aname, params, options[:script_name])

    # if options[:is_local] is not specified (nil)
    # we rely uniquely in actions_is_local?
    if action_is_local?(aname) or options[:is_local]
        stdin = Base64.strict_encode64(options[:stdin].to_s)
        execution = LocalCommand.run(command,
                                     log_method(id),
                                     stdin,
                                     @timeout)
    elsif options[:ssh_stream]
        if options[:stdin]
            cmdin = "cat << 'EOT' | #{command}"
            stdin = "#{options[:stdin]}\nEOT\n"
        else
            cmdin = command
            stdin = nil
        end

        execution = options[:ssh_stream].run(cmdin,
                                             stdin,
                                             command,
                                             @timeout)
    else
        execution = RemotesCommand.run(command,
                                       host,
                                       @remote_scripts_base_path,
                                       log_method(id),
                                       options[:stdin],
                                       @retries,
                                       @timeout)
    end

    result, info = get_info_from_execution(execution)

    if options[:respond]
        info = Zlib::Deflate.deflate(info, Zlib::BEST_COMPRESSION) if options[:zip]
        info = Base64.strict_encode64(info) if options[:base64]

        send_message(aname, result, id, info)
    end

    [result, info]
end

#start_driverObject

Start the driver. Reads from STDIN and executes methods associated with the messages



146
147
148
149
# File 'lib/OpenNebulaDriver.rb', line 146

def start_driver
    Thread.new { loop }
    start_listener
end