2014-08-01 00:46:18 +04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2016-01-08 01:14:05 +03: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 18:53:35 +03:00
|
|
|
"net/http"
|
2015-07-17 01:33:13 +03:00
|
|
|
"runtime"
|
2016-03-18 21:50:19 +03:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2015-03-25 10:44:12 +03:00
|
|
|
|
2016-05-31 18:18:17 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2015-11-03 20:43:36 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 22:55:17 +03:00
|
|
|
"github.com/docker/docker/container"
|
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 18:53:35 +03:00
|
|
|
"github.com/docker/docker/errors"
|
2016-03-18 21:50:19 +03:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2014-08-01 00:46:18 +04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2016-01-05 03:05:26 +03:00
|
|
|
containertypes "github.com/docker/engine-api/types/container"
|
2014-08-01 00:46:18 +04:00
|
|
|
)
|
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// ContainerStart starts a container.
|
2016-07-06 10:13:59 +03:00
|
|
|
func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, validateHostname bool) error {
|
2015-12-11 20:39:28 +03:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-17 02:06:35 +03:00
|
|
|
if err != nil {
|
2015-03-25 10:44:12 +03:00
|
|
|
return err
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03: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 18:53:35 +03:00
|
|
|
return fmt.Errorf("Cannot start a paused container, try unpause instead.")
|
2015-01-15 03:44:53 +03:00
|
|
|
}
|
|
|
|
|
2014-08-31 19: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 18:53:35 +03:00
|
|
|
err := fmt.Errorf("Container already started")
|
|
|
|
return errors.NewErrorWithStatusCode(err, http.StatusNotModified)
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
|
|
|
|
2015-08-08 01:24:18 +03:00
|
|
|
// Windows does not have the backwards compatibility issue here.
|
2015-07-17 01:33:13 +03: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 {
|
2015-12-01 01:44:34 +03:00
|
|
|
logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12")
|
2016-01-14 09:58:54 +03:00
|
|
|
oldNetworkMode := container.HostConfig.NetworkMode
|
2015-12-21 22:23:20 +03:00
|
|
|
if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-12 15:50:37 +03:00
|
|
|
if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-29 20:51:40 +03:00
|
|
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
2015-07-17 01:33:13 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-01-14 09:58:54 +03:00
|
|
|
newNetworkMode := container.HostConfig.NetworkMode
|
|
|
|
if string(oldNetworkMode) != string(newNetworkMode) {
|
|
|
|
// if user has change the network mode on starting, clean up the
|
|
|
|
// old networks. It is a deprecated feature and will be removed in Docker 1.12
|
|
|
|
container.NetworkSettings.Networks = nil
|
|
|
|
if err := container.ToDisk(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
container.InitDNSHostConfig()
|
2015-07-17 01:33:13 +03: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 18:53:35 +03:00
|
|
|
return fmt.Errorf("Supplying a hostconfig on start is not supported. It should be supplied on create")
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:05:21 +03:00
|
|
|
|
2015-08-06 14:55:56 +03:00
|
|
|
// check if hostConfig is in line with the current system settings.
|
|
|
|
// It may happen cgroups are umounted or the like.
|
2016-07-06 10:13:59 +03:00
|
|
|
if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false, validateHostname); err != nil {
|
2015-08-06 14:55:56 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-12-15 03:42:26 +03: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.
|
|
|
|
if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil {
|
|
|
|
return err
|
2015-08-06 14:55:56 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
return daemon.containerStart(container)
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
2015-11-03 04:06:09 +03:00
|
|
|
|
|
|
|
// Start starts a container
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) Start(container *container.Container) error {
|
2015-11-03 04:06:09 +03:00
|
|
|
return daemon.containerStart(container)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) containerStart(container *container.Container) (err error) {
|
2015-11-03 04:06:09 +03:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
if container.Running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03: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 18:53:35 +03:00
|
|
|
return fmt.Errorf("Container is marked for removal and cannot be started.")
|
2015-11-03 04:06:09 +03: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 22:55:17 +03:00
|
|
|
container.SetError(err)
|
2015-11-03 04:06:09 +03:00
|
|
|
// if no one else has set it, make sure we don't leave it at zero
|
2016-06-14 21:11:43 +03:00
|
|
|
if container.ExitCode() == 0 {
|
|
|
|
container.SetExitCode(128)
|
2015-11-03 04:06:09 +03:00
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
container.ToDisk()
|
2015-11-03 20:43:36 +03:00
|
|
|
daemon.Cleanup(container)
|
2015-11-03 04:06:09 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := daemon.conditionalMountOnStart(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure NetworkMode has an acceptable value. We do this to ensure
|
|
|
|
// backwards API compatibility.
|
2015-11-12 22:55:17 +03:00
|
|
|
container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig)
|
2015-11-03 04:06:09 +03:00
|
|
|
|
2015-11-03 21:25:09 +03:00
|
|
|
if err := daemon.initializeNetworking(container); err != nil {
|
2015-11-03 04:06:09 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-03-18 21:50:19 +03:00
|
|
|
|
|
|
|
spec, err := daemon.createSpec(container)
|
2015-11-03 04:06:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:49:50 +03:00
|
|
|
createOptions := []libcontainerd.CreateOption{libcontainerd.WithRestartManager(container.RestartManager(true))}
|
|
|
|
copts, err := daemon.getLibcontainerdCreateOptions(container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if copts != nil {
|
|
|
|
createOptions = append(createOptions, *copts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.containerd.Create(container.ID, *spec, createOptions...); err != nil {
|
2016-05-31 18:18:17 +03:00
|
|
|
errDesc := grpc.ErrorDesc(err)
|
|
|
|
logrus.Errorf("Create container failed with error: %s", errDesc)
|
2016-03-18 21:50:19 +03: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-05-24 00:49:50 +03:00
|
|
|
if strings.Contains(errDesc, container.Path) &&
|
|
|
|
(strings.Contains(errDesc, "executable file not found") ||
|
|
|
|
strings.Contains(errDesc, "no such file or directory") ||
|
|
|
|
strings.Contains(errDesc, "system cannot find the file specified")) {
|
2016-06-14 21:11:43 +03:00
|
|
|
container.SetExitCode(127)
|
2016-03-18 21:50:19 +03:00
|
|
|
}
|
|
|
|
// set to 126 for container cmd can't be invoked errors
|
2016-05-31 18:18:17 +03:00
|
|
|
if strings.Contains(errDesc, syscall.EACCES.Error()) {
|
2016-06-14 21:11:43 +03:00
|
|
|
container.SetExitCode(126)
|
2015-11-03 04:06:09 +03:00
|
|
|
}
|
|
|
|
|
2016-03-18 21:50:19 +03:00
|
|
|
container.Reset(false)
|
2016-04-07 06:38:12 +03:00
|
|
|
|
2016-05-31 18:18:17 +03:00
|
|
|
return fmt.Errorf("%s", errDesc)
|
2015-11-03 04:06:09 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 22:06:19 +03:00
|
|
|
return nil
|
2015-11-03 20:33:13 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 20:43:36 +03: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 22:55:17 +03:00
|
|
|
func (daemon *Daemon) Cleanup(container *container.Container) {
|
2015-11-03 20:43:36 +03:00
|
|
|
daemon.releaseNetwork(container)
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
container.UnmountIpcMounts(detachMounted)
|
2015-11-03 20:43:36 +03:00
|
|
|
|
2016-03-18 21:50:19 +03: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 20:43:36 +03:00
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
for _, eConfig := range container.ExecCommands.Commands() {
|
2015-11-21 01:35:16 +03:00
|
|
|
daemon.unregisterExecCommand(container, eConfig)
|
2015-11-03 20:43:36 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 01:27:04 +03:00
|
|
|
if container.BaseFS != "" {
|
|
|
|
if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil {
|
|
|
|
logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
|
|
|
|
}
|
2015-11-03 20:43:36 +03:00
|
|
|
}
|
2016-03-09 03:54:33 +03:00
|
|
|
container.CancelAttachContext()
|
2015-11-03 20:43:36 +03:00
|
|
|
}
|