Add a predefined name to containers
This commit is contained in:
parent
e684fbba7d
commit
5aa2d72da0
@ -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
18
engine/docker/name.go
Normal 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
10
engine/queue.go
Normal 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)))
|
||||
}
|
Reference in New Issue
Block a user