-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenesis.go
More file actions
130 lines (109 loc) · 2.34 KB
/
Copy pathgenesis.go
File metadata and controls
130 lines (109 loc) · 2.34 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
package genesis
import (
"crypto/md5"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/wx13/genesis/store"
)
// Facts stores discovered information about the target system.
type Facts struct {
Arch string
ArchType string
OS string
Hostname string
Username string
Distro string
}
// GatherFacts learns stuff about the target system.
func GatherFacts() Facts {
facts := Facts{}
// Set architecture facts.
facts.ArchType = runtime.GOARCH
facts.OS = runtime.GOOS
cmd := exec.Command("uname", "-m")
output, err := cmd.Output()
if err == nil {
facts.Arch = strings.TrimSpace(string(output))
}
// Learn linux distro.
b, err := ioutil.ReadFile("/etc/issue")
if err == nil {
f := strings.Fields(string(b))
facts.Distro = f[0]
}
facts.Hostname, _ = os.Hostname()
u, err := user.Current()
if err != nil {
facts.Username = u.Username
}
return facts
}
var Store *store.Store
var Tmpdir string
// Status represents a Pass/Fail/Unknown.
type Status int
const (
StatusPass Status = iota
StatusFail
StatusUnknown
)
// Module is an interface for all the modules.
type Module interface {
Install() (string, error)
Remove() (string, error)
Status() (Status, string, error)
ID() string // Description of module action
Files() []string // list of files needed by module
}
// Doer can do and undo things.
type Doer interface {
Do() (bool, error)
Undo() (bool, error)
Status() (Status, error)
ID() string
Files() []string
}
func DoerHash(doer Doer) string {
id := doer.ID()
return StringHash(id)
}
func StringHash(id string) string {
id = fmt.Sprintf("%x", md5.Sum([]byte(id)))
return id[:6]
}
// FileExists is a helper function to check if a file exists.
func FileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
// IsRunning checks to see if a process is running.
func IsRunning(pattern string) (bool, error) {
out, err := exec.Command("pgrep", pattern).CombinedOutput()
if err != nil {
return false, err
}
if len(out) > 0 {
return true, nil
}
return false, nil
}
// ExpandHome expands a leading tilde to the user's home directory.
func ExpandHome(name string) string {
if name[0] != '~' {
return name
}
user, err := user.Current()
if err != nil {
return name
}
return filepath.Join(user.HomeDir, name[1:])
}