Class: Macinbox::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/macinbox/cli.rb,
lib/macinbox/cli/options.rb

Constant Summary collapse

DEFAULT_OPTION_VALUES =
{
  :box_format      => "vmware_desktop",
  :box_name        => "macinbox",
  :disk_size       => 64,
  :fstype          => "APFS",
  :memory_size     => 2048,
  :cpu_count       => 2,
  :short_name      => "vagrant",
  :installer_path  => "/Applications/Install macOS Mojave.app",
  :vmware_path     => "/Applications/VMware Fusion.app",
  :parallels_path  => "/Applications/Parallels Desktop.app",
  :vmware_tools    => true,
  :auto_login      => true,
  :skip_mini_buddy => true,
  :hidpi           => true,
  :fullscreen      => true,
  :gui             => true,
  :debug           => false,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/macinbox/cli.rb', line 16

def self.run!(argv)
  begin
    self.new.start(argv)
  rescue Macinbox::Error => e
    Logger.error "Error: " + e.to_s
    exit 1
  end
  exit 0
end

Instance Method Details

#check_for_sudo_rootObject



147
148
149
150
151
# File 'lib/macinbox/cli.rb', line 147

def check_for_sudo_root
  if Process.uid != 0 or ENV["SUDO_USER"].nil?
    raise Macinbox::Error.new("script must be run as root with sudo")
  end
end

#parse_options(argv) ⇒ Object



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
# File 'lib/macinbox/cli/options.rb', line 29

def parse_options(argv)

  @options = DEFAULT_OPTION_VALUES

  @option_parser = OptionParser.new do |o|

    o.separator ''
    o.on(      '--box-format FORMAT',  'Format of the box (default: vmware_desktop)')  { |v| @options[:box_format] = v }
    o.separator ''
    o.on('-n', '--name NAME',          'Name of the box         (default: macinbox)') { |v| @options[:box_name] = v }
    o.on('-d', '--disk SIZE',          'Size (GB) of the disk   (default: 64)')       { |v| @options[:disk_size] = v }
    o.on('-t', '--fstype TYPE',        'Type of FS on the disk  (default: APFS)')     { |v| @options[:fstype] = v }
    o.on('-m', '--memory SIZE',        'Size (MB) of the memory (default: 2048)')     { |v| @options[:memory_size] = v }
    o.on('-c', '--cpu COUNT',          'Number of virtual cores (default: 2)')        { |v| @options[:cpu_count] = v }
    o.on('-s', '--short NAME',         'Short name of the user  (default: vagrant)')  { |v| @options[:short_name] = v }
    o.on('-f', '--full NAME',          'Full name of the user   (default: Vagrant)')  { |v| @options[:full_name] = v }
    o.on('-p', '--password PASSWORD',  'Password of the user    (default: vagrant)')  { |v| @options[:password] = v }
    o.separator ''
    o.on(      '--installer PATH',     'Path to the macOS installer app')             { |v| @options[:installer_path] = v }
    o.on(      '--vmware PATH',        'Path to the VMware Fusion app')               { |v| @options[:vmware_path] = v }
    o.on(      '--parallels PATH',     'Path to the Parallels Desktop app')           { |v| @options[:parallels_path] = v }
    o.separator ''
    o.on(      '--no-auto-login',      'Disable auto login')                          { |v| @options[:auto_login] = v }
    o.on(      '--no-skip-mini-buddy', 'Show the mini buddy on first login')          { |v| @options[:skip_mini_buddy] = v }
    o.on(      '--no-hidpi',           'Disable HiDPI resolutions')                   { |v| @options[:hidpi] = v }
    o.on(      '--no-fullscreen',      'Display the virtual machine GUI in a window') { |v| @options[:fullsceen] = v }
    o.on(      '--no-gui',             'Disable the GUI')                             { |v| @options[:gui] = v }
    o.separator ''
    o.on(      '--debug',              'Enable debug mode')                           { |v| @options[:debug] = v }
    o.separator ''
    o.on('-v', '--version')                                                           { puts "macinbox #{Macinbox::VERSION}"; exit }
    o.on('-h', '--help')                                                              { puts o; exit }

    o.parse(argv)

  end

  @options[:full_name] ||= @options[:short_name].capitalize
  @options[:password]  ||= @options[:short_name]

end

#start(argv) ⇒ Object



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
# File 'lib/macinbox/cli.rb', line 26

def start(argv)

  parse_options(argv)

  check_for_sudo_root

  if not File.exists? @options[:installer_path]
    raise Macinbox::Error.new("Installer app not found: #{@options[:installer_path]}")
  end

  if not ["vmware_fusion", "vmware_desktop", "parallels"].include? @options[:box_format]
    raise Macinbox::Error.new("Box format not supported: #{@options[:box_format]}")
  end

  if /^vmware_(fusion|desktop)$/ === @options[:box_format] && !File.exists?(@options[:vmware_path])
    raise Macinbox::Error.new("VMware Fusion app not found: #{@options[:vmware_path]}")
  end

  if /^parallels$/ === @options[:box_format] && !File.exists?(@options[:parallels_path])
    raise Macinbox::Error.new("Parallels Desktop app not found: #{@options[:parallels_path]}")
  end

  vagrant_home = ENV["VAGRANT_HOME"]

  if vagrant_home.nil? or vagrant_home.empty?
    vagrant_home = File.expand_path "~/.vagrant.d"
  end

  if !File.exist? vagrant_home
    raise Macinbox::Error.new("VAGRANT_HOME not found: #{vagrant_home}")
  end

  vagrant_boxes_dir = "#{vagrant_home}/boxes"

  if !File.exist? vagrant_boxes_dir
    Dir.mkdir vagrant_boxes_dir
  end

  root_temp_dir = Task.backtick %W[ /usr/bin/mktemp -d -t macinbox_root_temp ]
  user_temp_dir = Task.backtick %W[ sudo -u #{ENV["SUDO_USER"]} /usr/bin/mktemp -d -t macinbox_user_temp ]

  collector = Collector.new

  collector.add_temp_dir root_temp_dir
  collector.add_temp_dir user_temp_dir

  collector.on_cleanup do
    if @options[:debug]
      temp_dir_args = collector.temp_dirs.reverse.map { |o| o.shellescape }.join(" \\\n")
      Logger.error "WARNING: Temporary files were not removed. Run this command to remove them:"
      Logger.error "sudo rm -rf #{temp_dir_args}"
    else
      collector.remove_temp_dirs
    end
    STDERR.print TTY::Color::RESET
    STDERR.print TTY::Cursor::NORMAL
  end

  ["TERM", "INT", "EXIT"].each do |signal|
    trap signal do
      trap signal, "SYSTEM_DEFAULT" unless signal == "EXIT"
      Process.waitall
      Logger.reset_depth
      if @success
        Logger.info "Cleaning up..."
      else
        STDERR.puts
        Logger.error "Cleaning up..."
      end
      collector.cleanup!
      Process.kill(signal, Process.pid) unless signal == "EXIT"
    end
  end

  @options[:image_path] = "macinbox.dmg"
  @options[:vmdk_path] = "macinbox.vmdk"
  @options[:hdd_path] = "macinbox.hdd"
  @options[:box_path] = "macinbox.box"
  @options[:boxes_dir] = vagrant_boxes_dir
  @options[:collector] = collector

  Dir.chdir(root_temp_dir) do

    Logger.info "Creating image from installer..." do
      Actions::CreateImageFromInstaller.new(@options).run
    end

    case @options[:box_format]

    when /^vmware_(fusion|desktop)$/

      Logger.info "Creating VMDK from image..." do
        Actions::CreateVMDKFromImage.new(@options).run
      end

      Logger.info "Creating box from VMDK..." do
        Actions::CreateBoxFromVMDK.new(@options).run
      end

    when /^parallels$/

      Logger.info "Creating HDD from image..." do
        Actions::CreateHDDFromImage.new(@options).run
      end

      Logger.info "Creating box from HDD..." do
        Actions::CreateBoxFromHDD.new(@options).run
      end

    end

    Logger.info "Installing box..." do
      Actions::InstallBox.new(@options).run
    end

  end

  @success = true

end