woj-server/internal/service/runner/deps.go

85 lines
1.9 KiB
Go
Raw Normal View History

package runner
import (
2024-01-06 15:07:11 +08:00
"errors"
"fmt"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/e"
"git.0x7f.app/WOJ/woj-server/pkg/file"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/pkg/utils"
"go.uber.org/zap"
"os"
"path/filepath"
)
2024-01-06 15:07:11 +08:00
type depConfig struct {
tarball string
image string
dockerfile string
}
func (s *service) loadImage(cfg *depConfig) e.Status {
err := utils.NewTryErr().
Try(func() error {
// import from tarball
if !file.FileExist(cfg.tarball) {
2024-01-06 15:07:11 +08:00
return errors.New("tarball not exists")
}
return s.execute("bash", "-c", fmt.Sprintf("gzip -d -c %s | podman load", cfg.tarball))
}).
Or(func() error {
// pull from docker hub
return s.execute("podman", "pull", cfg.image)
}).
Or(func() error {
// build from dockerfile
if !file.FileExist(cfg.dockerfile) {
2024-01-06 15:07:11 +08:00
return errors.New("dockerfile not exists")
}
return s.execute("podman", "build", "-f", cfg.dockerfile, "-t", cfg.image, ".")
}).
Done()
if err != nil {
s.log.Warn("load image failed", zap.Error(err))
return e.RunnerDepsBuildFailed
}
return e.Success
}
func (s *service) EnsureDeps(force bool) e.Status {
2023-12-21 17:08:53 +08:00
mark := filepath.Join(Prefix, ".mark.image")
2024-01-06 15:07:11 +08:00
// check mark
if force {
_ = os.Remove(mark)
} else if file.FileExist(mark) {
return e.Success
}
2024-01-06 15:07:11 +08:00
// full
fullImage := &depConfig{
tarball: filepath.Join(TmpDir, "ubuntu-full.tar.gz"),
image: "git.0x7f.app/woj/ubuntu-full:latest",
dockerfile: filepath.Join(ScriptsDir, "ubuntu-full.Dockerfile"),
2023-08-11 23:56:58 +08:00
}
2024-01-06 15:07:11 +08:00
if s.loadImage(fullImage) != e.Success {
return e.RunnerDepsBuildFailed
}
// tiny
tinyImage := &depConfig{
tarball: filepath.Join(TmpDir, "ubuntu-tiny.tar.gz"),
image: "git.0x7f.app/woj/ubuntu-run:latest",
dockerfile: filepath.Join(ScriptsDir, "ubuntu-run.Dockerfile"),
}
if s.loadImage(tinyImage) != e.Success {
return e.RunnerDepsBuildFailed
}
2024-01-06 15:07:11 +08:00
// mark
_, _ = os.Create(mark)
return e.Success
}