1package executor
2
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10
11 "github.com/bnema/archup/internal/domain/ports"
12)
13
14// ChrootExecutor implements the ChrootExecutor port using chroot commands
15type ChrootExecutor struct {
16 logger ports.Logger
17}
18
19// NewChrootExecutor creates a new chroot executor with a logger
20func NewChrootExecutor(logger ports.Logger) *ChrootExecutor {
21 return &ChrootExecutor{
22 logger: logger,
23 }
24}
25
26// ExecuteInChroot runs a command inside a chroot environment
27func (ce *ChrootExecutor) ExecuteInChroot(ctx context.Context, chrootPath string, command string, args ...string) ([]byte, error) {
28 // Verify chroot path exists
29 if _, err := os.Stat(chrootPath); err != nil {
30 return nil, fmt.Errorf("chroot path does not exist: %w", err)
31 }
32
33 // Build chroot command: arch-chroot <path> <command> <args...>
34 allArgs := append([]string{chrootPath, command}, args...)
35 cmd := exec.CommandContext(ctx, "arch-chroot", allArgs...)
36
37 var stdout, stderr bytes.Buffer
38 cmd.Stdout = &stdout
39 cmd.Stderr = &stderr
40
41 if err := cmd.Run(); err != nil {
42 return nil, fmt.Errorf("chroot command failed: %w (stderr: %s)", err, stderr.String())
43 }
44
45 return stdout.Bytes(), nil
46}
47
48// ExecuteInChrootWithStdin runs a command inside a chroot with stdin
49func (ce *ChrootExecutor) ExecuteInChrootWithStdin(ctx context.Context, chrootPath string, stdin string, command string, args ...string) error {
50 if _, err := os.Stat(chrootPath); err != nil {
51 return fmt.Errorf("chroot path does not exist: %w", err)
52 }
53
54 allArgs := append([]string{chrootPath, command}, args...)
55 cmd := exec.CommandContext(ctx, "arch-chroot", allArgs...)
56 cmd.Stdin = bytes.NewBufferString(stdin)
57
58 var stdout, stderr bytes.Buffer
59 cmd.Stdout = &stdout
60 cmd.Stderr = &stderr
61
62 if err := cmd.Run(); err != nil {
63 return fmt.Errorf("chroot command failed: %w (stderr: %s)", err, stderr.String())
64 }
65
66 return nil
67}
68
69// ChrootSystemctl runs systemctl commands in chroot
70// This is a convenience method that ensures proper logging of systemctl operations
71func (ce *ChrootExecutor) ChrootSystemctl(ctx context.Context, logPath string, chrootPath string, args ...string) error {
72 // Verify paths exist
73 if _, err := os.Stat(chrootPath); err != nil {
74 return fmt.Errorf("chroot path does not exist: %w", err)
75 }
76
77 if logPath != "" {
78 if err := os.MkdirAll(filepath.Dir(logPath), 0755); err != nil {
79 return fmt.Errorf("failed to create log directory: %w", err)
80 }
81 }
82
83 // Build chroot command with systemctl
84 allArgs := append([]string{chrootPath, "systemctl"}, args...)
85 cmd := exec.CommandContext(ctx, "arch-chroot", allArgs...)
86
87 var stdout, stderr bytes.Buffer
88 cmd.Stdout = &stdout
89 cmd.Stderr = &stderr
90
91 if err := cmd.Run(); err != nil {
92 errMsg := fmt.Sprintf("chroot systemctl failed: %v (stderr: %s)", err, stderr.String())
93
94 // Log to file if specified
95 if logPath != "" {
96 logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
97 if err == nil {
98 if _, writeErr := logFile.WriteString(errMsg + "\n"); writeErr != nil {
99 _ = logFile.Close()
100 return fmt.Errorf("%s (failed to write log: %v)", errMsg, writeErr)
101 }
102 if closeErr := logFile.Close(); closeErr != nil {
103 return fmt.Errorf("%s (failed to close log: %v)", errMsg, closeErr)
104 }
105 }
106 }
107
108 return fmt.Errorf("%s", errMsg)
109 }
110
111 // Log success to file if specified
112 if logPath != "" {
113 logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
114 if err == nil {
115 if _, writeErr := fmt.Fprintf(logFile, "systemctl %v executed successfully\n", args); writeErr != nil {
116 _ = logFile.Close()
117 return fmt.Errorf("failed to write log: %w", writeErr)
118 }
119 if closeErr := logFile.Close(); closeErr != nil {
120 return fmt.Errorf("failed to close log: %w", closeErr)
121 }
122 }
123 }
124
125 return nil
126}