Can reverse sha-ed job type

This commit is contained in:
nemunaire 2021-07-31 17:35:29 +02:00
parent f2eacf41cf
commit 4e982b39f9
3 changed files with 64 additions and 21 deletions

View File

@ -3,7 +3,10 @@ package engine
import ( import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"log"
"strings" "strings"
"github.com/nemunaire/minifaas/jobs"
) )
const CTR_NAME_PREFIX = "minifaas" const CTR_NAME_PREFIX = "minifaas"
@ -13,7 +16,7 @@ func GenContainerPrefix(jobtype string) string {
} }
func ParseContainerName(name string) (jobtype, id string, err error) { func ParseContainerName(name string) (jobtype, id string, err error) {
if !strings.HasPrefix(name, "/"+CTR_NAME_PREFIX+"-") { if !strings.HasPrefix(strings.TrimPrefix(name, "/"), CTR_NAME_PREFIX+"-") {
return "", "", fmt.Errorf("This is not a %s job: starting with %q", CTR_NAME_PREFIX, name) return "", "", fmt.Errorf("This is not a %s job: starting with %q", CTR_NAME_PREFIX, name)
} }
@ -22,7 +25,14 @@ func ParseContainerName(name string) (jobtype, id string, err error) {
return "", "", fmt.Errorf("This is not a %s job: %q didn't has at least 3 args", CTR_NAME_PREFIX, name) return "", "", fmt.Errorf("This is not a %s job: %q didn't has at least 3 args", CTR_NAME_PREFIX, name)
} }
return tmp[1], strings.Join(tmp[2:], "-"), nil jobtype = jobs.GetJobType(tmp[1])
if jobtype == "" {
return "", "", fmt.Errorf("This is not a %s job: unknown job type %q", CTR_NAME_PREFIX, tmp[1])
}
id = strings.Join(tmp[2:], "-")
return
} }
func FilterRunningContainers(jobtype string, ctrs map[string]string) (ret []string) { func FilterRunningContainers(jobtype string, ctrs map[string]string) (ret []string) {
@ -40,7 +50,10 @@ func FilterRunningContainers(jobtype string, ctrs map[string]string) (ret []stri
func CountRunningContainers(jobtype string, ctrs map[string]string) (n int) { func CountRunningContainers(jobtype string, ctrs map[string]string) (n int) {
prefix := GenContainerPrefix(jobtype) prefix := GenContainerPrefix(jobtype)
log.Println(ctrs)
for cname, _ := range ctrs { for cname, _ := range ctrs {
log.Println(strings.TrimPrefix(cname, "/"), prefix)
if jobtype == "" || strings.HasPrefix(strings.TrimPrefix(cname, "/"), prefix) { if jobtype == "" || strings.HasPrefix(strings.TrimPrefix(cname, "/"), prefix) {
n += 1 n += 1
} }

27
jobs.go
View File

@ -4,25 +4,14 @@ import (
"github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/mount"
"github.com/nemunaire/minifaas/engine/docker" "github.com/nemunaire/minifaas/engine/docker"
"github.com/nemunaire/minifaas/jobs"
) )
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) { func RunJob(jobtype string) (string, error) {
job := jobs.GetJob(jobtype)
var mnts []mount.Mount var mnts []mount.Mount
if jobs[jobtype].DataMount { if job.DataMount {
myVolume, err := docker.CreateVolumeDir("/data", false) myVolume, err := docker.CreateVolumeDir("/data", false)
if err != nil { if err != nil {
return "", err return "", err
@ -31,12 +20,12 @@ func RunJob(jobtype string) (string, error) {
} }
// Check if the image is here // Check if the image is here
hasimg, err := docker.HasImage(jobs[jobtype].Image) hasimg, err := docker.HasImage(job.Image)
if err != nil { if err != nil {
return "", err return "", err
} }
if !hasimg { if !hasimg {
err = docker.PullImage(jobs[jobtype].Image) err = docker.PullImage(job.Image)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -44,8 +33,8 @@ func RunJob(jobtype string) (string, error) {
return docker.Create( return docker.Create(
jobtype, jobtype,
jobs[jobtype].Image, job.Image,
jobs[jobtype].Cmd, job.Cmd,
mnts, mnts,
) )
} }

41
jobs/jobs.go Normal file
View File

@ -0,0 +1,41 @@
package jobs
import (
"crypto/sha256"
"fmt"
)
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,
},
}
var revJobs = map[string]string{}
func init() {
for jobtype := range jobs {
revJobs[fmt.Sprintf("%x", sha256.Sum224([]byte(jobtype)))] = jobtype
}
}
func GetJobType(hashJobType string) (jobtype string) {
var ok bool
if jobtype, ok = revJobs[hashJobType]; !ok {
jobtype = ""
}
return
}
func GetJob(jobtype string) (job Job) {
return jobs[jobtype]
}