2020-03-21 20:31:29 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-04-02 15:35:27 +01:00
|
|
|
package xcaddy
|
2020-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-04-13 19:22:23 +01:00
|
|
|
"context"
|
2020-03-21 20:31:29 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-03-21 22:27:26 +00:00
|
|
|
"strings"
|
2020-03-21 20:31:29 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-13 19:22:23 +01:00
|
|
|
func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
|
2020-04-04 16:56:37 +01:00
|
|
|
// assume Caddy v2 if no semantic version is provided
|
2020-03-21 22:27:26 +00:00
|
|
|
caddyModulePath := defaultCaddyModulePath
|
2020-04-04 16:56:37 +01:00
|
|
|
if !strings.HasPrefix(b.CaddyVersion, "v") || !strings.Contains(b.CaddyVersion, ".") {
|
2020-03-21 22:27:26 +00:00
|
|
|
caddyModulePath += "/v2"
|
|
|
|
}
|
2020-04-04 16:56:37 +01:00
|
|
|
caddyModulePath, err := versionedModulePath(caddyModulePath, b.CaddyVersion)
|
2020-03-21 20:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up any SIV-incompatible module paths real quick
|
2020-04-04 16:56:37 +01:00
|
|
|
for i, p := range b.Plugins {
|
2020-07-16 00:45:32 +01:00
|
|
|
b.Plugins[i].PackagePath, err = versionedModulePath(p.PackagePath, p.Version)
|
2020-03-21 20:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the context for the main module template
|
2020-04-13 19:22:23 +01:00
|
|
|
tplCtx := goModTemplateContext{
|
2020-03-21 20:31:29 +00:00
|
|
|
CaddyModule: caddyModulePath,
|
|
|
|
}
|
2020-04-04 16:56:37 +01:00
|
|
|
for _, p := range b.Plugins {
|
2020-07-16 00:45:32 +01:00
|
|
|
tplCtx.Plugins = append(tplCtx.Plugins, p.PackagePath)
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate the template for the main module
|
|
|
|
var buf bytes.Buffer
|
|
|
|
tpl, err := template.New("main").Parse(mainModuleTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-13 19:22:23 +01:00
|
|
|
err = tpl.Execute(&buf, tplCtx)
|
2020-03-21 20:31:29 +00:00
|
|
|
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{
|
2020-04-04 16:56:37 +01:00
|
|
|
caddyVersion: b.CaddyVersion,
|
|
|
|
plugins: b.Plugins,
|
2020-03-21 20:31:29 +00:00
|
|
|
caddyModulePath: caddyModulePath,
|
|
|
|
tempFolder: tempFolder,
|
2020-05-07 17:21:12 +01:00
|
|
|
timeoutGoGet: b.TimeoutGet,
|
2020-07-16 00:44:06 +01:00
|
|
|
skipCleanup: b.SkipCleanup,
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the go module
|
|
|
|
log.Println("[INFO] Initializing Go module")
|
|
|
|
cmd := env.newCommand("go", "mod", "init", "caddy")
|
2020-04-13 19:22:23 +01:00
|
|
|
err = env.runCommand(ctx, cmd, 10*time.Second)
|
2020-03-21 20:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-25 06:01:44 +00:00
|
|
|
// specify module replacements before pinning versions
|
2020-04-13 19:22:23 +01:00
|
|
|
replaced := make(map[string]string)
|
|
|
|
for _, r := range b.Replacements {
|
2020-08-10 19:27:38 +01:00
|
|
|
log.Printf("[INFO] Replace %s => %s", r.Old.String(), r.New.String())
|
2020-04-04 16:56:37 +01:00
|
|
|
cmd := env.newCommand("go", "mod", "edit",
|
2020-08-10 19:27:38 +01:00
|
|
|
"-replace", fmt.Sprintf("%s=%s", r.Old.Param(), r.New.Param()))
|
2020-04-13 19:22:23 +01:00
|
|
|
err := env.runCommand(ctx, cmd, 10*time.Second)
|
2020-04-04 16:56:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-25 06:01:44 +00:00
|
|
|
}
|
2020-08-10 19:27:38 +01:00
|
|
|
replaced[r.Old.String()] = r.New.String()
|
2020-04-13 19:22:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for early abort
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
2020-03-25 06:01:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:31:29 +00:00
|
|
|
// pin versions by populating go.mod, first for Caddy itself and then plugins
|
|
|
|
log.Println("[INFO] Pinning versions")
|
2020-04-22 23:29:57 +01:00
|
|
|
err = env.execGoGet(ctx, caddyModulePath, env.caddyVersion)
|
2020-03-21 20:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-16 22:08:00 +01:00
|
|
|
nextPlugin:
|
2020-04-04 16:56:37 +01:00
|
|
|
for _, p := range b.Plugins {
|
2020-06-16 22:08:00 +01:00
|
|
|
// if module is locally available, do not "go get" it;
|
|
|
|
// also note that we iterate and check prefixes, because
|
|
|
|
// a plugin package may be a subfolder of a module, i.e.
|
|
|
|
// foo/a/plugin is within module foo/a.
|
|
|
|
for repl := range replaced {
|
2020-07-16 00:45:32 +01:00
|
|
|
if strings.HasPrefix(p.PackagePath, repl) {
|
2020-06-16 22:08:00 +01:00
|
|
|
continue nextPlugin
|
|
|
|
}
|
2020-03-25 06:01:44 +00:00
|
|
|
}
|
2020-07-16 00:45:32 +01:00
|
|
|
err = env.execGoGet(ctx, p.PackagePath, p.Version)
|
2020-03-21 20:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-13 19:22:23 +01:00
|
|
|
// check for early abort
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("[INFO] Build environment ready")
|
|
|
|
|
|
|
|
return env, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type environment struct {
|
|
|
|
caddyVersion string
|
2020-03-25 06:01:44 +00:00
|
|
|
plugins []Dependency
|
2020-03-21 20:31:29 +00:00
|
|
|
caddyModulePath string
|
|
|
|
tempFolder string
|
2020-05-07 17:21:12 +01:00
|
|
|
timeoutGoGet time.Duration
|
2020-07-16 00:44:06 +01:00
|
|
|
skipCleanup bool
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up the build environment, including deleting
|
|
|
|
// the temporary folder from the disk.
|
|
|
|
func (env environment) Close() error {
|
2020-07-16 00:44:06 +01:00
|
|
|
if env.skipCleanup {
|
|
|
|
log.Printf("[INFO] Skipping cleanup as requested; leaving folder intact: %s", env.tempFolder)
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-21 20:31:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:22:23 +01:00
|
|
|
func (env environment) runCommand(ctx context.Context, cmd *exec.Cmd, timeout time.Duration) error {
|
2020-03-21 20:31:29 +00:00
|
|
|
log.Printf("[INFO] exec (timeout=%s): %+v ", timeout, cmd)
|
|
|
|
|
2020-04-13 19:22:23 +01:00
|
|
|
if timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 19:22:23 +01:00
|
|
|
// start the command; if it fails to start, report error immediately
|
2020-03-21 20:31:29 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-13 19:22:23 +01:00
|
|
|
|
|
|
|
// wait for the command in a goroutine; the reason for this is
|
|
|
|
// very subtle: if, in our select, we do `case cmdErr := <-cmd.Wait()`,
|
|
|
|
// then that case would be chosen immediately, because cmd.Wait() is
|
|
|
|
// immediately available (even though it blocks for potentially a long
|
|
|
|
// time, it can be evaluated immediately). So we have to remove that
|
|
|
|
// evaluation from the `case` statement.
|
|
|
|
cmdErrChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
cmdErrChan <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// unblock either when the command finishes, or when the done
|
|
|
|
// channel is closed -- whichever comes first
|
|
|
|
select {
|
|
|
|
case cmdErr := <-cmdErrChan:
|
|
|
|
// process ended; report any error immediately
|
|
|
|
return cmdErr
|
|
|
|
case <-ctx.Done():
|
|
|
|
// context was canceled, either due to timeout or
|
|
|
|
// maybe a signal from higher up canceled the parent
|
|
|
|
// context; presumably, the OS also sent the signal
|
|
|
|
// to the child process, so wait for it to die
|
|
|
|
select {
|
|
|
|
case <-time.After(15 * time.Second):
|
|
|
|
cmd.Process.Kill()
|
|
|
|
case <-cmdErrChan:
|
|
|
|
}
|
|
|
|
return ctx.Err()
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:22:23 +01:00
|
|
|
func (env environment) execGoGet(ctx context.Context, modulePath, moduleVersion string) error {
|
2020-04-13 19:44:37 +01:00
|
|
|
mod := modulePath
|
|
|
|
if moduleVersion != "" {
|
|
|
|
mod += "@" + moduleVersion
|
|
|
|
}
|
2020-03-21 20:31:29 +00:00
|
|
|
cmd := env.newCommand("go", "get", "-d", "-v", mod)
|
2020-05-07 17:21:12 +01:00
|
|
|
return env.runCommand(ctx, cmd, env.timeoutGoGet)
|
2020-03-21 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 19:22:23 +01:00
|
|
|
type goModTemplateContext struct {
|
2020-03-21 20:31:29 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
`
|