Skip to content

Commit

Permalink
Add --verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
bacongravy committed Feb 11, 2019
1 parent 4888511 commit 5f78830
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Next version (Unreleased)

FEATURES:

- Add --verbose option to show the commands being run. [GH-28]

## 3.2.0 (February 2, 2019)

FEATURES:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Usage: macinbox [options]
--use-qemu Use qemu-img (vmware_desktop only)
--verbose Enable verbose mode
--debug Enable debug mode
-v, --version
Expand Down
5 changes: 2 additions & 3 deletions lib/macinbox/actions/check_macos_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def initialize(opts)
@installer_app = opts[:installer_path] or raise ArgumentError.new(":installer_path not specified")

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("Installer app not found") unless File.exist? @installer_app
end
Expand All @@ -21,13 +20,13 @@ def run
installer_os_version_components = installer_os_version.split(".") rescue [0, 0, 0]
installer_os_version_major = installer_os_version_components[0]
installer_os_version_minor = installer_os_version_components[1]
Logger.info "Installer macOS version detected: #{installer_os_version}" if @debug
Logger.info "Installer macOS version detected: #{installer_os_version}" if $verbose

host_os_version = Task.backtick %W[ /usr/bin/sw_vers -productVersion ]
host_os_version_components = host_os_version.split(".") rescue [0, 0, 0]
host_os_version_major = host_os_version_components[0]
host_os_version_minor = host_os_version_components[1]
Logger.info "Host macOS version detected: #{host_os_version}" if @debug
Logger.info "Host macOS version detected: #{host_os_version}" if $verbose

if installer_os_version_major != host_os_version_major || installer_os_version_minor != host_os_version_minor
Logger.error "Warning: host OS version (#{host_os_version}) and installer OS version (#{installer_os_version}) do not match"
Expand Down
3 changes: 1 addition & 2 deletions lib/macinbox/actions/create_box_from_hdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def initialize(opts)
@hidpi = opts[:hidpi]

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("HDD not found") unless File.exist? @input_hdd
end
Expand Down Expand Up @@ -55,7 +54,7 @@ def run
end
EOF

task_opts = @debug ? {} : { :out => File::NULL }
task_opts = $verbose ? {} : { :out => File::NULL }

Task.run %W[ prlctl create macinbox -o macos --no-hdd --dst #{@box_dir} ] + [task_opts]

Expand Down
3 changes: 1 addition & 2 deletions lib/macinbox/actions/create_box_from_vdi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def initialize(opts)
@hidpi = opts[:hidpi]

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("VDI not found") unless File.exist? @input_vdi
end
Expand Down Expand Up @@ -53,7 +52,7 @@ def run
end
EOF

task_opts = @debug ? {} : { :out => File::NULL }
task_opts = $verbose ? {} : { :out => File::NULL }

Task.run %W[ VBoxManage createvm --register --name macinbox --ostype MacOS1013_64 ] + [task_opts]

Expand Down
1 change: 0 additions & 1 deletion lib/macinbox/actions/create_box_from_vmdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def initialize(opts)
@hidpi = opts[:hidpi]

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("VMDK not found") unless File.exist? @input_vmdk
raise Macinbox::Error.new("Box format not supported: #{@box_format}") unless ["vmware_fusion", "vmware_desktop"].include? @box_format
Expand Down
7 changes: 3 additions & 4 deletions lib/macinbox/actions/create_hdd_from_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def initialize(opts)
@parallels_app = opts[:parallels_path] or raise ArgumentError.new(":parallels_path not specified")

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("input image not found") unless File.exist? @input_image
raise Macinbox::Error.new("Parallels Desktop not found") unless File.exist? @parallels_app
Expand Down Expand Up @@ -49,7 +48,7 @@ def copy_input_image

def attach_image
Logger.info "Attaching the image..." do
@disk = VirtualDisk.new(@image, @debug)
@disk = VirtualDisk.new(@image)
@collector.on_cleanup { @disk.detach! }
@image_mountpoint = "#{@temp_dir}/image_mountpoint"
FileUtils.mkdir @image_mountpoint
Expand All @@ -64,7 +63,7 @@ def install_parallels_tools

tools_image = "#{@parallels_app}/Contents/Resources/Tools/prl-tools-mac.iso"

tools_disk = VirtualDisk.new(tools_image, @debug)
tools_disk = VirtualDisk.new(tools_image)

@collector.on_cleanup { tools_disk.detach! }

Expand Down Expand Up @@ -171,7 +170,7 @@ def convert_image
EOF

prl_convert = "#{@parallels_app}/Contents/MacOS/prl_convert"
task_opts = @debug ? {} : { :out => File::NULL }
task_opts = $verbose ? {} : { :out => File::NULL }
Task.run %W[ #{prl_convert} #{@temp_dir}/macinbox.vmdk --allow-no-os --dst=#{@temp_dir} ] + [task_opts]
@disk.eject
end
Expand Down
9 changes: 4 additions & 5 deletions lib/macinbox/actions/create_image_from_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def initialize(opts)
@hidpi = opts[:hidpi]

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("Installer app not found") unless File.exist? @installer_app

Expand Down Expand Up @@ -69,7 +68,7 @@ def installer_is_on_root_filesystem
def create_wrapper_image
Logger.info "Creating and attaching wrapper disk image..." do
@wrapper_image = "#{@temp_dir}/wrapper.dmg"
@wrapper_disk = VirtualDisk.new(@wrapper_image, @debug)
@wrapper_disk = VirtualDisk.new(@wrapper_image)
@collector.on_cleanup { @wrapper_disk.detach! }
@wrapper_disk.create_from_folder(@installer_app)
@wrapper_disk.attach
Expand All @@ -81,7 +80,7 @@ def create_wrapper_image
def create_scratch_image
Logger.info "Creating and attaching a new blank disk image..." do
@scratch_image = "#{@temp_dir}/scratch.sparseimage"
@scratch_disk = VirtualDisk.new(@scratch_image, @debug)
@scratch_disk = VirtualDisk.new(@scratch_image)
@collector.on_cleanup { @scratch_disk.detach! }
@scratch_mountpoint = "#{@temp_dir}/scratch_mountpoint"
FileUtils.mkdir @scratch_mountpoint
Expand All @@ -97,7 +96,7 @@ def install_macos
install_info_plist = "#{@installer_app}/Contents/SharedSupport/InstallInfo.plist"
Task.run %W[ /usr/bin/touch #{@scratch_mountpoint}/.macinbox ]
cmd = %W[ /usr/sbin/installer -verboseR -dumplog -pkg #{install_info_plist} -target #{@scratch_mountpoint} ]
opts = @debug ? {} : { :err => [:child, :out] }
opts = $verbose ? {} : { :err => [:child, :out] }
Task.run_with_progress activity, cmd, opts do |line|
/^installer:%(.*)$/.match(line)[1].to_f rescue nil
end
Expand Down Expand Up @@ -190,7 +189,7 @@ def enable_passwordless_sudo
def enable_sshd
Logger.info "Enabling sshd..." do
scratch_launchd_disabled_plist = "#{@scratch_mountpoint}/private/var/db/com.apple.xpc.launchd/disabled.plist"
opts = @debug ? {} : { :out => File::NULL }
opts = $verbose ? {} : { :out => File::NULL }
Task.run %W[ /usr/libexec/PlistBuddy -c #{'Add :com.openssh.sshd bool False'} #{scratch_launchd_disabled_plist} ] + [opts]
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/macinbox/actions/create_vdi_from_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def initialize(opts)
@output_path = opts[:vdi_path] or raise ArgumentError.new(":vdi_path not specified")

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("input image not found") unless File.exist? @input_image
end
Expand Down Expand Up @@ -46,7 +45,7 @@ def copy_input_image

def attach_image
Logger.info "Attaching the image..." do
@disk = VirtualDisk.new(@image, @debug)
@disk = VirtualDisk.new(@image)
@collector.on_cleanup { @disk.detach! }
@disk.attach
end
Expand Down Expand Up @@ -80,7 +79,7 @@ def setup_efi_partition

def convert_image
Logger.info "Converting the image to VDI format..." do
task_opts = @debug ? {} : { :out => File::NULL }
task_opts = $verbose ? {} : { :out => File::NULL }
Task.run %W[ VBoxManage convertfromraw #{@disk.device} #{@temp_dir}/macinbox.vdi --format VDI ] + [task_opts]
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/macinbox/actions/create_vmdk_from_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def initialize(opts)
@use_qemu = opts[:use_qemu]

@collector = opts[:collector] or raise ArgumentError.new(":collector not specified")
@debug = opts[:debug]

raise Macinbox::Error.new("input image not found") unless File.exist? @input_image
raise Macinbox::Error.new("VMware Fusion not found") unless File.exist? @vmware_fusion_app
Expand Down Expand Up @@ -55,7 +54,7 @@ def copy_input_image

def attach_image
Logger.info "Attaching the image..." do
@disk = VirtualDisk.new(@image, @debug)
@disk = VirtualDisk.new(@image)
@collector.on_cleanup { @disk.detach! }
@image_mountpoint = "#{@temp_dir}/image_mountpoint"
FileUtils.mkdir @image_mountpoint
Expand All @@ -73,16 +72,16 @@ def install_vmware_tools
bundle_short_version = Task.backtick %W[ defaults read #{"/Applications/VMware Fusion.app/Contents/Info.plist"} CFBundleShortVersionString ]
darwin_iso_url = "http://softwareupdate.vmware.com/cds/vmw-desktop/fusion/#{bundle_short_version}/#{bundle_version}/packages/com.vmware.fusion.tools.darwin.zip.tar"
Dir.chdir(@temp_dir) do
Task.run %W[ /usr/bin/curl #{darwin_iso_url} -O ] + (@debug ? [] : %W[ -s -S ])
Task.run %W[ /usr/bin/curl #{darwin_iso_url} -O ] + ($verbose ? [] : %W[ -s -S ])
Task.run %W[ /usr/bin/tar -xf com.vmware.fusion.tools.darwin.zip.tar com.vmware.fusion.tools.darwin.zip ]
Task.run %W[ /usr/bin/unzip ] + (@debug ? [] : %W[ -qq ]) + %W[ com.vmware.fusion.tools.darwin.zip payload/darwin.iso ]
Task.run %W[ /usr/bin/unzip ] + ($verbose ? [] : %W[ -qq ]) + %W[ com.vmware.fusion.tools.darwin.zip payload/darwin.iso ]
end
tools_image = "#{@temp_dir}/payload/darwin.iso"
end
end

Logger.info "Installing the VMware Tools..." do
tools_disk = VirtualDisk.new(tools_image, @debug)
tools_disk = VirtualDisk.new(tools_image)
@collector.on_cleanup { tools_disk.detach! }
tools_mountpoint = "#{@temp_dir}/tools_mountpoint"
FileUtils.mkdir tools_mountpoint
Expand Down Expand Up @@ -130,7 +129,7 @@ def convert_image
Task.run %W[ /usr/local/bin/qemu-img convert -f dmg -O vmdk #{@temp_dir}/macinbox.dmg #{@temp_dir}/macinbox.vmdk ]
else
@disk.attach
task_opts = @debug ? {} : { :out => File::NULL }
task_opts = $verbose ? {} : { :out => File::NULL }
rawdiskCreator = "#{@vmware_fusion_app}/Contents/Library/vmware-rawdiskCreator"
vdiskmanager = "#{@vmware_fusion_app}/Contents/Library/vmware-vdiskmanager"
Dir.chdir(@temp_dir) do
Expand Down
1 change: 0 additions & 1 deletion lib/macinbox/actions/install_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def initialize(opts)
@boxes_dir = opts[:boxes_dir] or raise ArgumentError.new(":boxes_dir not specified")

@box_version = opts[:macos_version]
@debug = opts[:debug]

raise Macinbox::Error.new("box not found: #{@input_box}") unless File.exist? @input_box
raise Macinbox::Error.new("boxes directory not found: #{@boxes_dir}") unless File.exist? @boxes_dir
Expand Down
4 changes: 2 additions & 2 deletions lib/macinbox/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def start(argv)

check_for_sudo_root

collector = Collector.new(preserve_temp_dirs: @options[:debug])
collector = Collector.new(preserve_temp_dirs: $debug)

collector.on_cleanup do
STDERR.print TTY::Color::RESET
Expand All @@ -42,7 +42,7 @@ def start(argv)
raise Macinbox::Error.new("Installer disk image not found: #{@options[:installer_dmg]}")
end
Logger.info "Attaching installer disk image..."
installer_disk = VirtualDisk.new(@options[:installer_dmg], @options[:debug])
installer_disk = VirtualDisk.new(@options[:installer_dmg])
collector.on_cleanup { installer_disk.detach! }
installer_disk.attach
installer_disk.mount
Expand Down
4 changes: 3 additions & 1 deletion lib/macinbox/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CLI
:fullscreen => true,
:gui => true,
:use_qemu => false,
:verbose => false,
:debug => false,
}

Expand Down Expand Up @@ -58,7 +59,8 @@ def parse_options(argv)
o.separator ''
o.on( '--use-qemu', 'Use qemu-img (vmware_desktop only)') { |v| @options[:use_qemu] = v }
o.separator ''
o.on( '--debug', 'Enable debug mode') { |v| @options[:debug] = v }
o.on( '--verbose', 'Enable verbose mode') { |v| $verbose = v }
o.on( '--debug', 'Enable debug mode') { |v| $debug = $verbose = v }
o.separator ''
o.on('-v', '--version') { puts "macinbox #{Macinbox::VERSION}"; exit }
o.on('-h', '--help') { puts o; exit }
Expand Down
7 changes: 7 additions & 0 deletions lib/macinbox/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
require 'macinbox/logger'
require 'macinbox/tty'

require 'shellwords'

module Macinbox

class Task

def self.run(cmd)
Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
system(*cmd) or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$? >> 8}")
end

def self.run_as_sudo_user(cmd)
Logger.info "Running command: sudo -u #{ENV["SUDO_USER"]} #{Shellwords.join(cmd)}" if $verbose
system "sudo", "-u", ENV["SUDO_USER"], *cmd or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$?.to_i}")
end

Expand All @@ -33,6 +37,7 @@ def self.progress_bar(activity, percent_done)
def self.run_with_progress(activity, cmd, opts={})
STDERR.print TTY::Cursor::INVISIBLE
STDERR.print TTY::Line::CLEAR + TTY::Color::GREEN + progress_bar(activity, 0.0) + TTY::Color::RESET
Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
IO.popen cmd, opts do |pipe|
pipe.each_line do |line|
percent = yield line
Expand Down Expand Up @@ -66,10 +71,12 @@ def self.write_file_to_io_with_progress(source, destination)
end

def self.backtick(cmd)
Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
IO.popen(cmd).read.chomp
end

def self.run_with_input(cmd)
Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
IO.popen(cmd, "w") do |pipe|
yield pipe
end
Expand Down
11 changes: 5 additions & 6 deletions lib/macinbox/virtual_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ module Macinbox

class VirtualDisk

def initialize(image, debug)
def initialize(image)
@image = image
@debug = debug
@quiet_flag = @debug ? [] : %W[ -quiet ]
@task_opts = @debug ? [] : [{ :out => File::NULL }]
@quiet_flag = $verbose ? [] : %W[ -quiet ]
@task_opts = $verbose ? [] : [{ :out => File::NULL }]
end

def device
Expand Down Expand Up @@ -69,13 +68,13 @@ def eject
max_attempts = 5
for attempt in 1..max_attempts
begin
quiet = @debug ? [] : %W[ quiet ]
quiet = $verbose ? [] : %W[ quiet ]
Task.run %W[ /usr/sbin/diskutil ] + quiet + %W[ eject #{@disk_device} ] + @task_opts
unset_devices
break
rescue Macinbox::Error => error
raise if attempt == max_attempts
Logger.info "#{error.message}. Sleeping and retrying..." if @debug
Logger.info "Eject failed: #{error.message}. Sleeping and retrying..." if $verbose
sleep 15
end
end
Expand Down

0 comments on commit 5f78830

Please sign in to comment.