2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2017-04-17 18:07:56 -04:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2017-01-27 16:56:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type volumes map[string]composetypes.VolumeConfig
|
|
|
|
|
|
|
|
// Volumes from compose-file types to engine api types
|
2017-01-27 16:56:45 -05:00
|
|
|
func Volumes(serviceVolumes []composetypes.ServiceVolumeConfig, stackVolumes volumes, namespace Namespace) ([]mount.Mount, error) {
|
2016-12-05 16:14:08 -05:00
|
|
|
var mounts []mount.Mount
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
for _, volumeConfig := range serviceVolumes {
|
|
|
|
mount, err := convertVolumeToMount(volumeConfig, stackVolumes, namespace)
|
2016-12-05 16:14:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mounts = append(mounts, mount)
|
|
|
|
}
|
|
|
|
return mounts, nil
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
func convertVolumeToMount(
|
|
|
|
volume composetypes.ServiceVolumeConfig,
|
|
|
|
stackVolumes volumes,
|
|
|
|
namespace Namespace,
|
|
|
|
) (mount.Mount, error) {
|
|
|
|
result := mount.Mount{
|
2017-04-06 11:37:55 -04:00
|
|
|
Type: mount.Type(volume.Type),
|
|
|
|
Source: volume.Source,
|
|
|
|
Target: volume.Target,
|
|
|
|
ReadOnly: volume.ReadOnly,
|
|
|
|
Consistency: mount.Consistency(volume.Consistency),
|
2016-12-18 19:50:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
// Anonymous volumes
|
|
|
|
if volume.Source == "" {
|
|
|
|
return result, nil
|
2016-12-18 10:50:32 -05:00
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
if volume.Type == "volume" && volume.Bind != nil {
|
|
|
|
return result, errors.New("bind options are incompatible with type volume")
|
|
|
|
}
|
|
|
|
if volume.Type == "bind" && volume.Volume != nil {
|
|
|
|
return result, errors.New("volume options are incompatible with type bind")
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
if volume.Bind != nil {
|
|
|
|
result.BindOptions = &mount.BindOptions{
|
|
|
|
Propagation: mount.Propagation(volume.Bind.Propagation),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Binds volumes
|
|
|
|
if volume.Type == "bind" {
|
|
|
|
return result, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
stackVolume, exists := stackVolumes[volume.Source]
|
2016-12-05 16:14:08 -05:00
|
|
|
if !exists {
|
2017-03-24 10:43:28 -04:00
|
|
|
return result, errors.Errorf("undefined volume %q", volume.Source)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
result.Source = namespace.Scope(volume.Source)
|
|
|
|
result.VolumeOptions = &mount.VolumeOptions{}
|
2016-12-05 16:14:08 -05:00
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
if volume.Volume != nil {
|
|
|
|
result.VolumeOptions.NoCopy = volume.Volume.NoCopy
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-11-06 17:03:43 -05:00
|
|
|
if stackVolume.Name != "" {
|
|
|
|
result.Source = stackVolume.Name
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
// External named volumes
|
|
|
|
if stackVolume.External.External {
|
|
|
|
return result, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
result.VolumeOptions.Labels = AddStackLabel(namespace, stackVolume.Labels)
|
|
|
|
if stackVolume.Driver != "" || stackVolume.DriverOpts != nil {
|
|
|
|
result.VolumeOptions.DriverConfig = &mount.Driver{
|
|
|
|
Name: stackVolume.Driver,
|
|
|
|
Options: stackVolume.DriverOpts,
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
|
|
|
|
// Named volumes
|
|
|
|
return result, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|