forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
112 lines (94 loc) · 2.53 KB
/
socket.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
package systemd
import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"
log "github.com/coreos/fleet/third_party/github.com/golang/glog"
)
type SystemdSocket struct {
manager *SystemdManager
name string
}
func NewSystemdSocket(manager *SystemdManager, name string) *SystemdSocket {
return &SystemdSocket{manager, name}
}
func (ss *SystemdSocket) Name() string {
return ss.name
}
func (ss *SystemdSocket) State() (string, string, string, []string, error) {
loadState, activeState, subState, err := ss.manager.getUnitStates(ss.name)
if err != nil {
return "", "", "", nil, err
}
payload, _ := ss.Payload()
sockets := parseSocketFile(payload)
sockStrings := []string{}
for _, sock := range sockets {
sockStrings = append(sockStrings, sock.String(ss.manager.Machine.State().PublicIP))
}
return loadState, activeState, subState, sockStrings, nil
}
func (ss *SystemdSocket) Payload() (string, error) {
return ss.manager.readUnit(ss.Name())
}
func parseSocketFile(contents string) []ListenSocket {
lines := strings.Split(contents, "\n")
listenLines := filterListenLines(lines)
sockets := make([]ListenSocket, 0)
for _, line := range listenLines {
socket, err := NewListenSocketFromListenConfig(line)
//TODO: do something more with this err
if err == nil {
sockets = append(sockets, *socket)
} else {
log.V(1).Infof("Unable to parse Listen line in socket file: %s", err)
}
}
return sockets
}
type ListenSocket struct {
Type string
Port int
}
func (ls *ListenSocket) String(ip string) string {
hostport := net.JoinHostPort(ip, strconv.Itoa(ls.Port))
u := url.URL{Scheme: ls.Type, Host: hostport}
return u.String()
}
func NewListenSocketFromListenConfig(cfg string) (*ListenSocket, error) {
sockType, port, err := parseListenLine(cfg)
if err == nil {
return &ListenSocket{sockType, port}, nil
} else {
return nil, err
}
}
func filterListenLines(lines []string) []string {
var filtered []string
for _, line := range lines {
if strings.HasPrefix(line, "Listen") {
filtered = append(filtered, line)
}
}
return filtered
}
func parseListenLine(line string) (string, int, error) {
keyMap := map[string]string{
"ListenSequentialPacket": "unix",
"ListenDatagram": "udp",
"ListenStream": "tcp",
}
parts := strings.SplitN(line, "=", 2)
key, ok := keyMap[parts[0]]
if !ok {
return "", 0, errors.New(fmt.Sprintf("unrecognized key %s", parts[0]))
}
port, err := strconv.Atoi(parts[1])
if err != nil {
return "", 0, errors.New(fmt.Sprintf("Unparseable value %s", parts[1]))
}
return key, port, nil
}