rusty-stack 0.3.2

A comprehensive ML environment installer and platform toolkit optimized for AMD GPUs with ROCm support
Documentation
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
//! Legacy adapter — invokes shell scripts with correct argument forwarding.
//!
//! This module provides:
//! - [`LegacyAdapter`] — invokes `install_*.sh` scripts with correct arguments
//!
//! # Design
//!
//! The legacy adapter is the fallback executor for components that don't yet
//! have Rust-native implementations. It discovers the correct shell script
//! from the component registry, forwards arguments correctly, and captures
//! stdout/stderr and exit codes for parity verification.
//!
//! # Validation Assertions
//!
//! - **VAL-MIGR-010**: No Rust adapter → falls back to install_*.sh with correct args
//! - **VAL-MIGR-012**: Output format matches shell for migrated components
//! - **VAL-MIGR-013**: Exit codes match shell for success/failure/partial cases

use super::{Adapter, AdapterError, AdapterOutput};
use crate::core::types::ExecutorKind;
use std::path::PathBuf;
use std::process::Command;
use std::time::Instant;

// ===========================================================================
// LegacyAdapter
// ===========================================================================

/// Adapter that invokes legacy shell scripts for component installation.
///
/// The adapter discovers the correct `install_*.sh` script from the component
/// registry and executes it via `bash` with correct argument forwarding.
///
/// # Script Discovery
///
/// For a component like `pytorch`, the adapter looks up the installer script
/// from the registry (e.g., `install_pytorch_rocm.sh`) and resolves it
/// relative to the configured scripts directory.
///
/// # Argument Forwarding
///
/// The adapter forwards the component version as an argument to the script:
/// ```bash
/// bash <scripts_dir>/install_pytorch_rocm.sh <version>
/// ```
///
/// Some scripts may accept additional environment variables or flags;
/// the adapter passes these through the process environment.
pub struct LegacyAdapter {
    /// Root directory containing install scripts.
    scripts_dir: PathBuf,
}

impl LegacyAdapter {
    /// Create a new legacy adapter with the given scripts directory.
    pub fn new(scripts_dir: impl Into<PathBuf>) -> Self {
        Self {
            scripts_dir: scripts_dir.into(),
        }
    }

    /// Resolve the script path for a component ID.
    ///
    /// Uses the component registry to find the installer script name,
    /// then resolves it relative to the scripts directory.
    pub fn resolve_script_path(&self, component_id: &str) -> Option<PathBuf> {
        let info = crate::platform::registry::get_component(component_id)?;
        if info.installer_script.is_empty() {
            return None;
        }
        Some(self.scripts_dir.join(&info.installer_script))
    }

    /// Build the command for invoking the install script.
    ///
    /// The command is: `bash <script_path> <version>`
    pub fn build_command(
        &self,
        component_id: &str,
        version: &str,
    ) -> Result<Command, AdapterError> {
        let script_path = self.resolve_script_path(component_id).ok_or_else(|| {
            AdapterError::AdapterNotFound {
                component_id: component_id.to_string(),
            }
        })?;

        let mut cmd = Command::new("bash");
        cmd.arg(&script_path);
        if !version.is_empty() {
            cmd.arg(version);
        }

        Ok(cmd)
    }
}

impl Adapter for LegacyAdapter {
    fn execute(&self, component_id: &str, version: &str) -> Result<AdapterOutput, AdapterError> {
        let start = Instant::now();

        let script_path = self.resolve_script_path(component_id).ok_or_else(|| {
            AdapterError::AdapterNotFound {
                component_id: component_id.to_string(),
            }
        })?;

        // Check if the script exists
        if !script_path.exists() {
            return Err(AdapterError::InvocationError {
                component_id: component_id.to_string(),
                reason: format!("script not found: {}", script_path.display()),
            });
        }

        // Build and execute the command
        let mut cmd = Command::new("bash");
        cmd.arg(&script_path);
        if !version.is_empty() {
            cmd.arg(version);
        }

        // Capture stdout and stderr
        let output = cmd.output().map_err(|e| AdapterError::InvocationError {
            component_id: component_id.to_string(),
            reason: format!("failed to execute script: {e}"),
        })?;

        let stdout = String::from_utf8_lossy(&output.stdout).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();
        let exit_code = output.status.code().unwrap_or(-1);
        let duration_ms = start.elapsed().as_millis() as u64;

        Ok(AdapterOutput {
            component_id: component_id.to_string(),
            executor_kind: ExecutorKind::LegacyScript,
            stdout,
            stderr,
            exit_code,
            duration_ms,
        })
    }

    fn executor_kind(&self) -> ExecutorKind {
        ExecutorKind::LegacyScript
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    // -----------------------------------------------------------------------
    // Test Helpers
    // -----------------------------------------------------------------------

    /// Create a temp directory with a mock install script.
    fn create_mock_script_dir() -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::tempdir().unwrap();
        let scripts_dir = dir.path().to_path_buf();

        // Create mock install scripts
        std::fs::write(
            scripts_dir.join("install_pytorch_rocm.sh"),
            "#!/bin/bash\necho \"PyTorch $1 installed successfully\"\nexit 0\n",
        )
        .unwrap();

        std::fs::write(
            scripts_dir.join("install_triton_multi.sh"),
            "#!/bin/bash\necho \"Triton $1 installed successfully\"\nexit 0\n",
        )
        .unwrap();

        std::fs::write(
            scripts_dir.join("install_flash_attention_ck.sh"),
            "#!/bin/bash\necho \"Flash Attention $1 installed successfully\"\nexit 0\n",
        )
        .unwrap();

        // A script that fails
        std::fs::write(
            scripts_dir.join("install_deepspeed.sh"),
            "#!/bin/bash\necho \"Error: DeepSpeed $1 installation failed\" >&2\nexit 1\n",
        )
        .unwrap();

        // A script that partially succeeds
        std::fs::write(
            scripts_dir.join("install_vllm_multi.sh"),
            "#!/bin/bash\necho \"vLLM $1 partially installed\"\necho \"Warning: some deps missing\" >&2\nexit 2\n",
        )
        .unwrap();

        (dir, scripts_dir)
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-010: Legacy fallback with correct argument forwarding
    // -----------------------------------------------------------------------

    #[test]
    fn test_legacy_adapter_forwards_version_argument() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let result = adapter.execute("pytorch", "2.5.0").unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("2.5.0"));
        assert!(result.stdout.contains("PyTorch"));
    }

    #[test]
    fn test_legacy_adapter_forwards_correct_script_per_component() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        // PyTorch → install_pytorch_rocm.sh
        let result = adapter.execute("pytorch", "2.5.0").unwrap();
        assert!(result.stdout.contains("PyTorch"));

        // Triton → install_triton_multi.sh
        let result = adapter.execute("triton", "3.1.0").unwrap();
        assert!(result.stdout.contains("Triton"));
        assert!(result.stdout.contains("3.1.0"));
    }

    #[test]
    fn test_legacy_adapter_forwards_multiple_components() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        // Flash Attention → install_flash_attention_ck.sh
        let result = adapter.execute("flash-attn", "2.6.0").unwrap();
        assert!(result.stdout.contains("Flash Attention"));
        assert!(result.stdout.contains("2.6.0"));
        assert_eq!(result.exit_code, 0);
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-012: Output format matches shell
    // -----------------------------------------------------------------------

    #[test]
    fn test_legacy_adapter_captures_stdout() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let result = adapter.execute("pytorch", "2.5.0").unwrap();
        assert!(!result.stdout.is_empty());
        assert!(result.stdout.contains("installed"));
    }

    #[test]
    fn test_legacy_adapter_captures_stderr() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        // DeepSpeed script writes to stderr
        let result = adapter.execute("deepspeed", "0.14.0").unwrap();
        assert!(!result.stderr.is_empty());
        assert!(result.stderr.contains("Error"));
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-013: Exit codes match shell for success/failure/partial
    // -----------------------------------------------------------------------

    #[test]
    fn test_legacy_adapter_exit_code_success() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let result = adapter.execute("pytorch", "2.5.0").unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.is_success());
    }

    #[test]
    fn test_legacy_adapter_exit_code_failure() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let result = adapter.execute("deepspeed", "0.14.0").unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(!result.is_success());
    }

    #[test]
    fn test_legacy_adapter_exit_code_partial() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let result = adapter.execute("vllm", "0.6.0").unwrap();
        assert_eq!(result.exit_code, 2);
        assert!(!result.is_success());
        assert!(!result.stdout.is_empty());
        assert!(!result.stderr.is_empty());
    }

    // -----------------------------------------------------------------------
    // Script resolution
    // -----------------------------------------------------------------------

    #[test]
    fn test_resolve_script_path_known_component() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let path = adapter.resolve_script_path("pytorch");
        assert!(path.is_some());
        let path = path.unwrap();
        assert!(path.to_string_lossy().contains("install_pytorch_rocm.sh"));
    }

    #[test]
    fn test_resolve_script_path_unknown_component() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let path = adapter.resolve_script_path("nonexistent");
        assert!(path.is_none());
    }

    #[test]
    fn test_resolve_script_path_no_installer() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        // rocm-smi has no installer script
        let path = adapter.resolve_script_path("rocm-smi");
        assert!(path.is_none());
    }

    // -----------------------------------------------------------------------
    // Invocation errors
    // -----------------------------------------------------------------------

    #[test]
    fn test_legacy_adapter_script_not_found() {
        let dir = tempfile::tempdir().unwrap();
        let adapter = LegacyAdapter::new(dir.path());

        // pytorch has a registry entry but the script doesn't exist
        let result = adapter.execute("pytorch", "2.5.0");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, AdapterError::InvocationError { .. }));
        assert!(err.to_string().contains("script not found"));
    }

    #[test]
    fn test_legacy_adapter_unknown_component() {
        let dir = tempfile::tempdir().unwrap();
        let adapter = LegacyAdapter::new(dir.path());

        let result = adapter.execute("nonexistent", "1.0.0");
        assert!(matches!(result, Err(AdapterError::AdapterNotFound { .. })));
    }

    // -----------------------------------------------------------------------
    // Command building
    // -----------------------------------------------------------------------

    #[test]
    fn test_build_command_includes_version() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let cmd = adapter.build_command("pytorch", "2.5.0").unwrap();
        let formatted = format!("{cmd:?}");
        assert!(formatted.contains("2.5.0"));
        assert!(formatted.contains("install_pytorch_rocm.sh"));
    }

    #[test]
    fn test_build_command_empty_version() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        // Empty version should not add an argument
        let cmd = adapter.build_command("pytorch", "").unwrap();
        let formatted = format!("{cmd:?}");
        assert!(formatted.contains("install_pytorch_rocm.sh"));
    }

    // -----------------------------------------------------------------------
    // Executor kind
    // -----------------------------------------------------------------------

    #[test]
    fn test_legacy_adapter_executor_kind() {
        let dir = tempfile::tempdir().unwrap();
        let adapter = LegacyAdapter::new(dir.path());
        assert_eq!(adapter.executor_kind(), ExecutorKind::LegacyScript);
    }

    // -----------------------------------------------------------------------
    // Duration tracking
    // -----------------------------------------------------------------------

    #[test]
    fn test_legacy_adapter_tracks_duration() {
        let (_dir, scripts_dir) = create_mock_script_dir();
        let adapter = LegacyAdapter::new(&scripts_dir);

        let result = adapter.execute("pytorch", "2.5.0").unwrap();
        // Duration should be tracked (u64 is always >= 0, just verify it's populated)
        let _ = result.duration_ms;
    }
}