-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_store.go
182 lines (158 loc) · 4.95 KB
/
script_store.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/ds"
"github.com/ecnepsnai/limits"
"github.com/ecnepsnai/otto/server/environ"
)
func (s *scriptStoreObject) ScriptWithID(id string) *Script {
obj, err := s.Table.Get(id)
if err != nil {
log.Error("Error getting script: id='%s' error='%s'", id, err.Error())
return nil
}
if obj == nil {
return nil
}
script, k := obj.(Script)
if !k {
log.Fatal("Error getting script: id='%s' error='%s'", id, "invalid type")
}
return &script
}
func (s *scriptStoreObject) ScriptWithName(name string) *Script {
obj, err := s.Table.GetUnique("Name", name)
if err != nil {
log.Error("Error getting script: name='%s' error='%s'", name, err.Error())
return nil
}
if obj == nil {
return nil
}
script, k := obj.(Script)
if !k {
log.Fatal("Error getting script: name='%s' error='%s'", name, "invalid type")
}
return &script
}
func (s *scriptStoreObject) AllScripts() []Script {
objects, err := s.Table.GetAll(&ds.GetOptions{Sorted: true, Ascending: true})
if err != nil {
log.Error("Error listing scripts: error='%s'", err.Error())
return []Script{}
}
if len(objects) == 0 {
return []Script{}
}
scripts := make([]Script, len(objects))
for i, obj := range objects {
script, k := obj.(Script)
if !k {
log.Fatal("Error listing scripts: error='%s'", "invalid type")
}
scripts[i] = script
}
return scripts
}
type newScriptParameters struct {
Name string
Executable string
Script string
Environment []environ.Variable
RunAs RunAs
WorkingDirectory string
AfterExecution string
AttachmentIDs []string
}
func (s *scriptStoreObject) NewScript(params newScriptParameters) (*Script, *Error) {
if s.ScriptWithName(params.Name) != nil {
log.Warn("Script with name '%s' already exists", params.Name)
return nil, ErrorUser("Script with name '%s' already exists", params.Name)
}
if params.AfterExecution != "" && !IsClientAction(params.AfterExecution) {
return nil, ErrorUser("Invalid client action %s", params.AfterExecution)
}
if err := environ.Validate(params.Environment); err != nil {
return nil, ErrorUser(err.Error())
}
script := Script{
ID: newID(),
Name: params.Name,
Executable: params.Executable,
Script: params.Script,
Environment: params.Environment,
RunAs: params.RunAs,
Enabled: true,
WorkingDirectory: params.WorkingDirectory,
AfterExecution: params.AfterExecution,
AttachmentIDs: params.AttachmentIDs,
}
if err := limits.Check(script); err != nil {
return nil, ErrorUser(err.Error())
}
if err := s.Table.Add(script); err != nil {
log.Error("Error adding new script '%s': %s", params.Name, err.Error())
return nil, ErrorFrom(err)
}
log.Info("Added new script '%s'", params.Name)
return &script, nil
}
type editScriptParameters struct {
Name string
Enabled bool
Executable string
Script string
Environment []environ.Variable
RunAs RunAs
WorkingDirectory string
AfterExecution string
AttachmentIDs []string
}
func (s *scriptStoreObject) EditScript(script *Script, params editScriptParameters) (*Script, *Error) {
if existingScript := s.ScriptWithName(params.Name); existingScript != nil && existingScript.ID != script.ID {
log.Warn("Script with name '%s' already exists", params.Name)
return nil, ErrorUser("Script with name '%s' already exists", params.Name)
}
if params.AfterExecution != "" && !IsClientAction(params.AfterExecution) {
return nil, ErrorUser("Invalid client action %s", params.AfterExecution)
}
if err := environ.Validate(params.Environment); err != nil {
return nil, ErrorUser(err.Error())
}
script.Name = params.Name
script.Enabled = params.Enabled
script.Executable = params.Executable
script.Script = params.Script
script.Environment = params.Environment
script.RunAs = params.RunAs
script.WorkingDirectory = params.WorkingDirectory
script.AfterExecution = params.AfterExecution
script.AttachmentIDs = params.AttachmentIDs
if err := limits.Check(script); err != nil {
return nil, ErrorUser(err.Error())
}
if err := s.Table.Update(*script); err != nil {
log.Error("Error updating script '%s': %s", params.Name, err.Error())
return nil, ErrorFrom(err)
}
log.Info("Updating script '%s'", params.Name)
return script, nil
}
func (s *scriptStoreObject) DeleteScript(script *Script) *Error {
for _, schedule := range ScheduleCache.All() {
if schedule.ScriptID == script.ID {
return ErrorUser("Script is used by schedule %s", schedule.Name)
}
}
if err := s.Table.Delete(*script); err != nil {
log.Error("Error deleting script '%s': %s", script.Name, err.Error())
return ErrorFrom(err)
}
for _, id := range script.AttachmentIDs {
if err := AttachmentStore.DeleteAttachment(id); err != nil {
log.Error("Error deleting attachment '%s': %s", id, err.Message)
}
}
GroupStore.CleanupDeadScripts()
log.Info("Deleting script '%s'", script.Name)
return nil
}