File: meta.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (204 lines) | stat: -rw-r--r-- 6,579 bytes parent folder | download | duplicates (4)
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
require "forwardable"
require "thread"

require "log4r"

require "vagrant/util/retryable"

require File.expand_path("../base", __FILE__)

module VagrantPlugins
  module ProviderVirtualBox
    module Driver
      class Meta < Base
        # This is raised if the VM is not found when initializing a driver
        # with a UUID.
        class VMNotFound < StandardError; end

        # We use forwardable to do all our driver forwarding
        extend Forwardable

        # We cache the read VirtualBox version here once we have one,
        # since during the execution of Vagrant, it likely doesn't change.
        @@version = nil
        @@version_lock = Mutex.new

        # The UUID of the virtual machine we represent
        attr_reader :uuid

        # The version of virtualbox that is running.
        attr_reader :version

        include Vagrant::Util::Retryable

        def initialize(uuid=nil)
          # Setup the base
          super()

          @logger = Log4r::Logger.new("vagrant::provider::virtualbox::meta")
          @uuid = uuid

          @@version_lock.synchronize do
            if !@@version
              # Read and assign the version of VirtualBox we know which
              # specific driver to instantiate.
              begin
                @@version = read_version
              rescue Vagrant::Errors::CommandUnavailable,
                Vagrant::Errors::CommandUnavailableWindows
                # This means that VirtualBox was not found, so we raise this
                # error here.
                raise Vagrant::Errors::VirtualBoxNotDetected
              end
            end
          end

          # Instantiate the proper version driver for VirtualBox
          @logger.debug("Finding driver for VirtualBox version: #{@@version}")
          driver_map   = {
            "4.0" => Version_4_0,
            "4.1" => Version_4_1,
            "4.2" => Version_4_2,
            "4.3" => Version_4_3,
            "5.0" => Version_5_0,
            "5.1" => Version_5_1,
            "5.2" => Version_5_2,
            "6.0" => Version_6_0,
            "6.1" => Version_6_1,
            "7.0" => Version_7_0,
          }

          if @@version.start_with?("4.2.14")
            # VirtualBox 4.2.14 just doesn't work with Vagrant, so show error
            raise Vagrant::Errors::VirtualBoxBrokenVersion040214
          end

          driver_klass = nil
          driver_map.each do |key, klass|
            if @@version.start_with?(key)
              driver_klass = klass
              break
            end
          end

          if !driver_klass
            supported_versions = driver_map.keys.sort.join(", ")
            raise Vagrant::Errors::VirtualBoxInvalidVersion,
              supported_versions: supported_versions
          end

          @logger.info("Using VirtualBox driver: #{driver_klass}")
          @driver = driver_klass.new(@uuid)
          @version = @@version

          if @uuid
            # Verify the VM exists, and if it doesn't, then don't worry
            # about it (mark the UUID as nil)
            raise VMNotFound if !@driver.vm_exists?(@uuid)
          end
        end

        def_delegators :@driver,
          :attach_disk,
          :clear_forwarded_ports,
          :clear_shared_folders,
          :clone_disk,
          :clonevm,
          :close_medium,
          :create_dhcp_server,
          :create_disk,
          :create_host_only_network,
          :create_snapshot,
          :delete,
          :delete_snapshot,
          :delete_unused_host_only_networks,
          :discard_saved_state,
          :enable_adapters,
          :execute_command,
          :export,
          :forward_ports,
          :get_port_and_device,
          :get_storage_controller,
          :halt,
          :import,
          :list_snapshots,
          :list_hdds,
          :read_forwarded_ports,
          :read_bridged_interfaces,
          :read_dhcp_servers,
          :read_guest_additions_version,
          :read_guest_ip,
          :read_guest_property,
          :read_host_only_interfaces,
          :read_mac_address,
          :read_mac_addresses,
          :read_machine_folder,
          :read_network_interfaces,
          :read_state,
          :read_storage_controllers,
          :read_used_ports,
          :read_vms,
          :reconfig_host_only,
          :remove_dhcp_server,
          :remove_disk,
          :resize_disk,
          :restore_snapshot,
          :resume,
          :set_mac_address,
          :set_name,
          :share_folders,
          :show_medium_info,
          :ssh_port,
          :start,
          :suspend,
          :vdi_to_vmdk,
          :verify!,
          :verify_image,
          :vm_exists?,
          :vmdk_to_vdi

        protected

        # This returns the version of VirtualBox that is running.
        #
        # @return [String]
        def read_version
          # The version string is usually in one of the following formats:
          #
          # * 4.1.8r1234
          # * 4.1.8r1234_OSE
          # * 4.1.8_MacPortsr1234
          #
          # Below accounts for all of these.

          # Note: We split this into multiple lines because apparently "".split("_")
          # is [], so we have to check for an empty array in between.
          output = ""
          retryable(on: Vagrant::Errors::VirtualBoxVersionEmpty, tries: 3, sleep: 1) do
            output = execute("--version")
            if output =~ /vboxdrv kernel module is not loaded/ ||
              output =~ /VirtualBox kernel modules are not loaded/i
              raise Vagrant::Errors::VirtualBoxKernelModuleNotLoaded
            elsif output =~ /Please install/
              # Check for installation incomplete warnings, for example:
              # "WARNING: The character device /dev/vboxdrv does not
              # exist. Please install the virtualbox-ose-dkms package and
              # the appropriate headers, most likely linux-headers-generic."
              raise Vagrant::Errors::VirtualBoxInstallIncomplete
            elsif output.chomp == ""
              # This seems to happen on Windows for uncertain reasons.
              # Raise an error otherwise the error is that they have an
              # incompatible version of VirtualBox which isn't true.
              raise Vagrant::Errors::VirtualBoxVersionEmpty,
                vboxmanage: @vboxmanage_path.to_s
            end
          end

          parts = output.split("_")
          return nil if parts.empty?
          parts[0].split("r")[0]
        end
      end
    end
  end
end