Create a list of known jobs
This commit is contained in:
parent
5aa2d72da0
commit
1fd0a2d8f3
12
app.go
12
app.go
@ -8,7 +8,6 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/gin-gonic/gin"
|
||||
"nhooyr.io/websocket"
|
||||
@ -50,21 +49,12 @@ func NewApp(cfg *config.Config) App {
|
||||
})
|
||||
|
||||
router.GET("/api/run", func(c *gin.Context) {
|
||||
myVolume, err := docker.CreateVolumeDir("/data", false)
|
||||
ctrid, err := RunJob("counter")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
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})
|
||||
})
|
||||
|
||||
|
@ -41,7 +41,7 @@ func PsName() (ret map[string]string, err error) {
|
||||
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()
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -63,7 +63,7 @@ func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
|
||||
Architecture: "amd64",
|
||||
OS: "linux",
|
||||
},
|
||||
genContainerName(image),
|
||||
genContainerName(jobtype),
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -7,12 +7,12 @@ import (
|
||||
"github.com/nemunaire/minifaas/engine"
|
||||
)
|
||||
|
||||
func genContainerName(image string) string {
|
||||
func genContainerName(jobtype string) string {
|
||||
uuid := make([]byte, 24)
|
||||
_, err := rand.Read(uuid)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s-%x", engine.GenContainerPrefix(image), uuid)
|
||||
return fmt.Sprintf("%s%x", engine.GenContainerPrefix(jobtype), uuid)
|
||||
}
|
||||
|
@ -5,6 +5,6 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func GenContainerPrefix(image string) string {
|
||||
return fmt.Sprintf("minifaas-%x-", sha256.Sum224([]byte(image)))
|
||||
func GenContainerPrefix(jobtype string) string {
|
||||
return fmt.Sprintf("minifaas-%x-", sha256.Sum224([]byte(jobtype)))
|
||||
}
|
||||
|
39
jobs.go
Normal file
39
jobs.go
Normal 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,
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user