Class: Macinbox::Actions::CreateBoxFromHDD

Inherits:
Object
  • Object
show all
Defined in:
lib/macinbox/actions/create_box_from_hdd.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ CreateBoxFromHDD

Returns a new instance of CreateBoxFromHDD.

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/macinbox/actions/create_box_from_hdd.rb', line 15

def initialize(opts)
  @input_hdd         = opts[:hdd_path]        or raise ArgumentError.new(":hdd_path not specified")
  @output_path       = opts[:box_path]        or raise ArgumentError.new(":box_path not specified")

  @box_name          = opts[:box_name]        or raise ArgumentError.new(":box_name not specified")
  @cpu_count         = opts[:cpu_count]       or raise ArgumentError.new(":cpu_count not specified")
  @memory_size       = opts[:memory_size]     or raise ArgumentError.new(":memory_size not specified")

  @gui               = opts[:gui]
  @fullscreen        = opts[:fullscreen]
  @hidpi             = opts[:hidpi]

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

  raise Macinbox::Error.new("HDD not found") unless File.exist? @input_hdd
end

Instance Method Details

#runObject



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
# File 'lib/macinbox/actions/create_box_from_hdd.rb', line 32

def run
  @temp_dir = Task.backtick %W[ /usr/bin/mktemp -d -t create_box_from_hdd ]
  @collector.add_temp_dir @temp_dir

  Logger.info "Assembling the box contents..." do

    @box_dir = "#{@temp_dir}/#{@box_name}.box"

    FileUtils.mkdir @box_dir

    File.write "#{@box_dir}/metadata.json", <<~EOF
      {"provider": "parallels"}
    EOF

    File.write "#{@box_dir}/Vagrantfile", <<~EOF
      Vagrant.configure(2) do |config|
        config.vm.box_check_update = false
        config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: true
        config.vm.synced_folder ".", "/vagrant", disabled: true
        config.vm.provider "parallels" do |prl|
          prl.customize ["set", :id, "--startup-view", "#{@gui ? (@fullscreen ? "fullscreen" : "window") : "headless"}"]
        end
      end
    EOF

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

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

    @collector.on_cleanup do
      Task.run %W[ prlctl unregister macinbox ] + [task_opts]
    end

    Macinbox::copyfiles(from: @input_hdd, to: "#{@box_dir}/macinbox.pvm/macinbox.hdd", recursive: true)
    
    Task.run %W[ prl_disk_tool convert --merge --hdd #{@box_dir}/macinbox.pvm/macinbox.hdd ] + [task_opts]
    Task.run %W[ prlctl set macinbox --device-add hdd --image #{@box_dir}/macinbox.pvm/macinbox.hdd ] + [task_opts]
    Task.run %W[ prlctl set macinbox --high-resolution #{@hidpi ? "on" : "off"} ] + [task_opts]
    Task.run %W[ prlctl set macinbox --cpus #{@cpu_count} ] + [task_opts]
    Task.run %W[ prlctl set macinbox --memsize #{@memory_size} ] + [task_opts]

  end

  Logger.info "Moving the box to the destination..." do
    FileUtils.chown ENV["SUDO_USER"], nil, @box_dir
    FileUtils.mv @box_dir, @output_path
  end

end