forked from pufferpanel/pufferpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
196 lines (172 loc) · 5.71 KB
/
server.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright 2019 Padduck, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pufferpanel
import (
"context"
"os/exec"
"runtime"
"strings"
"time"
"github.com/docker/docker/client"
)
type Server struct {
Type
Variables map[string]Variable `json:"data,omitempty"`
Display string `json:"display,omitempty"`
Environment interface{} `json:"environment,omitempty"`
SupportedEnvironments []interface{} `json:"supportedEnvironments,omitempty"`
Installation []interface{} `json:"install,omitempty"`
Uninstallation []interface{} `json:"uninstall,omitempty"`
Identifier string `json:"id,omitempty"`
Execution Execution `json:"run,omitempty"`
Tasks map[string]Task `json:"tasks,omitempty"`
Requirements Requirements `json:"requirements,omitempty"`
}
type Task struct {
Name string `json:"name,omitempty" binding:"required"`
CronSchedule string `json:"cronSchedule,omitempty"`
Operations []interface{} `json:"operations,omitempty" binding:"required"`
}
type Variable struct {
Type
Description string `json:"desc,omitempty"`
Display string `json:"display,omitempty"`
Internal bool `json:"internal,omitempty"`
Required bool `json:"required,omitempty"`
Value interface{} `json:"value,omitempty"`
UserEditable bool `json:"userEdit,omitempty"`
Options []VariableOption `json:"options,omitempty"`
}
type VariableOption struct {
Value interface{} `json:"value"`
Display string `json:"display"`
}
type Execution struct {
Command string `json:"command,omitempty"`
StopCommand string `json:"stop,omitempty"`
Disabled bool `json:"disabled,omitempty"`
AutoStart bool `json:"autostart,omitempty"`
AutoRestartFromCrash bool `json:"autorecover,omitempty"`
AutoRestartFromGraceful bool `json:"autorestart,omitempty"`
PreExecution []interface{} `json:"pre,omitempty"`
PostExecution []interface{} `json:"post,omitempty"`
StopCode int `json:"stopCode,omitempty"`
EnvironmentVariables map[string]string `json:"environmentVars,omitempty"`
LegacyRun string `json:"program,omitempty"`
LegacyArguments []string `json:"arguments,omitempty"`
WorkingDirectory string `json:"workingDirectory,omitempty"`
}
type Type struct {
Type string `json:"type"`
}
type Requirements struct {
Binaries []string `json:"binaries,omitempty"`
OS string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
}
func (s *Server) CopyFrom(replacement *Server) {
s.Variables = replacement.Variables
s.Tasks = replacement.Tasks
s.Type = replacement.Type
s.Execution = replacement.Execution
s.Display = replacement.Display
s.Installation = replacement.Installation
s.Uninstallation = replacement.Uninstallation
s.Environment = replacement.Environment
s.SupportedEnvironments = replacement.SupportedEnvironments
s.Requirements = replacement.Requirements
}
func (r Requirements) Test(server Server) error {
osReq := parseRequirementRow(r.OS)
if len(osReq) > 0 {
passes := false
for _, v := range osReq {
if v == runtime.GOOS {
passes = true
break
}
}
if !passes {
return ErrUnsupportedOS(runtime.GOOS, strings.ReplaceAll(r.OS, "||", " OR "))
}
}
archReq := parseRequirementRow(r.Arch)
if len(archReq) > 0 {
passes := false
for _, v := range archReq {
if v == runtime.GOARCH {
passes = true
break
}
}
if !passes {
return ErrUnsupportedArch(runtime.GOARCH, strings.ReplaceAll(r.Arch, "||", " OR "))
}
}
//check to see if we support the environment
//AKA.... if docker, do we support it
var envType Type
err := UnmarshalTo(server.Environment, &envType)
if err != nil {
return err
}
if envType.Type == "docker" {
d, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return ErrDockerNotSupported
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err = d.Ping(ctx)
if err != nil {
return ErrDockerNotSupported
}
} else {
//we cannot check in docker if the binary requirements are good, so we'll skip it for docker
//and check them now
for _, v := range r.Binaries {
binaries := parseRequirementRow(v)
found := true
for k, binary := range binaries {
parsed := ReplaceTokens(binary, server.DataToMap())
binaries[k] = parsed
_, err := exec.LookPath(parsed)
if err != nil {
found = false
}
}
if !found {
return ErrMissingBinary(strings.Join(binaries, " OR "))
}
}
}
return nil
}
func (s Server) DataToMap() map[string]interface{} {
var result = make(map[string]interface{})
for k, v := range s.Variables {
result[k] = v.Value
}
result["serverId"] = s.Identifier
return result
}
func parseRequirementRow(str string) []string {
if str == "" {
return []string{}
}
d := strings.Split(str, "||")
for k, v := range d {
d[k] = strings.TrimSpace(v)
}
return d
}