forked from flynn/flynn
/
run.go
194 lines (177 loc) · 4.65 KB
/
run.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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/docker/docker/pkg/term"
"github.com/flynn/flynn/controller/client"
ct "github.com/flynn/flynn/controller/types"
"github.com/flynn/flynn/pkg/cluster"
"github.com/flynn/flynn/pkg/shutdown"
"github.com/flynn/go-docopt"
)
func init() {
cmd := register("run", runRun, `
usage: flynn run [-d] [-r <release>] [-e <entrypoint>] [-l] [--] <command> [<argument>...]
Run a job.
Options:
-d, --detached run job without connecting io streams (implies --enable-log)
-r <release> id of release to run (defaults to current app release)
-e <entrypoint> [DEPRECATED] overwrite the default entrypoint of the release's image
-l, --enable-log send output to log streams
`)
cmd.optsFirst = true
}
// Declared here for Windows portability
const SIGWINCH syscall.Signal = 28
func runRun(args *docopt.Args, client controller.Client) error {
config := runConfig{
App: mustApp(),
Detached: args.Bool["--detached"],
Release: args.String["-r"],
Args: append([]string{args.String["<command>"]}, args.All["<argument>"].([]string)...),
ReleaseEnv: true,
Exit: true,
DisableLog: !args.Bool["--detached"] && !args.Bool["--enable-log"],
}
if config.Release == "" {
release, err := client.GetAppRelease(config.App)
if err == controller.ErrNotFound {
return errors.New("No app release, specify a release with -release")
}
if err != nil {
return err
}
if release.ImageArtifactID() == "" {
return errors.New("App release has no image, push a release first")
}
config.Release = release.ID
}
if e := args.String["-e"]; e != "" {
fmt.Fprintln(os.Stderr, "WARN: The -e flag is deprecated and will be removed in future versions, use <command> as the entrypoint")
config.Args = append([]string{e}, config.Args...)
}
return runJob(client, config)
}
type runConfig struct {
App string
Detached bool
Release string
ReleaseEnv bool
Args []string
Env map[string]string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
DisableLog bool
Exit bool
}
func runJob(client controller.Client, config runConfig) error {
req := &ct.NewJob{
Args: config.Args,
TTY: config.Stdin == nil && config.Stdout == nil && term.IsTerminal(os.Stdin.Fd()) && term.IsTerminal(os.Stdout.Fd()) && !config.Detached,
ReleaseID: config.Release,
Env: config.Env,
ReleaseEnv: config.ReleaseEnv,
DisableLog: config.DisableLog,
}
if config.Stdin == nil {
config.Stdin = os.Stdin
}
if config.Stdout == nil {
config.Stdout = os.Stdout
}
if config.Stderr == nil {
config.Stderr = os.Stderr
}
if req.TTY {
if req.Env == nil {
req.Env = make(map[string]string)
}
ws, err := term.GetWinsize(os.Stdin.Fd())
if err != nil {
return err
}
req.Columns = int(ws.Width)
req.Lines = int(ws.Height)
req.Env["COLUMNS"] = strconv.Itoa(int(ws.Width))
req.Env["LINES"] = strconv.Itoa(int(ws.Height))
req.Env["TERM"] = os.Getenv("TERM")
}
if config.Detached {
job, err := client.RunJobDetached(config.App, req)
if err != nil {
return err
}
log.Println(job.ID)
return nil
}
rwc, err := client.RunJobAttached(config.App, req)
if err != nil {
return err
}
defer rwc.Close()
attachClient := cluster.NewAttachClient(rwc)
var termState *term.State
if req.TTY {
termState, err = term.MakeRaw(os.Stdin.Fd())
if err != nil {
return err
}
// Restore the terminal if we return without calling os.Exit
defer term.RestoreTerminal(os.Stdin.Fd(), termState)
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, SIGWINCH)
for range ch {
ws, err := term.GetWinsize(os.Stdin.Fd())
if err != nil {
return
}
attachClient.ResizeTTY(ws.Height, ws.Width)
attachClient.Signal(int(SIGWINCH))
}
}()
}
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
sig := <-ch
attachClient.Signal(int(sig.(syscall.Signal)))
time.Sleep(10 * time.Second)
attachClient.Signal(int(syscall.SIGKILL))
}()
go func() {
io.Copy(attachClient, config.Stdin)
attachClient.CloseWrite()
}()
childDone := make(chan struct{})
shutdown.BeforeExit(func() {
<-childDone
})
exitStatus, err := attachClient.Receive(config.Stdout, config.Stderr)
close(childDone)
if err != nil {
return err
}
if req.TTY {
term.RestoreTerminal(os.Stdin.Fd(), termState)
}
if config.Exit {
shutdown.ExitWithCode(exitStatus)
}
if exitStatus != 0 {
return RunExitError(exitStatus)
}
return nil
}
type RunExitError int
func (e RunExitError) Error() string {
return fmt.Sprintf("remote job exited with status %d", e)
}