Skip to content

Commit

Permalink
Merge pull request moby#4 from aanand/compose-on-swarm
Browse files Browse the repository at this point in the history
Support 'volumes'
  • Loading branch information
dnephin committed Oct 25, 2016
2 parents 244c040 + c8d2c44 commit c6fa14b
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 158 deletions.
91 changes: 90 additions & 1 deletion cli/command/stack/deploy.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/aanand/compose-file/loader"
composetypes "github.com/aanand/compose-file/types"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/mount"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/cli"
Expand Down Expand Up @@ -93,7 +95,14 @@ func getConfigFile(filename string) (*composetypes.ConfigFile, error) {
if err != nil {
return nil, err
}
return loader.ParseYAML(bytes, filename)
config, err := loader.ParseYAML(bytes)
if err != nil {
return nil, err
}
return &composetypes.ConfigFile{
Filename: filename,
Config: config,
}, nil
}

func createNetworks(
Expand Down Expand Up @@ -166,6 +175,80 @@ func convertNetworks(
return nets
}

func convertVolumes(
serviceVolumes []string,
stackVolumes map[string]composetypes.VolumeConfig,
namespace string,
) ([]mount.Mount, error) {
var mounts []mount.Mount

for _, volumeString := range serviceVolumes {
var (
source, target string
mountType mount.Type
readOnly bool
volumeOptions *mount.VolumeOptions
)

// TODO: split Windows path mappings properly
parts := strings.SplitN(volumeString, ":", 3)

if len(parts) == 3 {
source = parts[0]
target = parts[1]
if parts[2] == "ro" {
readOnly = true
}
} else if len(parts) == 2 {
source = parts[0]
target = parts[1]
} else if len(parts) == 1 {
target = parts[0]
}

// TODO: catch Windows paths here
if strings.HasPrefix(source, "/") {
mountType = mount.TypeBind
} else {
mountType = mount.TypeVolume

stackVolume, exists := stackVolumes[source]
if !exists {
// TODO: better error message (include service name)
return nil, fmt.Errorf("Undefined volume: %s", source)
}

if stackVolume.ExternalName != "" {
source = stackVolume.ExternalName
} else {
volumeOptions = &mount.VolumeOptions{
Labels: stackVolume.Labels,
}

if stackVolume.Driver != "" {
volumeOptions.DriverConfig = &mount.Driver{
Name: stackVolume.Driver,
Options: stackVolume.DriverOpts,
}
}

// TODO: remove this duplication
source = fmt.Sprintf("%s_%s", namespace, source)
}
}

mounts = append(mounts, mount.Mount{
Type: mountType,
Source: source,
Target: target,
ReadOnly: readOnly,
VolumeOptions: volumeOptions,
})
}

return mounts, nil
}

func deployServices(
ctx context.Context,
dockerCli *command.DockerCli,
Expand Down Expand Up @@ -256,6 +339,11 @@ func convertService(
return swarm.ServiceSpec{}, err
}

mounts, err := convertVolumes(service.Volumes, volumes, namespace)
if err != nil {
return swarm.ServiceSpec{}, err
}

serviceSpec := swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: name,
Expand All @@ -270,6 +358,7 @@ func convertService(
Labels: getStackLabels(namespace, service.Deploy.Labels),
Dir: service.WorkingDir,
User: service.User,
Mounts: mounts,
},
Placement: &swarm.Placement{
Constraints: service.Deploy.Placement.Constraints,
Expand Down
3 changes: 3 additions & 0 deletions vendor/src/github.com/aanand/compose-file/.gitignore
@@ -0,0 +1,3 @@
.dobi
.glide
vendor
26 changes: 26 additions & 0 deletions vendor/src/github.com/aanand/compose-file/.pre-commit-config.yaml
@@ -0,0 +1,26 @@
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: 'v0.5.1'
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
files: '(\.(go|md|sh|yml|yaml|json|ini|rst)|Dockerfile.*)$'
exclude: '^vendor/'
- id: trailing-whitespace
files: '(\.(go|md|sh|yml|yaml|json|ini|rst)|Dockerfile.*)$'
args: ['--no-markdown-linebreak-ext']
exclude: '^vendor/'
- id: check-yaml
exclude: '^vendor/'
- id: check-json
exclude: '^vendor/'

- repo: git://github.com/dnephin/pre-commit-golang
sha: HEAD
hooks:
- id: go-fmt
exclude: '^vendor/'
args: ['-s']
- id: go-lint
exclude: '^(vendor/|schema/bindata\.go)'
- id: go-vet
exclude: '^vendor/'
2 changes: 1 addition & 1 deletion vendor/src/github.com/aanand/compose-file/Makefile
@@ -1,5 +1,5 @@
SCHEMA_GO := schema/bindata.go
SCHEMA_JSON := schema/data/config_schema_v2.1.json
SCHEMA_JSON := schema/data/config_schema_v3.json

test:
go test ./loader ./schema
Expand Down
48 changes: 48 additions & 0 deletions vendor/src/github.com/aanand/compose-file/dobi.yaml
@@ -0,0 +1,48 @@

meta:
project: compose-file


mount=source:
bind: .
path: /go/src/github.com/aanand/compose-file


image=builder:
image: compose-file-dev
context: dockerfiles/
dockerfile: Dockerfile.build


job=shell:
use: builder
interactive: true
mounts: [source]
command: bash

job=test-unit:
use: builder
mounts: [source]
command: "bash -c 'go test -v $(glide novendor)'"

job=deps:
use: builder
mounts: [source]
command: "glide install"
artifact: vendor/

job=generate:
use: builder
mounts: [source]
command: "go generate ./schema"
artifact: schema/bindata.go

job=watch:
use: builder
mounts: [source]
command: "script/watch"
interactive: true


alias=test:
tasks: [test-unit]
@@ -0,0 +1,21 @@
FROM golang:1.6-alpine

RUN apk add -U git bash curl tree
RUN export GLIDE=v0.12.0; \
export SRC=https://github.com/Masterminds/glide/releases/download/; \
curl -sL ${SRC}/${GLIDE}/glide-${GLIDE}-linux-amd64.tar.gz | \
tar -xz linux-amd64/glide && \
mv linux-amd64/glide /usr/bin/glide && \
chmod +x /usr/bin/glide

RUN go get github.com/dnephin/filewatcher && \
cp /go/bin/filewatcher /usr/bin/ && \
rm -rf /go/src/* /go/pkg/* /go/bin/*

RUN go get github.com/jteeuwen/go-bindata/... && \
cp /go/bin/go-bindata /usr/bin/ && \
rm -rf /go/src/* /go/pkg/* /go/bin/*

WORKDIR /go/src/github.com/aanand/compose-file
ENV PS1="# "
ENV CGO_ENABLED=0
18 changes: 15 additions & 3 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.

1 change: 1 addition & 0 deletions vendor/src/github.com/aanand/compose-file/glide.yaml
Expand Up @@ -5,3 +5,4 @@ import:
- package: github.com/xeipuuv/gojsonreference
- package: github.com/xeipuuv/gojsonschema
- package: gopkg.in/yaml.v2
- package: github.com/docker/go-units
@@ -1,4 +1,4 @@
version: "2.1"
version: "3"

services:
foo:
Expand Down

0 comments on commit c6fa14b

Please sign in to comment.