2022-10-22 17:38:39 +08:00
|
|
|
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"
|
2024-01-06 16:06:16 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/pkg/file"
|
2023-07-14 21:47:11 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
2022-10-22 17:38:39 +08:00
|
|
|
"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
|
2024-01-06 17:31:00 +08:00
|
|
|
if !file.Exist(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
|
2024-01-06 17:31:00 +08:00
|
|
|
if !file.Exist(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
|
|
|
|
}
|
|
|
|
|
2022-10-22 17:38:39 +08:00
|
|
|
func (s *service) EnsureDeps(force bool) e.Status {
|
2023-12-21 17:08:53 +08:00
|
|
|
mark := filepath.Join(Prefix, ".mark.image")
|
2022-10-22 17:38:39 +08:00
|
|
|
|
2024-01-06 15:07:11 +08:00
|
|
|
// check mark
|
2022-10-22 17:38:39 +08:00
|
|
|
if force {
|
|
|
|
_ = os.Remove(mark)
|
2024-01-06 17:31:00 +08:00
|
|
|
} else if file.Exist(mark) {
|
2022-10-22 17:38:39 +08:00
|
|
|
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 {
|
2022-10-22 17:38:39 +08:00
|
|
|
return e.RunnerDepsBuildFailed
|
|
|
|
}
|
|
|
|
|
2024-01-06 15:07:11 +08:00
|
|
|
// mark
|
|
|
|
_, _ = os.Create(mark)
|
|
|
|
|
2022-10-22 17:38:39 +08:00
|
|
|
return e.Success
|
|
|
|
}
|