2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-04-06 19:32:14 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-17 07:42:42 +00:00
|
|
|
"fmt"
|
2019-04-06 19:32:14 +00:00
|
|
|
"io"
|
2021-12-13 18:51:53 +00:00
|
|
|
"os"
|
2019-04-06 19:32:14 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2019-09-14 12:21:16 +00:00
|
|
|
"github.com/drone/envsubst"
|
2024-01-12 14:24:30 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/common"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/docker"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/kubernetes"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local"
|
|
|
|
backendTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/matrix"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the exec command.
|
2021-10-27 19:03:14 +00:00
|
|
|
var Command = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "exec",
|
2022-10-18 01:24:12 +00:00
|
|
|
Usage: "execute a local pipeline",
|
2023-04-29 08:12:36 +00:00
|
|
|
ArgsUsage: "[path/to/.woodpecker.yaml]",
|
2021-12-13 18:51:53 +00:00
|
|
|
Action: run,
|
2023-12-08 08:36:53 +00:00
|
|
|
Flags: utils.MergeSlices(flags, docker.Flags, kubernetes.Flags, local.Flags),
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:51:53 +00:00
|
|
|
func run(c *cli.Context) error {
|
|
|
|
return common.RunPipelineFunc(c, execFile, execDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execDir(c *cli.Context, dir string) error {
|
|
|
|
// TODO: respect pipeline dependency
|
|
|
|
repoPath, _ := filepath.Abs(filepath.Dir(dir))
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
repoPath = convertPathForWindows(repoPath)
|
|
|
|
}
|
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if it is a regular file (not dir)
|
2023-04-29 08:12:36 +00:00
|
|
|
if info.Mode().IsRegular() && (strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml")) {
|
2021-12-13 18:51:53 +00:00
|
|
|
fmt.Println("#", info.Name())
|
|
|
|
_ = runExec(c, path, repoPath) // TODO: should we drop errors or store them and report back?
|
|
|
|
fmt.Println("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func execFile(c *cli.Context, file string) error {
|
|
|
|
repoPath, _ := filepath.Abs(filepath.Dir(file))
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
repoPath = convertPathForWindows(repoPath)
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
2021-12-13 18:51:53 +00:00
|
|
|
return runExec(c, file, repoPath)
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
|
2021-12-13 18:51:53 +00:00
|
|
|
func runExec(c *cli.Context, file, repoPath string) error {
|
2022-08-25 06:39:19 +00:00
|
|
|
dat, err := os.ReadFile(file)
|
2020-11-17 07:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
axes, err := matrix.ParseString(string(dat))
|
|
|
|
if err != nil {
|
2023-12-29 20:19:42 +00:00
|
|
|
return fmt.Errorf("parse matrix fail")
|
2020-11-17 07:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(axes) == 0 {
|
|
|
|
axes = append(axes, matrix.Axis{})
|
|
|
|
}
|
|
|
|
for _, axis := range axes {
|
2021-12-13 18:51:53 +00:00
|
|
|
err := execWithAxis(c, file, repoPath, axis)
|
2020-11-17 07:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:51:53 +00:00
|
|
|
func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error {
|
2021-09-21 03:47:08 +00:00
|
|
|
metadata := metadataFromContext(c, axis)
|
2019-04-06 19:32:14 +00:00
|
|
|
environ := metadata.Environ()
|
2021-09-24 14:29:26 +00:00
|
|
|
var secrets []compiler.Secret
|
2023-04-08 11:15:28 +00:00
|
|
|
for key, val := range metadata.Workflow.Matrix {
|
2019-04-06 19:32:14 +00:00
|
|
|
environ[key] = val
|
|
|
|
secrets = append(secrets, compiler.Secret{
|
|
|
|
Name: key,
|
|
|
|
Value: val,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:24:30 +00:00
|
|
|
pipelineEnv := make(map[string]string)
|
2019-04-06 19:32:14 +00:00
|
|
|
for _, env := range c.StringSlice("env") {
|
|
|
|
envs := strings.SplitN(env, "=", 2)
|
2024-01-12 14:24:30 +00:00
|
|
|
pipelineEnv[envs[0]] = envs[1]
|
|
|
|
if oldVar, exists := environ[envs[0]]; exists {
|
|
|
|
// override existing values, but print a warning
|
|
|
|
log.Warn().Msgf("environment variable '%s' had value '%s', but got overwritten", envs[0], oldVar)
|
2022-07-30 06:06:03 +00:00
|
|
|
}
|
|
|
|
environ[envs[0]] = envs[1]
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := envsubst.ParseFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
confstr, err := tmpl.Execute(func(name string) string {
|
|
|
|
return environ[name]
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := yaml.ParseString(confstr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure volumes for local execution
|
|
|
|
volumes := c.StringSlice("volumes")
|
|
|
|
if c.Bool("local") {
|
|
|
|
var (
|
|
|
|
workspaceBase = conf.Workspace.Base
|
|
|
|
workspacePath = conf.Workspace.Path
|
|
|
|
)
|
|
|
|
if workspaceBase == "" {
|
|
|
|
workspaceBase = c.String("workspace-base")
|
|
|
|
}
|
|
|
|
if workspacePath == "" {
|
|
|
|
workspacePath = c.String("workspace-path")
|
|
|
|
}
|
|
|
|
|
|
|
|
volumes = append(volumes, c.String("prefix")+"_default:"+workspaceBase)
|
2021-12-13 18:51:53 +00:00
|
|
|
volumes = append(volumes, repoPath+":"+path.Join(workspaceBase, workspacePath))
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lint the yaml file
|
2023-11-04 14:30:47 +00:00
|
|
|
if lerr := linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{
|
|
|
|
File: path.Base(file),
|
|
|
|
RawConfig: confstr,
|
|
|
|
Workflow: conf,
|
|
|
|
}}); lerr != nil {
|
2019-04-06 19:32:14 +00:00
|
|
|
return lerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// compiles the yaml file
|
2022-10-05 23:49:23 +00:00
|
|
|
compiled, err := compiler.New(
|
2019-04-06 19:32:14 +00:00
|
|
|
compiler.WithEscalated(
|
|
|
|
c.StringSlice("privileged")...,
|
|
|
|
),
|
|
|
|
compiler.WithVolumes(volumes...),
|
|
|
|
compiler.WithWorkspace(
|
|
|
|
c.String("workspace-base"),
|
|
|
|
c.String("workspace-path"),
|
|
|
|
),
|
|
|
|
compiler.WithNetworks(
|
|
|
|
c.StringSlice("network")...,
|
|
|
|
),
|
|
|
|
compiler.WithPrefix(
|
|
|
|
c.String("prefix"),
|
|
|
|
),
|
2023-08-07 19:13:26 +00:00
|
|
|
compiler.WithProxy(compiler.ProxyOptions{
|
|
|
|
NoProxy: c.String("backend-no-proxy"),
|
|
|
|
HTTPProxy: c.String("backend-http-proxy"),
|
|
|
|
HTTPSProxy: c.String("backend-https-proxy"),
|
|
|
|
}),
|
2019-04-06 19:32:14 +00:00
|
|
|
compiler.WithLocal(
|
|
|
|
c.Bool("local"),
|
|
|
|
),
|
|
|
|
compiler.WithNetrc(
|
|
|
|
c.String("netrc-username"),
|
|
|
|
c.String("netrc-password"),
|
|
|
|
c.String("netrc-machine"),
|
|
|
|
),
|
|
|
|
compiler.WithMetadata(metadata),
|
|
|
|
compiler.WithSecret(secrets...),
|
2024-01-12 14:24:30 +00:00
|
|
|
compiler.WithEnviron(pipelineEnv),
|
2019-04-06 19:32:14 +00:00
|
|
|
).Compile(conf)
|
2022-10-05 23:49:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-26 02:02:42 +00:00
|
|
|
|
2023-07-08 20:17:09 +00:00
|
|
|
backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c)
|
2024-02-08 23:04:43 +00:00
|
|
|
backends := []backendTypes.Backend{
|
|
|
|
kubernetes.New(),
|
2024-02-08 15:33:22 +00:00
|
|
|
docker.New(),
|
|
|
|
local.New(),
|
2024-02-08 23:04:43 +00:00
|
|
|
}
|
|
|
|
backendEngine, err := backend.FindBackend(backendCtx, backends, c.String("backend-engine"))
|
2022-02-26 02:02:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
if _, err = backendEngine.Load(backendCtx); err != nil {
|
2019-04-06 19:32:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), c.Duration("timeout"))
|
|
|
|
defer cancel()
|
2022-02-28 08:27:31 +00:00
|
|
|
ctx = utils.WithContextSigtermCallback(ctx, func() {
|
2023-04-08 11:15:28 +00:00
|
|
|
fmt.Println("ctrl+c received, terminating process")
|
2022-02-28 08:27:31 +00:00
|
|
|
})
|
2019-04-06 19:32:14 +00:00
|
|
|
|
|
|
|
return pipeline.New(compiled,
|
|
|
|
pipeline.WithContext(ctx),
|
|
|
|
pipeline.WithTracer(pipeline.DefaultTracer),
|
|
|
|
pipeline.WithLogger(defaultLogger),
|
2023-12-14 18:20:47 +00:00
|
|
|
pipeline.WithBackend(backendEngine),
|
2022-06-15 16:11:20 +00:00
|
|
|
pipeline.WithDescription(map[string]string{
|
|
|
|
"CLI": "exec",
|
|
|
|
}),
|
2022-11-23 14:35:24 +00:00
|
|
|
).Run(c.Context)
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func convertPathForWindows(path string) string {
|
|
|
|
base := filepath.VolumeName(path)
|
|
|
|
if len(base) == 2 {
|
|
|
|
path = strings.TrimPrefix(path, base)
|
|
|
|
base = strings.ToLower(strings.TrimSuffix(base, ":"))
|
|
|
|
return "/" + base + filepath.ToSlash(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.ToSlash(path)
|
|
|
|
}
|
|
|
|
|
2024-01-14 09:54:02 +00:00
|
|
|
var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.Reader) error {
|
2024-01-09 14:22:59 +00:00
|
|
|
logStream := NewLineWriter(step.Name, step.UUID)
|
2024-01-14 09:54:02 +00:00
|
|
|
_, err := io.Copy(logStream, rc)
|
2021-11-23 14:36:52 +00:00
|
|
|
return err
|
2019-04-06 19:32:14 +00:00
|
|
|
})
|