From 43c6da22b976b6050f86dca50564a4c2b08caee0 Mon Sep 17 00:00:00 2001 From: baude Date: Thu, 10 Jan 2019 12:15:33 -0600 Subject: Add darwin support for remote-client Add the ability to cross-compile podman remote for OSX. Also, add image exists and tag to remote-client. Signed-off-by: baude --- Makefile | 5 +- cmd/podman/exists.go | 11 +- cmd/podman/main.go | 11 +- cmd/podman/platform_linux.go | 17 +++ cmd/podman/platform_unsupported.go | 6 + cmd/podman/tag.go | 8 +- libpod/adapter/runtime_remote.go | 18 ++- libpod/container_api.go | 20 ++++ libpod/container_attach.go | 179 ---------------------------- libpod/container_attach_linux.go | 162 +++++++++++++++++++++++++ libpod/container_attach_unsupported.go | 11 ++ libpod/lock/shm_lock_manager_unsupported.go | 4 +- libpod/runtime_volume_unsupported.go | 19 +++ pkg/rootless/rootless_unsupported.go | 13 +- pkg/spec/config_unsupported.go | 4 + 15 files changed, 286 insertions(+), 202 deletions(-) create mode 100644 cmd/podman/platform_linux.go create mode 100644 cmd/podman/platform_unsupported.go delete mode 100644 libpod/container_attach.go create mode 100644 libpod/container_attach_linux.go create mode 100644 libpod/container_attach_unsupported.go create mode 100644 libpod/runtime_volume_unsupported.go diff --git a/Makefile b/Makefile index 73dc79400..838de9774 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GO ?= go DESTDIR ?= / -EPOCH_TEST_COMMIT ?= e1732a5213147e3c0b7bf60b55a332c3720ecb4b +EPOCH_TEST_COMMIT ?= bd40dcfc2bc7c9014ea1f33482fb63aacbcdfe87 HEAD ?= HEAD CHANGELOG_BASE ?= HEAD~ CHANGELOG_TARGET ?= HEAD @@ -109,6 +109,9 @@ podman: .gopathok $(PODMAN_VARLINK_DEPENDENCIES) podman-remote: .gopathok $(PODMAN_VARLINK_DEPENDENCIES) $(GO) build -ldflags '$(LDFLAGS_PODMAN)' -tags "$(BUILDTAGS) remoteclient" -o bin/$@ $(PROJECT)/cmd/podman +podman-remote-darwin: .gopathok $(PODMAN_VARLINK_DEPENDENCIES) + GOOS=darwin $(GO) build -ldflags '$(LDFLAGS_PODMAN)' -tags "remoteclient containers_image_openpgp exclude_graphdriver_devicemapper" -o bin/$@ $(PROJECT)/cmd/podman + local-cross: $(CROSS_BUILD_TARGETS) bin/podman.cross.%: .gopathok diff --git a/cmd/podman/exists.go b/cmd/podman/exists.go index 2e2559ec7..bd1bc24ec 100644 --- a/cmd/podman/exists.go +++ b/cmd/podman/exists.go @@ -5,6 +5,7 @@ import ( "github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/containers/libpod/libpod" + "github.com/containers/libpod/libpod/adapter" "github.com/containers/libpod/libpod/image" "github.com/pkg/errors" "github.com/urfave/cli" @@ -66,13 +67,15 @@ func imageExistsCmd(c *cli.Context) error { if len(args) > 1 || len(args) < 1 { return errors.New("you may only check for the existence of one image at a time") } - runtime, err := libpodruntime.GetRuntime(c) + localRuntime, err := adapter.GetRuntime(c) if err != nil { return errors.Wrapf(err, "could not get runtime") } - defer runtime.Shutdown(false) - if _, err := runtime.ImageRuntime().NewFromLocal(args[0]); err != nil { - if errors.Cause(err) == image.ErrNoSuchImage { + defer localRuntime.Runtime.Shutdown(false) + if _, err := localRuntime.NewImageFromLocal(args[0]); err != nil { + //TODO we need to ask about having varlink defined errors exposed + //so we can reuse them + if errors.Cause(err) == image.ErrNoSuchImage || err.Error() == "io.podman.ImageNotFound" { os.Exit(1) } return err diff --git a/cmd/podman/main.go b/cmd/podman/main.go index 604404827..ce60bbfb7 100644 --- a/cmd/podman/main.go +++ b/cmd/podman/main.go @@ -120,7 +120,7 @@ func main() { os.Exit(1) } args := c.Args() - if args.Present() { + if args.Present() && rootless.IsRootless() { if _, notRequireRootless := cmdsNotRequiringRootless[args.First()]; !notRequireRootless { became, ret, err := rootless.BecomeRootInUserNS() if err != nil { @@ -265,11 +265,10 @@ func main() { Usage: "output logging information to syslog as well as the console", }, } - if _, err := os.Stat("/etc/containers/registries.conf"); err != nil { - if os.IsNotExist(err) { - logrus.Warn("unable to find /etc/containers/registries.conf. some podman (image shortnames) commands may be limited") - } - } + // Check if /etc/containers/registries.conf exists when running in + // in a local environment. + CheckForRegistries() + if err := app.Run(os.Args); err != nil { if debug { logrus.Errorf(err.Error()) diff --git a/cmd/podman/platform_linux.go b/cmd/podman/platform_linux.go new file mode 100644 index 000000000..2127923ae --- /dev/null +++ b/cmd/podman/platform_linux.go @@ -0,0 +1,17 @@ +// +build linux + +package main + +import ( + "os" + + "github.com/sirupsen/logrus" +) + +func CheckForRegistries() { + if _, err := os.Stat("/etc/containers/registries.conf"); err != nil { + if os.IsNotExist(err) { + logrus.Warn("unable to find /etc/containers/registries.conf. some podman (image shortnames) commands may be limited") + } + } +} diff --git a/cmd/podman/platform_unsupported.go b/cmd/podman/platform_unsupported.go new file mode 100644 index 000000000..f39eeaf63 --- /dev/null +++ b/cmd/podman/platform_unsupported.go @@ -0,0 +1,6 @@ +// +build !linux + +package main + +func CheckForRegistries() { +} diff --git a/cmd/podman/tag.go b/cmd/podman/tag.go index 29f66d41e..c99e5d173 100644 --- a/cmd/podman/tag.go +++ b/cmd/podman/tag.go @@ -1,7 +1,7 @@ package main import ( - "github.com/containers/libpod/cmd/podman/libpodruntime" + "github.com/containers/libpod/libpod/adapter" "github.com/pkg/errors" "github.com/urfave/cli" ) @@ -23,13 +23,13 @@ func tagCmd(c *cli.Context) error { if len(args) < 2 { return errors.Errorf("image name and at least one new name must be specified") } - runtime, err := libpodruntime.GetRuntime(c) + localRuntime, err := adapter.GetRuntime(c) if err != nil { return errors.Wrapf(err, "could not create runtime") } - defer runtime.Shutdown(false) + defer localRuntime.Runtime.Shutdown(false) - newImage, err := runtime.ImageRuntime().NewFromLocal(args[0]) + newImage, err := localRuntime.NewImageFromLocal(args[0]) if err != nil { return err } diff --git a/libpod/adapter/runtime_remote.go b/libpod/adapter/runtime_remote.go index 2f22dd36b..0fe5c449a 100644 --- a/libpod/adapter/runtime_remote.go +++ b/libpod/adapter/runtime_remote.go @@ -66,6 +66,7 @@ type remoteImage struct { Names []string Digest digest.Digest isParent bool + Runtime *LocalRuntime } // GetImages returns a slice of containerimages over a varlink connection @@ -80,7 +81,7 @@ func (r *LocalRuntime) GetImages() ([]*ContainerImage, error) { if len(i.RepoTags) > 1 { name = i.RepoTags[0] } - newImage, err := imageInListToContainerImage(i, name) + newImage, err := imageInListToContainerImage(i, name, r) if err != nil { return nil, err } @@ -89,7 +90,7 @@ func (r *LocalRuntime) GetImages() ([]*ContainerImage, error) { return newImages, nil } -func imageInListToContainerImage(i iopodman.ImageInList, name string) (*ContainerImage, error) { +func imageInListToContainerImage(i iopodman.ImageInList, name string, runtime *LocalRuntime) (*ContainerImage, error) { imageParts, err := image.DecomposeString(name) if err != nil { return nil, err @@ -111,6 +112,7 @@ func imageInListToContainerImage(i iopodman.ImageInList, name string) (*Containe Repository: imageParts.Registry, Names: i.RepoTags, isParent: i.IsParent, + Runtime: runtime, } return &ContainerImage{ri}, nil } @@ -121,7 +123,7 @@ func (r *LocalRuntime) NewImageFromLocal(name string) (*ContainerImage, error) { if err != nil { return nil, err } - return imageInListToContainerImage(img, name) + return imageInListToContainerImage(img, name, r) } @@ -173,3 +175,13 @@ func (ci *ContainerImage) Labels(ctx context.Context) (map[string]string, error) func (ci *ContainerImage) Dangling() bool { return len(ci.Names()) == 0 } + +// TagImage ... +func (ci *ContainerImage) TagImage(tag string) error { + _, err := iopodman.TagImage().Call(ci.Runtime.Conn, ci.ID(), tag) + return err +} + +func (r RemoteRuntime) RemoveImage(force bool) error { + return nil +} diff --git a/libpod/container_api.go b/libpod/container_api.go index 4eaf737b0..149867759 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -3,6 +3,7 @@ package libpod import ( "context" "fmt" + "io" "io/ioutil" "os" "strconv" @@ -413,6 +414,25 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user, workDir return waitErr } +// AttachStreams contains streams that will be attached to the container +type AttachStreams struct { + // OutputStream will be attached to container's STDOUT + OutputStream io.WriteCloser + // ErrorStream will be attached to container's STDERR + ErrorStream io.WriteCloser + // InputStream will be attached to container's STDIN + InputStream io.Reader + // AttachOutput is whether to attach to STDOUT + // If false, stdout will not be attached + AttachOutput bool + // AttachError is whether to attach to STDERR + // If false, stdout will not be attached + AttachError bool + // AttachInput is whether to attach to STDIN + // If false, stdout will not be attached + AttachInput bool +} + // Attach attaches to a container func (c *Container) Attach(streams *AttachStreams, keys string, resize <-chan remotecommand.TerminalSize) error { if !c.batched { diff --git a/libpod/container_attach.go b/libpod/container_attach.go deleted file mode 100644 index f925c3897..000000000 --- a/libpod/container_attach.go +++ /dev/null @@ -1,179 +0,0 @@ -package libpod - -import ( - "fmt" - "io" - "net" - "os" - "path/filepath" - - "github.com/containers/libpod/pkg/kubeutils" - "github.com/containers/libpod/utils" - "github.com/docker/docker/pkg/term" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" - "golang.org/x/sys/unix" - "k8s.io/client-go/tools/remotecommand" -) - -//#include -// extern int unix_path_length(){struct sockaddr_un addr; return sizeof(addr.sun_path) - 1;} -import "C" - -/* Sync with stdpipe_t in conmon.c */ -const ( - AttachPipeStdin = 1 - AttachPipeStdout = 2 - AttachPipeStderr = 3 -) - -// AttachStreams contains streams that will be attached to the container -type AttachStreams struct { - // OutputStream will be attached to container's STDOUT - OutputStream io.WriteCloser - // ErrorStream will be attached to container's STDERR - ErrorStream io.WriteCloser - // InputStream will be attached to container's STDIN - InputStream io.Reader - // AttachOutput is whether to attach to STDOUT - // If false, stdout will not be attached - AttachOutput bool - // AttachError is whether to attach to STDERR - // If false, stdout will not be attached - AttachError bool - // AttachInput is whether to attach to STDIN - // If false, stdout will not be attached - AttachInput bool -} - -// Attach to the given container -// Does not check if state is appropriate -func (c *Container) attach(streams *AttachStreams, keys string, resize <-chan remotecommand.TerminalSize, startContainer bool) error { - if !streams.AttachOutput && !streams.AttachError && !streams.AttachInput { - return errors.Wrapf(ErrInvalidArg, "must provide at least one stream to attach to") - } - - // Check the validity of the provided keys first - var err error - detachKeys := []byte{} - if len(keys) > 0 { - detachKeys, err = term.ToBytes(keys) - if err != nil { - return errors.Wrapf(err, "invalid detach keys") - } - } - - logrus.Debugf("Attaching to container %s", c.ID()) - - return c.attachContainerSocket(resize, detachKeys, streams, startContainer) -} - -// attachContainerSocket connects to the container's attach socket and deals with the IO -// TODO add a channel to allow interrupting -func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, detachKeys []byte, streams *AttachStreams, startContainer bool) error { - kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) { - controlPath := filepath.Join(c.bundlePath(), "ctl") - controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0) - if err != nil { - logrus.Debugf("Could not open ctl file: %v", err) - return - } - defer controlFile.Close() - - logrus.Debugf("Received a resize event: %+v", size) - if _, err = fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width); err != nil { - logrus.Warnf("Failed to write to control file to resize terminal: %v", err) - } - }) - - socketPath := c.AttachSocketPath() - - maxUnixLength := int(C.unix_path_length()) - if maxUnixLength < len(socketPath) { - socketPath = socketPath[0:maxUnixLength] - } - - logrus.Debug("connecting to socket ", socketPath) - - conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: socketPath, Net: "unixpacket"}) - if err != nil { - return errors.Wrapf(err, "failed to connect to container's attach socket: %v", socketPath) - } - defer conn.Close() - - if startContainer { - if err := c.start(); err != nil { - return err - } - } - - receiveStdoutError := make(chan error) - go func() { - receiveStdoutError <- redirectResponseToOutputStreams(streams.OutputStream, streams.ErrorStream, streams.AttachOutput, streams.AttachError, conn) - }() - - stdinDone := make(chan error) - go func() { - var err error - if streams.AttachInput { - _, err = utils.CopyDetachable(conn, streams.InputStream, detachKeys) - conn.CloseWrite() - } - stdinDone <- err - }() - - select { - case err := <-receiveStdoutError: - return err - case err := <-stdinDone: - if _, ok := err.(utils.DetachError); ok { - return nil - } - if streams.AttachOutput || streams.AttachError { - return <-receiveStdoutError - } - } - return nil -} - -func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, writeOutput, writeError bool, conn io.Reader) error { - var err error - buf := make([]byte, 8192+1) /* Sync with conmon STDIO_BUF_SIZE */ - for { - nr, er := conn.Read(buf) - if nr > 0 { - var dst io.Writer - var doWrite bool - switch buf[0] { - case AttachPipeStdout: - dst = outputStream - doWrite = writeOutput - case AttachPipeStderr: - dst = errorStream - doWrite = writeError - default: - logrus.Infof("Received unexpected attach type %+d", buf[0]) - } - - if doWrite { - nw, ew := dst.Write(buf[1:nr]) - if ew != nil { - err = ew - break - } - if nr != nw+1 { - err = io.ErrShortWrite - break - } - } - } - if er == io.EOF { - break - } - if er != nil { - err = er - break - } - } - return err -} diff --git a/libpod/container_attach_linux.go b/libpod/container_attach_linux.go new file mode 100644 index 000000000..1d6f0bd96 --- /dev/null +++ b/libpod/container_attach_linux.go @@ -0,0 +1,162 @@ +//+build linux + +package libpod + +import ( + "fmt" + "io" + "net" + "os" + "path/filepath" + + "github.com/containers/libpod/pkg/kubeutils" + "github.com/containers/libpod/utils" + "github.com/docker/docker/pkg/term" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "golang.org/x/sys/unix" + "k8s.io/client-go/tools/remotecommand" +) + +//#include +// extern int unix_path_length(){struct sockaddr_un addr; return sizeof(addr.sun_path) - 1;} +import "C" + +/* Sync with stdpipe_t in conmon.c */ +const ( + AttachPipeStdin = 1 + AttachPipeStdout = 2 + AttachPipeStderr = 3 +) + +// Attach to the given container +// Does not check if state is appropriate +func (c *Container) attach(streams *AttachStreams, keys string, resize <-chan remotecommand.TerminalSize, startContainer bool) error { + if !streams.AttachOutput && !streams.AttachError && !streams.AttachInput { + return errors.Wrapf(ErrInvalidArg, "must provide at least one stream to attach to") + } + + // Check the validity of the provided keys first + var err error + detachKeys := []byte{} + if len(keys) > 0 { + detachKeys, err = term.ToBytes(keys) + if err != nil { + return errors.Wrapf(err, "invalid detach keys") + } + } + + logrus.Debugf("Attaching to container %s", c.ID()) + + return c.attachContainerSocket(resize, detachKeys, streams, startContainer) +} + +// attachContainerSocket connects to the container's attach socket and deals with the IO +// TODO add a channel to allow interrupting +func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, detachKeys []byte, streams *AttachStreams, startContainer bool) error { + kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) { + controlPath := filepath.Join(c.bundlePath(), "ctl") + controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0) + if err != nil { + logrus.Debugf("Could not open ctl file: %v", err) + return + } + defer controlFile.Close() + + logrus.Debugf("Received a resize event: %+v", size) + if _, err = fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width); err != nil { + logrus.Warnf("Failed to write to control file to resize terminal: %v", err) + } + }) + + socketPath := c.AttachSocketPath() + + maxUnixLength := int(C.unix_path_length()) + if maxUnixLength < len(socketPath) { + socketPath = socketPath[0:maxUnixLength] + } + + logrus.Debug("connecting to socket ", socketPath) + + conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: socketPath, Net: "unixpacket"}) + if err != nil { + return errors.Wrapf(err, "failed to connect to container's attach socket: %v", socketPath) + } + defer conn.Close() + + if startContainer { + if err := c.start(); err != nil { + return err + } + } + + receiveStdoutError := make(chan error) + go func() { + receiveStdoutError <- redirectResponseToOutputStreams(streams.OutputStream, streams.ErrorStream, streams.AttachOutput, streams.AttachError, conn) + }() + + stdinDone := make(chan error) + go func() { + var err error + if streams.AttachInput { + _, err = utils.CopyDetachable(conn, streams.InputStream, detachKeys) + conn.CloseWrite() + } + stdinDone <- err + }() + + select { + case err := <-receiveStdoutError: + return err + case err := <-stdinDone: + if _, ok := err.(utils.DetachError); ok { + return nil + } + if streams.AttachOutput || streams.AttachError { + return <-receiveStdoutError + } + } + return nil +} + +func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, writeOutput, writeError bool, conn io.Reader) error { + var err error + buf := make([]byte, 8192+1) /* Sync with conmon STDIO_BUF_SIZE */ + for { + nr, er := conn.Read(buf) + if nr > 0 { + var dst io.Writer + var doWrite bool + switch buf[0] { + case AttachPipeStdout: + dst = outputStream + doWrite = writeOutput + case AttachPipeStderr: + dst = errorStream + doWrite = writeError + default: + logrus.Infof("Received unexpected attach type %+d", buf[0]) + } + + if doWrite { + nw, ew := dst.Write(buf[1:nr]) + if ew != nil { + err = ew + break + } + if nr != nw+1 { + err = io.ErrShortWrite + break + } + } + } + if er == io.EOF { + break + } + if er != nil { + err = er + break + } + } + return err +} diff --git a/libpod/container_attach_unsupported.go b/libpod/container_attach_unsupported.go new file mode 100644 index 000000000..068652b29 --- /dev/null +++ b/libpod/container_attach_unsupported.go @@ -0,0 +1,11 @@ +//+build !linux + +package libpod + +import ( + "k8s.io/client-go/tools/remotecommand" +) + +func (c *Container) attach(streams *AttachStreams, keys string, resize <-chan remotecommand.TerminalSize, startContainer bool) error { + return ErrNotImplemented +} diff --git a/libpod/lock/shm_lock_manager_unsupported.go b/libpod/lock/shm_lock_manager_unsupported.go index a1340fcd1..cbdb2f7bc 100644 --- a/libpod/lock/shm_lock_manager_unsupported.go +++ b/libpod/lock/shm_lock_manager_unsupported.go @@ -9,12 +9,12 @@ import "fmt" type SHMLockManager struct{} // NewSHMLockManager is not supported on this platform -func NewSHMLockManager(numLocks uint32) (Manager, error) { +func NewSHMLockManager(path string, numLocks uint32) (Manager, error) { return nil, fmt.Errorf("not supported") } // OpenSHMLockManager is not supported on this platform -func OpenSHMLockManager(numLocks uint32) (Manager, error) { +func OpenSHMLockManager(path string, numLocks uint32) (Manager, error) { return nil, fmt.Errorf("not supported") } diff --git a/libpod/runtime_volume_unsupported.go b/libpod/runtime_volume_unsupported.go new file mode 100644 index 000000000..d87459759 --- /dev/null +++ b/libpod/runtime_volume_unsupported.go @@ -0,0 +1,19 @@ +// +build !linux + +package libpod + +import ( + "context" +) + +func (r *Runtime) removeVolume(ctx context.Context, v *Volume, force, prune bool) error { + return ErrNotImplemented +} + +func (r *Runtime) newVolume(ctx context.Context, options ...VolumeCreateOption) (*Volume, error) { + return nil, ErrNotImplemented +} + +func (r *Runtime) NewVolume(ctx context.Context, options ...VolumeCreateOption) (*Volume, error) { + return nil, ErrNotImplemented +} diff --git a/pkg/rootless/rootless_unsupported.go b/pkg/rootless/rootless_unsupported.go index d72402c9f..1823c023e 100644 --- a/pkg/rootless/rootless_unsupported.go +++ b/pkg/rootless/rootless_unsupported.go @@ -14,7 +14,7 @@ func IsRootless() bool { // BecomeRootInUserNS is a stub function that always returns false and an // error on unsupported OS's func BecomeRootInUserNS() (bool, int, error) { - return false, -1, errors.New("this function is not supported on this os") + return false, -1, errors.New("this function is not supported on this os1") } // GetRootlessUID returns the UID of the user in the parent userNS @@ -34,11 +34,18 @@ func SkipStorageSetup() bool { // JoinNS re-exec podman in a new userNS and join the user namespace of the specified // PID. func JoinNS(pid uint) (bool, int, error) { - return false, -1, errors.New("this function is not supported on this os") + return false, -1, errors.New("this function is not supported on this os2") } // JoinNSPath re-exec podman in a new userNS and join the owner user namespace of the // specified path. func JoinNSPath(path string) (bool, int, error) { - return false, -1, errors.New("this function is not supported on this os") + return false, -1, errors.New("this function is not supported on this os3") +} + +// JoinDirectUserAndMountNS re-exec podman in a new userNS and join the user and mount +// namespace of the specified PID without looking up its parent. Useful to join directly +// the conmon process. +func JoinDirectUserAndMountNS(pid uint) (bool, int, error) { + return false, -1, errors.New("this function is not supported on this os4") } diff --git a/pkg/spec/config_unsupported.go b/pkg/spec/config_unsupported.go index c2a58696d..160414878 100644 --- a/pkg/spec/config_unsupported.go +++ b/pkg/spec/config_unsupported.go @@ -26,3 +26,7 @@ func (c *CreateConfig) createBlockIO() (*spec.LinuxBlockIO, error) { func makeThrottleArray(throttleInput []string, rateType int) ([]spec.LinuxThrottleDevice, error) { return nil, errors.New("function not implemented") } + +func devicesFromPath(g *generate.Generator, devicePath string) error { + return errors.New("function not implemented") +} -- cgit v1.2.3-54-g00ecf