From 1e6413e3fde448d3b44fc36fd966d8d57af811f5 Mon Sep 17 00:00:00 2001
From: Matthew Heon <matthew.heon@pm.me>
Date: Thu, 2 May 2019 11:37:59 -0400
Subject: [PATCH] Add an InvalidState varlink error for Init

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
---
 cmd/podman/errors_remote.go          |  2 ++
 cmd/podman/varlink/io.podman.varlink |  7 +++++--
 pkg/adapter/containers_remote.go     | 11 ++++++++++-
 pkg/varlinkapi/containers.go         |  3 +++
 4 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/cmd/podman/errors_remote.go b/cmd/podman/errors_remote.go
index ab255ea565..1e276be10c 100644
--- a/cmd/podman/errors_remote.go
+++ b/cmd/podman/errors_remote.go
@@ -33,6 +33,8 @@ func outputError(err error) {
 			ne = errors.New(e.Reason)
 		case *iopodman.VolumeNotFound:
 			ne = errors.New(e.Reason)
+		case *iopodman.InvalidState:
+			ne = errors.New(e.Reason)
 		case *iopodman.ErrorOccurred:
 			ne = errors.New(e.Reason)
 		default:
diff --git a/cmd/podman/varlink/io.podman.varlink b/cmd/podman/varlink/io.podman.varlink
index 1b6113b7cf..912d001e91 100644
--- a/cmd/podman/varlink/io.podman.varlink
+++ b/cmd/podman/varlink/io.podman.varlink
@@ -1233,7 +1233,7 @@ error PodNotFound (name: string, reason: string)
 # VolumeNotFound means the volume could not be found by the name or ID in local storage.
 error VolumeNotFound (id: string, reason: string)
 
-# PodContainerError means a container associated with a pod failed to preform an operation. It contains
+# PodContainerError means a container associated with a pod failed to perform an operation. It contains
 # a container ID of the container that failed.
 error PodContainerError (podname: string, errors: []PodContainerErrorData)
 
@@ -1241,6 +1241,9 @@ error PodContainerError (podname: string, errors: []PodContainerErrorData)
 # the pod ID.
 error NoContainersInPod (name: string)
 
+# InvalidState indicates that a container or pod was in an improper state for the requested operation
+error InvalidState (id: string, reason: string)
+
 # ErrorOccurred is a generic error for an error that occurs during the execution.  The actual error message
 # is includes as part of the error's text.
 error ErrorOccurred (reason: string)
@@ -1249,4 +1252,4 @@ error ErrorOccurred (reason: string)
 error RuntimeError (reason: string)
 
 # The Podman endpoint requires that you use a streaming connection.
-error WantsMoreRequired (reason: string)
+error WantsMoreRequired (reason: string)
\ No newline at end of file
diff --git a/pkg/adapter/containers_remote.go b/pkg/adapter/containers_remote.go
index 268ac09e89..d1e4b4dcd4 100644
--- a/pkg/adapter/containers_remote.go
+++ b/pkg/adapter/containers_remote.go
@@ -251,7 +251,16 @@ func (r *LocalRuntime) InitContainers(ctx context.Context, cli *cliconfig.InitVa
 	for _, id := range ids {
 		initialized, err := iopodman.InitContainer().Call(r.Conn, id)
 		if err != nil {
-			failures[id] = err
+			if cli.All {
+				switch err.(type) {
+				case *iopodman.InvalidState:
+					ok = append(ok, initialized)
+				default:
+					failures[id] = err
+				}
+			} else {
+				failures[id] = err
+			}
 		} else {
 			ok = append(ok, initialized)
 		}
diff --git a/pkg/varlinkapi/containers.go b/pkg/varlinkapi/containers.go
index 861e3210bc..c8be41636c 100644
--- a/pkg/varlinkapi/containers.go
+++ b/pkg/varlinkapi/containers.go
@@ -372,6 +372,9 @@ func (i *LibpodAPI) InitContainer(call iopodman.VarlinkCall, name string) error
 		return call.ReplyContainerNotFound(name, err.Error())
 	}
 	if err := ctr.Init(getContext()); err != nil {
+		if errors.Cause(err) == libpod.ErrCtrStateInvalid {
+			return call.ReplyInvalidState(ctr.ID(), err.Error())
+		}
 		return call.ReplyErrorOccurred(err.Error())
 	}
 	return call.ReplyInitContainer(ctr.ID())