-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.go
182 lines (161 loc) · 4.23 KB
/
script.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package server
import (
"github.com/ecnepsnai/otto"
"github.com/ecnepsnai/otto/server/environ"
)
// Script describes an otto script
type Script struct {
ID string `ds:"primary"`
Name string `ds:"unique" min:"1" max:"140"`
Enabled bool `ds:"index"`
Executable string `min:"1"`
Script string `min:"1"`
Environment []environ.Variable
RunAs RunAs
WorkingDirectory string
AfterExecution string
AttachmentIDs []string
}
// RunAs describes the properties of which user runs a script
type RunAs struct {
Inherit bool
UID uint32
GID uint32
}
// Groups all groups with this script enabled
func (s *Script) Groups() []Group {
enabledGroups := []Group{}
groups := GroupStore.AllGroups()
for _, group := range groups {
hasScript := false
for _, scriptID := range group.ScriptIDs {
if scriptID == s.ID {
hasScript = true
break
}
}
if !hasScript {
continue
}
enabledGroups = append(enabledGroups, group)
}
return enabledGroups
}
// ScriptEnabledHost describes a host where a script is eanbled on it by a group
type ScriptEnabledHost struct {
ScriptID string
ScriptName string
GroupID string
GroupName string
HostID string
HostName string
}
// Hosts all hosts with this script enabled
func (s *Script) Hosts() []ScriptEnabledHost {
enabledHosts := []ScriptEnabledHost{}
for _, group := range s.Groups() {
hosts, err := group.Hosts()
if err != nil {
return []ScriptEnabledHost{}
}
ehs := make([]ScriptEnabledHost, len(hosts))
for i, host := range hosts {
ehs[i] = ScriptEnabledHost{
ScriptID: s.ID,
ScriptName: s.Name,
GroupID: group.ID,
GroupName: group.Name,
HostID: host.ID,
HostName: host.Name,
}
}
enabledHosts = append(enabledHosts, ehs...)
}
return enabledHosts
}
func (s *scriptStoreObject) SetGroups(script *Script, groupIDs []string) *Error {
groups := map[string]bool{}
allGroups := GroupStore.AllGroups()
for _, group := range allGroups {
var i = -1
for y, groupID := range groupIDs {
if groupID == group.ID {
i = y
break
}
}
groups[group.ID] = i != -1
}
for groupID, enable := range groups {
group := GroupCache.ByID(groupID)
if group == nil {
return ErrorUser("No group with ID %s", groupID)
}
var i = -1
for y, scriptID := range group.ScriptIDs {
if scriptID == script.ID {
i = y
break
}
}
if i == -1 && enable {
group.ScriptIDs = append(group.ScriptIDs, script.ID)
log.Debug("Enabling script '%s' on group '%s'", script.Name, group.Name)
} else if i != -1 && !enable {
group.ScriptIDs = append(group.ScriptIDs[:i], group.ScriptIDs[i+1:]...)
log.Debug("Disabling script '%s' on group '%s'", script.Name, group.Name)
} else {
continue
}
if err := GroupStore.Table.Update(*group); err != nil {
log.Error("Error updating group '%s': %s", group.Name, err.Error())
return ErrorFrom(err)
}
}
return nil
}
// Attachments all files for this script
func (s *Script) Attachments() ([]Attachment, *Error) {
if len(s.AttachmentIDs) == 0 {
return []Attachment{}, nil
}
attachments := make([]Attachment, len(s.AttachmentIDs))
for i, id := range s.AttachmentIDs {
attachment := AttachmentStore.AttachmentWithID(id)
if attachment == nil {
log.Error("Unknown attachment found on script: attachment_id='%s' script_id='%s'", id, s.ID)
log.Warn("Triggering cleanup of attachments")
AttachmentStore.Cleanup()
return nil, ErrorUser("A temporary error occurred, reload the page to try again")
}
attachments[i] = *attachment
}
return attachments, nil
}
func (s Script) OttoScript() (*otto.Script, *Error) {
fileIDs, err := s.Attachments()
if err != nil {
return nil, err
}
files := make([]otto.File, len(s.AttachmentIDs))
for i, file := range fileIDs {
file, erro := file.OttoFile()
if erro != nil {
return nil, ErrorFrom(erro)
}
files[i] = *file
}
return &otto.Script{
Name: s.Name,
RunAs: otto.RunAs{
UID: s.RunAs.UID,
GID: s.RunAs.GID,
Inherit: s.RunAs.Inherit,
},
Executable: s.Executable,
Data: []byte(s.Script),
WorkingDirectory: s.WorkingDirectory,
Environment: map[string]string{},
Files: files,
}, nil
}