Module: WifiWand::Platforms::Mac::Helper::Installer
- Defined in:
- lib/wifi_wand/platforms/mac/helper/installer.rb
Constant Summary collapse
- INSTALL_ROLLBACK_ERROR =
StandardError excludes process-control and VM-level exceptions like Interrupt, SystemExit, and NoMemoryError.
StandardError
Class Method Summary collapse
- .backup_legacy_bundle_metadata(publish_token) ⇒ Object
- .backup_legacy_metadata_file(path, publish_token) ⇒ Object
- .bundle_release_path(publish_token) ⇒ Object
- .cleanup_legacy_bundle_metadata_backups(backup_paths) ⇒ Object
- .cleanup_previous_release(previous_release_path) ⇒ Object
- .ensure_helper_installed(out_stream: $stdout, timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
- .finalize_helper_output_reader(reader, stream, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
- .helper_bundle_valid?(bundle_path, timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Boolean
- .helper_exited_within_grace_period?(wait_thr, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Boolean
- .helper_help_output?(command_output) ⇒ Boolean
- .helper_installed_and_valid?(timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Boolean
- .helper_output_reader(stream) ⇒ Object
- .install_helper_bundle(out_stream: $stdout, force: false, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
- .install_lock_path ⇒ Object
- .install_manifest_path ⇒ Object
- .installed_bundle_current? ⇒ Boolean
- .legacy_code_resources_path ⇒ Object
- .legacy_executable_symlink_path(publish_token) ⇒ Object
- .legacy_info_plist_path ⇒ Object
- .migrate_legacy_bundle_to_release(release_bundle_path, publish_token) ⇒ Object
- .publish_release_symlink(release_bundle_path, publish_token) ⇒ Object
- .publish_staged_bundle(staged_bundle_path) ⇒ Object
- .read_install_manifest ⇒ Object
- .resolved_installed_bundle_target ⇒ Object
- .resolved_legacy_release_target ⇒ Object
- .restore_legacy_bundle_metadata(backup_paths, publish_token) ⇒ Object
- .restore_legacy_metadata_file(backup_path, target_path, publish_token) ⇒ Object
- .run_bounded_helper_command(executable_path, command, timeout_seconds: nil, on_timeout: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
- .stage_helper_bundle(staged_bundle_path) ⇒ Object
- .staged_bundle_symlink_path(publish_token) ⇒ Object
- .switch_legacy_bundle_executable(release_bundle_path, publish_token) ⇒ Object
- .sync_legacy_bundle_metadata(release_bundle_path, publish_token) ⇒ Object
- .terminate_helper_process(wait_thr, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
- .unique_publish_token ⇒ Object
- .with_install_lock ⇒ Object
- .write_manifest ⇒ Object
Class Method Details
.backup_legacy_bundle_metadata(publish_token) ⇒ Object
323 324 325 326 327 328 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 323 module_function def (publish_token) { info_plist: (legacy_info_plist_path, publish_token), code_resources: (legacy_code_resources_path, publish_token), } end |
.backup_legacy_metadata_file(path, publish_token) ⇒ Object
330 331 332 333 334 335 336 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 330 module_function def (path, publish_token) return unless File.exist?(path) backup_path = "#{path}.backup-#{publish_token}" FileUtils.cp(path, backup_path) backup_path end |
.bundle_release_path(publish_token) ⇒ Object
267 268 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 267 module_function def bundle_release_path(publish_token) = File.join(Bundle.versioned_install_dir, ".#{Bundle::BUNDLE_NAME}.release-#{publish_token}") |
.cleanup_legacy_bundle_metadata_backups(backup_paths) ⇒ Object
354 355 356 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 354 module_function def (backup_paths) FileUtils.rm_f(backup_paths.values.compact) end |
.cleanup_previous_release(previous_release_path) ⇒ Object
294 295 296 297 298 299 300 301 302 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 294 module_function def cleanup_previous_release(previous_release_path) return unless previous_release_path prefix = "#{Bundle.versioned_install_dir}/.#{Bundle::BUNDLE_NAME}.release-" return unless previous_release_path.start_with?(prefix) return if previous_release_path == resolved_installed_bundle_target FileUtils.rm_rf(previous_release_path) end |
.ensure_helper_installed(out_stream: $stdout, timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 29 module_function def ensure_helper_installed(out_stream: $stdout, timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) if helper_installed_and_valid?( timeout_seconds: timeout_seconds, timeout_configuration: timeout_configuration ) return Bundle.installed_bundle_path end install_helper_bundle(out_stream: out_stream, timeout_configuration: timeout_configuration) unless helper_installed_and_valid?( timeout_seconds: timeout_seconds, timeout_configuration: timeout_configuration ) raise 'Helper installation failed validation after installation.' end Bundle.installed_bundle_path end |
.finalize_helper_output_reader(reader, stream, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 172 module_function def finalize_helper_output_reader(reader, stream, timeout_configuration: Bundle.default_timeout_configuration) return unless reader begin stream.close unless stream.closed? rescue IOError nil ensure reader.join(timeout_configuration.helper_output_reader_join_seconds) end end |
.helper_bundle_valid?(bundle_path, timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Boolean
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 87 module_function def helper_bundle_valid?(bundle_path, timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) executable_path = File.join(bundle_path, 'Contents', 'MacOS', Bundle::EXECUTABLE_NAME) return false unless File.executable?(executable_path) return false unless File.exist?(File.join(bundle_path, 'Contents', 'Info.plist')) = {} [:timeout_seconds] = timeout_seconds if timeout_seconds helper_result = run_bounded_helper_command( executable_path, 'help', timeout_configuration: timeout_configuration, ** ) return false unless helper_result return false unless helper_result[:status].success? command_output = "#{helper_result[:stdout]}#{helper_result[:stderr]}".strip helper_help_output?(command_output) end |
.helper_exited_within_grace_period?(wait_thr, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Boolean
200 201 202 203 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 200 module_function def helper_exited_within_grace_period?(wait_thr, timeout_configuration: Bundle.default_timeout_configuration) !!wait_thr.join(timeout_configuration.helper_termination_wait_seconds) end |
.helper_help_output?(command_output) ⇒ Boolean
108 109 110 111 112 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 108 module_function def helper_help_output?(command_output) return false if command_output.empty? command_output.match?(/wifiwand helper|usage:/i) end |
.helper_installed_and_valid?(timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 17 module_function def helper_installed_and_valid?(timeout_seconds: nil, timeout_configuration: Bundle.default_timeout_configuration) = {} [:timeout_seconds] = timeout_seconds if timeout_seconds Bundle.helper_bundle_valid?( Bundle.installed_bundle_path, timeout_configuration: timeout_configuration, ** ) && Bundle.installed_bundle_current? end |
.helper_output_reader(stream) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 163 module_function def helper_output_reader(stream) Thread.new do Thread.current.report_on_exception = false stream.read rescue IOError '' end end |
.install_helper_bundle(out_stream: $stdout, force: false, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
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 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 50 module_function def install_helper_bundle(out_stream: $stdout, force: false, timeout_configuration: Bundle.default_timeout_configuration) with_install_lock do if !force && helper_installed_and_valid?(timeout_configuration: timeout_configuration) return Bundle.installed_bundle_path end out_stream&.puts 'Installing WifiWand macOS helper...' Dir.mktmpdir( "#{Bundle::BUNDLE_NAME}.tmp-", Bundle.versioned_install_dir ) do |temp_dir| staged_bundle_path = File.join(temp_dir, Bundle::BUNDLE_NAME) stage_helper_bundle(staged_bundle_path) unless helper_bundle_valid?(staged_bundle_path, timeout_configuration: timeout_configuration) raise 'Staged helper installation failed validation.' end publish_staged_bundle(staged_bundle_path) write_manifest end end out_stream&.puts('Helper bundle installed from pre-signed binary.') Bundle.installed_bundle_path end |
.install_lock_path ⇒ Object
80 81 82 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 80 module_function def install_lock_path File.join(Bundle.versioned_install_dir, '.install.lock') end |
.install_manifest_path ⇒ Object
84 85 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 84 module_function def install_manifest_path = File.join(Bundle.versioned_install_dir, Bundle::MANIFEST_FILENAME) |
.installed_bundle_current? ⇒ Boolean
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 114 module_function def installed_bundle_current? manifest = read_install_manifest if manifest manifest['helper_version'] == Bundle.helper_version && manifest['bundle_fingerprint'] == Bundle.bundle_fingerprint(Bundle.installed_bundle_path) && manifest['bundle_fingerprint'] == Bundle.bundle_fingerprint(Bundle.source_bundle_path) else false end end |
.legacy_code_resources_path ⇒ Object
282 283 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 282 module_function def legacy_code_resources_path = File.join(Bundle.installed_bundle_path, 'Contents', '_CodeSignature', 'CodeResources') |
.legacy_executable_symlink_path(publish_token) ⇒ Object
274 275 276 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 274 module_function def legacy_executable_symlink_path(publish_token) "#{Bundle.installed_executable_path}.link-#{publish_token}" end |
.legacy_info_plist_path ⇒ Object
278 279 280 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 278 module_function def legacy_info_plist_path File.join(Bundle.installed_bundle_path, 'Contents', 'Info.plist') end |
.migrate_legacy_bundle_to_release(release_bundle_path, publish_token) ⇒ Object
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 256 module_function def migrate_legacy_bundle_to_release(release_bundle_path, publish_token) backup_paths = (publish_token) (release_bundle_path, publish_token) switch_legacy_bundle_executable(release_bundle_path, publish_token) rescue INSTALL_ROLLBACK_ERROR (backup_paths, publish_token) raise ensure (backup_paths) if defined?(backup_paths) end |
.publish_release_symlink(release_bundle_path, publish_token) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 233 module_function def publish_release_symlink(release_bundle_path, publish_token) symlink_path = staged_bundle_symlink_path(publish_token) File.symlink(File.basename(release_bundle_path), symlink_path) if File.symlink?(Bundle.installed_bundle_path) previous_release_path = resolved_installed_bundle_target File.rename(symlink_path, Bundle.installed_bundle_path) cleanup_previous_release(previous_release_path) return end if File.exist?(Bundle.installed_bundle_path) FileUtils.rm_f(symlink_path) if File.symlink?(symlink_path) migrate_legacy_bundle_to_release(release_bundle_path, publish_token) return end File.rename(symlink_path, Bundle.installed_bundle_path) rescue INSTALL_ROLLBACK_ERROR FileUtils.rm_f(symlink_path) if File.symlink?(symlink_path) raise end |
.publish_staged_bundle(staged_bundle_path) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 220 module_function def publish_staged_bundle(staged_bundle_path) publish_token = unique_publish_token release_bundle_path = bundle_release_path(publish_token) File.rename(staged_bundle_path, release_bundle_path) begin publish_release_symlink(release_bundle_path, publish_token) rescue INSTALL_ROLLBACK_ERROR FileUtils.rm_rf(release_bundle_path) raise end end |
.read_install_manifest ⇒ Object
402 403 404 405 406 407 408 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 402 module_function def read_install_manifest if File.exist?(install_manifest_path) JSON.parse(File.read(install_manifest_path)) end rescue JSON::ParserError nil end |
.resolved_installed_bundle_target ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 285 module_function def resolved_installed_bundle_target return unless File.symlink?(Bundle.installed_bundle_path) File.( File.readlink(Bundle.installed_bundle_path), Bundle.versioned_install_dir ) end |
.resolved_legacy_release_target ⇒ Object
374 375 376 377 378 379 380 381 382 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 374 module_function def resolved_legacy_release_target return unless File.symlink?(Bundle.installed_executable_path) File.( File.readlink(Bundle.installed_executable_path), Bundle.versioned_install_dir ) .sub(%r{/Contents/MacOS/[^/]+\z}, '') end |
.restore_legacy_bundle_metadata(backup_paths, publish_token) ⇒ Object
338 339 340 341 342 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 338 module_function def (backup_paths, publish_token) (backup_paths[:info_plist], legacy_info_plist_path, publish_token) (backup_paths[:code_resources], legacy_code_resources_path, publish_token) end |
.restore_legacy_metadata_file(backup_path, target_path, publish_token) ⇒ Object
344 345 346 347 348 349 350 351 352 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 344 module_function def (backup_path, target_path, publish_token) return unless backup_path && File.exist?(backup_path) staged_restore_path = "#{target_path}.restore-#{publish_token}" FileUtils.cp(backup_path, staged_restore_path) File.rename(staged_restore_path, target_path) ensure FileUtils.rm_f(staged_restore_path) if defined?(staged_restore_path) end |
.run_bounded_helper_command(executable_path, command, timeout_seconds: nil, on_timeout: nil, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
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 156 157 158 159 160 161 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 128 module_function def run_bounded_helper_command( executable_path, command, timeout_seconds: nil, on_timeout: nil, timeout_configuration: Bundle.default_timeout_configuration ) Open3.popen3(executable_path, command) do |stdin, stdout, stderr, wait_thr| stdin.close stdout_reader = helper_output_reader(stdout) stderr_reader = helper_output_reader(stderr) timeout_seconds ||= Bundle.helper_command_timeout_seconds( command, timeout_configuration: timeout_configuration ) wait_result = wait_thr.join(timeout_seconds) unless wait_result on_timeout&.call(command, timeout_seconds) terminate_helper_process(wait_thr, timeout_configuration: timeout_configuration) return nil end { stdout: stdout_reader.value, stderr: stderr_reader.value, status: wait_thr.value, } ensure finalize_helper_output_reader(stdout_reader, stdout, timeout_configuration: timeout_configuration) finalize_helper_output_reader(stderr_reader, stderr, timeout_configuration: timeout_configuration) end rescue Errno::ENOENT nil end |
.stage_helper_bundle(staged_bundle_path) ⇒ Object
216 217 218 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 216 module_function def stage_helper_bundle(staged_bundle_path) FileUtils.cp_r(Bundle.source_bundle_path, staged_bundle_path) end |
.staged_bundle_symlink_path(publish_token) ⇒ Object
270 271 272 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 270 module_function def staged_bundle_symlink_path(publish_token) "#{Bundle.installed_bundle_path}.link-#{publish_token}" end |
.switch_legacy_bundle_executable(release_bundle_path, publish_token) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 358 module_function def switch_legacy_bundle_executable(release_bundle_path, publish_token) previous_release_path = resolved_legacy_release_target staged_executable_link_path = legacy_executable_symlink_path(publish_token) release_executable_path = File.join(release_bundle_path, 'Contents', 'MacOS', Bundle::EXECUTABLE_NAME) File.symlink(release_executable_path, staged_executable_link_path) File.rename(staged_executable_link_path, Bundle.installed_executable_path) cleanup_previous_release(previous_release_path) rescue INSTALL_ROLLBACK_ERROR staged_link_needs_cleanup = defined?(staged_executable_link_path) && File.symlink?(staged_executable_link_path) FileUtils.rm_f(staged_executable_link_path) if staged_link_needs_cleanup raise end |
.sync_legacy_bundle_metadata(release_bundle_path, publish_token) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 304 module_function def (release_bundle_path, publish_token) release_info_plist_path = File.join(release_bundle_path, 'Contents', 'Info.plist') staged_info_plist_path = "#{legacy_info_plist_path}.tmp-#{publish_token}" release_code_resources_path = File.join(release_bundle_path, 'Contents', '_CodeSignature', 'CodeResources') staged_code_resources_path = "#{legacy_code_resources_path}.tmp-#{publish_token}" FileUtils.mkdir_p(File.dirname(legacy_info_plist_path)) FileUtils.cp(release_info_plist_path, staged_info_plist_path) File.rename(staged_info_plist_path, legacy_info_plist_path) FileUtils.mkdir_p(File.dirname(legacy_code_resources_path)) FileUtils.cp(release_code_resources_path, staged_code_resources_path) File.rename(staged_code_resources_path, legacy_code_resources_path) ensure FileUtils.rm_f(staged_info_plist_path) if defined?(staged_info_plist_path) FileUtils.rm_f(staged_code_resources_path) if defined?(staged_code_resources_path) end |
.terminate_helper_process(wait_thr, timeout_configuration: Bundle.default_timeout_configuration) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 185 module_function def terminate_helper_process(wait_thr, timeout_configuration: Bundle.default_timeout_configuration) pid = wait_thr.pid Process.kill('TERM', pid) return if helper_exited_within_grace_period?(wait_thr, timeout_configuration: timeout_configuration) return unless wait_thr.alive? Process.kill('KILL', pid) helper_exited_within_grace_period?(wait_thr, timeout_configuration: timeout_configuration) rescue Errno::ESRCH, Errno::ECHILD nil end |
.unique_publish_token ⇒ Object
384 385 386 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 384 module_function def unique_publish_token "#{Process.pid}-#{Thread.current.object_id}-#{SecureRandom.hex(6)}" end |
.with_install_lock ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 205 module_function def with_install_lock FileUtils.mkdir_p(Bundle.versioned_install_dir) File.open(install_lock_path, File::RDWR | File::CREAT, 0o644) do |lock_file| lock_file.flock(File::LOCK_EX) yield ensure lock_file.flock(File::LOCK_UN) end end |
.write_manifest ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/wifi_wand/platforms/mac/helper/installer.rb', line 388 module_function def write_manifest FileUtils.mkdir_p(Bundle.versioned_install_dir) File.write( File.join(Bundle.versioned_install_dir, 'VERSION'), Bundle.helper_version ) File.write(install_manifest_path, JSON.pretty_generate( { 'helper_version' => Bundle.helper_version, 'bundle_fingerprint' => Bundle.bundle_fingerprint(Bundle.source_bundle_path), } )) end |