Skip to content

Commit

Permalink
Merge pull request #65 from stormcat24/feature/params_0.2.0
Browse files Browse the repository at this point in the history
Support new params of Task definition for 0.2.0
  • Loading branch information
Akinori Yamada committed Dec 20, 2015
2 parents 27af100 + 0f0a0e9 commit f0fdfa4
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 75 deletions.
7 changes: 3 additions & 4 deletions aws/autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/stormcat24/ecs-formation/util"
)

type AutoscalingApi struct {
Expand All @@ -13,7 +12,7 @@ type AutoscalingApi struct {
func (self *AutoscalingApi) DescribeAutoScalingGroups(groups []string) (map[string]*autoscaling.Group, error) {

params := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: util.ConvertPointerString(groups),
AutoScalingGroupNames: aws.StringSlice(groups),
}

asgmap := map[string]*autoscaling.Group{}
Expand Down Expand Up @@ -60,7 +59,7 @@ func (self *AutoscalingApi) AttachLoadBalancers(group string, lb []string) (*aut

params := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: aws.String(group),
LoadBalancerNames: util.ConvertPointerString(lb),
LoadBalancerNames: aws.StringSlice(lb),
}

result, err := self.service.AttachLoadBalancers(params)
Expand All @@ -74,7 +73,7 @@ func (self *AutoscalingApi) DetachLoadBalancers(group string, lb []string) (*aut

params := &autoscaling.DetachLoadBalancersInput{
AutoScalingGroupName: aws.String(group),
LoadBalancerNames: util.ConvertPointerString(lb),
LoadBalancerNames: aws.StringSlice(lb),
}

result, err := self.service.DetachLoadBalancers(params)
Expand Down
3 changes: 1 addition & 2 deletions aws/elb_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/stormcat24/ecs-formation/util"
)

type ElbApi struct {
Expand All @@ -13,7 +12,7 @@ type ElbApi struct {
func (self *ElbApi) DescribeLoadBalancers(names []string) (*elb.DescribeLoadBalancersOutput, error) {

params := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: util.ConvertPointerString(names),
LoadBalancerNames: aws.StringSlice(names),
}

result, err := self.service.DescribeLoadBalancers(params)
Expand Down
40 changes: 33 additions & 7 deletions operation/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,41 @@ func createTaskPlans(controller *task.TaskDefinitionController, projectDir strin
plans := controller.CreateTaskUpdatePlans(taskDefs)

for _, plan := range plans {
logger.Main.Infof("Task Definition '%s'", plan.Name)
logger.Main.Infof("Task Definition '%v'", plan.Name)

for _, add := range plan.NewContainers {
util.PrintlnCyan(fmt.Sprintf(" (+) %s", add.Name))
util.PrintlnCyan(fmt.Sprintf(" image: %s", add.Image))
util.PrintlnCyan(fmt.Sprintf(" ports: %s", add.Ports))
util.PrintlnCyan(fmt.Sprintf(" environment:\n%s", util.StringValueWithIndent(add.Environment, 4)))
util.PrintlnCyan(fmt.Sprintf(" links: %s", add.Links))
util.PrintlnCyan(fmt.Sprintf(" volumes: %s", add.Volumes))
util.PrintlnCyan(fmt.Sprintf(" (+) %v", add.Name))
util.PrintlnCyan(fmt.Sprintf(" image: %v", add.Image))
util.PrintlnCyan(fmt.Sprintf(" ports: %v", add.Ports))
util.PrintlnCyan(fmt.Sprintf(" environment:\n%v", util.StringValueWithIndent(add.Environment, 4)))
util.PrintlnCyan(fmt.Sprintf(" links: %v", add.Links))
util.PrintlnCyan(fmt.Sprintf(" volumes: %v", add.Volumes))
util.PrintlnCyan(fmt.Sprintf(" volumes_from: %v", add.VolumesFrom))
util.PrintlnCyan(fmt.Sprintf(" memory: %v", add.Memory))
util.PrintlnCyan(fmt.Sprintf(" cpu_units: %v", add.CpuUnits))
util.PrintlnCyan(fmt.Sprintf(" essential: %v", add.Essential))
util.PrintlnCyan(fmt.Sprintf(" entry_point: %v", add.EntryPoint))
util.PrintlnCyan(fmt.Sprintf(" command: %v", add.Command))
util.PrintlnCyan(fmt.Sprintf(" disable_networking: %v", add.DisableNetworking))
util.PrintlnCyan(fmt.Sprintf(" dns_search: %v", add.DnsSearchDomains))
util.PrintlnCyan(fmt.Sprintf(" dns: %v", add.DnsServers))
if len(add.DockerLabels) > 0 {
util.PrintlnCyan(fmt.Sprintf(" labels: %v", util.StringValueWithIndent(add.DockerLabels, 4)))
}
util.PrintlnCyan(fmt.Sprintf(" security_opt: %v", add.DockerSecurityOptions))
util.PrintlnCyan(fmt.Sprintf(" extra_hosts: %v", add.ExtraHosts))
util.PrintlnCyan(fmt.Sprintf(" hostname: %v", add.Hostname))
util.PrintlnCyan(fmt.Sprintf(" log_driver: %v", add.LogDriver))
if len(add.LogOpt) > 0 {
util.PrintlnCyan(fmt.Sprintf(" log_opt: %v", util.StringValueWithIndent(add.LogOpt, 4)))
}
util.PrintlnCyan(fmt.Sprintf(" privileged: %v", add.Privileged))
util.PrintlnCyan(fmt.Sprintf(" read_only: %v", add.ReadonlyRootFilesystem))
if len(add.Ulimits) > 0 {
util.PrintlnCyan(fmt.Sprintf(" ulimits: %v", util.StringValueWithIndent(add.Ulimits, 4)))
}
util.PrintlnCyan(fmt.Sprintf(" user: %v", add.User))
util.PrintlnCyan(fmt.Sprintf(" working_dir: %v", add.WorkingDirectory))
}

util.Println()
Expand Down
45 changes: 31 additions & 14 deletions task/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@ type TaskDefinition struct {
}

type ContainerDefinition struct {
Name string
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Environment map[string]string `yaml:"environment"`
Links []string `yaml:"links"`
Volumes []string `yaml:"volumes"`
VolumesFrom []string `yaml:"volumes_from"`
Memory int64 `yaml:"memory"`
CpuUnits int64 `yaml:"cpu_units"`
Essential bool `yaml:"essential"`
EntryPoint string `yaml:"entry_point"`
Command string `yaml:"command"`
DisableNetworking bool `yaml:"disable_networking"`
DnsSearchDomains []string `yaml:"dns_search_domains"`
Name string
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Environment map[string]string `yaml:"environment"`
Links []string `yaml:"links"`
Volumes []string `yaml:"volumes"`
VolumesFrom []string `yaml:"volumes_from"`
Memory int64 `yaml:"memory"`
CpuUnits int64 `yaml:"cpu_units"`
Essential bool `yaml:"essential"`
EntryPoint string `yaml:"entry_point"`
Command string `yaml:"command"`
DisableNetworking bool `yaml:"disable_networking"`
DnsSearchDomains []string `yaml:"dns_search"`
DnsServers []string `yaml:"dns"`
DockerLabels map[string]string `yaml:"labels"`
DockerSecurityOptions []string `yaml:"security_opt"`
ExtraHosts []string `yaml:"extra_hosts"`
Hostname string `yaml:"hostname"`
LogDriver string `yaml:"log_driver"`
LogOpt map[string]string `yaml:"log_opt"`
Privileged bool `yaml:"privileged"`
ReadonlyRootFilesystem bool `yaml:"read_only"`
Ulimits map[string]Ulimit `yaml:"ulimits"`
User string `yaml:"user"`
WorkingDirectory string `yaml:"working_dir"`
}

type Ulimit struct {
Soft int64 `yaml:"soft"`
Hard int64 `yaml:"hard"`
}

func CreateTaskDefinition(taskDefName string, data string) (*TaskDefinition, error) {
Expand Down
80 changes: 66 additions & 14 deletions task/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package task

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/mattn/go-shellwords"
Expand Down Expand Up @@ -198,21 +199,40 @@ func createContainerDefinition(con *ContainerDefinition) (*ecs.ContainerDefiniti
return nil, []*ecs.Volume{}, err
}

extraHosts, err := toHostEntry(con.ExtraHosts)
if err != nil {
return nil, []*ecs.Volume{}, err
}

return &ecs.ContainerDefinition{
Cpu: aws.Int64(con.CpuUnits),
Command: commands,
EntryPoint: entryPoints,
Environment: toKeyValuePairs(con.Environment),
Essential: aws.Bool(con.Essential),
Image: aws.String(con.Image),
Links: util.ConvertPointerString(con.Links),
Memory: aws.Int64(con.Memory),
MountPoints: mountPoints,
Name: aws.String(con.Name),
PortMappings: portMappings,
VolumesFrom: volumesFrom,
DisableNetworking: aws.Bool(con.DisableNetworking),
DnsSearchDomains: util.ConvertPointerString(con.DnsSearchDomains),
Cpu: aws.Int64(con.CpuUnits),
Command: commands,
EntryPoint: entryPoints,
Environment: toKeyValuePairs(con.Environment),
Essential: aws.Bool(con.Essential),
Image: aws.String(con.Image),
Links: aws.StringSlice(con.Links),
Memory: aws.Int64(con.Memory),
MountPoints: mountPoints,
Name: aws.String(con.Name),
PortMappings: portMappings,
VolumesFrom: volumesFrom,
DisableNetworking: aws.Bool(con.DisableNetworking),
DnsSearchDomains: aws.StringSlice(con.DnsSearchDomains),
DnsServers: aws.StringSlice(con.DnsServers),
DockerLabels: aws.StringMap(con.DockerLabels),
DockerSecurityOptions: aws.StringSlice(con.DockerSecurityOptions),
ExtraHosts: extraHosts,
Hostname: aws.String(con.Hostname),
LogConfiguration: &ecs.LogConfiguration{
LogDriver: aws.String(con.LogDriver),
Options: aws.StringMap(con.LogOpt),
},
Privileged: aws.Bool(con.Privileged),
ReadonlyRootFilesystem: aws.Bool(con.ReadonlyRootFilesystem),
Ulimits: toUlimits(con.Ulimits),
User: aws.String(con.User),
WorkingDirectory: aws.String(con.WorkingDirectory),
}, volumes, nil
}

Expand All @@ -229,3 +249,35 @@ func parseEntrypoint(target string) ([]*string, error) {
}
return result, nil
}

func toHostEntry(entries []string) ([]*ecs.HostEntry, error) {

values := []*ecs.HostEntry{}
for _, e := range entries {
tokens := strings.Split(e, ":")
if len(tokens) != 2 {
return []*ecs.HostEntry{}, fmt.Errorf("'%v' is invalid extra_host definition.", e)
}

values = append(values, &ecs.HostEntry{
Hostname: aws.String(tokens[0]),
IpAddress: aws.String(tokens[1]),
})
}

return values, nil
}

func toUlimits(entries map[string]Ulimit) []*ecs.Ulimit {

values := []*ecs.Ulimit{}
for name, limit := range entries {
values = append(values, &ecs.Ulimit{
Name: aws.String(name),
SoftLimit: aws.Int64(limit.Soft),
HardLimit: aws.Int64(limit.Hard),
})
}

return values
}
115 changes: 115 additions & 0 deletions task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ func TestCreateContainerDefinition(t *testing.T) {
DnsSearchDomains: []string{
"test.dns.domain",
},
DnsServers: []string{
"test.dns.server",
},
DockerLabels: map[string]string{
"LABEL1": "VALUE1",
},
DockerSecurityOptions: []string{
"ECS_SELINUX_CAPABLE=true",
},
ExtraHosts: []string{
"host1:192.168.1.100",
},
LogDriver: "syslog",
LogOpt: map[string]string{
"syslog-address": "tcp://192.168.0.42:123",
},
Privileged: true,
ReadonlyRootFilesystem: true,
Ulimits: map[string]Ulimit{
"nofile": Ulimit{
Soft: 20000,
Hard: 40000,
},
},
User: "hoge-user",
WorkingDirectory: "/hoge",
}

con, volumes, _ := createContainerDefinition(&input)
Expand Down Expand Up @@ -184,4 +210,93 @@ func TestCreateContainerDefinition(t *testing.T) {
if input.DnsSearchDomains[0] != *con.DnsSearchDomains[0] {
t.Errorf("DnsSearchDomains[0]: expect = %v, but actual = %v", input.DnsSearchDomains[0], *con.DnsSearchDomains[0])
}

if 1 != len(con.DnsServers) {
t.Fatalf("len(DnsServers): expect = %v, but actual = %v", 1, len(con.DnsServers))
}

if input.DnsServers[0] != *con.DnsServers[0] {
t.Errorf("DnsServers[0]: expect = %v, but actual = %v", input.DnsServers[0], *con.DnsServers[0])
}

if len(input.DockerLabels) != len(con.DockerLabels) {
t.Fatalf("len(DockerLabels): expect = %v, but actual = %v", len(input.DockerLabels), len(con.DockerLabels))
}

if val, ok := con.DockerLabels["LABEL1"]; ok {
if "VALUE1" != *val {
t.Errorf("DockerLabels.LABEL1: expect = %v, but actual = %v", "VALUE1", val)
}
} else {
t.Errorf("DockerLabels.LABEL1: not found")
}

if 1 != len(con.DockerSecurityOptions) {
t.Fatalf("len(DockerSecurityOptions): expect = %v, but actual = %v", 1, len(con.DockerSecurityOptions))
}

if input.DockerSecurityOptions[0] != *con.DockerSecurityOptions[0] {
t.Errorf("DockerSecurityOptions[0]: expect = %v, but actual = %v", input.DockerSecurityOptions[0], *con.DockerSecurityOptions[0])
}

if len(input.ExtraHosts) != len(con.ExtraHosts) {
t.Fatalf("len(ExtraHosts): expect = %v, but actual = %v", len(input.ExtraHosts), len(con.ExtraHosts))
}

if "host1" != *con.ExtraHosts[0].Hostname {
t.Errorf("ExtraHosts[0].Hostname: expect = %v, but actual = %v", "host1", *con.ExtraHosts[0].Hostname)
}

if "192.168.1.100" != *con.ExtraHosts[0].IpAddress {
t.Errorf("ExtraHosts[0].IpAddress: expect = %v, but actual = %v", "192.168.1.100", *con.ExtraHosts[0].IpAddress)
}

if input.Hostname != *con.Hostname {
t.Errorf("Hostname: expect = %v, but actual = %v", input.Hostname, *con.Hostname)
}

if input.LogDriver != *con.LogConfiguration.LogDriver {
t.Errorf("LogConfiguration.LogDriver: expect = %v, but actual = %v", input.LogDriver, *con.LogConfiguration.LogDriver)
}

if val, ok := con.LogConfiguration.Options["syslog-address"]; ok {
if "tcp://192.168.0.42:123" != *val {
t.Errorf("LogConfiguration.Options.syslog-address: expect = %v, but actual = %v", "tcp://192.168.0.42:123", val)
}
} else {
t.Errorf("LogConfiguration.Options.syslog-address: not found")
}

if input.Privileged != *con.Privileged {
t.Errorf("Privileged: expect = %v, but actual = %v", input.Privileged, *con.Privileged)
}

if input.ReadonlyRootFilesystem != *con.ReadonlyRootFilesystem {
t.Errorf("ReadonlyRootFilesystem: expect = %v, but actual = %v", input.ReadonlyRootFilesystem, *con.ReadonlyRootFilesystem)
}

if len(input.Ulimits) != len(con.Ulimits) {
t.Fatalf("len(Ulimits): expect = %v, but actual = %v", 1, len(con.Ulimits))
}

if "nofile" != *con.Ulimits[0].Name {
t.Errorf("Ulimits[0].Name: expect = %v, but actual = %v", "nofile", *con.Ulimits[0].Name)
}

if 20000 != *con.Ulimits[0].SoftLimit {
t.Errorf("Ulimits[0].SoftLimit: expect = %v, but actual = %v", 20000, *con.Ulimits[0].SoftLimit)
}

if 40000 != *con.Ulimits[0].HardLimit {
t.Errorf("Ulimits[0].HardLimit: expect = %v, but actual = %v", 40000, *con.Ulimits[0].HardLimit)
}

if input.User != *con.User {
t.Errorf("User: expect = %v, but actual = %v", input.User, *con.User)
}

if input.WorkingDirectory != *con.WorkingDirectory {
t.Errorf("WorkingDirectory: expect = %v, but actual = %v", input.WorkingDirectory, *con.WorkingDirectory)
}

}
11 changes: 0 additions & 11 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ var (
variablePattern = regexp.MustCompile(`\$\{([\w_-]+)\}`)
)

func ConvertPointerString(values []string) []*string {

merged := make([]*string, len(values))

for i := 0; i < len(values); i++ {
merged[i] = &values[i]
}

return merged
}

func StringValueWithIndent(value interface{}, indent int) string {
sr := strings.NewReader(awsutil.Prettify(value))

Expand Down
Loading

0 comments on commit f0fdfa4

Please sign in to comment.