-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunit.go
More file actions
146 lines (133 loc) · 3.46 KB
/
unit.go
File metadata and controls
146 lines (133 loc) · 3.46 KB
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
package client
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"regexp"
"strconv"
"strings"
"github.com/coreos/fleet/unit"
)
// path hierarchy for finding systemd service templates
var templatePaths = []string{
os.Getenv("DEISCTL_UNITS"),
"~/.deis/units",
"/var/lib/deis/units",
}
// Units returns a list of units filtered by target
func (c *FleetClient) Units(target string) (units []string, err error) {
allUnits, err := c.Fleet.Units()
if err != nil {
return
}
var r *regexp.Regexp
if strings.HasSuffix(target, "-data") {
r = regexp.MustCompile(`deis\-(` + target + `)\.service`)
} else if strings.Contains(target, "@") {
r = regexp.MustCompile(`deis\-(` + target + `)\.service`)
} else {
r = regexp.MustCompile(`deis\-(` + target + `)@([\d]+)\.service`)
}
for _, u := range allUnits {
match := r.MatchString(u.Name)
if match {
units = append(units, u.Name)
}
}
return
}
// nextUnit returns the next unit number for a given component
func (c *FleetClient) nextUnit(component string) (num int, err error) {
units, err := c.Units(component)
if err != nil {
return
}
num, err = nextUnitNum(units)
if err != nil {
return
}
return
}
// lastUnit returns the last unit number for a given component
func (c *FleetClient) lastUnit(component string) (num int, err error) {
units, err := c.Units(component)
if err != nil {
return
}
num, err = lastUnitNum(units)
if err != nil {
return
}
return
}
// NewUnit takes a component type and returns a Fleet unit
// that includes the relevant systemd service template
func NewUnit(component string) (uf *unit.UnitFile, err error) {
template, err := readTemplate(component)
if err != nil {
return
}
uf, err = unit.NewUnitFile(string(template))
if err != nil {
return
}
return
}
// NewDataUnit takes a component type and returns a Fleet unit
// that is hard-scheduled to a machine ID
func NewDataUnit(component string, machineID string) (uf *unit.UnitFile, err error) {
template, err := readTemplate(component)
if err != nil {
return
}
// replace CHANGEME with random machineID
replaced := strings.Replace(string(template), "CHANGEME", machineID, 1)
uf, err = unit.NewUnitFile(replaced)
if err != nil {
return
}
return
}
// formatUnitName returns a properly formatted systemd service name
// using the given component type and number
func formatUnitName(component string, num int) (unitName string, err error) {
if num == 0 {
return "deis-" + component + ".service", nil
}
return "deis-" + component + "@" + strconv.Itoa(num) + ".service", nil
}
// readTemplate returns the contents of a systemd template for the given component
func readTemplate(component string) (out []byte, err error) {
templateName := "deis-" + component + ".service"
var templateFile string
// look in $DEISCTL_UNITS env var, then the local and global root paths
for _, p := range templatePaths {
if p == "" {
continue
}
p, _ := ExpandUser(p)
filename := path.Join(p, templateName)
if _, err := os.Stat(filename); err == nil {
templateFile = filename
break
}
}
if templateFile == "" {
return nil, fmt.Errorf("Could not find unit template for %v", component)
}
out, err = ioutil.ReadFile(templateFile)
if err != nil {
return
}
return
}
// ExpandUser replaces "~" in a string with the current user's home directory.
func ExpandUser(path string) (string, error) {
user, err := user.Current()
if err != nil {
return path, err
}
return strings.Replace(path, "~/", user.HomeDir+"/", 1), nil
}