inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#![allow(dead_code, unused_imports, unused_variables, clippy::ptr_arg)]
//! # macOS-Specific Upgrade Handler
//!
//! Handles macOS-specific upgrade operations including App Bundle management,
//! code signing verification, and system integration.

use super::{
    PlatformUpgradeHandler, UpgradeConfig, UpgradeError, UpgradeResult,
    platform::BasePlatformHandler,
};
use anyhow::Result;
use std::path::PathBuf;
use std::process::Command;
use tracing::{debug, info, warn};

/// macOS-specific upgrade handler
pub struct MacOSUpgradeHandler {
    base: BasePlatformHandler,
}

impl MacOSUpgradeHandler {
    /// Create a new macOS upgrade handler
    pub fn new(config: &UpgradeConfig) -> Result<Self> {
        let base = BasePlatformHandler::new(config)?;

        Ok(Self { base })
    }

    /// Install from macOS App Bundle (.app)
    async fn install_app_bundle(&self, package_path: &PathBuf) -> UpgradeResult<()> {
        info!("Installing macOS App Bundle: {:?}", package_path);

        // Verify it's a valid App Bundle
        if !self.is_valid_app_bundle(package_path)? {
            return Err(UpgradeError::InvalidPackage(
                "Invalid macOS App Bundle".to_string(),
            ));
        }

        // Verify code signature if enabled
        if self.base.config.require_signatures {
            self.verify_code_signature(package_path).await?;
        }

        // Get installation directory (usually /Applications)
        let install_dir = PathBuf::from("/Applications");
        let app_name = package_path
            .file_name()
            .ok_or_else(|| UpgradeError::InvalidPackage("Invalid app bundle name".to_string()))?;

        let target_path = install_dir.join(app_name);

        // Stop any running instances of the app
        self.quit_application().await?;

        // Remove existing app bundle if it exists
        if target_path.exists() {
            self.remove_app_bundle(&target_path).await?;
        }

        // Copy new app bundle to Applications
        self.copy_app_bundle(package_path, &target_path).await?;

        // Update Launch Services database
        self.update_launch_services().await?;

        info!("macOS App Bundle installation completed");
        Ok(())
    }

    /// Install from Homebrew
    async fn install_homebrew(&self, package_name: &str) -> UpgradeResult<()> {
        info!("Installing via Homebrew: {}", package_name);

        // Check if Homebrew is installed
        if !self.is_homebrew_installed() {
            return Err(UpgradeError::InstallationFailed(
                "Homebrew not installed".to_string(),
            ));
        }

        // Update Homebrew
        let output = Command::new("brew")
            .arg("update")
            .output()
            .map_err(|e| UpgradeError::InstallationFailed(e.to_string()))?;

        if !output.status.success() {
            warn!(
                "Homebrew update failed: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        // Install or upgrade the package
        let output = Command::new("brew")
            .args(["upgrade", package_name])
            .output()
            .map_err(|e| UpgradeError::InstallationFailed(e.to_string()))?;

        if !output.status.success() {
            // If upgrade fails, try install
            let output = Command::new("brew")
                .args(["install", package_name])
                .output()
                .map_err(|e| UpgradeError::InstallationFailed(e.to_string()))?;

            if !output.status.success() {
                return Err(UpgradeError::InstallationFailed(format!(
                    "Homebrew installation failed: {}",
                    String::from_utf8_lossy(&output.stderr)
                )));
            }
        }

        info!("Homebrew installation completed");
        Ok(())
    }

    /// Verify macOS code signature
    async fn verify_code_signature(&self, app_path: &PathBuf) -> UpgradeResult<()> {
        debug!("Verifying code signature for: {:?}", app_path);

        let output = Command::new("codesign")
            .args(["--verify", "--deep", "--strict"])
            .arg(app_path)
            .output()
            .map_err(|e| UpgradeError::VerificationFailed(e.to_string()))?;

        if !output.status.success() {
            return Err(UpgradeError::VerificationFailed(format!(
                "Code signature verification failed: {}",
                String::from_utf8_lossy(&output.stderr)
            )));
        }

        // Check if the signature is from a trusted developer
        let output = Command::new("codesign")
            .args(["-dv", "--verbose=4"])
            .arg(app_path)
            .output()
            .map_err(|e| UpgradeError::VerificationFailed(e.to_string()))?;

        let signature_info = String::from_utf8_lossy(&output.stderr);
        debug!("Code signature info: {}", signature_info);

        // In a production system, you would check against known developer certificates
        if signature_info.contains("Developer ID Application")
            || signature_info.contains("Mac App Store")
        {
            debug!("Code signature verification passed");
            Ok(())
        } else {
            Err(UpgradeError::VerificationFailed(
                "Untrusted code signature".to_string(),
            ))
        }
    }

    /// Check if path is a valid App Bundle
    fn is_valid_app_bundle(&self, path: &PathBuf) -> UpgradeResult<bool> {
        if !path.exists() || !path.is_dir() {
            return Ok(false);
        }

        let extension = path.extension().and_then(|ext| ext.to_str());

        if extension != Some("app") {
            return Ok(false);
        }

        // Check for required App Bundle structure
        let contents_dir = path.join("Contents");
        let info_plist = contents_dir.join("Info.plist");
        let macos_dir = contents_dir.join("MacOS");

        Ok(contents_dir.exists() && info_plist.exists() && macos_dir.exists())
    }

    /// Quit the application gracefully
    async fn quit_application(&self) -> UpgradeResult<()> {
        debug!("Attempting to quit application gracefully");

        // Try to quit via AppleScript first
        let output = Command::new("osascript")
            .args(["-e", "tell application \"Inferno\" to quit"])
            .output();

        if let Ok(output) = output {
            if output.status.success() {
                // Wait for application to quit
                tokio::time::sleep(std::time::Duration::from_secs(3)).await;
                return Ok(());
            }
        }

        // Fallback to force quit
        let output = Command::new("pkill").args(["-f", "Inferno"]).output();

        if let Ok(output) = output {
            if output.status.success() {
                tokio::time::sleep(std::time::Duration::from_secs(2)).await;
            }
        }

        Ok(())
    }

    /// Remove existing app bundle
    async fn remove_app_bundle(&self, app_path: &PathBuf) -> UpgradeResult<()> {
        debug!("Removing existing app bundle: {:?}", app_path);

        // Use rm -rf for complete removal
        let output = Command::new("rm")
            .args(["-rf", app_path.to_str().unwrap()])
            .output()
            .map_err(|e| UpgradeError::InstallationFailed(e.to_string()))?;

        if !output.status.success() {
            return Err(UpgradeError::InstallationFailed(format!(
                "Failed to remove existing app bundle: {}",
                String::from_utf8_lossy(&output.stderr)
            )));
        }

        Ok(())
    }

    /// Copy app bundle to target location
    async fn copy_app_bundle(&self, source: &PathBuf, target: &PathBuf) -> UpgradeResult<()> {
        debug!("Copying app bundle from {:?} to {:?}", source, target);

        let output = Command::new("cp")
            .args(["-R", source.to_str().unwrap(), target.to_str().unwrap()])
            .output()
            .map_err(|e| UpgradeError::InstallationFailed(e.to_string()))?;

        if !output.status.success() {
            return Err(UpgradeError::InstallationFailed(format!(
                "Failed to copy app bundle: {}",
                String::from_utf8_lossy(&output.stderr)
            )));
        }

        Ok(())
    }

    /// Update Launch Services database
    async fn update_launch_services(&self) -> UpgradeResult<()> {
        debug!("Updating Launch Services database");

        let output = Command::new("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister")
            .args(["-kill", "-r", "-domain", "local", "-domain", "system", "-domain", "user"])
            .output();

        if let Ok(output) = output {
            if !output.status.success() {
                warn!(
                    "Launch Services update failed: {}",
                    String::from_utf8_lossy(&output.stderr)
                );
            }
        } else {
            warn!("Could not update Launch Services database");
        }

        Ok(())
    }

    /// Check if Homebrew is installed
    fn is_homebrew_installed(&self) -> bool {
        Command::new("brew")
            .arg("--version")
            .output()
            .map(|output| output.status.success())
            .unwrap_or(false)
    }

    /// Get app bundle info from Info.plist
    fn get_app_info(&self, app_path: &PathBuf) -> UpgradeResult<AppBundleInfo> {
        let info_plist = app_path.join("Contents/Info.plist");

        let output = Command::new("plutil")
            .args(["-convert", "json", "-o", "-"])
            .arg(&info_plist)
            .output()
            .map_err(|e| UpgradeError::InvalidPackage(e.to_string()))?;

        if !output.status.success() {
            return Err(UpgradeError::InvalidPackage(
                "Failed to read Info.plist".to_string(),
            ));
        }

        let json_str = String::from_utf8(output.stdout)
            .map_err(|e| UpgradeError::InvalidPackage(e.to_string()))?;

        let info: serde_json::Value = serde_json::from_str(&json_str)
            .map_err(|e| UpgradeError::InvalidPackage(e.to_string()))?;

        Ok(AppBundleInfo {
            bundle_identifier: info["CFBundleIdentifier"]
                .as_str()
                .unwrap_or("unknown")
                .to_string(),
            bundle_version: info["CFBundleVersion"]
                .as_str()
                .unwrap_or("unknown")
                .to_string(),
            bundle_name: info["CFBundleName"]
                .as_str()
                .unwrap_or("unknown")
                .to_string(),
        })
    }

    /// Check for macOS system updates that might interfere
    async fn check_system_updates(&self) -> UpgradeResult<()> {
        debug!("Checking for macOS system updates");

        let output = Command::new("softwareupdate")
            .args(["--list", "--no-scan"])
            .output()
            .map_err(|e| UpgradeError::Internal(e.to_string()))?;

        if output.status.success() {
            let update_list = String::from_utf8_lossy(&output.stdout);
            if update_list.contains("restart") {
                warn!(
                    "System updates requiring restart are available. Consider installing them after the upgrade."
                );
            }
        }

        Ok(())
    }

    /// Enable/disable Gatekeeper temporarily if needed
    async fn manage_gatekeeper(&self, disable: bool) -> UpgradeResult<()> {
        if disable {
            debug!("Temporarily disabling Gatekeeper");
            let output = Command::new("sudo")
                .args(["spctl", "--master-disable"])
                .output();

            if let Ok(output) = output {
                if !output.status.success() {
                    warn!(
                        "Failed to disable Gatekeeper: {}",
                        String::from_utf8_lossy(&output.stderr)
                    );
                }
            }
        } else {
            debug!("Re-enabling Gatekeeper");
            let output = Command::new("sudo")
                .args(["spctl", "--master-enable"])
                .output();

            if let Ok(output) = output {
                if !output.status.success() {
                    warn!(
                        "Failed to re-enable Gatekeeper: {}",
                        String::from_utf8_lossy(&output.stderr)
                    );
                }
            }
        }

        Ok(())
    }
}

#[async_trait::async_trait]
impl PlatformUpgradeHandler for MacOSUpgradeHandler {
    fn supports_seamless_upgrade(&self) -> bool {
        self.base.supports_seamless_upgrade()
    }

    async fn prepare_for_upgrade(&self) -> Result<()> {
        info!("Preparing macOS system for upgrade");

        // Stop services
        let _stopped = self.base.stop_services().await?;

        // Check for system updates
        self.check_system_updates().await?;

        // Temporarily disable Gatekeeper if needed for unsigned packages
        if !self.base.config.require_signatures {
            self.manage_gatekeeper(true).await?;
        }

        Ok(())
    }

    async fn install_update(&self, package_path: &PathBuf) -> Result<()> {
        let extension = package_path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("");

        match extension.to_lowercase().as_str() {
            "app" => {
                self.install_app_bundle(package_path).await?;
            }
            "pkg" => {
                // Use macOS installer for PKG files
                let output = Command::new("installer")
                    .args(["-pkg", package_path.to_str().unwrap(), "-target", "/"])
                    .output()
                    .map_err(|e| anyhow::anyhow!("PKG installation failed: {}", e))?;

                if !output.status.success() {
                    return Err(anyhow::anyhow!(
                        "PKG installation failed: {}",
                        String::from_utf8_lossy(&output.stderr)
                    ));
                }
            }
            "tar" | "tgz" | "tar.gz" => {
                self.base.install_self_extractor(package_path).await?;
            }
            _ => {
                return Err(anyhow::anyhow!(
                    "Unsupported package format for macOS: {}",
                    extension
                ));
            }
        }

        Ok(())
    }

    async fn restart_application(&self) -> Result<()> {
        info!("Restarting application on macOS");

        let current_exe = std::env::current_exe()?;

        // Launch the application in the background
        let _child = Command::new("nohup")
            .arg(&current_exe)
            .arg("serve") // Start in server mode
            .spawn()?;

        Ok(())
    }

    async fn verify_installation(&self) -> Result<bool> {
        self.base.verify_installation().await.map_err(Into::into)
    }

    async fn cleanup_after_upgrade(&self) -> Result<()> {
        // Re-enable Gatekeeper if it was disabled
        if !self.base.config.require_signatures {
            self.manage_gatekeeper(false).await?;
        }

        self.base.cleanup_after_upgrade().await?;
        Ok(())
    }

    fn requires_elevated_privileges(&self) -> bool {
        self.base.requires_elevated_privileges()
    }

    fn get_installation_directory(&self) -> PathBuf {
        self.base.get_installation_directory()
    }

    fn get_backup_directory(&self) -> PathBuf {
        self.base.get_backup_directory()
    }
}

/// App Bundle information
#[derive(Debug, Clone)]
struct AppBundleInfo {
    bundle_identifier: String,
    bundle_version: String,
    bundle_name: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_macos_handler_creation() {
        let config = UpgradeConfig::default();
        let handler = MacOSUpgradeHandler::new(&config);

        #[cfg(target_os = "macos")]
        {
            assert!(handler.is_ok());
            let handler = handler.unwrap();
            assert!(handler.supports_seamless_upgrade());
        }

        #[cfg(not(target_os = "macos"))]
        {
            // On non-macOS platforms, we still want the code to compile
            // but the handler creation might not work
            println!("macOS handler test skipped on non-macOS platform");
        }
    }

    #[test]
    fn test_homebrew_detection() {
        let config = UpgradeConfig::default();

        #[cfg(target_os = "macos")]
        {
            if let Ok(handler) = MacOSUpgradeHandler::new(&config) {
                let has_homebrew = handler.is_homebrew_installed();
                println!("Homebrew installed: {}", has_homebrew);
            }
        }
    }
}