Add a predefined name to containers

This commit is contained in:
nemunaire 2021-05-09 00:55:45 +02:00
parent e684fbba7d
commit 5aa2d72da0
3 changed files with 54 additions and 1 deletions

View File

@ -16,6 +16,31 @@ func newCli() (*client.Client, error) {
return client.NewClientWithOpts(client.FromEnv)
}
func Ps() ([]types.Container, error) {
cli, err := newCli()
if err != nil {
return nil, err
}
return cli.ContainerList(context.Background(), types.ContainerListOptions{})
}
func PsName() (ret map[string]string, err error) {
containers, err := Ps()
if err != nil {
return nil, err
}
ret = map[string]string{}
for _, container := range containers {
for _, name := range container.Names {
ret[name] = container.ID
}
}
return ret, err
}
func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
cli, err := newCli()
if err != nil {
@ -38,7 +63,7 @@ func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
Architecture: "amd64",
OS: "linux",
},
"",
genContainerName(image),
)
if err != nil {
return "", err

18
engine/docker/name.go Normal file
View File

@ -0,0 +1,18 @@
package docker
import (
"crypto/rand"
"fmt"
"github.com/nemunaire/minifaas/engine"
)
func genContainerName(image 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)
}

10
engine/queue.go Normal file
View File

@ -0,0 +1,10 @@
package engine
import (
"crypto/sha256"
"fmt"
)
func GenContainerPrefix(image string) string {
return fmt.Sprintf("minifaas-%x-", sha256.Sum224([]byte(image)))
}