-
Notifications
You must be signed in to change notification settings - Fork 302
/
compose_app.go
145 lines (131 loc) · 4.43 KB
/
compose_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2015-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package compose
import (
"os"
"strconv"
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/container"
composeFactory "github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/factory"
ecscompose "github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/project"
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
"github.com/flynn/go-shlex"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// COMPOSE
// displayTitle flag is used to print the title for the fields
const displayTitle = true
// ProjectAction is an adapter to allow the use of ordinary functions as libcompose actions.
// Any function that has the appropriate signature can be register as an action on a urfave/cli command.
//
// cli.Command{
// Name: "ps",
// Usage: "List containers",
// Action: app.WithProject(factory, app.ProjectPs),
// }
type ProjectAction func(project ecscompose.Project, c *cli.Context)
// WithProject is an helper function to create a cli.Command action with a ProjectFactory.
func WithProject(factory composeFactory.ProjectFactory, action ProjectAction, isService bool) func(context *cli.Context) {
return func(context *cli.Context) {
// TODO, instead of passing isService around, we can determine
// the command name cliContext.Parent().Command.Name = service and set appropriate context
// However, parentContext is not being set appropriately by cli. Investigate.
p, err := factory.Create(context, isService)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Unable to create and read ECS Compose Project")
}
action(p, context)
}
}
// ProjectCreate creates the task definition required for the containers but does not start them.
func ProjectCreate(p ecscompose.Project, c *cli.Context) {
err := p.Create()
if err != nil {
log.Fatal(err)
}
}
// ProjectStart starts containers.
func ProjectStart(p ecscompose.Project, c *cli.Context) {
err := p.Start()
if err != nil {
log.Fatal(err)
}
}
// ProjectUp brings all containers up.
func ProjectUp(p ecscompose.Project, c *cli.Context) {
err := p.Up()
if err != nil {
log.Fatal(err)
}
}
// ProjectPs lists the containers.
func ProjectPs(p ecscompose.Project, c *cli.Context) {
allInfo, err := p.Info(c.String(flags.DesiredTaskStatus))
if err != nil {
log.Fatal(err)
}
os.Stdout.WriteString(allInfo.String(container.ContainerInfoColumns, displayTitle))
}
// ProjectRun starts containers and executes one-time command against the container
// TODO These only account for command overrides within a ContainerOverride: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerOverride.html
func ProjectRun(p ecscompose.Project, c *cli.Context) {
args := c.Args()
if len(args)%2 != 0 {
log.Fatal("Please pass arguments in the form: CONTAINER \"COMMAND ...\" [CONTAINER \"COMMAND...\"] ...")
}
commandOverrides := make(map[string][]string)
for i := 0; i < len(args); i += 2 {
parts, err := shlex.Split(args[i+1])
if err != nil {
log.WithFields(log.Fields{
"container-name": args[i],
"error": err,
}).Fatal("Unable to parse run commands")
}
commandOverrides[args[i]] = parts
}
err := p.Run(commandOverrides)
if err != nil {
log.Fatal(err)
}
}
// ProjectScale scales containers.
func ProjectScale(p ecscompose.Project, c *cli.Context) {
if len(c.Args()) != 1 {
log.Fatal("Please pass arguments in the form: ecs-cli compose scale COUNT")
}
count, err := strconv.Atoi(c.Args().First())
if err != nil {
log.Fatal("Please pass an integer value for argument COUNT")
}
err = p.Scale(count)
if err != nil {
log.Fatal(err)
}
}
// ProjectStop brings all containers down.
func ProjectStop(p ecscompose.Project, c *cli.Context) {
err := p.Stop()
if err != nil {
log.Fatal(err)
}
}
// ProjectDown brings all containers down.
func ProjectDown(p ecscompose.Project, c *cli.Context) {
err := p.Down()
if err != nil {
log.Fatal(err)
}
}