Create a list of known jobs

This commit is contained in:
nemunaire 2021-05-09 18:02:23 +02:00
parent 5aa2d72da0
commit 1fd0a2d8f3
5 changed files with 46 additions and 17 deletions

12
app.go
View File

@ -8,7 +8,6 @@ import (
"os" "os"
"time" "time"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"nhooyr.io/websocket" "nhooyr.io/websocket"
@ -50,21 +49,12 @@ func NewApp(cfg *config.Config) App {
}) })
router.GET("/api/run", func(c *gin.Context) { router.GET("/api/run", func(c *gin.Context) {
myVolume, err := docker.CreateVolumeDir("/data", false) ctrid, err := RunJob("counter")
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
} }
ctrid, err := docker.Run(
"alpine",
[]string{"sh", "-c", "touch /data/work_done; for i in `seq 10`; do echo $i; sleep 0.5; done"},
[]mount.Mount{*myVolume},
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"jobid": ctrid}) c.JSON(http.StatusOK, gin.H{"jobid": ctrid})
}) })

View File

@ -41,7 +41,7 @@ func PsName() (ret map[string]string, err error) {
return ret, err return ret, err
} }
func Run(image string, cmd []string, mounts []mount.Mount) (string, error) { func Run(jobtype string, image string, cmd []string, mounts []mount.Mount) (string, error) {
cli, err := newCli() cli, err := newCli()
if err != nil { if err != nil {
return "", err return "", err
@ -63,7 +63,7 @@ func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
Architecture: "amd64", Architecture: "amd64",
OS: "linux", OS: "linux",
}, },
genContainerName(image), genContainerName(jobtype),
) )
if err != nil { if err != nil {
return "", err return "", err

View File

@ -7,12 +7,12 @@ import (
"github.com/nemunaire/minifaas/engine" "github.com/nemunaire/minifaas/engine"
) )
func genContainerName(image string) string { func genContainerName(jobtype string) string {
uuid := make([]byte, 24) uuid := make([]byte, 24)
_, err := rand.Read(uuid) _, err := rand.Read(uuid)
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
return fmt.Sprintf("%s-%x", engine.GenContainerPrefix(image), uuid) return fmt.Sprintf("%s%x", engine.GenContainerPrefix(jobtype), uuid)
} }

View File

@ -5,6 +5,6 @@ import (
"fmt" "fmt"
) )
func GenContainerPrefix(image string) string { func GenContainerPrefix(jobtype string) string {
return fmt.Sprintf("minifaas-%x-", sha256.Sum224([]byte(image))) return fmt.Sprintf("minifaas-%x-", sha256.Sum224([]byte(jobtype)))
} }

39
jobs.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"github.com/docker/docker/api/types/mount"
"github.com/nemunaire/minifaas/engine/docker"
)
type Job struct {
Image string
Cmd []string
DataMount bool
}
var jobs = map[string]Job{
"counter": {
Image: "alpine",
Cmd: []string{"sh", "-c", "touch /data/work_done; for i in `seq 10`; do echo $i; sleep 0.5; done"},
DataMount: true,
},
}
func RunJob(jobtype string) (string, error) {
var mnts []mount.Mount
if jobs[jobtype].DataMount {
myVolume, err := docker.CreateVolumeDir("/data", false)
if err != nil {
return "", err
}
mnts = append(mnts, *myVolume)
}
return docker.Run(
jobtype,
jobs[jobtype].Image,
jobs[jobtype].Cmd,
mnts,
)
}