Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stack yaml parser #163

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions api/rpc/stack/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package stack

import (
"github.com/docker/go-connections/nat"
"gopkg.in/yaml.v2"
"strings"
)

type serviceMap struct {
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Replicas int `yaml:"replicas"`
Environment interface{} `yaml:"environment"`
Labels interface{} `yaml:"labels"`
}

// Service represents a docker service for use in a docker stack
type Service struct {
Name string
Image string
Ports map[nat.Port][]nat.PortBinding
Replicas int
Environment map[string]string
Labels map[string]string
}

func parseStackYaml(in string) (out []Service, err error) {
out = []Service{}
b := []byte(in)
sm, err := parseAsServiceMap(b)
if err != nil {
return
}
for n, d := range sm {
e := map[string]string{}
l := map[string]string{}
em, ok := d.Environment.(map[interface{}]interface{})
if ok {
for k, v := range em {
e[k.(string)] = v.(string)
}
}
ea, ok := d.Environment.([]interface{})
if ok {
for _, s := range ea {
a := strings.Split(s.(string), "=")
k := a[0]
v := a[1]
e[k] = v
}
}
lm, ok := d.Labels.(map[interface{}]interface{})
if ok {
for k, v := range lm {
l[k.(string)] = v.(string)
}
}
la, ok := d.Labels.([]interface{})
if ok {
for _, s := range la {
a := strings.Split(s.(string), "=")
k := a[0]
v := a[1]
l[k] = v
}
}
_, ports, err := nat.ParsePortSpecs(d.Ports)
if err != nil {
return nil, err
}
r := d.Replicas
if r == 0 {
r = 1
}
out = append(out, Service{
Name: n,
Image: d.Image,
Ports: ports,
Replicas: r,
Environment: e,
Labels: l,
})
}
return
}

func parseAsServiceMap(b []byte) (out map[string]serviceMap, err error) {
err = yaml.Unmarshal(b, &out)
return
}
84 changes: 84 additions & 0 deletions api/rpc/stack/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package stack

import (
"testing"
)

var examples = []string{
`web:
image: appcelerator.io/amp-demo
ports:
- 80:3000
replicas: 3
environment:
REDIS_PASSWORD: password
redis:
image: redis
environment:
- PASSWORD=password`,
`tutum-cron:
image: sillelien/tutum-cron
environment:
BACKUP_HOURLY_CRON_SCHEDULE: '0 * * * *'
BACKUP_DAILY_CRON_SCHEDULE: '0 3 * * *'`,
`influxdbData:
image: busybox

influxdb:
image: tutum/influxdb:0.9
environment:
- PRE_CREATE_DB=cadvisor
ports:
- "8083:8083"
- "8086:8086"`,
`lb:
image: 'tutum/haproxy:latest'
ports:
- '80:80'

web-green:
image: 'borja/bluegreen:v1'`,
`btsync:
image: "tutum/btsync"
replicas: 3`,
`lb:
image: 'tutum/haproxy:0.2'
ports:
- '80:80'
mysql:
image: 'mysql:5.6'
environment:
- MYSQL_ROOT_PASSWORD=**USE_A_STRONG_PASSWORD**
varnish:
image: 'benhall/docker-varnish:latest'
environment:
- VARNISH_BACKEND_HOST=backend
- VARNISH_BACKEND_PORT=80
- 'VIRTUAL_HOST=example.com,www.example.com'
ports:
- '8080:80'
wordpress:
image: 'wordpress:4.3-apache'
environment:
- WORDPRESS_DB_NAME=wp_example
- WORDPRESS_TABLE_PREFIX=wp_`,
`lb:
image: dockercloud/haproxy
ports:
- "80:80"
web:
image: dockercloud/quickstart-python
replicas: 4
redis:
image: redis`,
}

func TestParseStackYaml(t *testing.T) {
for _, example := range examples {
out, err := parseStackYaml(example)
if err != nil {
t.Fatal(err)
}
t.Log(out)
}
}
11 changes: 11 additions & 0 deletions api/rpc/stack/stack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stack

import (
"fmt"
"github.com/appcelerator/amp/data/storage"
"github.com/docker/docker/pkg/stringid"
"golang.org/x/net/context"
Expand All @@ -13,6 +14,11 @@ type Stack struct {

// Create implements stack.StackServer
func (stack *Stack) Create(ctx context.Context, in *CreateRequest) (*CreateReply, error) {
services, err := parseStackYaml(in.StackDefinition)
if err != nil {
return nil, err
}
fmt.Println(services)
// Build reply
reply := CreateReply{
StackId: stringid.GenerateNonCryptoID(),
Expand All @@ -22,6 +28,11 @@ func (stack *Stack) Create(ctx context.Context, in *CreateRequest) (*CreateReply

// Up implements stack.StackServer
func (stack *Stack) Up(ctx context.Context, in *UpRequest) (*UpReply, error) {
services, err := parseStackYaml(in.Stackfile)
if err != nil {
return nil, err
}
fmt.Println(services)
reply := UpReply{
StackId: stringid.GenerateNonCryptoID(),
}
Expand Down
17 changes: 14 additions & 3 deletions api/rpc/stack/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"testing"
"time"

"context"
"github.com/appcelerator/amp/api/rpc/stack"
"github.com/appcelerator/amp/api/server"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

Expand All @@ -21,6 +21,17 @@ const (
elasticsearchDefaultURL = "http://localhost:9200"
kafkaDefaultURL = "localhost:9092"
influxDefaultURL = "http://localhost:8086"
example = `web:
image: appcelerator.io/amp-demo
ports:
- 80:3000
replicas: 3
environment:
REDIS_PASSWORD: password
redis:
image: redis
environment:
- PASSWORD=password`
)

var (
Expand Down Expand Up @@ -85,15 +96,15 @@ func TestMain(m *testing.M) {
}

func TestShouldCreateAStackSuccessfully(t *testing.T) {
r, err := client.Create(ctx, &stack.CreateRequest{StackDefinition: ""})
r, err := client.Create(ctx, &stack.CreateRequest{StackDefinition: example})
if err != nil {
t.Error(err)
}
assert.NotEmpty(t, r.StackId, "We should have a non empty stack id")
}

func TestShouldUpStackSuccessfully(t *testing.T) {
r, err := client.Up(ctx, &stack.UpRequest{Stackfile: ""})
r, err := client.Up(ctx, &stack.UpRequest{Stackfile: example})
if err != nil {
t.Error(err)
}
Expand Down