Can define volumes to collect artifacts
This commit is contained in:
parent
d2a49e5740
commit
e3a911cd05
6 changed files with 149 additions and 10 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/client"
|
||||
oci "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
|
@ -29,7 +30,7 @@ func Ps() ([]types.Container, error) {
|
|||
return containers, nil
|
||||
}
|
||||
|
||||
func Run(image string, cmd []string) (string, error) {
|
||||
func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
|
||||
cli, err := newCli()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -42,8 +43,13 @@ func Run(image string, cmd []string) (string, error) {
|
|||
AttachStderr: true,
|
||||
Image: image,
|
||||
Cmd: cmd,
|
||||
Volumes: map[string]struct{}{
|
||||
"/data": {},
|
||||
},
|
||||
},
|
||||
&container.HostConfig{
|
||||
Mounts: mounts,
|
||||
},
|
||||
&container.HostConfig{},
|
||||
&network.NetworkingConfig{},
|
||||
&oci.Platform{
|
||||
Architecture: "amd64",
|
||||
|
|
|
|||
68
engine/docker/volumes.go
Normal file
68
engine/docker/volumes.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
)
|
||||
|
||||
func CreateVolumeDir(target string, readOnly bool) (*mount.Mount, error) {
|
||||
abs, err := filepath.Abs("artifacts")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir(abs, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &mount.Mount{
|
||||
Type: mount.TypeBind,
|
||||
Source: dir,
|
||||
Target: target,
|
||||
ReadOnly: readOnly,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetArtifactsVolumes(id string) (ret []string, err error) {
|
||||
abs, err := filepath.Abs("artifacts")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mnt, err := GetVolumes(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, m := range mnt {
|
||||
if m.Type == mount.TypeBind && strings.HasPrefix(m.Source, abs) {
|
||||
ret = append(ret, strings.TrimPrefix(m.Source, abs))
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetVolumes(id string) (ret []types.MountPoint, err error) {
|
||||
cli, err := newCli()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctr, err := cli.ContainerInspect(context.Background(), id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, mnt := range ctr.Mounts {
|
||||
ret = append(ret, mnt)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in a new issue