This file is indexed.

/usr/lib/one/ruby/VirtualMachineDriver.rb is in opennebula 4.12.3+dfsg-3.1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 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
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# -------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs        #
#                                                                            #
# 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                                                   #
#                                                                            #
# http://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.                                             #
#--------------------------------------------------------------------------- #
require "OpenNebulaDriver"
require "CommandManager"
require 'base64'
require 'rexml/document'

# 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
class VirtualMachineDriver < OpenNebulaDriver

    # Virtual Machine Driver Protocol constants
    ACTION = {
        :deploy      => "DEPLOY",
        :shutdown    => "SHUTDOWN",
        :reboot      => "REBOOT",
        :reset       => "RESET",
        :cancel      => "CANCEL",
        :save        => "SAVE",
        :restore     => "RESTORE",
        :migrate     => "MIGRATE",
        :poll        => "POLL",
        :log         => "LOG",
        :attach_disk => "ATTACHDISK",
        :detach_disk => "DETACHDISK",
        :snapshot_create => "SNAPSHOTCREATE",
        :snapshot_revert => "SNAPSHOTREVERT",
        :snapshot_delete => "SNAPSHOTDELETE",
        :cleanup         => "CLEANUP",
        :attach_nic  => "ATTACHNIC",
        :detach_nic  => "DETACHNIC"
    }

    POLL_ATTRIBUTE = {
        :usedmemory => "USEDMEMORY",
        :usedcpu    => "USEDCPU",
        :nettx      => "NETTX",
        :netrx      => "NETRX",
        :state      => "STATE"
    }

    VM_STATE = {
        :active  => 'a',
        :paused  => 'p',
        :error   => 'e',
        :deleted => 'd',
        :unknown => '-'
    }

    HOST_ARG = 1

    # Register default actions for the protocol.
    #
    # @param [String] directory path inside remotes path where the scripts
    #   reside
    # @param [Hash] options options for OpenNebula driver (check the available
    #   options in {OpenNebulaDriver#initialize})
    # @option options [Boolean] :threaded (true) enables or disables threads
    def initialize(directory, options={})
        @options={
            :threaded    => true,
            :single_host => true
        }.merge!(options)

        super(directory, @options)

        @hosts   = Array.new

        register_action(ACTION[:deploy].to_sym,          method("deploy"))
        register_action(ACTION[:shutdown].to_sym,        method("shutdown"))
        register_action(ACTION[:reboot].to_sym,          method("reboot"))
        register_action(ACTION[:reset].to_sym,           method("reset"))
        register_action(ACTION[:cancel].to_sym,          method("cancel"))
        register_action(ACTION[:save].to_sym,            method("save"))
        register_action(ACTION[:restore].to_sym,         method("restore"))
        register_action(ACTION[:migrate].to_sym,         method("migrate"))
        register_action(ACTION[:poll].to_sym,            method("poll"))
        register_action(ACTION[:attach_disk].to_sym,     method("attach_disk"))
        register_action(ACTION[:detach_disk].to_sym,     method("detach_disk"))
        register_action(ACTION[:snapshot_create].to_sym,
                        method("snapshot_create"))
        register_action(ACTION[:snapshot_revert].to_sym,
                        method("snapshot_revert"))
        register_action(ACTION[:snapshot_delete].to_sym,
                        method("snapshot_delete"))
        register_action(ACTION[:cleanup].to_sym,         method("cleanup"))
        register_action(ACTION[:attach_nic].to_sym,  method("attach_nic"))
        register_action(ACTION[:detach_nic].to_sym,  method("detach_nic"))
    end

    # Decodes the encoded XML driver message received from the core
    #
    # @param [String] drv_message the driver message
    # @return [REXML::Element] the root element of the decoded XML message
    def decode(drv_message)
        message = Base64.decode64(drv_message)
        xml_doc = REXML::Document.new(message)

        xml_doc.root
    end

    # Execute a command associated to an action and id in a remote host.
    def remotes_action(command, id, host, action, remote_dir, std_in=nil)
        super(command,id,host,ACTION[action],remote_dir,std_in)
    end

    # Execute a command associated to an action and id on localhost
    def local_action(command, id, action)
        super(command,id,ACTION[action])
    end

    # Virtual Machine Manager Protocol Actions (generic implementation)
    def deploy(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:deploy],RESULT[:failure],id,error)
    end

    def shutdown(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:shutdown],RESULT[:failure],id,error)
    end

    def reboot(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:reboot],RESULT[:failure],id,error)
    end

    def reset(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:reset],RESULT[:failure],id,error)
    end

    def cancel(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:cancel],RESULT[:failure],id,error)
    end

    def save(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:save],RESULT[:failure],id,error)
    end

    def restore(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:restore],RESULT[:failure],id,error)
    end

    def migrate(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:migrate],RESULT[:failure],id,error)
    end

    def poll(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:poll],RESULT[:failure],id,error)
    end

    def attach_disk(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:attach_disk],RESULT[:failure],id,error)
    end

    def detach_disk(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:detach_disk],RESULT[:failure],id,error)
    end

    def attach_nic(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:attach_nic],RESULT[:failure],id,error)
    end

    def detach_nic(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:detach_nic],RESULT[:failure],id,error)
    end

    def snapshot_create(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:snapshot_create],RESULT[:failure],id,error)
    end

    def snapshot_revert(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:snapshot_revert],RESULT[:failure],id,error)
    end

    def snapshot_delete(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:snapshot_delete],RESULT[:failure],id,error)
    end

    def cleanup(id, drv_message)
        error = "Action not implemented by driver #{self.class}"
        send_message(ACTION[:cleanup],RESULT[:failure],id,error)
    end

private

    # Interface to handle the pending events from the ActionManager Interface
    def delete_running_action(action_id)
        if @options[:single_host]
            delete_running_action_single_host(action_id)
        else
            super(action_id)
        end
    end

    def delete_running_action_single_host(action_id)
        action=@action_running[action_id]
        if action
            @hosts.delete(action[:host])
            @action_running.delete(action_id)
        end
    end

    def get_first_runable
        if @options[:single_host]
            get_first_runable_single_host
        else
            super
        end
    end

    def get_first_runable_single_host
        action_index=nil
        @action_queue.each_with_index do |action, index|
            if !action.keys.include?(:host)
                if action[:args].length == 2
                    begin
                        xml=decode(action[:args].last)
                        host=xml.elements['HOST']
                        action[:host]=host.text if host
                    rescue
                        action[:host]=nil
                    end
                else
                    action[:host]=nil
                end
            end

            if action.keys.include?(:host) && action[:host]
                if !@hosts.include?(action[:host])
                    action_index=index
                    break
                end
           else
                action_index=index
                break
           end
        end

        return action_index
    end

    def get_runable_action
        if @options[:single_host]
            get_runable_action_single_host
        else
            super
        end
    end

    def get_runable_action_single_host
        action_index=get_first_runable

        if action_index
            action=@action_queue[action_index]
        else
            action=nil
        end

        if action
            @hosts << action[:host] if action[:host]
            @action_queue.delete_at(action_index)
        end

        return action
    end

    def empty_queue
        if @options[:single_host]
            empty_queue_single_host
        else
            super
        end
    end

    def empty_queue_single_host
        get_first_runable==nil
    end
end

################################################################
################################################################

if __FILE__ == $0

    class TemplateDriver < VirtualMachineDriver
        def initialize
            super('vmm/dummy',
                :concurrency => 15,
                :threaded => true)
        end

        def deploy(id, host, remote_dfile, not_used)
            #MUST return deploy_id if deployment was successfull
            deploy_id = "-"
            send_message(ACTION[:deploy],RESULT[:success],id,deploy_id)
        end

        def shutdown(id, host, deploy_id, not_used)
            send_message(ACTION[:shutdown],RESULT[:success],id)
        end

        def cancel(id, host, deploy_id, not_used)
            send_message(ACTION[:cancel],RESULT[:success],id)
        end

        def save(id, host, deploy_id, file)
            send_message(ACTION[:save],RESULT[:success],id)
        end

        def restore(id, host, deploy_id , file)
            send_message(ACTION[:restore],RESULT[:success],id)
        end

        def migrate(id, host, deploy_id, dest_host)
            send_message(ACTION[:migrate],RESULT[:success],id)
        end

        def poll(id, host, deploy_id, not_used)
            # monitor_info: string in the form "VAR=VAL VAR=VAL ... VAR=VAL"
            # known VAR are in POLL_ATTRIBUTES. VM states VM_STATES
            monitor_info = "#{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:active]} " \
                           "#{POLL_ATTRIBUTE[:nettx]}=12345"

            send_message(ACTION[:poll],RESULT[:success],id,monitor_info)
        end
    end

    sd = TemplateDriver.new
    sd.start_driver
end