forked from caddyserver/xcaddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
197 lines (172 loc) · 4.7 KB
/
environment.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
// Copyright 2020 Matthew Holt
//
// 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 builder
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func newEnvironment(caddyVersion string, plugins []CaddyPlugin) (*environment, error) {
// assume v2 if no semantic version is provided
caddyModulePath := defaultCaddyModulePath
if !strings.HasPrefix(caddyVersion, "v") || !strings.Contains(caddyVersion, ".") {
caddyModulePath += "/v2"
}
caddyModulePath, err := versionedModulePath(caddyModulePath, caddyVersion)
if err != nil {
return nil, err
}
// clean up any SIV-incompatible module paths real quick
for i, p := range plugins {
plugins[i].ModulePath, err = versionedModulePath(p.ModulePath, p.Version)
if err != nil {
return nil, err
}
}
// create the context for the main module template
ctx := moduleTemplateContext{
CaddyModule: caddyModulePath,
}
for _, p := range plugins {
ctx.Plugins = append(ctx.Plugins, p.ModulePath)
}
// evaluate the template for the main module
var buf bytes.Buffer
tpl, err := template.New("main").Parse(mainModuleTemplate)
if err != nil {
return nil, err
}
err = tpl.Execute(&buf, ctx)
if err != nil {
return nil, err
}
// create the folder in which the build environment will operate
tempFolder, err := newTempFolder()
if err != nil {
return nil, err
}
defer func() {
if err != nil {
err2 := os.RemoveAll(tempFolder)
if err2 != nil {
err = fmt.Errorf("%w; additionally, cleaning up folder: %v", err, err2)
}
}
}()
log.Printf("[INFO] Temporary folder: %s", tempFolder)
// write the main module file to temporary folder
mainPath := filepath.Join(tempFolder, "main.go")
log.Printf("[INFO] Writing main module: %s", mainPath)
err = ioutil.WriteFile(mainPath, buf.Bytes(), 0644)
if err != nil {
return nil, err
}
env := &environment{
caddyVersion: caddyVersion,
plugins: plugins,
caddyModulePath: caddyModulePath,
tempFolder: tempFolder,
}
// initialize the go module
log.Println("[INFO] Initializing Go module")
cmd := env.newCommand("go", "mod", "init", "caddy")
err = env.runCommand(cmd, 10*time.Second)
if err != nil {
return nil, err
}
// pin versions by populating go.mod, first for Caddy itself and then plugins
log.Println("[INFO] Pinning versions")
err = env.execGoGet(caddyModulePath, caddyVersion)
if err != nil {
return nil, err
}
for _, p := range plugins {
err = env.execGoGet(p.ModulePath, p.Version)
if err != nil {
return nil, err
}
}
log.Println("[INFO] Build environment ready")
return env, nil
}
type environment struct {
caddyVersion string
plugins []CaddyPlugin
caddyModulePath string
tempFolder string
}
// Close cleans up the build environment, including deleting
// the temporary folder from the disk.
func (env environment) Close() error {
log.Printf("[INFO] Cleaning up temporary folder: %s", env.tempFolder)
return os.RemoveAll(env.tempFolder)
}
func (env environment) newCommand(command string, args ...string) *exec.Cmd {
cmd := exec.Command(command, args...)
cmd.Dir = env.tempFolder
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func (env environment) runCommand(cmd *exec.Cmd, timeout time.Duration) error {
log.Printf("[INFO] exec (timeout=%s): %+v ", timeout, cmd)
// no timeout? this is easy; just run it
if timeout == 0 {
return cmd.Run()
}
// otherwise start it and use a timer
err := cmd.Start()
if err != nil {
return err
}
timer := time.AfterFunc(timeout, func() {
err = fmt.Errorf("timed out (builder-enforced)")
cmd.Process.Kill()
})
waitErr := cmd.Wait()
timer.Stop()
if err != nil {
return err
}
return waitErr
}
func (env environment) execGoGet(modulePath, moduleVersion string) error {
mod := modulePath + "@" + moduleVersion
cmd := env.newCommand("go", "get", "-d", "-v", mod)
return env.runCommand(cmd, 60*time.Second)
}
type moduleTemplateContext struct {
CaddyModule string
Plugins []string
}
const mainModuleTemplate = `package main
import (
caddycmd "{{.CaddyModule}}/cmd"
// plug in Caddy modules here
_ "{{.CaddyModule}}/modules/standard"
{{- range .Plugins}}
_ "{{.}}"
{{- end}}
)
func main() {
caddycmd.Main()
}
`