-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathrun.go
214 lines (179 loc) · 5.43 KB
/
run.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package pkgcontext
import (
"context"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"sync/atomic"
"syscall"
"github.com/pkg/errors"
"github.com/Southclaws/sampctl/build"
"github.com/Southclaws/sampctl/print"
"github.com/Southclaws/sampctl/run"
"github.com/Southclaws/sampctl/runtime"
"github.com/Southclaws/sampctl/util"
)
// Run will create a temporary server runtime and run the package output AMX as a gamemode using the
// runtime configuration in the package info.
func (pcx *PackageContext) Run(ctx context.Context, output io.Writer, input io.Reader) (err error) {
err = pcx.RunPrepare(ctx)
if err != nil {
return errors.Wrap(err, "failed to prepare package for running")
}
err = runtime.Run(ctx, pcx.ActualRuntime, pcx.CacheDir, true, false, output, input)
if err != nil {
return errors.Wrap(err, "failed to run package")
}
return
}
// RunWatch runs the Run code on file changes
func (pcx *PackageContext) RunWatch(ctx context.Context) (err error) {
err = pcx.RunPrepare(ctx)
if err != nil {
err = errors.Wrap(err, "failed to prepare")
return
}
var (
errorCh = make(chan error)
signals = make(chan os.Signal, 1)
trigger = make(chan build.Problems)
running atomic.Value
ctxInner, cancel = context.WithCancel(ctx)
)
defer cancel()
running.Store(false)
go func() {
errorCh <- pcx.BuildWatch(ctx, pcx.BuildName, pcx.ForceEnsure, pcx.BuildFile, pcx.Relative, trigger)
}()
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
print.Verb(pcx.Package, "starting run watcher")
loop:
for {
select {
case sig := <-signals:
fmt.Println("") // insert newline after the ^C
print.Info("signal received", sig, "stopping run watcher...")
break loop
case err = <-errorCh:
cancel()
break loop
case problems := <-trigger:
print.Info("build finished")
for _, problem := range problems {
if problem.Severity > build.ProblemWarning {
continue loop
}
}
if running.Load().(bool) {
fmt.Println("watch-run: killing existing runtime process")
cancel()
fmt.Println("watch-run: killed existing runtime process")
// re-create context and canceler
ctxInner, cancel = context.WithCancel(ctx)
defer cancel()
}
err = runtime.CopyFileToRuntime(pcx.CacheDir, pcx.ActualRuntime.Version, util.FullPath(pcx.Package.Output))
if err != nil {
err = errors.Wrap(err, "failed to copy amx file to temporary runtime directory")
print.Erro(err)
}
fmt.Println("watch-run: executing package code")
go func() {
running.Store(true)
err = runtime.Run(ctxInner, pcx.ActualRuntime, pcx.CacheDir, true, false, os.Stdout, os.Stdin)
running.Store(false)
if err != nil {
print.Erro(err)
}
fmt.Println("watch-run: finished")
}()
}
}
print.Info("finished running run watcher")
return err
}
// RunPrepare prepares the context directory for executing the server. It
// generates a server.cfg and ensures plugins.
func (pcx *PackageContext) RunPrepare(ctx context.Context) (err error) {
var (
filename = filepath.Join(pcx.Package.LocalPath, pcx.Package.Output)
problems build.Problems
canRun = true
)
if !util.Exists(filename) || pcx.ForceBuild {
problems, _, err = pcx.Build(ctx, pcx.BuildName, pcx.ForceEnsure, false, pcx.Relative, pcx.BuildFile)
if err != nil {
return
}
for _, problem := range problems {
if problem.Severity > build.ProblemWarning {
canRun = false
break
}
}
}
if !canRun {
err = errors.New("build failed, can not run")
return
}
print.Verb("getting runtime config")
pcx.ActualRuntime, err = pcx.Package.GetRuntimeConfig(pcx.Runtime)
if err != nil {
return
}
pcx.ActualRuntime.Gamemodes = []string{strings.TrimSuffix(filepath.Base(pcx.Package.Output), ".amx")}
pcx.ActualRuntime.AppVersion = pcx.AppVersion
pcx.ActualRuntime.Format = pcx.Package.Format
if pcx.Container {
pcx.ActualRuntime.Container = &run.ContainerConfig{MountCache: true}
pcx.ActualRuntime.Platform = "linux"
} else {
pcx.ActualRuntime.Platform = pcx.Platform
}
if !pcx.Package.Local {
print.Verb(pcx.Package, "package is not local, preparing temporary runtime")
scriptfiles := filepath.Join(pcx.Package.LocalPath, "scriptfiles")
if !util.Exists(scriptfiles) {
scriptfiles = ""
}
err = runtime.PrepareRuntimeDirectory(
pcx.CacheDir,
pcx.ActualRuntime.Version,
pcx.ActualRuntime.Platform,
scriptfiles)
if err != nil {
err = errors.Wrap(err, "failed to prepare temporary runtime area")
return
}
err = runtime.CopyFileToRuntime(pcx.CacheDir, pcx.ActualRuntime.Version, filename)
if err != nil {
err = errors.Wrap(err, "failed to copy amx file to temporary runtime directory")
return
}
pcx.ActualRuntime.WorkingDir = runtime.GetRuntimePath(pcx.CacheDir, pcx.ActualRuntime.Version)
} else {
print.Verb(pcx.Package, "package is local, using working directory")
pcx.ActualRuntime.WorkingDir = pcx.Package.LocalPath
pcx.ActualRuntime.Format = pcx.Package.Format
err = pcx.ActualRuntime.Validate()
if err != nil {
return
}
}
print.Verb(pcx.Package, "gathering plugins pre-run")
pcx.ActualRuntime.PluginDeps, err = pcx.GatherPlugins()
if err != nil {
err = errors.Wrap(err, "failed to gather plugins")
return
}
print.Verb(pcx.Package, "ensuring runtime pre-run")
err = runtime.Ensure(ctx, pcx.GitHub, &pcx.ActualRuntime, pcx.NoCache)
if err != nil {
err = errors.Wrap(err, "failed to ensure runtime")
return
}
return nil
}