Skip to content

Commit

Permalink
vagrant disposable!
Browse files Browse the repository at this point in the history
Add custom plugin command to boot UTM VM in disposable mode
  • Loading branch information
naveenrajm7 committed Jul 25, 2024
1 parent 7f0d1a1 commit 3206657
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/vagrant_utm/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Action # rubocop:disable Metrics/ModuleLength
# Autoloading action blocks
action_root = Pathname.new(File.expand_path("action", __dir__))
autoload :Boot, action_root.join("boot")
autoload :BootDisposable, action_root.join("boot_disposable")
autoload :CheckAccessible, action_root.join("check_accessible")
autoload :CheckCreated, action_root.join("check_created")
autoload :CheckGuestAdditions, action_root.join("check_guest_additions")
Expand Down Expand Up @@ -239,6 +240,23 @@ def self.action_start
end
end

# This action start VM in disposable mode.
# UTM equivalent of `utmctl start <uuid> --disposable`
def self.action_start_disposable
Vagrant::Action::Builder.new.tap do |b|
b.use CheckUtm
b.use ConfigValidate
b.use Call, IsRunning do |env1, b2|
if env1[:result]
b2.use MessageAlreadyRunning
next
end
# If the VM is NOT running, then start in disposable mode
b2.use BootDisposable
end
end
end

# This action is primarily responsible for suspending the VM.
# UTM equivalent of `utmctl suspend <uuid>`
def self.action_suspend
Expand Down
22 changes: 22 additions & 0 deletions lib/vagrant_utm/action/boot_disposable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module VagrantPlugins
module Utm
module Action
# Action to start the virtual machine in disposable mode.
class BootDisposable
def initialize(app, _env)
@app = app
end

def call(env)
# Start up the VM in disposable mode.
env[:ui].warn I18n.t("vagrant_utm.actions.vm.boot.disposable")
env[:machine].provider.driver.start_disposable

@app.call(env)
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/vagrant_utm/disposable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module VagrantPlugins
module Utm
# Run VM as a snapshot and do not save changes to disk.
class Disposable < Vagrant.plugin(2, :command)
def execute
with_target_vms do |machine|
machine.action(:start_disposable)
end

0
end
end
end
end
4 changes: 4 additions & 0 deletions lib/vagrant_utm/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def set_name(name); end # rubocop:disable Naming/AccessorMethodName
# @return [void]
def start; end

# Starts the virtual machine in disposable mode.
# @return [void]
def start_disposable; end

# Deletes the virtual machine references by this driver.
# @return [void]
def delete; end
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant_utm/driver/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def initialize(uuid = nil) # rubocop:disable Metrics/CyclomaticComplexity,Metric
:list,
:read_state,
:start,
:start_disposable,
:vm_exists?,
:halt,
:suspend,
Expand Down
4 changes: 4 additions & 0 deletions lib/vagrant_utm/driver/version_4_5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def start
execute("start", @uuid)
end

def start_disposable
execute("start", @uuid, "--disposable")
end

def halt
execute("stop", @uuid)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/vagrant_utm/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class Plugin < Vagrant.plugin("2")
Provider
end

# Register the command
command "disposable" do
require_relative "disposable"
Disposable
end

# Load the translation files
def self.setup_i18n
I18n.load_path << File.expand_path("locales/en.yml", Utm.source_root)
Expand Down
5 changes: 4 additions & 1 deletion lib/vagrant_utm/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ def initialize(machine)
machine_id_changed
end

# Execute the action with the given name.
# @see Vagrant::Plugin::V1::Provider#action
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)

Expand Down
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ en:
1. Headover to UTM.
2. Open the action menu of VM by a secondary click on a virtual machine %{name} in the list.
3. Select 'Share' and save the file to a location.
boot:
disposable: |-
WARNING: The UTM virtual machine is booting in disposable mode.
Changes made to the VM will be lost when the VM is powered off.

0 comments on commit 3206657

Please sign in to comment.