-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunit.go
More file actions
56 lines (50 loc) · 1.38 KB
/
unit.go
File metadata and controls
56 lines (50 loc) · 1.38 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
package deisctl
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"github.com/coreos/fleet/unit"
)
// path hierarchy for finding systemd service templates
var rootPaths = []string{"/run/deis/units", "units", "../units"}
// NewUnit takes a component type and returns a Fleet unit
// that includes the relevant systemd service template
func NewUnit(component string) (u *unit.Unit, err error) {
template, err := readTemplate(component)
if err != nil {
return
}
u, err = unit.NewUnit(string(template))
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) {
unitName = "deis-" + component + "." + strconv.Itoa(num) + ".service"
return
}
// 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
for _, rootPath := range rootPaths {
filename := path.Join(rootPath, component, 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
}