forked from rancher/rancher-compose
/
app.go
216 lines (195 loc) · 5.49 KB
/
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package app
import (
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/libcompose/cli/app"
"github.com/docker/libcompose/cli/command"
"github.com/docker/libcompose/cli/logger"
"github.com/docker/libcompose/lookup"
"github.com/docker/libcompose/project"
rLookup "github.com/rancher/rancher-compose/lookup"
"github.com/rancher/rancher-compose/rancher"
"github.com/rancher/rancher-compose/upgrade"
)
type ProjectFactory struct {
}
func (p *ProjectFactory) Create(c *cli.Context) (*project.Project, error) {
rancherComposeFile, err := rancher.ResolveRancherCompose(c.GlobalString("file"),
c.GlobalString("rancher-file"))
if err != nil {
return nil, err
}
qLookup, err := rLookup.NewQuestionLookup(rancherComposeFile, &lookup.OsEnvLookup{})
if err != nil {
return nil, err
}
envLookup, err := rLookup.NewFileEnvLookup(c.GlobalString("env-file"), qLookup)
if err != nil {
return nil, err
}
context := &rancher.Context{
Context: project.Context{
ConfigLookup: &lookup.FileConfigLookup{},
EnvironmentLookup: envLookup,
LoggerFactory: logger.NewColorLoggerFactory(),
},
RancherComposeFile: c.GlobalString("rancher-file"),
Url: c.GlobalString("url"),
AccessKey: c.GlobalString("access-key"),
SecretKey: c.GlobalString("secret-key"),
PullCached: c.Bool("cached"),
Uploader: &rancher.S3Uploader{},
}
qLookup.Context = context
command.Populate(&context.Context, c)
context.Upgrade = c.Bool("upgrade") || c.Bool("force-upgrade")
context.ForceUpgrade = c.Bool("force-upgrade")
context.Rollback = c.Bool("rollback")
context.BatchSize = int64(c.Int("batch-size"))
context.Interval = int64(c.Int("interval"))
context.ConfirmUpgrade = c.Bool("confirm-upgrade")
context.Pull = c.Bool("pull")
return rancher.NewProject(context)
}
func UpgradeCommand(factory app.ProjectFactory) cli.Command {
return cli.Command{
Name: "upgrade",
Usage: "Perform rolling upgrade between services",
Action: app.WithProject(factory, Upgrade),
Flags: []cli.Flag{
cli.IntFlag{
Name: "batch-size",
Usage: "Number of containers to upgrade at once",
Value: 2,
},
cli.IntFlag{
Name: "scale",
Usage: "Final number of running containers",
Value: -1,
},
cli.IntFlag{
Name: "interval",
Usage: "Update interval in milliseconds",
Value: 2000,
},
cli.BoolTFlag{
Name: "update-links",
Usage: "Update inbound links on target service",
},
cli.BoolFlag{
Name: "wait,w",
Usage: "Wait for upgrade to complete",
},
cli.BoolFlag{
Name: "pull, p",
Usage: "Before doing the upgrade do an image pull on all hosts that have the image already",
},
cli.BoolFlag{
Name: "cleanup, c",
Usage: "Remove the original service definition once upgraded, implies --wait",
},
},
}
}
func UpCommand(factory app.ProjectFactory) cli.Command {
return cli.Command{
Name: "up",
Usage: "Bring all services up",
Action: app.WithProject(factory, ProjectUp),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "pull, p",
Usage: "Before doing the upgrade do an image pull on all hosts that have the image already",
},
cli.BoolFlag{
Name: "d",
Usage: "Do not block and log",
},
cli.BoolFlag{
Name: "upgrade, u, recreate",
Usage: "Upgrade if service has changed",
},
cli.BoolFlag{
Name: "force-upgrade, force-recreate",
Usage: "Upgrade regardless if service has changed",
},
cli.BoolFlag{
Name: "confirm-upgrade, c",
Usage: "Confirm that the upgrade was success and delete old containers",
},
cli.BoolFlag{
Name: "rollback, r",
Usage: "Rollback to the previous deployed version",
},
cli.IntFlag{
Name: "batch-size",
Usage: "Number of containers to upgrade at once",
Value: 2,
},
cli.IntFlag{
Name: "interval",
Usage: "Update interval in milliseconds",
Value: 1000,
},
},
}
}
func PullCommand(factory app.ProjectFactory) cli.Command {
return cli.Command{
Name: "pull",
Usage: "Pulls images for services",
Action: app.WithProject(factory, app.ProjectPull),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "cached, c",
Usage: "Only update hosts that have the image cached, don't pull new",
},
},
}
}
func CreateCommand(factory app.ProjectFactory) cli.Command {
return cli.Command{
Name: "create",
Usage: "Create all services but do not start",
Action: app.WithProject(factory, ProjectCreate),
}
}
func ProjectCreate(p *project.Project, c *cli.Context) {
if err := p.Create(c.Args()...); err != nil {
logrus.Fatal(err)
}
// This is to fix circular links... What!? It works.
if err := p.Create(c.Args()...); err != nil {
logrus.Fatal(err)
}
}
func ProjectUp(p *project.Project, c *cli.Context) {
if err := p.Create(c.Args()...); err != nil {
logrus.Fatal(err)
}
if err := p.Up(c.Args()...); err != nil {
logrus.Fatal(err)
}
if !c.Bool("d") {
// wait forever
<-make(chan interface{})
}
}
func Upgrade(p *project.Project, c *cli.Context) {
args := c.Args()
if len(args) != 2 {
logrus.Fatalf("Please pass arguments in the form: [from service] [to service]")
}
err := upgrade.Upgrade(p, args[0], args[1], upgrade.UpgradeOpts{
BatchSize: c.Int("batch-size"),
IntervalMillis: c.Int("interval"),
FinalScale: c.Int("scale"),
UpdateLinks: c.Bool("update-links"),
Wait: c.Bool("wait"),
CleanUp: c.Bool("cleanup"),
Pull: c.Bool("pull"),
})
if err != nil {
logrus.Fatal(err)
}
}