forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
163 lines (147 loc) · 3.8 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
package main
import (
"errors"
"io"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/docker/docker/pkg/term"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-docopt"
"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"
)
func init() {
cmd := register("run", runRun, `
usage: flynn run [-d] [-r <release>] [-e <entrypoint>] [--] <command> [<argument>...]
Run a job.
Options:
-d, --detached run job without connecting io streams
-r <release> id of release to run (defaults to current app release)
-e <entrypoint> overwrite the default entrypoint of the release's image
`)
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,
}
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
}
config.Release = release.ID
}
if e := args.String["-e"]; e != "" {
config.Entrypoint = []string{e}
}
return runJob(client, config)
}
type runConfig struct {
App string
Detached bool
Release string
ReleaseEnv bool
Entrypoint []string
Args []string
Env map[string]string
}
func runJob(client *controller.Client, config runConfig) error {
req := &ct.NewJob{
Cmd: config.Args,
TTY: term.IsTerminal(os.Stdin.Fd()) && term.IsTerminal(os.Stdout.Fd()) && !config.Detached,
ReleaseID: config.Release,
Entrypoint: config.Entrypoint,
Env: config.Env,
ReleaseEnv: config.ReleaseEnv,
}
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, os.Stdin)
attachClient.CloseWrite()
}()
childDone := make(chan struct{})
shutdown.BeforeExit(func() {
<-childDone
})
exitStatus, err := attachClient.Receive(os.Stdout, os.Stderr)
close(childDone)
if err != nil {
return err
}
if req.TTY {
term.RestoreTerminal(os.Stdin.Fd(), termState)
}
shutdown.ExitWithCode(exitStatus)
panic("unreached")
}