mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-22 18:41:37 +00:00
Use Docker API for rm, create, export, and inspect
Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
parent
fcdfe5c356
commit
37f57cfa84
@ -13,7 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
@ -4,6 +4,7 @@ package main
|
|||||||
// and also using the Docker API not shelling out
|
// and also using the Docker API not shelling out
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -13,8 +14,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/engine-api/client"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -107,131 +109,54 @@ func dockerRunInput(input io.Reader, args ...string) ([]byte, error) {
|
|||||||
|
|
||||||
func dockerCreate(image string) (string, error) {
|
func dockerCreate(image string) (string, error) {
|
||||||
log.Debugf("docker create: %s", image)
|
log.Debugf("docker create: %s", image)
|
||||||
docker, err := exec.LookPath("docker")
|
cli, err := dockerClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("Docker does not seem to be installed")
|
return "", errors.New("could not initialize Docker API client")
|
||||||
}
|
}
|
||||||
// we do not ever run the container, so /dev/null is used as command
|
// we do not ever run the container, so /dev/null is used as command
|
||||||
args := []string{"create", image, "/dev/null"}
|
config := &container.Config{
|
||||||
cmd := exec.Command(docker, args...)
|
Cmd: []string{"/dev/null"},
|
||||||
|
Image: image,
|
||||||
stderrPipe, err := cmd.StderrPipe()
|
}
|
||||||
|
respBody, err := cli.ContainerCreate(context.Background(), config, nil, nil, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
stdoutPipe, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout, err := ioutil.ReadAll(stdoutPipe)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
stderr, err := ioutil.ReadAll(stderrPipe)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("%v: %s", err, stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
container := strings.TrimSpace(string(stdout))
|
|
||||||
log.Debugf("docker create: %s...Done", image)
|
log.Debugf("docker create: %s...Done", image)
|
||||||
return container, nil
|
return respBody.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerExport(container string) ([]byte, error) {
|
func dockerExport(container string) ([]byte, error) {
|
||||||
log.Debugf("docker export: %s", container)
|
log.Debugf("docker export: %s", container)
|
||||||
docker, err := exec.LookPath("docker")
|
cli, err := dockerClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, errors.New("Docker does not seem to be installed")
|
return []byte{}, errors.New("could not initialize Docker API client")
|
||||||
}
|
}
|
||||||
args := []string{"export", container}
|
responseBody, err := cli.ContainerExport(context.Background(), container)
|
||||||
cmd := exec.Command(docker, args...)
|
if err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
|
defer responseBody.Close()
|
||||||
|
|
||||||
stderrPipe, err := cmd.StderrPipe()
|
output := bytes.NewBuffer(nil)
|
||||||
|
_, err = io.Copy(output, responseBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
stdoutPipe, err := cmd.StdoutPipe()
|
return output.Bytes(), nil
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout, err := ioutil.ReadAll(stdoutPipe)
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
stderr, err := ioutil.ReadAll(stderrPipe)
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, fmt.Errorf("%v: %s", err, stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("docker export: %s...Done", container)
|
|
||||||
return stdout, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerRm(container string) error {
|
func dockerRm(container string) error {
|
||||||
log.Debugf("docker rm: %s", container)
|
log.Debugf("docker rm: %s", container)
|
||||||
docker, err := exec.LookPath("docker")
|
cli, err := dockerClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Docker does not seem to be installed")
|
return errors.New("could not initialize Docker API client")
|
||||||
}
|
}
|
||||||
args := []string{"rm", container}
|
if err = cli.ContainerRemove(context.Background(), container, types.ContainerRemoveOptions{}); err != nil {
|
||||||
cmd := exec.Command(docker, args...)
|
|
||||||
|
|
||||||
stderrPipe, err := cmd.StderrPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
stdoutPipe, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = ioutil.ReadAll(stdoutPipe)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
stderr, err := ioutil.ReadAll(stderrPipe)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%v: %s", err, stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("docker rm: %s...Done", container)
|
log.Debugf("docker rm: %s...Done", container)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -296,14 +221,14 @@ func dockerClient() (*client.Client, error) {
|
|||||||
func dockerInspectImage(cli *client.Client, image string) (types.ImageInspect, error) {
|
func dockerInspectImage(cli *client.Client, image string) (types.ImageInspect, error) {
|
||||||
log.Debugf("docker inspect image: %s", image)
|
log.Debugf("docker inspect image: %s", image)
|
||||||
|
|
||||||
inspect, _, err := cli.ImageInspectWithRaw(context.Background(), image, false)
|
inspect, _, err := cli.ImageInspectWithRaw(context.Background(), image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if client.IsErrImageNotFound(err) {
|
if client.IsErrImageNotFound(err) {
|
||||||
pullErr := dockerPull(image, false)
|
pullErr := dockerPull(image, false)
|
||||||
if pullErr != nil {
|
if pullErr != nil {
|
||||||
return types.ImageInspect{}, pullErr
|
return types.ImageInspect{}, pullErr
|
||||||
}
|
}
|
||||||
inspect, _, err = cli.ImageInspectWithRaw(context.Background(), image, false)
|
inspect, _, err = cli.ImageInspectWithRaw(context.Background(), image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.ImageInspect{}, err
|
return types.ImageInspect{}, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user