Class: VagrantPlugins::Utm::Provider
- Inherits:
-
Object
- Object
- VagrantPlugins::Utm::Provider
- Defined in:
- lib/vagrant_utm/provider.rb
Overview
Provider that is responsible for managing the virtual machine and exposing it to Vagrant.
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
The driver for this provider.
Class Method Summary collapse
- .installed? ⇒ Boolean
-
.usable?(raise_error = false) ⇒ Boolean
Check if the provider is usable.
Instance Method Summary collapse
- #action(name) ⇒ Object
-
#initialize(machine) ⇒ Provider
constructor
Initialize the provider with given machine.
-
#machine_id_changed ⇒ Object
If the machine ID changed, then we need to rebuild our underlying driver.
-
#ssh_info ⇒ Object
Returns the SSH info for accessing the UTM VM.
-
#state ⇒ Symbol
Return the state of UTM virtual machine by actually querying utmctl.
-
#to_s ⇒ String
Returns a human-friendly string version of this provider which includes the machine’s ID that this provider represents, if it has one.
Constructor Details
#initialize(machine) ⇒ Provider
Initialize the provider with given machine.
41 42 43 44 45 46 47 48 49 |
# File 'lib/vagrant_utm/provider.rb', line 41 def initialize(machine) super @logger = Log4r::Logger.new("vagrant::provider::utm") @machine = machine # This method will load in our driver, so we call it now to # initialize it. machine_id_changed end |
Instance Attribute Details
#driver ⇒ Object (readonly)
The driver for this provider.
10 11 12 |
# File 'lib/vagrant_utm/provider.rb', line 10 def driver @driver end |
Class Method Details
.installed? ⇒ Boolean
12 13 14 15 16 17 18 19 20 |
# File 'lib/vagrant_utm/provider.rb', line 12 def self.installed? Driver::Meta.new true rescue Errors::UtmInvalidVersion, Errors::UtmNotDetected raise if raise_error false end |
.usable?(raise_error = false) ⇒ Boolean
Check if the provider is usable. rubocop:disable Style/OptionalBooleanParameter
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vagrant_utm/provider.rb', line 24 def self.usable?(raise_error = false) raise Errors::MacOSRequired unless Vagrant::Util::Platform.darwin? # Instantiate the driver, which will determine the UTM # version and all that, which checks for UTM being present. Driver::Meta.new true rescue Errors::MacOSRequired, Errors::UtmInvalidVersion, Errors::UtmNotDetected raise if raise_error false end |
Instance Method Details
#action(name) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/vagrant_utm/provider.rb', line 52 def action(name) # Attempt to get the action method from the Action class if it # exists, otherwise return nil to show that we don't support the # given action. action_method = "action_#{name}" return Action.send(action_method) if Action.respond_to?(action_method) nil end |
#machine_id_changed ⇒ Object
If the machine ID changed, then we need to rebuild our underlying driver.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/vagrant_utm/provider.rb', line 64 def machine_id_changed id = @machine.id begin @logger.debug("Instantiating the driver for machine ID: #{@machine.id.inspect}") @driver = Driver::Meta.new(id) rescue Driver::Meta::VMNotFound # The virtual machine doesn't exist, so we probably have a stale # ID. Just clear the id out of the machine and reload it. @logger.debug("VM not found! Clearing saved machine ID and reloading.") id = nil retry end end |
#ssh_info ⇒ Object
Returns the SSH info for accessing the UTM VM.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/vagrant_utm/provider.rb', line 80 def ssh_info # If the VM is not running (utm, started) then we can't possibly SSH into it # TODO: We should use the state 'running', rather than 'started' # UTM provider, which does not expose 'running' state. So we use 'started' state return nil if state.id != :started # Return what we know # host = IP address of the VM # port = the SSH port # username = vagrant, the default vagrant user # private_key_path = get the private key of the VM (default ~/.vagrant.d/insecure_private_key) # Return ssh info for connector to connect to the VM # If VM has shared network adapter in UTM, then we can use the IP address of the VM # If we have multiple network adapters, we need to pick the right one, read_guest_ip returns just first IP # Also, since Vagrant by default adds port forwarding for ssh port 22, # we might aswell use the forwarded ports to connect to the VM using the localhost. # and the forwarded port. # So we use 127.0.0.1 and the forwarded port to connect to the VM. { host: "127.0.0.1", port: @driver.ssh_port(@machine.config.ssh.guest_port) } end |
#state ⇒ Symbol
Return the state of UTM virtual machine by actually querying utmctl.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/vagrant_utm/provider.rb', line 109 def state @logger.info("Getting state of '#{@machine.id}'") # Determine the ID of the state here. state_id = nil state_id = :not_created unless @driver.uuid state_id ||= @driver.read_state state_id ||= :unknown # Translate into short/long descriptions short = state_id.to_s.gsub("_", " ") long = I18n.t("vagrant_utm.commands.status.#{state_id}") # If we're not created, then specify the special ID flag state_id = Vagrant::MachineState::NOT_CREATED_ID if state_id == :not_created # Return the state Vagrant::MachineState.new(state_id, short, long) end |
#to_s ⇒ String
Returns a human-friendly string version of this provider which includes the machine’s ID that this provider represents, if it has one.
134 135 136 137 |
# File 'lib/vagrant_utm/provider.rb', line 134 def to_s id = @machine.id || "new VM" "UTM (#{id})" end |