Skip to content

Commit

Permalink
Merge pull request moby#5 from aanand/hostname
Browse files Browse the repository at this point in the history
WIP: support hostname
  • Loading branch information
dnephin committed Oct 28, 2016
2 parents c6fa14b + bb2527a commit a0a7686
Show file tree
Hide file tree
Showing 79 changed files with 7,560 additions and 1,829 deletions.
1 change: 1 addition & 0 deletions api/types/swarm/container.go
Expand Up @@ -12,6 +12,7 @@ type ContainerSpec struct {
Labels map[string]string `json:",omitempty"`
Command []string `json:",omitempty"`
Args []string `json:",omitempty"`
Hostname string `json:",omitempty"`
Env []string `json:",omitempty"`
Dir string `json:",omitempty"`
User string `json:",omitempty"`
Expand Down
23 changes: 12 additions & 11 deletions cli/command/stack/deploy.go
Expand Up @@ -124,7 +124,7 @@ func createNetworks(
}

for internalName, network := range networks {
if network.ExternalName != "" {
if network.External.Name != "" {
continue
}

Expand Down Expand Up @@ -218,8 +218,8 @@ func convertVolumes(
return nil, fmt.Errorf("Undefined volume: %s", source)
}

if stackVolume.ExternalName != "" {
source = stackVolume.ExternalName
if stackVolume.External.Name != "" {
source = stackVolume.External.Name
} else {
volumeOptions = &mount.VolumeOptions{
Labels: stackVolume.Labels,
Expand Down Expand Up @@ -351,14 +351,15 @@ func convertService(
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: swarm.ContainerSpec{
Image: service.Image,
Command: service.Entrypoint,
Args: service.Command,
Env: convertEnvironment(service.Environment),
Labels: getStackLabels(namespace, service.Deploy.Labels),
Dir: service.WorkingDir,
User: service.User,
Mounts: mounts,
Image: service.Image,
Command: service.Entrypoint,
Args: service.Command,
Hostname: service.Hostname,
Env: convertEnvironment(service.Environment),
Labels: getStackLabels(namespace, service.Deploy.Labels),
Dir: service.WorkingDir,
User: service.User,
Mounts: mounts,
},
Placement: &swarm.Placement{
Constraints: service.Deploy.Placement.Constraints,
Expand Down
34 changes: 18 additions & 16 deletions daemon/cluster/convert/container.go
Expand Up @@ -12,14 +12,15 @@ import (

func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {
containerSpec := types.ContainerSpec{
Image: c.Image,
Labels: c.Labels,
Command: c.Command,
Args: c.Args,
Env: c.Env,
Dir: c.Dir,
User: c.User,
Groups: c.Groups,
Image: c.Image,
Labels: c.Labels,
Command: c.Command,
Args: c.Args,
Hostname: c.Hostname,
Env: c.Env,
Dir: c.Dir,
User: c.User,
Groups: c.Groups,
}

// Mounts
Expand Down Expand Up @@ -61,14 +62,15 @@ func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {

func containerToGRPC(c types.ContainerSpec) (*swarmapi.ContainerSpec, error) {
containerSpec := &swarmapi.ContainerSpec{
Image: c.Image,
Labels: c.Labels,
Command: c.Command,
Args: c.Args,
Env: c.Env,
Dir: c.Dir,
User: c.User,
Groups: c.Groups,
Image: c.Image,
Labels: c.Labels,
Command: c.Command,
Args: c.Args,
Hostname: c.Hostname,
Env: c.Env,
Dir: c.Dir,
User: c.User,
Groups: c.Groups,
}

if c.StopGracePeriod != nil {
Expand Down
2 changes: 1 addition & 1 deletion hack/vendor.sh
Expand Up @@ -147,7 +147,7 @@ clone git github.com/docker/containerd 52ef1ceb4b660c42cf4ea9013180a5663968d4c7
clone git github.com/tonistiigi/fifo 8c56881ce5e63e19e2dfc495c8af0fb90916467d

# cluster
clone git github.com/docker/swarmkit 3b221eb0391d34ae0b9dac65df02b5b64de6dff2
clone git github.com/docker/swarmkit 056b833c84e3927ae49f5d4ad4fb0d048fdd6f2d
clone git github.com/golang/mock bd3c8e81be01eef76d4b503f5e687d2d1354d2d9
clone git github.com/gogo/protobuf v0.3
clone git github.com/cloudflare/cfssl 7fb22c8cba7ecaf98e4082d22d65800cf45e042a
Expand Down
2 changes: 1 addition & 1 deletion vendor/src/github.com/aanand/compose-file/Makefile
Expand Up @@ -2,7 +2,7 @@ SCHEMA_GO := schema/bindata.go
SCHEMA_JSON := schema/data/config_schema_v3.json

test:
go test ./loader ./schema
go test ./{loader,schema,template,interpolation}

schema: $(SCHEMA_GO)

Expand Down
6 changes: 4 additions & 2 deletions vendor/src/github.com/aanand/compose-file/glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions vendor/src/github.com/aanand/compose-file/glide.yaml
@@ -1,8 +1,9 @@
package: .
import:
- package: github.com/mattn/go-shellwords
- package: github.com/stretchr/testify
- package: github.com/xeipuuv/gojsonreference
- package: github.com/xeipuuv/gojsonschema
- package: gopkg.in/yaml.v2
- package: github.com/docker/go-units
- package: github.com/mattn/go-shellwords
- package: github.com/stretchr/testify
- package: github.com/xeipuuv/gojsonreference
- package: github.com/xeipuuv/gojsonschema
- package: gopkg.in/yaml.v2
- package: github.com/docker/go-units
- package: github.com/mitchellh/mapstructure
@@ -0,0 +1,89 @@
package interpolation

import (
"fmt"

"github.com/aanand/compose-file/template"
"github.com/aanand/compose-file/types"
)

func Interpolate(config types.Dict, section string, mapping template.Mapping) (types.Dict, error) {
out := types.Dict{}

for name, item := range config {
if item == nil {
out[name] = nil
continue
}
interpolatedItem, err := interpolateSectionItem(name, item.(types.Dict), section, mapping)
if err != nil {
return nil, err
}
out[name] = interpolatedItem
}

return out, nil
}

func interpolateSectionItem(
name string,
item types.Dict,
section string,
mapping template.Mapping,
) (types.Dict, error) {

out := types.Dict{}

for key, value := range item {
interpolatedValue, err := recursiveInterpolate(value, mapping)
if err != nil {
return nil, fmt.Errorf(
"Invalid interpolation format for %#v option in %s %#v: %#v",
key, section, name, err.Template,
)
}
out[key] = interpolatedValue
}

return out, nil

}

func recursiveInterpolate(
value interface{},
mapping template.Mapping,
) (interface{}, *template.InvalidTemplateError) {

switch value := value.(type) {

case string:
return template.Substitute(value, mapping)

case types.Dict:
out := types.Dict{}
for key, elem := range value {
interpolatedElem, err := recursiveInterpolate(elem, mapping)
if err != nil {
return nil, err
}
out[key] = interpolatedElem
}
return out, nil

case []interface{}:
out := make([]interface{}, len(value))
for i, elem := range value {
interpolatedElem, err := recursiveInterpolate(elem, mapping)
if err != nil {
return nil, err
}
out[i] = interpolatedElem
}
return out, nil

default:
return value, nil

}

}
@@ -0,0 +1,59 @@
package interpolation

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/aanand/compose-file/types"
)

var defaults = map[string]string{
"USER": "jenny",
"FOO": "bar",
}

func defaultMapping(name string) (string, bool) {
val, ok := defaults[name]
return val, ok
}

func TestInterpolate(t *testing.T) {
services := types.Dict{
"servicea": types.Dict{
"image": "example:${USER}",
"volumes": []interface{}{"$FOO:/target"},
"logging": types.Dict{
"driver": "${FOO}",
"options": types.Dict{
"user": "$USER",
},
},
},
}
expected := types.Dict{
"servicea": types.Dict{
"image": "example:jenny",
"volumes": []interface{}{"bar:/target"},
"logging": types.Dict{
"driver": "bar",
"options": types.Dict{
"user": "jenny",
},
},
},
}
result, err := Interpolate(services, "service", defaultMapping)
assert.NoError(t, err)
assert.Equal(t, expected, result)
}

func TestInvalidInterpolation(t *testing.T) {
services := types.Dict{
"servicea": types.Dict{
"image": "${",
},
}
_, err := Interpolate(services, "service", defaultMapping)
assert.EqualError(t, err, `Invalid interpolation format for "image" option in service "servicea": "${"`)
}

0 comments on commit a0a7686

Please sign in to comment.