Skip to content

Commit

Permalink
support docker volumes. upgrade mount->volumes in syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieudelaro committed May 27, 2016
1 parent 40e7747 commit f8146fb
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 76 deletions.
23 changes: 12 additions & 11 deletions config/config_base.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package config

import (
"errors"
containerFilepath "github.com/matthieudelaro/nut/container/filepath"
Utils "github.com/matthieudelaro/nut/utils"
)

type VolumeBase struct {
}
func (self *VolumeBase) getHostPath() string {
func (self *VolumeBase) getVolumeName() string {
return ""
}
func (self *VolumeBase) getContainerPath() string {
func (self *VolumeBase) getOptions() string {
return ""
}
func (self *VolumeBase) getOptions() string {

type DeviceBase struct {
}
func (self *DeviceBase) getHostPath() string {
return ""
}
func (self *VolumeBase) fullHostPath(context Utils.Context) (string, error) {
return "", errors.New("VolumeBase.fullHostPath() must be overloaded.")
func (self *DeviceBase) getContainerPath() string {
return ""
}
func (self *VolumeBase) fullContainerPath(context Utils.Context) (string, error) {
return "", errors.New("VolumeBase.fullContainerPath() must be overloaded.")
func (self *DeviceBase) getOptions() string {
return ""
}

type BaseEnvironmentBase struct {
Expand Down Expand Up @@ -69,8 +70,8 @@ type ConfigBase struct {
func (self *ConfigBase) getEnvironmentVariables() map[string]string {
return make(map[string]string)
}
func (self *ConfigBase) getDevices() map[string]Volume {
return make(map[string]Volume)
func (self *ConfigBase) getDevices() map[string]Device {
return make(map[string]Device)
}
func (self *ConfigBase) getPorts() []string {
return []string{}
Expand Down
23 changes: 16 additions & 7 deletions config/config_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import (
Utils "github.com/matthieudelaro/nut/utils"
)


type Bind interface {
getOptions() (string)
}
type Volume interface {
getVolumeName() string
getFullHostPath(context Utils.Context) (string, error)
getFullContainerPath(context Utils.Context) (string, error)

// implement Bind
getOptions() (string)
}
type Device interface {
getHostPath() string
getContainerPath() string

// implement Bind
getOptions() (string)
fullHostPath(context Utils.Context) (string, error)
fullContainerPath(context Utils.Context) (string, error)
}
type BaseEnvironment interface {
getFilePath() string
getGitHub() string
}


type Config interface {
getDockerImage() string
getProjectName() string
Expand All @@ -30,7 +39,7 @@ type Config interface {
getVolumes() map[string]Volume
getMacros() map[string]Macro
getEnvironmentVariables() map[string]string
getDevices() map[string]Volume
getDevices() map[string]Device
getPorts() []string
getEnableGui() (bool, bool)
getEnableNvidiaDevices() (bool, bool)
Expand All @@ -56,7 +65,7 @@ type Project interface { // extends Config interface
getVolumes() map[string]Volume
getMacros() map[string]Macro
getEnvironmentVariables() map[string]string
getDevices() map[string]Volume
getDevices() map[string]Device
getPorts() []string
getEnableGui() (bool, bool)
getEnableNvidiaDevices() (bool, bool)
Expand Down Expand Up @@ -86,7 +95,7 @@ type Macro interface { // extends Config interface
getVolumes() map[string]Volume
getMacros() map[string]Macro
getEnvironmentVariables() map[string]string
getDevices() map[string]Volume
getDevices() map[string]Device
getPorts() []string
getEnableGui() (bool, bool)
getEnableNvidiaDevices() (bool, bool)
Expand Down
1 change: 0 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestFromNutPackage(t *testing.T) {
log.Debug("------Tests of main.go")

var volume Volume
volume = &VolumeBase{}
volume = &VolumeV6{}
log.Debug("OK ", volume)

Expand Down
4 changes: 2 additions & 2 deletions config/config_v5.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type VolumeV5 struct {
Host string `yaml:host_path`
Container string `yaml:container_path`
}
func (self *VolumeV5) fullHostPath(context Utils.Context) (string, error) {
func (self *VolumeV5) getFullHostPath(context Utils.Context) (string, error) {
clean := filepath.Clean(self.Host)
if filepath.IsAbs(clean) {
return clean, nil
} else {
return filepath.Join(context.GetRootDirectory(), clean), nil
}
}
func (self *VolumeV5) fullContainerPath(context Utils.Context) (string, error) {
func (self *VolumeV5) getFullContainerPath(context Utils.Context) (string, error) {
clean := containerFilepath.Clean(self.Container)
if containerFilepath.IsAbs(clean) {
return clean, nil
Expand Down
4 changes: 2 additions & 2 deletions config/config_v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type VolumeV6 struct {
Host string `yaml:host_path`
Container string `yaml:container_path`
}
func (self *VolumeV6) fullHostPath(context Utils.Context) (string, error) {
func (self *VolumeV6) getFullHostPath(context Utils.Context) (string, error) {
clean := filepath.Clean(self.Host)
if filepath.IsAbs(clean) {
return clean, nil
} else {
return filepath.Join(context.GetRootDirectory(), clean), nil
}
}
func (self *VolumeV6) fullContainerPath(context Utils.Context) (string, error) {
func (self *VolumeV6) getFullContainerPath(context Utils.Context) (string, error) {

clean := containerFilepath.Clean(self.Container)
if containerFilepath.IsAbs(clean) {
Expand Down
74 changes: 48 additions & 26 deletions config/config_v7.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import(
"errors"
"path/filepath"
Utils "github.com/matthieudelaro/nut/utils"
containerFilepath "github.com/matthieudelaro/nut/container/filepath"
Expand All @@ -9,37 +10,61 @@ import(
type VolumeV7 struct {
VolumeBase `yaml:"inheritedValues,inline"`

VolumeName string `yaml:"volume_name,omitempty"`
Host string `yaml:"host_path,omitempty"`
Container string `yaml:"container_path,omitempty"`
Container string `yaml:"container_path"`
Options string `yaml:"options,omitempty"`
}
func (self *VolumeV7) getHostPath() string {
return self.Host
}
func (self *VolumeV7) getContainerPath() string {
return self.Container
func (self *VolumeV7) getVolumeName() string {
return self.VolumeName
}
func (self *VolumeV7) getOptions() string {
return self.Options
}
func (self *VolumeV7) fullHostPath(context Utils.Context) (string, error) {
clean := filepath.Clean(self.Host)
if filepath.IsAbs(clean) {
return clean, nil
func (self *VolumeV7) getFullHostPath(context Utils.Context) (string, error) {
if self.Host == "" {
return "", errors.New("Undefined host path")
} else {
return filepath.Join(context.GetRootDirectory(), clean), nil
clean := filepath.Clean(self.Host)
if filepath.IsAbs(clean) {
return clean, nil
} else {
return filepath.Join(context.GetRootDirectory(), clean), nil
}
}
}
func (self *VolumeV7) fullContainerPath(context Utils.Context) (string, error) {
clean := containerFilepath.Clean(self.Container)
if containerFilepath.IsAbs(clean) {
return clean, nil
func (self *VolumeV7) getFullContainerPath(context Utils.Context) (string, error) {
if self.Container == "" {
return "", errors.New("Undefined container path")
} else {
return containerFilepath.Join(context.GetRootDirectory(), clean), nil
clean := containerFilepath.Clean(self.Container)
if containerFilepath.IsAbs(clean) {
return clean, nil
} else {
return containerFilepath.Join(context.GetRootDirectory(), clean), nil
}
}
}


type DeviceV7 struct {
DeviceBase `yaml:"inheritedValues,inline"`

Host string `yaml:"host_path"`
Container string `yaml:"container_path"`
Options string `yaml:"options,omitempty"`
}
func (self *DeviceV7) getHostPath() string {
return self.Host
}
func (self *DeviceV7) getContainerPath() string {
return self.Container
}
func (self *DeviceV7) getOptions() string {
return self.Options
}


type BaseEnvironmentV7 struct {
BaseEnvironmentBase `yaml:"inheritedValues,inline"`

Expand All @@ -57,7 +82,7 @@ type ConfigV7 struct {
ConfigBase `yaml:"inheritedValues,inline"`

DockerImage string `yaml:"docker_image,omitempty"`
Mount map[string][]string `yaml:"mount,omitempty"`
Volume map[string]VolumeV7 `yaml:"volumes,omitempty"`
WorkingDir string `yaml:"container_working_directory,omitempty"`
EnvironmentVariables map[string]string `yaml:"environment,omitempty"`
Ports []string `yaml:"ports,omitempty"`
Expand All @@ -68,7 +93,7 @@ type ConfigV7 struct {
Detached string `yaml:"detached,omitempty"`
UTSMode string `yaml:"uts,omitempty"`
NetworkMode string `yaml:"net,omitempty"`
Devices map[string]VolumeV7 `yaml:"devices,omitempty"`
Devices map[string]DeviceV7 `yaml:"devices,omitempty"`
parent Config
}
func (self *ConfigV7) getDockerImage() string {
Expand All @@ -88,19 +113,16 @@ type ConfigV7 struct {
}
func (self *ConfigV7) getVolumes() map[string]Volume {
cacheVolumes := make(map[string]Volume)
for name, data := range(self.Mount) {
cacheVolumes[name] = &VolumeV7{
Host: data[0],
Container: data[1],
}
for name, data := range(self.Volume) {
cacheVolumes[name] = &data
}
return cacheVolumes
}
func (self *ConfigV7) getEnvironmentVariables() map[string]string {
return self.EnvironmentVariables
}
func (self *ConfigV7) getDevices() map[string]Volume {
cacheVolumes := make(map[string]Volume)
func (self *ConfigV7) getDevices() map[string]Device {
cacheVolumes := make(map[string]Device)
for name, data := range(self.Devices) {
cacheVolumes[name] = &data
}
Expand Down Expand Up @@ -207,7 +229,7 @@ type MacroV7 struct {

func NewConfigV7(parent Config) *ConfigV7 {
return &ConfigV7{
Mount: make(map[string][]string),
Volume: make(map[string]VolumeV7),
parent: parent,
}
}
Expand Down
7 changes: 3 additions & 4 deletions config/config_v7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestFromNutPackageV7(t *testing.T) {
log.Debug("------Tests of main.go")

var volume Volume
volume = &VolumeBase{}
volume = &VolumeV7{}
log.Debug("OK ", volume)

Expand Down Expand Up @@ -304,7 +303,7 @@ func TestParsingV7(t *testing.T) {
detached bool
UTSMode string
NetworkMode string
Devices map[string]Volume
Devices map[string]Device
}

nutFiles := []Tuple{}
Expand All @@ -323,8 +322,8 @@ devices:
ports: []string{},
docker_image: "golang:1.7",
detached: false,
Devices: map[string]Volume{
"first": &VolumeV7{
Devices: map[string]Device{
"first": &DeviceV7{
Host: "/dev/1",
Container: "/dev/1",
Options: "rw",
Expand Down
17 changes: 10 additions & 7 deletions config/functions_over_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import (
)

// Define methods over interfaces
func GetHostPath(volume Volume) string {
func GetHostPath(volume Device) string {
return volume.getHostPath()
}
func GetContainerPath(volume Volume) string {
func GetContainerPath(volume Device) string {
return volume.getContainerPath()
}
func GetOptions(volume Volume) string {
return volume.getOptions()
func GetOptions(bind Bind) string {
return bind.getOptions()
}

func GetVolumeName(volume Volume) string {
return volume.getVolumeName()
}
func GetFullHostPath(volume Volume, context Utils.Context) (string, error) {
return volume.fullHostPath(context)
return volume.getFullHostPath(context)
}
func GetFullContainerPath(volume Volume, context Utils.Context) (string, error) {
return volume.fullContainerPath(context)
return volume.getFullContainerPath(context)
}

func SetParentProject(child Project, parent Project) {
Expand Down Expand Up @@ -154,7 +157,7 @@ func GetEnvironmentVariables(config Config) map[string]string {
return items
}

func GetDevices(config Config) map[string]Volume {
func GetDevices(config Config) map[string]Device {
items := config.getDevices()

var parent = config.getParent()
Expand Down
14 changes: 9 additions & 5 deletions config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ func TruthyString(s string) (bool, bool) {
// Returns the first conflict element from the map, or nil if
// there wasn't any conflict.
func CheckConflict(context Utils.Context, key string, newPoint Volume, mountingPoints map[string]Volume) Volume {
h, errh := newPoint.fullHostPath(context)
c, errc := newPoint.fullContainerPath(context)
h, errh := newPoint.getFullHostPath(context)
c, errc := newPoint.getFullContainerPath(context)

for key2, mountingPoint2 := range mountingPoints {
// log.Debug("child point ", key)
h2, errh2 := mountingPoint2.fullHostPath(context)
c2, errc2 := mountingPoint2.fullContainerPath(context)
h2, errh2 := mountingPoint2.getFullHostPath(context)
c2, errc2 := mountingPoint2.getFullContainerPath(context)
if key2 == key ||
h == h2 ||
c == c2 ||
errh != nil || errc != nil || errh2 != nil || errc2 != nil {
(newPoint.getVolumeName() != "" && newPoint.getVolumeName() == mountingPoint2.getVolumeName()){
// || errh != nil || errc != nil || errh2 != nil || errc2 != nil {
// log.Debug("conflic between mounting points ", key, " and ", key2)
if errh != nil || errc != nil || errh2 != nil || errc2 != nil {
log.Debug("warning while checking conflic between volumes ", key, " and ", key2)
}
return mountingPoint2
}
}
Expand Down
Loading

0 comments on commit f8146fb

Please sign in to comment.