forked from flynn-archive/flynn-receive
/
flynn-receive.go
183 lines (157 loc) · 3.62 KB
/
flynn-receive.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/flynn/go-discoverd"
"github.com/flynn/go-dockerclient"
lc "github.com/flynn/lorne/client"
"github.com/flynn/lorne/types"
sc "github.com/flynn/sampi/client"
"github.com/flynn/sampi/types"
)
// WARNING: assumes one host at the moment
var sched *sc.Client
var host *lc.Client
var hostid string
func init() {
var err error
sched, err = sc.New()
if err != nil {
log.Fatal(err)
}
hostid = findHost()
host, err = lc.New(hostid)
if err != nil {
log.Fatal(err)
}
}
func main() {
root := "/var/lib/demo/apps"
services, _ := discoverd.Services("shelf", discoverd.DefaultTimeout)
if len(services) < 1 {
panic("Shelf is not discoverable")
}
shelfHost := services[0].Addr
app := os.Args[2]
os.MkdirAll(root+"/"+app, 0755)
fmt.Printf("-----> Building %s...\n", app)
scheduleAndAttach(sc.RandomJobID(app+"-build."), docker.Config{
Image: "flynn/slugbuilder",
Cmd: []string{"http://" + shelfHost + "/" + app + ".tgz"},
Tty: false,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
OpenStdin: true,
StdinOnce: true,
})
fmt.Printf("-----> Deploying %s ...\n", app)
jobid := sc.RandomJobID(app + "-web.")
stopIfExists(jobid)
scheduleWithTcpPort(jobid, docker.Config{
Image: "flynn/slugrunner",
Cmd: []string{"start", "web"},
Tty: false,
AttachStdin: false,
AttachStdout: false,
AttachStderr: false,
OpenStdin: false,
StdinOnce: false,
Env: []string{
"SLUG_URL=http://" + shelfHost + "/" + app + ".tgz",
"SD_NAME=" + app,
},
})
time.Sleep(1 * time.Second)
fmt.Printf("=====> Application deployed:\n")
fmt.Printf(" http://10.0.2.15:%s\n", getPort(jobid))
fmt.Println("")
}
func shell(cmdline string) string {
out, err := exec.Command("bash", "-c", cmdline).Output()
if err != nil {
panic(err)
}
return strings.Trim(string(out), " \n")
}
func stopIfExists(jobid string) {
_, err := host.GetJob(jobid)
if err != nil {
return
}
if err := host.StopJob(jobid); err != nil {
return
}
}
func scheduleWithTcpPort(jobid string, config docker.Config) {
schedReq := &sampi.ScheduleReq{
Incremental: true,
HostJobs: map[string][]*sampi.Job{hostid: {{ID: jobid, Config: &config, TCPPorts: 1}}},
}
if _, err := sched.Schedule(schedReq); err != nil {
log.Fatal(err)
}
}
func getPort(jobid string) string {
job, err := host.GetJob(jobid)
if err != nil {
log.Fatal(err)
}
for portspec := range job.Job.Config.ExposedPorts {
return strings.Split(portspec, "/")[0]
}
return ""
}
func findHost() string {
state, err := sched.State()
if err != nil {
log.Fatal(err)
}
var firstHost string
for k := range state {
firstHost = k
break
}
if firstHost == "" {
log.Fatal("no hosts")
}
return firstHost
}
func scheduleAndAttach(jobid string, config docker.Config) {
client, err := lc.New(hostid)
if err != nil {
log.Fatal(err)
}
conn, attachWait, err := client.Attach(&lorne.AttachReq{
JobID: jobid,
Flags: lorne.AttachFlagStdout | lorne.AttachFlagStderr | lorne.AttachFlagStdin | lorne.AttachFlagStream,
}, true)
if err != nil {
log.Fatal(err)
}
schedReq := &sampi.ScheduleReq{
Incremental: true,
HostJobs: map[string][]*sampi.Job{hostid: {{ID: jobid, Config: &config}}},
}
if _, err := sched.Schedule(schedReq); err != nil {
log.Fatal(err)
}
if err := attachWait(); err != nil {
log.Fatal(err)
}
go func() {
io.Copy(conn, os.Stdin)
conn.CloseWrite()
}()
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
fmt.Fprintln(os.Stdout, scanner.Text()[8:])
}
conn.Close()
}