forked from st3v/scope-bosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (86 loc) · 1.69 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/st3v/scope-bosh/bosh"
)
var (
pluginsRoot string
hostname string
jobSpecPath string
)
func init() {
flag.StringVar(
&pluginsRoot,
"pluginsRoot",
"/var/run/scope/plugins",
"root directory for scope plugin sockets",
)
flag.StringVar(
&jobSpecPath,
"jobSpec",
"/var/vcap/bosh/spec.json",
"path to Bosh job spec",
)
flag.StringVar(
&hostname,
"hostname",
"",
"hostname as reported by scope",
)
}
func main() {
flag.Parse()
if hostname == "" {
var err error
hostname, err = os.Hostname()
if err != nil {
log.Fatal(err)
}
}
log.Printf("Starting on %s...\n", hostname)
socket := filepath.Join(pluginsRoot, "bosh", "bosh.sock")
listener, err := listen(socket)
if err != nil {
log.Fatal(err)
}
defer func() {
listener.Close()
os.RemoveAll(filepath.Dir(socket))
}()
handleSignals()
plugin := bosh.NewPlugin(hostname, jobSpecPath)
http.HandleFunc("/report", plugin.Report)
log.Fatal(http.Serve(listener, nil))
}
func listen(socket string) (net.Listener, error) {
os.RemoveAll(filepath.Dir(socket))
if err := os.MkdirAll(filepath.Dir(socket), 0700); err != nil {
return nil, fmt.Errorf(
"error creating directory %q: %v",
filepath.Dir(socket),
err,
)
}
listener, err := net.Listen("unix", socket)
if err != nil {
return nil, fmt.Errorf("error listening on %q: %v", socket, err)
}
log.Printf("Listening on unix://%s\n", socket)
return listener, nil
}
func handleSignals() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
go func() {
<-interrupt
os.Exit(0)
}()
}