2014-07-31 16:46:18 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2016-01-07 17:14:05 -05:00
|
|
|
"fmt"
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
"net/http"
|
2015-07-16 18:33:13 -04:00
|
|
|
"runtime"
|
2016-03-18 14:50:19 -04:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-07-20 19:11:28 -04:00
|
|
|
"time"
|
2015-03-25 03:44:12 -04:00
|
|
|
|
2016-05-31 11:18:17 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2015-11-03 12:43:36 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-06-07 03:45:21 -04:00
|
|
|
apierrors "github.com/docker/docker/api/errors"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2014-07-31 16:46:18 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerStart starts a container.
|
2016-11-30 13:22:07 -05:00
|
|
|
func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, checkpoint string, checkpointDir string) error {
|
2016-10-27 20:43:57 -04:00
|
|
|
if checkpoint != "" && !daemon.HasExperimental() {
|
2016-06-07 03:45:21 -04:00
|
|
|
return apierrors.NewBadRequestError(fmt.Errorf("checkpoint is only supported in experimental mode"))
|
2016-10-27 20:43:57 -04:00
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.IsPaused() {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("Cannot start a paused container, try unpause instead.")
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
if container.IsRunning() {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
err := fmt.Errorf("Container already started")
|
2016-06-07 03:45:21 -04:00
|
|
|
return apierrors.NewErrorWithStatusCode(err, http.StatusNotModified)
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 18:24:18 -04:00
|
|
|
// Windows does not have the backwards compatibility issue here.
|
2015-07-16 18:33:13 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// This is kept for backward compatibility - hostconfig should be passed when
|
|
|
|
// creating a container, not during start.
|
|
|
|
if hostConfig != nil {
|
2016-09-20 06:38:56 -04:00
|
|
|
logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and has been removed in Docker 1.12")
|
2016-01-14 01:58:54 -05:00
|
|
|
oldNetworkMode := container.HostConfig.NetworkMode
|
2015-12-21 14:23:20 -05:00
|
|
|
if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-12 07:50:37 -05:00
|
|
|
if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
2015-07-16 18:33:13 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-14 01:58:54 -05:00
|
|
|
newNetworkMode := container.HostConfig.NetworkMode
|
|
|
|
if string(oldNetworkMode) != string(newNetworkMode) {
|
|
|
|
// if user has change the network mode on starting, clean up the
|
2016-09-20 06:38:56 -04:00
|
|
|
// old networks. It is a deprecated feature and has been removed in Docker 1.12
|
2016-01-14 01:58:54 -05:00
|
|
|
container.NetworkSettings.Networks = nil
|
|
|
|
if err := container.ToDisk(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
container.InitDNSHostConfig()
|
2015-07-16 18:33:13 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hostConfig != nil {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("Supplying a hostconfig on start is not supported. It should be supplied on create")
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-08-06 07:55:56 -04:00
|
|
|
// check if hostConfig is in line with the current system settings.
|
|
|
|
// It may happen cgroups are umounted or the like.
|
2016-11-30 13:22:07 -05:00
|
|
|
if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false); err != nil {
|
2015-08-06 07:55:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-14 19:42:26 -05:00
|
|
|
// Adapt for old containers in case we have updates in this function and
|
|
|
|
// old containers never have chance to call the new function in create stage.
|
2016-11-03 12:44:40 -04:00
|
|
|
if hostConfig != nil {
|
|
|
|
if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-06 07:55:56 -04:00
|
|
|
}
|
|
|
|
|
2016-09-19 12:01:16 -04:00
|
|
|
return daemon.containerStart(container, checkpoint, checkpointDir, true)
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
|
|
|
|
// Start starts a container
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) Start(container *container.Container) error {
|
2016-09-19 12:01:16 -04:00
|
|
|
return daemon.containerStart(container, "", "", true)
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// containerStart prepares the container to run by setting up everything the
|
|
|
|
// container needs, such as storage and networking, as well as links
|
|
|
|
// between containers. The container is left waiting for a signal to
|
|
|
|
// begin running.
|
2016-09-19 12:01:16 -04:00
|
|
|
func (daemon *Daemon) containerStart(container *container.Container, checkpoint string, checkpointDir string, resetRestartManager bool) (err error) {
|
2016-07-20 19:11:28 -04:00
|
|
|
start := time.Now()
|
2015-11-02 20:06:09 -05:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2016-10-05 16:29:56 -04:00
|
|
|
if resetRestartManager && container.Running { // skip this check if already in restarting step and resetRestartManager==false
|
2015-11-02 20:06:09 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.RemovalInProgress || container.Dead {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("Container is marked for removal and cannot be started.")
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we encounter an error during start we need to ensure that any other
|
|
|
|
// setup has been cleaned up properly
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-11-12 14:55:17 -05:00
|
|
|
container.SetError(err)
|
2015-11-02 20:06:09 -05:00
|
|
|
// if no one else has set it, make sure we don't leave it at zero
|
2016-06-14 14:11:43 -04:00
|
|
|
if container.ExitCode() == 0 {
|
|
|
|
container.SetExitCode(128)
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
container.ToDisk()
|
2016-12-07 01:37:08 -05:00
|
|
|
|
|
|
|
container.Reset(false)
|
|
|
|
|
2015-11-03 12:43:36 -05:00
|
|
|
daemon.Cleanup(container)
|
2016-08-02 22:11:01 -04:00
|
|
|
// if containers AutoRemove flag is set, remove it after clean up
|
|
|
|
if container.HostConfig.AutoRemove {
|
|
|
|
container.Unlock()
|
|
|
|
if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
|
|
|
|
logrus.Errorf("can't remove container %s: %v", container.ID, err)
|
|
|
|
}
|
|
|
|
container.Lock()
|
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := daemon.conditionalMountOnStart(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.initializeNetworking(container); err != nil {
|
2015-11-02 20:06:09 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
spec, err := daemon.createSpec(container)
|
2015-11-02 20:06:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-05 16:29:56 -04:00
|
|
|
createOptions, err := daemon.getLibcontainerdCreateOptions(container)
|
2016-05-23 17:49:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-05 16:29:56 -04:00
|
|
|
|
|
|
|
if resetRestartManager {
|
|
|
|
container.ResetRestartManager(true)
|
2016-05-23 17:49:50 -04:00
|
|
|
}
|
|
|
|
|
2016-09-19 12:01:16 -04:00
|
|
|
if checkpointDir == "" {
|
|
|
|
checkpointDir = container.CheckpointDir()
|
|
|
|
}
|
|
|
|
|
2016-12-19 07:22:45 -05:00
|
|
|
if daemon.saveApparmorConfig(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-19 12:01:16 -04:00
|
|
|
if err := daemon.containerd.Create(container.ID, checkpoint, checkpointDir, *spec, container.InitializeStdio, createOptions...); err != nil {
|
2016-05-31 11:18:17 -04:00
|
|
|
errDesc := grpc.ErrorDesc(err)
|
2016-11-04 17:56:28 -04:00
|
|
|
contains := func(s1, s2 string) bool {
|
|
|
|
return strings.Contains(strings.ToLower(s1), s2)
|
|
|
|
}
|
2016-05-31 11:18:17 -04:00
|
|
|
logrus.Errorf("Create container failed with error: %s", errDesc)
|
2016-03-18 14:50:19 -04:00
|
|
|
// if we receive an internal error from the initial start of a container then lets
|
|
|
|
// return it instead of entering the restart loop
|
|
|
|
// set to 127 for container cmd not found/does not exist)
|
2016-11-04 17:56:28 -04:00
|
|
|
if contains(errDesc, container.Path) &&
|
|
|
|
(contains(errDesc, "executable file not found") ||
|
|
|
|
contains(errDesc, "no such file or directory") ||
|
|
|
|
contains(errDesc, "system cannot find the file specified")) {
|
2016-06-14 14:11:43 -04:00
|
|
|
container.SetExitCode(127)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
// set to 126 for container cmd can't be invoked errors
|
2016-11-04 17:56:28 -04:00
|
|
|
if contains(errDesc, syscall.EACCES.Error()) {
|
2016-06-14 14:11:43 -04:00
|
|
|
container.SetExitCode(126)
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
|
|
|
|
2016-07-18 10:21:48 -04:00
|
|
|
// attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
|
2016-11-04 17:56:28 -04:00
|
|
|
if contains(errDesc, syscall.ENOTDIR.Error()) {
|
2016-07-18 10:21:48 -04:00
|
|
|
errDesc += ": Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type"
|
|
|
|
container.SetExitCode(127)
|
|
|
|
}
|
|
|
|
|
2016-05-31 11:18:17 -04:00
|
|
|
return fmt.Errorf("%s", errDesc)
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:11:28 -04:00
|
|
|
containerActions.WithValues("start").UpdateSince(start)
|
|
|
|
|
2015-11-14 14:06:19 -05:00
|
|
|
return nil
|
2015-11-03 12:33:13 -05:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:43:36 -05:00
|
|
|
// Cleanup releases any network resources allocated to the container along with any rules
|
|
|
|
// around how containers are linked together. It also unmounts the container's root filesystem.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) Cleanup(container *container.Container) {
|
2015-11-03 12:43:36 -05:00
|
|
|
daemon.releaseNetwork(container)
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
container.UnmountIpcMounts(detachMounted)
|
2015-11-03 12:43:36 -05:00
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := daemon.conditionalUnmountOnCleanup(container); err != nil {
|
|
|
|
// FIXME: remove once reference counting for graphdrivers has been refactored
|
|
|
|
// Ensure that all the mounts are gone
|
|
|
|
if mountid, err := daemon.layerStore.GetMountID(container.ID); err == nil {
|
|
|
|
daemon.cleanupMountsByID(mountid)
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 12:43:36 -05:00
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
if err := container.UnmountSecrets(); err != nil {
|
|
|
|
logrus.Warnf("%s cleanup: failed to unmount secrets: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
for _, eConfig := range container.ExecCommands.Commands() {
|
2015-11-20 17:35:16 -05:00
|
|
|
daemon.unregisterExecCommand(container, eConfig)
|
2015-11-03 12:43:36 -05:00
|
|
|
}
|
|
|
|
|
2016-03-29 18:27:04 -04:00
|
|
|
if container.BaseFS != "" {
|
2016-10-03 13:53:06 -04:00
|
|
|
if err := container.UnmountVolumes(daemon.LogVolumeEvent); err != nil {
|
2016-03-29 18:27:04 -04:00
|
|
|
logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
|
|
|
|
}
|
2015-11-03 12:43:36 -05:00
|
|
|
}
|
2016-03-08 19:54:33 -05:00
|
|
|
container.CancelAttachContext()
|
2015-11-03 12:43:36 -05:00
|
|
|
}
|