Class: Macinbox::Actions::CreateImageFromInstaller

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

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ CreateImageFromInstaller

Returns a new instance of CreateImageFromInstaller.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 16

def initialize(opts)
  @installer_app     = opts[:installer_path]  or raise ArgumentError.new(":installer_path not specified")
  @output_path       = opts[:image_path]      or raise ArgumentError.new(":image_path not specified")
  @vmware_fusion_app = opts[:vmware_path]
  @parallels_app     = opts[:parallels_path]
  @user_script       = opts[:user_script]

  @disk_size         = opts[:disk_size]       or raise ArgumentError.new(":disk_size not specified")
  @fstype            = opts[:fstype]          or raise ArgumentError.new(":fstype not specified")
  @short_name        = opts[:short_name]      or raise ArgumentError.new(":short_name not specified")
  @full_name         = opts[:full_name]       or raise ArgumentError.new(":full_name not specified")
  @password          = opts[:password]        or raise ArgumentError.new(":password not specified")

  @box_format        = opts[:box_format]
          = opts[:auto_login]
  @skip_mini_buddy   = opts[:skip_mini_buddy]
  @hidpi             = opts[:hidpi]

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

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

  raise ArgumentError.new(":vmware_path not specified") if @box_format == "vmware_desktop" && !opts[:vmware_path]
  raise ArgumentError.new(":parallels_path not specified") if @box_format == "parallels" && !opts[:parallels_path]
end

Instance Method Details

#automate_user_account_creationObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 126

def 
  Logger.info "Configuring the primary user account..." do
    scratch_installer_configuration_file = "#{@scratch_mountpoint}/private/var/db/.InstallerConfiguration"
    File.write scratch_installer_configuration_file, "      <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n      <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n      <plist version=\"1.0\">\n      <dict>\n        <key>Users</key>\n        <array>\n          <dict>\n            <key>admin</key>\n            <true/>\n            <key>autologin</key>\n            <\#{@auto_login ? true : false}/>\n            <key>fullName</key>\n            <string>\#{@full_name}</string>\n            <key>shortName</key>\n            <string>\#{@short_name}</string>\n            <key>password</key>\n            <string>\#{@password}</string>\n            <key>skipMiniBuddy</key>\n            <\#{@skip_mini_buddy ? true : false}/>\n          </dict>\n        </array>\n      </dict>\n      </plist>\n    EOF\n  end\nend\n"

#automate_vagrant_ssh_key_installationObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 157

def automate_vagrant_ssh_key_installation
  if @short_name == "vagrant"
    Logger.info "Installing the default insecure vagrant ssh key..." do
      contents = "        while [ ! -e /Users/vagrant ]; do\n          sleep 1\n        done\n        while [ `stat -f %u /Users/vagrant` == 0 ]; do\n          sleep 1\n        done\n        if [ ! -e /Users/vagrant/.ssh ]; then\n          mkdir /Users/vagrant/.ssh\n          chmod 0700 /Users/vagrant/.ssh\n          chown `stat -f %u /Users/vagrant` /Users/vagrant/.ssh\n        fi\n        echo \"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key\" >> /Users/vagrant/.ssh/authorized_keys\n        chmod 0600 /Users/vagrant/.ssh/authorized_keys\n        chown `stat -f %u /Users/vagrant` /Users/vagrant/.ssh/authorized_keys\n      EOF\n      File.write @scratch_rc_vagrant, contents, mode: 'a'\n    end\n  end\nend\n"

#create_rc_vagrantObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 109

def create_rc_vagrant
  scratch_rc_installer_cleanup = "#{@scratch_mountpoint}/private/etc/rc.installer_cleanup"
  @scratch_rc_vagrant = "#{@scratch_mountpoint}/private/etc/rc.vagrant"
  File.write scratch_rc_installer_cleanup, "    #!/bin/sh\n    rm -f /etc/rc.installer_cleanup\n    /etc/rc.vagrant &\n    exit 0\n  EOF\n  FileUtils.chmod 0755, scratch_rc_installer_cleanup\n  File.write @scratch_rc_vagrant, <<~EOF\n    #!/bin/sh\n    rm -f /etc/rc.vagrant\n  EOF\n  FileUtils.chmod 0755, @scratch_rc_vagrant\nend\n"

#create_scratch_imageObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 82

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)
    @collector.on_cleanup { @scratch_disk.detach! }
    @scratch_mountpoint = "#{@temp_dir}/scratch_mountpoint"
    FileUtils.mkdir @scratch_mountpoint
    @scratch_disk.create(@disk_size, @fstype)
    @scratch_disk.attach
    @scratch_disk.mount(at: @scratch_mountpoint, owners: true)
  end
end

#create_temp_dirObject



59
60
61
62
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 59

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

#create_wrapper_imageObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 70

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)
    @collector.on_cleanup { @wrapper_disk.detach! }
    @wrapper_disk.create_from_folder(@installer_app)
    @wrapper_disk.attach
    @wrapper_disk.mount
    @installer_app = "#{@wrapper_disk.mountpoint}/#{File.basename @installer_app}"
  end
end

#enable_hidpiObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 199

def enable_hidpi
  if @hidpi
    Logger.info "Enabling HiDPI resolutions..." do
      scratch_windowserver_preferences = "#{@scratch_mountpoint}/Library/Preferences/com.apple.windowserver.plist"
      File.write scratch_windowserver_preferences, "        <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n        <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n        <plist version=\"1.0\">\n        <dict>\n          <key>DisplayResolutionEnabled</key>\n          <true/>\n        </dict>\n        </plist>\n      EOF\n    end\n  end\nend\n"

#enable_passwordless_sudoObject



181
182
183
184
185
186
187
188
189
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 181

def enable_passwordless_sudo
  Logger.info "Enabling password-less sudo..." do
    scratch_sudoers_d_user_rule_file = "#{@scratch_mountpoint}/private/etc/sudoers.d/#{@short_name}"
    File.write scratch_sudoers_d_user_rule_file, "      \#{@short_name} ALL=(ALL) NOPASSWD: ALL\n    EOF\n    FileUtils.chmod 0440, scratch_sudoers_d_user_rule_file\n  end\nend\n"

#enable_sshdObject



191
192
193
194
195
196
197
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 191

def enable_sshd
  Logger.info "Enabling sshd..." do
    scratch_launchd_disabled_plist = "#{@scratch_mountpoint}/private/var/db/com.apple.xpc.launchd/disabled.plist"
    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

#install_macosObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 95

def install_macos
  Logger.info "Installing macOS..." do
    activity = Logger.prefix + "installer"
    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 = $verbose ? {} : { :err => [:child, :out] }
    Task.run_with_progress activity, cmd, opts do |line|
      /^installer:%(.*)$/.match(line)[1].to_f rescue nil
    end
    @wrapper_disk.detach! if @wrapper_disk
  end
end

#installer_is_on_root_filesystemObject



64
65
66
67
68
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 64

def installer_is_on_root_filesystem
  root_device = Task.backtick %W[ /usr/bin/stat -f %d / ]
  installer_device = Task.backtick %W[ /usr/bin/stat -f %d #{@installer_app} ]
  root_device == installer_device
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 42

def run
  create_temp_dir
  if installer_is_on_root_filesystem
    create_wrapper_image
  end
  create_scratch_image
  install_macos
  create_rc_vagrant
  
  automate_vagrant_ssh_key_installation
  enable_passwordless_sudo
  enable_sshd
  enable_hidpi
  run_user_script
  save_image
end

#run_user_scriptObject



217
218
219
220
221
222
223
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 217

def run_user_script
  if @user_script
    Logger.info "Running user script..." do
      Task.run %W[ #{@user_script} #{@scratch_mountpoint} ]
    end
  end
end

#save_imageObject



225
226
227
228
229
230
231
# File 'lib/macinbox/actions/create_image_from_installer.rb', line 225

def save_image
  Logger.info "Saving the image..." do
    @scratch_disk.eject
    FileUtils.chown ENV["SUDO_USER"], nil, @scratch_image
    FileUtils.mv @scratch_image, @output_path
  end
end