2015-11-12 14:55:17 -05:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
|
|
|
"github.com/docker/docker/daemon/links"
|
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2016-08-17 12:38:34 -04:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2015-11-12 14:55:17 -05:00
|
|
|
)
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
func u32Ptr(i int64) *uint32 { u := uint32(i); return &u }
|
|
|
|
func fmPtr(i int64) *os.FileMode { fm := os.FileMode(i); return &fm }
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
|
|
|
|
var env []string
|
2015-09-03 20:51:04 -04:00
|
|
|
children := daemon.children(container)
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
|
2015-11-12 14:55:17 -05:00
|
|
|
if bridgeSettings == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
for linkAlias, child := range children {
|
|
|
|
if !child.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
|
|
|
return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
|
2015-09-03 20:51:04 -04:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
|
2015-09-03 20:51:04 -04:00
|
|
|
if childBridgeSettings == nil {
|
|
|
|
return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
link := links.NewLink(
|
|
|
|
bridgeSettings.IPAddress,
|
|
|
|
childBridgeSettings.IPAddress,
|
|
|
|
linkAlias,
|
|
|
|
child.Config.Env,
|
|
|
|
child.Config.ExposedPorts,
|
|
|
|
)
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
for _, envVar := range link.ToEnv() {
|
|
|
|
env = append(env, envVar)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-03 20:51:04 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
return env, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSize returns the real size & virtual size of the container.
|
|
|
|
func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
|
|
|
|
var (
|
|
|
|
sizeRw, sizeRootfs int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := daemon.Mount(container); err != nil {
|
|
|
|
logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
|
|
|
defer daemon.Unmount(container)
|
|
|
|
|
|
|
|
sizeRw, err = container.RWLayer.Size()
|
|
|
|
if err != nil {
|
2015-12-16 15:32:16 -05:00
|
|
|
logrus.Errorf("Driver %s couldn't return diff size of container %s: %s",
|
|
|
|
daemon.GraphDriverName(), container.ID, err)
|
2015-11-12 14:55:17 -05:00
|
|
|
// FIXME: GetSize should return an error. Not changing it now in case
|
|
|
|
// there is a side-effect.
|
|
|
|
sizeRw = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent := container.RWLayer.Parent(); parent != nil {
|
|
|
|
sizeRootfs, err = parent.Size()
|
|
|
|
if err != nil {
|
|
|
|
sizeRootfs = -1
|
|
|
|
} else if sizeRw != -1 {
|
|
|
|
sizeRootfs += sizeRw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectToNetwork connects a container to a network
|
2016-01-07 19:18:34 -05:00
|
|
|
func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
|
2016-04-08 18:25:07 -04:00
|
|
|
if endpointConfig == nil {
|
|
|
|
endpointConfig = &networktypes.EndpointSettings{}
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
if !container.Running {
|
2016-01-11 20:13:39 -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 errRemovalContainer(container.ID)
|
2016-01-11 20:13:39 -05:00
|
|
|
}
|
2016-01-20 20:51:41 -05:00
|
|
|
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
|
2016-01-11 20:13:39 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-04-08 18:25:07 -04:00
|
|
|
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
2016-01-11 20:13:39 -05:00
|
|
|
} else {
|
|
|
|
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
if err := container.ToDiskLocking(); err != nil {
|
|
|
|
return fmt.Errorf("Error saving container to disk: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-03 14:10:27 -05:00
|
|
|
// DisconnectFromNetwork disconnects container from network n.
|
2016-08-26 16:08:28 -04:00
|
|
|
func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, networkName string, force bool) error {
|
|
|
|
n, err := daemon.FindNetwork(networkName)
|
|
|
|
if !container.Running || (err != nil && force) {
|
2016-01-11 20:13:39 -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 errRemovalContainer(container.ID)
|
2016-01-11 20:13:39 -05:00
|
|
|
}
|
2016-09-03 20:14:48 -04:00
|
|
|
// In case networkName is resolved we will use n.Name()
|
|
|
|
// this will cover the case where network id is passed.
|
|
|
|
if n != nil {
|
|
|
|
networkName = n.Name()
|
|
|
|
}
|
2016-08-26 16:08:28 -04:00
|
|
|
if _, ok := container.NetworkSettings.Networks[networkName]; !ok {
|
|
|
|
return fmt.Errorf("container %s is not connected to the network %s", container.ID, networkName)
|
2016-01-11 20:13:39 -05:00
|
|
|
}
|
2016-08-26 16:08:28 -04:00
|
|
|
delete(container.NetworkSettings.Networks, networkName)
|
|
|
|
} else if err == nil {
|
|
|
|
if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
|
|
|
|
return runconfig.ErrConflictHostNetwork
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:56:36 -05:00
|
|
|
if err := disconnectFromNetwork(container, n, false); err != nil {
|
2016-01-11 20:13:39 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-08-26 16:08:28 -04:00
|
|
|
} else {
|
|
|
|
return err
|
2015-12-25 02:09:37 -05:00
|
|
|
}
|
2015-12-03 14:10:27 -05:00
|
|
|
|
|
|
|
if err := container.ToDiskLocking(); err != nil {
|
|
|
|
return fmt.Errorf("Error saving container to disk: %v", err)
|
|
|
|
}
|
2015-12-21 19:45:31 -05:00
|
|
|
|
2016-08-26 16:08:28 -04:00
|
|
|
if n != nil {
|
|
|
|
attributes := map[string]string{
|
|
|
|
"container": container.ID,
|
|
|
|
}
|
|
|
|
daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
|
2015-12-21 19:45:31 -05:00
|
|
|
}
|
2015-12-25 02:09:37 -05:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-03 14:10:27 -05:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
|
|
|
|
containerID := container.HostConfig.IpcMode.Container()
|
2015-12-11 12:39:28 -05:00
|
|
|
c, err := daemon.GetContainer(containerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !c.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
|
|
|
return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-02-02 07:36:13 -05:00
|
|
|
if c.IsRestarting() {
|
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 nil, errContainerIsRestarting(container.ID)
|
2016-02-02 07:36:13 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-05-06 14:56:03 -04:00
|
|
|
func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
|
|
|
|
containerID := container.HostConfig.PidMode.Container()
|
|
|
|
c, err := daemon.GetContainer(containerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !c.IsRunning() {
|
|
|
|
return nil, fmt.Errorf("cannot join PID of a non running container: %s", containerID)
|
|
|
|
}
|
|
|
|
if c.IsRestarting() {
|
|
|
|
return nil, errContainerIsRestarting(container.ID)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
|
2016-03-18 14:50:19 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
c.ShmPath, err = c.ShmResourcePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.IpcMode.IsContainer() {
|
|
|
|
ic, err := daemon.getIpcContainer(c)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
c.ShmPath = ic.ShmPath
|
|
|
|
} else if c.HostConfig.IpcMode.IsHost() {
|
|
|
|
if _, err := os.Stat("/dev/shm"); err != nil {
|
|
|
|
return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
c.ShmPath = "/dev/shm"
|
|
|
|
} else {
|
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
|
|
|
if !c.HasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := c.ShmResourcePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
shmSize := container.DefaultSHMSize
|
|
|
|
if c.HostConfig.ShmSize != 0 {
|
|
|
|
shmSize = c.HostConfig.ShmSize
|
|
|
|
}
|
|
|
|
shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
|
|
|
|
if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
|
|
|
|
return fmt.Errorf("mounting shm tmpfs: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) mountVolumes(container *container.Container) error {
|
|
|
|
mounts, err := daemon.setupMounts(container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mounts {
|
|
|
|
dest, err := container.GetResourcePath(m.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat os.FileInfo
|
|
|
|
stat, err = os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := "rbind,ro"
|
|
|
|
if m.Writable {
|
|
|
|
opts = "rbind,rw"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-13 14:22:41 -04:00
|
|
|
|
|
|
|
// mountVolumes() seems to be called for temporary mounts
|
|
|
|
// outside the container. Soon these will be unmounted with
|
|
|
|
// lazy unmount option and given we have mounted the rbind,
|
|
|
|
// all the submounts will propagate if these are shared. If
|
|
|
|
// daemon is running in host namespace and has / as shared
|
|
|
|
// then these unmounts will propagate and unmount original
|
|
|
|
// mount as well. So make all these mounts rprivate.
|
|
|
|
// Do not use propagation property of volume as that should
|
|
|
|
// apply only when mounting happen inside the container.
|
|
|
|
if err := mount.MakeRPrivate(dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func killProcessDirectly(container *container.Container) error {
|
|
|
|
if _, err := container.WaitStop(10 * time.Second); err != nil {
|
|
|
|
// Ensure that we don't kill ourselves
|
|
|
|
if pid := container.GetPID(); pid != 0 {
|
|
|
|
logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
|
|
|
|
if err := syscall.Kill(pid, 9); err != nil {
|
|
|
|
if err != syscall.ESRCH {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-04 15:41:06 -05:00
|
|
|
e := errNoSuchProcess{pid, 9}
|
|
|
|
logrus.Debug(e)
|
|
|
|
return e
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
func specDevice(d *configs.Device) specs.Device {
|
|
|
|
return specs.Device{
|
|
|
|
Type: string(d.Type),
|
|
|
|
Path: d.Path,
|
|
|
|
Major: d.Major,
|
|
|
|
Minor: d.Minor,
|
|
|
|
FileMode: fmPtr(int64(d.FileMode)),
|
|
|
|
UID: u32Ptr(int64(d.Uid)),
|
|
|
|
GID: u32Ptr(int64(d.Gid)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:01:12 -04:00
|
|
|
func specDeviceCgroup(d *configs.Device) specs.DeviceCgroup {
|
|
|
|
t := string(d.Type)
|
|
|
|
return specs.DeviceCgroup{
|
|
|
|
Allow: true,
|
|
|
|
Type: &t,
|
|
|
|
Major: &d.Major,
|
|
|
|
Minor: &d.Minor,
|
|
|
|
Access: &d.Permissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []specs.Device, devPermissions []specs.DeviceCgroup, err error) {
|
2016-02-24 20:50:39 -05:00
|
|
|
resolvedPathOnHost := deviceMapping.PathOnHost
|
|
|
|
|
|
|
|
// check if it is a symbolic link
|
|
|
|
if src, e := os.Lstat(deviceMapping.PathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2016-04-23 11:00:57 -04:00
|
|
|
if linkedPathOnHost, e := filepath.EvalSymlinks(deviceMapping.PathOnHost); e == nil {
|
2016-02-24 20:50:39 -05:00
|
|
|
resolvedPathOnHost = linkedPathOnHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device, err := devices.DeviceFromPath(resolvedPathOnHost, deviceMapping.CgroupPermissions)
|
2015-11-12 14:55:17 -05:00
|
|
|
// if there was no error, return the device
|
|
|
|
if err == nil {
|
|
|
|
device.Path = deviceMapping.PathInContainer
|
2016-03-24 15:01:12 -04:00
|
|
|
return append(devs, specDevice(device)), append(devPermissions, specDeviceCgroup(device)), nil
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the device is not a device node
|
|
|
|
// try to see if it's a directory holding many devices
|
|
|
|
if err == devices.ErrNotADevice {
|
|
|
|
|
|
|
|
// check if it is a directory
|
2016-02-24 20:50:39 -05:00
|
|
|
if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
// mount the internal devices recursively
|
2016-02-24 20:50:39 -05:00
|
|
|
filepath.Walk(resolvedPathOnHost, func(dpath string, f os.FileInfo, e error) error {
|
2015-11-12 14:55:17 -05:00
|
|
|
childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
|
|
|
|
if e != nil {
|
|
|
|
// ignore the device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the device to userSpecified devices
|
2016-02-24 20:50:39 -05:00
|
|
|
childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, deviceMapping.PathInContainer, 1)
|
2016-03-18 14:50:19 -04:00
|
|
|
devs = append(devs, specDevice(childDevice))
|
2016-03-24 15:01:12 -04:00
|
|
|
devPermissions = append(devPermissions, specDeviceCgroup(childDevice))
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devs) > 0 {
|
2016-03-24 15:01:12 -04:00
|
|
|
return devs, devPermissions, nil
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:01:12 -04:00
|
|
|
return devs, devPermissions, fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func detachMounted(path string) error {
|
|
|
|
return syscall.Unmount(path, syscall.MNT_DETACH)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLinkable(child *container.Container) bool {
|
|
|
|
// A container is linkable only if it belongs to the default network
|
2016-03-09 23:33:21 -05:00
|
|
|
_, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
|
2015-11-12 14:55:17 -05:00
|
|
|
return ok
|
|
|
|
}
|
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
|
|
|
|
|
|
|
func errRemovalContainer(containerID string) error {
|
|
|
|
return fmt.Errorf("Container %s is marked for removal and cannot be connected or disconnected to the network", containerID)
|
|
|
|
}
|
2016-04-20 18:35:11 -04:00
|
|
|
|
|
|
|
func enableIPOnPredefinedNetwork() bool {
|
|
|
|
return false
|
|
|
|
}
|