feat: #6 [2] Rewrite EnsureDeps
This commit is contained in:
parent
8d66639cdc
commit
8e3222daab
@ -32,10 +32,9 @@ func init() {
|
|||||||
TmpDir = path.Join(Prefix, TmpDir)
|
TmpDir = path.Join(Prefix, TmpDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) execute(script string, args ...string) error {
|
func (s *service) execute(exe string, args ...string) error {
|
||||||
p := filepath.Join(ScriptsDir, script)
|
cmd := exec.Command(exe, args...)
|
||||||
cmd := exec.Command(p, args...)
|
cmd.Dir = Prefix
|
||||||
cmd.Dir = ScriptsDir
|
|
||||||
if s.verbose {
|
if s.verbose {
|
||||||
cmd.Stdout = os.Stderr
|
cmd.Stdout = os.Stderr
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
@ -1,35 +1,83 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"git.0x7f.app/WOJ/woj-server/internal/e"
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
||||||
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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 !utils.FileExist(cfg.tarball) {
|
||||||
|
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 !utils.FileExist(cfg.dockerfile) {
|
||||||
|
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 {
|
func (s *service) EnsureDeps(force bool) e.Status {
|
||||||
mark := filepath.Join(Prefix, ".mark.image")
|
mark := filepath.Join(Prefix, ".mark.image")
|
||||||
|
|
||||||
|
// check mark
|
||||||
if force {
|
if force {
|
||||||
_ = os.Remove(mark)
|
_ = os.Remove(mark)
|
||||||
} else if utils.FileExist(mark) {
|
} else if utils.FileExist(mark) {
|
||||||
return e.Success
|
return e.Success
|
||||||
}
|
}
|
||||||
|
|
||||||
script := filepath.Join(ScriptsDir, "prepare_images.sh")
|
// full
|
||||||
cmd := exec.Command(script)
|
fullImage := &depConfig{
|
||||||
cmd.Dir = ScriptsDir
|
tarball: filepath.Join(TmpDir, "ubuntu-full.tar.gz"),
|
||||||
if s.verbose {
|
image: "git.0x7f.app/woj/ubuntu-full:latest",
|
||||||
cmd.Stdout = os.Stdout
|
dockerfile: filepath.Join(ScriptsDir, "ubuntu-full.Dockerfile"),
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
}
|
}
|
||||||
err := cmd.Run()
|
if s.loadImage(fullImage) != e.Success {
|
||||||
if err != nil {
|
|
||||||
s.log.Warn("prebuild docker images failed", zap.Error(err))
|
|
||||||
return e.RunnerDepsBuildFailed
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// mark
|
||||||
|
_, _ = os.Create(mark)
|
||||||
|
|
||||||
return e.Success
|
return e.Success
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user