Skip to content

Commit

Permalink
fix read state
Browse files Browse the repository at this point in the history
  • Loading branch information
bill committed Sep 30, 2021
1 parent 5c2b70d commit fbe5536
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
vagrant-qemu (0.2.21)
vagrant-qemu (0.2.22)

GEM
remote: https://rubygems.org/
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a [Vagrant](http://www.vagrantup.com) 2.5+ plugin that adds a
qemu provider to Vagrant, allowing Vagrant to control and provision
machines using qemu.

## Install
## Environment preparation
First make sure `qemu` works in your environment.
If qemu is not installed yet,
```
Expand All @@ -16,11 +16,16 @@ to enable graphical acceleration:
```
brew install knazarov/qemu-virgl/qemu-virgl
```
If `vagrant` is not installed yet, you should [install it](https://www.vagrantup.com)

## Demo
## Build and install
```
rake build && vagrant plugin install pkg/vagrant-qemu-0.2.21.gem
```

## Run a demo

### Run a sample
Only tested on m1 macbook:
Below is to load and run an Ubuntu desktop, only tested on m1 macbook:
```shell
vagrant init billyan2018/devbox \
--box-version 0.1.0
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Bundler::GemHelper.install_tasks
# Install the `spec` task so that we can run tests.

# Default task is to run the unit tests
task :default => :spec
## task :default => :spec
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.21
0.2.22
28 changes: 2 additions & 26 deletions lib/vagrant-qemu/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@ def self.action_provision
end
end

# This action is called to read the SSH info of the machine. The
# resulting state is expected to be put into the `:machine_ssh_info`
# key.
def self.action_read_ssh_info
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ReadSSHInfo
end
end


# This action is called to read the state of the machine. The
# resulting state is expected to be put into the `:machine_state_id`
Expand All @@ -54,29 +46,13 @@ def self.action_read_state
# This action is called to SSH into the machine.
def self.action_ssh
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b2|
if !env[:result]
b2.use MessageNotCreated
next
end

b2.use SSHExec
end
end
end

def self.action_ssh_run
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b2|
if !env[:result]
b2.use MessageNotCreated
next
end

b2.use SSHRun
end
end
end

Expand All @@ -101,7 +77,7 @@ def self.action_up
autoload :IsCreated, action_root.join("is_created")
autoload :MessageAlreadyCreated, action_root.join("message_already_created")
autoload :MessageNotCreated, action_root.join("message_not_created")
autoload :ReadSSHInfo, action_root.join("read_ssh_info")
# autoload :ReadSSHInfo, action_root.join("read_ssh_info")
autoload :ReadState, action_root.join("read_state")
autoload :RunInstance, action_root.join("run_instance")
autoload :TimedProvision, action_root.join("timed_provision")
Expand Down
26 changes: 16 additions & 10 deletions lib/vagrant-qemu/action/read_state.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "log4r"

require "json"
module VagrantPlugins
module Qemu
module Action
Expand All @@ -9,23 +9,29 @@ class ReadState
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_qemu::action::read_state")
@env
end

def call(env)
env[:machine_state_id] = read_state(env[:machine])
env[:machine_state_id] = read_state(env)

@app.call(env)
end

def read_state(machine)
return :not_created if machine.id.nil?

# Return the state
output = %x{ #{machine.provider_config.script} read-state #{machine.id} }
if $?.to_i > 0
raise Errors::QemuError, :message => "Failure: #{env[:machine].provider_config.script} read-state #{machine.id}"
def read_state(env)
machine = env[:machine]
pid = machine.id
env[:ui].info("======= #{pid}")
if pid.nil? || !(pid.is_a? Numeric)
:not_created
else
begin
Process.getpgid( pid )
:created
rescue Errno::ESRCH
:not_created
end
end
output.strip
end
end
end
Expand Down
26 changes: 15 additions & 11 deletions lib/vagrant-qemu/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ def disk_file
end

def firmware_location
"/opt/homebrew/share/qemu"
@provider_config.firmware_location || "/opt/homebrew/share/qemu"
end

def prepare_shell_command(env)

firmware_path = firmware_location
env[:ui].info("==Firmware: #{firmware_path}")
disk_file_location = self.disk_file
env[:ui].info("==Disk: #{disk_file_location}")
%{
Expand All @@ -39,8 +42,8 @@ def prepare_shell_command(env)
-display cocoa,gl=es \
-device e1000,netdev=net0 \
-netdev user,id=net0 \
-drive "if=pflash,format=raw,file=#{firmware_location}/edk2-aarch64-code.fd,readonly=on" \
-drive "if=pflash,format=raw,file=#{firmware_location}/edk2-arm-vars.fd,discard=on" \
-drive "if=pflash,format=raw,file=#{firmware_path}/edk2-aarch64-code.fd,readonly=on" \
-drive "if=pflash,format=raw,file=#{firmware_path}/edk2-arm-vars.fd,discard=on" \
-drive "if=virtio,format=qcow2,file=#{disk_file_location},discard=on" \
-chardev qemu-vdagent,id=spice,name=vdagent,clipboard=on \
-device virtio-serial-pci \
Expand All @@ -56,20 +59,21 @@ def call(env)
# Get the configs
@provider_config = env[:machine].provider_config
# Launch!
env[:ui].info(I18n.t("vagrant_qemu.launching_instance"))
env[:ui].info("vagrant_qemu.launching_instance")


env[:ui].info(JSON.pretty_generate (env[:machine]))
env[:ui].info(JSON.pretty_generate(@provider_config))


shell_command = prepare_shell_command(env)
output = `#{shell_command}`
if $?.to_i > 0
raise Errors::QemuError, :message => "Failure with command: #{shell_command}..."
end

env[:machine].id = output.split(/\s+/)[0]

env[:machine].id = spawn(shell_command)

# output = `#{shell_command}`
## if $?.to_i > 0
# raise Errors::QemuError, :message => "Failure with command: #{shell_command}..."
#end

# Wait for the instance to be ready first
env[:metrics]["instance_ready_time"] = Util::Timer.time do
Expand Down Expand Up @@ -111,7 +115,7 @@ def call(env)
@logger.info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}")

# Ready and booted!
env[:ui].info(I18n.t("vagrant_qemu.ready"))
env[:ui].info("vagrant_qemu.ready")
end

# Terminate the instance if we were interrupted
Expand Down
5 changes: 3 additions & 2 deletions lib/vagrant-qemu/action/terminate_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def initialize(app, env)

def call(env)
# Destroy the server and remove the tracking ID
pid = env[:machine].id
env[:ui].info(I18n.t("vagrant_qemu.terminating"))
output = %x{ #{env[:machine].provider_config.script} terminate-instance #{env[:machine].id}}
output = %x{ kill -9 #{pid} }
if $?.to_i > 0
raise Errors::QemuError, :message => "Failure: #{env[:machine].provider_config.script} terminate-instance #{env[:machine].id}"
raise Errors::QemuError, :message => "Failure: terminate-instance #{env[:machine].id}"
end

puts output
Expand Down
12 changes: 10 additions & 2 deletions lib/vagrant-qemu/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
module VagrantPlugins
module Qemu
class Config < Vagrant.plugin("2", :config)
# The ID of the disk_file to use.
# The disk_file to use.
#
# @return [String]
attr_accessor :disk_file

# The firmware to use.
#
# @return [String]
attr_accessor :firmware_location

# The timeout to wait for an instance to become ready.
#
# @return [Fixnum]
Expand All @@ -29,7 +34,8 @@ class Config < Vagrant.plugin("2", :config)
attr_accessor :run_args

def initialize
@disk_file = UNSET_VALUE
@disk_file = UNSET_VALUE
@firmware_location = UNSET_VALUE
@instance_ready_timeout = UNSET_VALUE
@user_data = UNSET_VALUE
@script = UNSET_VALUE
Expand All @@ -53,6 +59,8 @@ def finalize!
# disk_filemust be nil, since we can't default that
@disk_file= nil if @disk_file== UNSET_VALUE

@firmware_location= nil if @firmware_location == UNSET_VALUE

# Set the default timeout for waiting for an instance to be ready
@instance_ready_timeout = 120 if @instance_ready_timeout == UNSET_VALUE

Expand Down
12 changes: 6 additions & 6 deletions lib/vagrant-qemu/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.make_provider nm_provider
provider(nm_provider) do
# Setup logging and i18n
setup_logging
# setup_i18n
setup_i18n

# Return the provider
require_relative "provider"
Expand All @@ -37,12 +37,12 @@ def self.make_provider nm_provider
end

make_provider(:qemu)

make_provider("libvirt")
# This initializes the internationalization strings.
## def self.setup_i18n
# I18n.load_path << File.expand_path("locales/en.yml", Qemu.source_root)
# I18n.reload!
## end
def self.setup_i18n
I18n.load_path << File.expand_path("locales/en.yml", Qemu.source_root)
I18n.reload!
end

# This sets up our log level to be whatever VAGRANT_LOG is.
def self.setup_logging
Expand Down
11 changes: 2 additions & 9 deletions lib/vagrant-qemu/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ def action(name)
nil
end

def ssh_info
# Run a custom action called "read_ssh_info" which does what it
# says and puts the resulting SSH info into the `:machine_ssh_info`
# key in the environment.
env = @machine.action("read_ssh_info")
env[:machine_ssh_info]
end

def state
# Run a custom action we define called "read_state" which does
Expand All @@ -34,8 +27,8 @@ def state
state_id = env[:machine_state_id]

# Get the short and long description
short = I18n.t("vagrant_qemu.states.short_#{state_id}")
long = I18n.t("vagrant_qemu.states.long_#{state_id}")
short = "vagrant_qemu.states.short_#{state_id}"
long = "vagrant_qemu.states.long_#{state_id}"

# Return the MachineState object
Vagrant::MachineState.new(state_id, short, long)
Expand Down

0 comments on commit fbe5536

Please sign in to comment.