| 1 | package containerenv |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "regexp" |
| 12 | "strings" |
| 13 | |
| 14 | "github.com/sylvinus/agent-vm/internal/host" |
| 15 | ) |
| 16 | |
| 17 | const profileLabel = "cn.cage.profile" |
| 18 | |
| 19 | type Config struct { |
| 20 | Engine string `json:"engine"` |
| 21 | Executable string `json:"executable"` |
| 22 | Socket string `json:"socket,omitempty"` |
| 23 | Image string `json:"image"` |
| 24 | CPUs int `json:"cpus"` |
| 25 | MemoryBytes int64 `json:"memory_bytes"` |
| 26 | } |
| 27 | |
| 28 | type Runtime struct { |
| 29 | profile string |
| 30 | config Config |
| 31 | identity string |
| 32 | } |
| 33 | |
| 34 | var imageIDPattern = regexp.MustCompile(`^(?:sha256:|[a-zA-Z0-9][a-zA-Z0-9./:_-]*@sha256:)[a-f0-9]{64}$`) |
| 35 | |
| 36 | func (c Config) Validate() error { |
| 37 | if c.Engine != "docker" && c.Engine != "podman" { |
| 38 | return errors.New("容器引擎必须是 rootless Docker 或 Podman") |
| 39 | } |
| 40 | if !filepath.IsAbs(c.Executable) || strings.ContainsAny(c.Executable, "\x00\r\n") { |
| 41 | return errors.New("容器引擎必须使用绝对路径") |
| 42 | } |
| 43 | if c.Engine == "docker" && (!strings.HasPrefix(c.Socket, "unix:///") || strings.ContainsAny(c.Socket, "\x00\r\n?#")) { |
| 44 | return errors.New("Docker 必须使用独立的本机 rootless Unix socket") |
| 45 | } |
| 46 | if c.Engine == "podman" && c.Socket != "" { |
| 47 | return errors.New("Podman 运行环境不使用远程服务 socket") |
| 48 | } |
| 49 | if !imageIDPattern.MatchString(c.Image) { |
| 50 | return errors.New("容器镜像必须固定为 SHA-256 image ID 或 registry digest") |
| 51 | } |
| 52 | if c.CPUs < 1 || c.CPUs > 256 || c.MemoryBytes < 512<<20 { |
| 53 | return errors.New("容器 CPU 或内存限制无效") |
| 54 | } |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | func Configured(profile string) bool { |
| 59 | _, err := os.Lstat(filepath.Join(profile, "container.json")) |
| 60 | return !os.IsNotExist(err) |
| 61 | } |
| 62 | |
| 63 | func Load(profile string) (*Runtime, error) { |
| 64 | profile, err := filepath.Abs(profile) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | path := filepath.Join(profile, "container.json") |
| 69 | if err := host.CheckPrivateFile(path); err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | file, err := os.Open(path) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | defer file.Close() |
| 77 | var config Config |
| 78 | decoder := json.NewDecoder(io.LimitReader(file, 64<<10)) |
| 79 | decoder.DisallowUnknownFields() |
| 80 | if err := decoder.Decode(&config); err != nil { |
| 81 | return nil, errors.New("容器配置格式无效") |
| 82 | } |
| 83 | var extra any |
| 84 | if decoder.Decode(&extra) != io.EOF { |
| 85 | return nil, errors.New("容器配置必须只有一个 JSON 对象") |
| 86 | } |
| 87 | if err := config.Validate(); err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | return newRuntime(profile, config), nil |
| 91 | } |
| 92 | |
| 93 | func Configure(profile string, config Config) (*Runtime, error) { |
| 94 | if err := config.Validate(); err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | profile, err := filepath.Abs(profile) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | if err := host.EnsurePrivateDir(profile); err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | |
| 106 | if Configured(profile) { |
| 107 | existing, err := Load(profile) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | if existing.config != config { |
| 112 | return nil, errors.New("已有容器配置不能直接替换;请保留现有环境并执行迁移") |
| 113 | } |
| 114 | return existing, nil |
| 115 | } |
| 116 | if err := writePrivateJSON(filepath.Join(profile, "container.json"), config); err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | return newRuntime(profile, config), nil |
| 120 | } |
| 121 | |
| 122 | func newRuntime(profile string, config Config) *Runtime { |
| 123 | digest := sha256.Sum256([]byte(profile)) |
| 124 | return &Runtime{profile: profile, config: config, identity: hex.EncodeToString(digest[:16])} |
| 125 | } |
| 126 | |
| 127 | func (r *Runtime) Config() Config { return r.config } |
| 128 | func (r *Runtime) InstanceDir() string { |
| 129 | return filepath.Join(r.profile, "state", "container", "environment") |
| 130 | } |
| 131 | func (r *Runtime) SSHConfig() string { return filepath.Join(r.InstanceDir(), "ssh.config") } |
| 132 | func (r *Runtime) SSHTarget() string { return "cage-container" } |
| 133 | func (r *Runtime) Name() string { return "cage-" + r.identity } |
| 134 | |
| 135 | func writePrivateJSON(path string, value any) error { |
| 136 | if err := host.EnsurePrivateDir(filepath.Dir(path)); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | data, err := json.Marshal(value) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | file, err := os.CreateTemp(filepath.Dir(path), ".container-*") |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | defer os.Remove(file.Name()) |
| 148 | if err = host.ProtectFile(file.Name()); err == nil { |
| 149 | _, err = file.Write(data) |
| 150 | } |
| 151 | if err == nil { |
| 152 | err = file.Sync() |
| 153 | } |
| 154 | if closeErr := file.Close(); err == nil { |
| 155 | err = closeErr |
| 156 | } |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | return os.Rename(file.Name(), path) |
| 161 | } |