-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
96 lines (75 loc) · 2.12 KB
/
magefile.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
//+build mage
package main
import (
"text/template"
"bytes"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type ServiceTemplateData struct {
ConfigurationFileLocation string
InstallPrefix string
ExecutableName string
}
var (
serviceTmplData = ServiceTemplateData{
configurationFileLocation,
installPrefix,
executableName,
}
filesToInstall = map[string]string{
installPrefix + executableName: buildPath + executableName,
configurationFileLocation + "config.toml": "config.toml",
}
buildEnv = map[string]string{
"CGO_ENABLED": "0",
}
)
func Build() error {
return sh.RunWithV(buildEnv, "go", "build", "-o", buildPath + executableName)
}
func Install() error {
if _, err := os.Stat(buildPath + executableName); os.IsNotExist(err) {
mg.Deps(Build)
} else {
return err
}
if installSystemdService {
mg.Deps(InstallSystemdService)
}
for dst, src := range filesToInstall {
dstPath := filepath.Dir(dst)
if _, err := os.Stat(dstPath); os.IsNotExist(err) {
if err = os.MkdirAll(dstPath, 0755); err != nil {
return err
}
} else if err != nil {
return err
}
if err := sh.Copy(dst, src); err != nil {
return err
}
}
return nil
}
func GenServiceTemplate() error {
// Execute Systemd service template
tmpl := template.Must(template.ParseFiles("cp-prometheus-exporter.service.template"))
buffer := &bytes.Buffer{}
if err := tmpl.Execute(buffer, serviceTmplData); err != nil {
return err
}
return os.WriteFile(buildPath + "cp-prometheus-exporter.service", buffer.Bytes(), 0664)
}
func Clean() error {
return os.RemoveAll(buildPath)
}
func InstallSystemdService() error {
mg.Deps(GenServiceTemplate)
if err := sh.Copy("/usr/lib/systemd/system/cp-prometheus-exporter.service", buildPath + "cp-prometheus-exporter.service"); err != nil {
return err
}
return sh.RunV("systemctl", "daemon-reload")
}