-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathruntime_apple.go
More file actions
94 lines (79 loc) · 2.46 KB
/
Copy pathruntime_apple.go
File metadata and controls
94 lines (79 loc) · 2.46 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
// AppleContainersCLIRuntime shells out to Apple's `container` CLI.
type AppleContainersCLIRuntime struct {
binary string
}
type appleContainerSystemStatus struct {
Status string `json:"status"`
}
func newAppleContainersCLIRuntime() (*AppleContainersCLIRuntime, error) {
path, err := exec.LookPath("container")
if err != nil {
return nil, fmt.Errorf("Apple Containers CLI not found: %w", err)
}
return &AppleContainersCLIRuntime{binary: path}, nil
}
func (a *AppleContainersCLIRuntime) Close() error {
return nil
}
func (a *AppleContainersCLIRuntime) Ping(ctx context.Context) error {
out, err := exec.CommandContext(ctx, a.binary, "system", "status", "--format", "json").Output()
if err != nil {
return fmt.Errorf("apple container service not reachable (start with: container system start): %w", err)
}
var status appleContainerSystemStatus
if err := json.Unmarshal(out, &status); err != nil {
return fmt.Errorf("parsing apple container system status: %w", err)
}
if status.Status != "running" {
return fmt.Errorf("apple container service is %q, not running (start with: container system start)", status.Status)
}
return nil
}
func (a *AppleContainersCLIRuntime) ImageExists(ctx context.Context, ref string) (bool, error) {
out, err := exec.CommandContext(ctx, a.binary, "image", "list", "-q").Output()
if err != nil {
return false, err
}
// -q outputs one "name:tag" per line, so we can do an exact line match.
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if strings.TrimSpace(line) == ref {
return true, nil
}
}
return false, nil
}
func (a *AppleContainersCLIRuntime) PullImage(ctx context.Context, ref string) error {
cmd := exec.CommandContext(ctx, a.binary, "image", "pull", ref)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (a *AppleContainersCLIRuntime) RunContainer(ctx context.Context, opts RunOptions) error {
args := []string{"run", "--rm"}
for _, b := range opts.Binds {
args = append(args, "-v", b)
}
for _, e := range opts.Env {
args = append(args, "-e", e)
}
for _, m := range opts.Mounts {
args = append(args, "--mount", m)
}
// Pass through extra args verbatim
args = append(args, opts.Extra...)
args = append(args, opts.Image)
args = append(args, opts.Cmd...)
cmd := exec.CommandContext(ctx, a.binary, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}