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.
|
|
|
|
|
2022-03-10 21:07:02 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-01 23:08:02 +00:00
|
|
|
"errors"
|
2022-11-06 12:36:34 +00:00
|
|
|
"fmt"
|
2022-03-10 21:07:02 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-04-02 14:47:22 +00:00
|
|
|
"path/filepath"
|
2023-10-28 11:38:47 +00:00
|
|
|
"runtime"
|
2023-10-09 07:11:08 +00:00
|
|
|
"slices"
|
2023-08-07 13:39:58 +00:00
|
|
|
"sync"
|
2022-03-10 21:07:02 +00:00
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-11-02 14:45:18 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2023-10-28 11:38:47 +00:00
|
|
|
"golang.org/x/text/encoding/unicode"
|
|
|
|
"golang.org/x/text/transform"
|
2022-11-06 12:36:34 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
2022-03-10 21:07:02 +00:00
|
|
|
)
|
|
|
|
|
2023-05-17 12:53:23 +00:00
|
|
|
type workflowState struct {
|
2023-08-07 13:39:58 +00:00
|
|
|
stepCMDs map[string]*exec.Cmd
|
|
|
|
baseDir string
|
|
|
|
homeDir string
|
|
|
|
workspaceDir string
|
|
|
|
pluginGitBinary string
|
2023-05-17 12:53:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 21:07:02 +00:00
|
|
|
type local struct {
|
2023-11-02 14:45:18 +00:00
|
|
|
tempDir string
|
2023-08-07 13:39:58 +00:00
|
|
|
workflows sync.Map
|
|
|
|
output io.ReadCloser
|
|
|
|
pluginGitBinary string
|
2023-11-02 06:58:32 +00:00
|
|
|
os, arch string
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
// New returns a new local Backend.
|
|
|
|
func New() types.Backend {
|
2023-11-02 06:58:32 +00:00
|
|
|
return &local{
|
|
|
|
os: runtime.GOOS,
|
|
|
|
arch: runtime.GOARCH,
|
|
|
|
}
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *local) Name() string {
|
|
|
|
return "local"
|
|
|
|
}
|
|
|
|
|
2023-03-19 19:24:43 +00:00
|
|
|
func (e *local) IsAvailable(context.Context) bool {
|
2022-03-10 21:07:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
func (e *local) Load(ctx context.Context) (*types.BackendInfo, error) {
|
2023-11-02 14:45:18 +00:00
|
|
|
c, ok := ctx.Value(types.CliContext).(*cli.Context)
|
|
|
|
if ok {
|
|
|
|
e.tempDir = c.String("backend-local-temp-dir")
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
e.loadClone()
|
2023-05-17 12:53:23 +00:00
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
return &types.BackendInfo{
|
2023-11-02 06:58:32 +00:00
|
|
|
Platform: e.os + "/" + e.arch,
|
2023-11-01 14:38:37 +00:00
|
|
|
}, nil
|
2023-05-17 12:53:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
// SetupWorkflow the pipeline environment.
|
2023-08-07 13:39:58 +00:00
|
|
|
func (e *local) SetupWorkflow(_ context.Context, _ *types.Config, taskUUID string) error {
|
2023-07-20 18:39:20 +00:00
|
|
|
log.Trace().Str("taskUUID", taskUUID).Msg("create workflow environment")
|
|
|
|
|
2023-11-02 14:45:18 +00:00
|
|
|
baseDir, err := os.MkdirTemp(e.tempDir, "woodpecker-local-*")
|
2023-04-19 22:56:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:53:23 +00:00
|
|
|
state := &workflowState{
|
|
|
|
stepCMDs: make(map[string]*exec.Cmd),
|
|
|
|
baseDir: baseDir,
|
|
|
|
workspaceDir: filepath.Join(baseDir, "workspace"),
|
|
|
|
homeDir: filepath.Join(baseDir, "home"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(state.homeDir, 0o700); err != nil {
|
2023-04-19 22:56:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-03-10 21:07:02 +00:00
|
|
|
|
2023-05-17 12:53:23 +00:00
|
|
|
if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
e.saveState(taskUUID, state)
|
2023-05-17 12:53:23 +00:00
|
|
|
|
2022-03-10 21:07:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
// StartStep the pipeline step.
|
|
|
|
func (e *local) StartStep(ctx context.Context, step *types.Step, taskUUID string) error {
|
|
|
|
log.Trace().Str("taskUUID", taskUUID).Msgf("start step %s", step.Name)
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
state, err := e.getState(taskUUID)
|
2023-05-17 12:53:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-10 21:07:02 +00:00
|
|
|
// Get environment variables
|
2022-10-30 23:26:49 +00:00
|
|
|
env := os.Environ()
|
2022-10-28 15:38:53 +00:00
|
|
|
for a, b := range step.Environment {
|
2022-11-06 12:36:34 +00:00
|
|
|
// append allowed env vars to command env
|
|
|
|
if !slices.Contains(notAllowedEnvVarOverwrites, a) {
|
2022-10-30 23:26:49 +00:00
|
|
|
env = append(env, a+"="+b)
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 10:54:10 +00:00
|
|
|
// Set HOME and CI_WORKSPACE
|
2023-05-17 12:53:23 +00:00
|
|
|
env = append(env, "HOME="+state.homeDir)
|
2023-10-01 12:56:49 +00:00
|
|
|
env = append(env, "USERPROFILE="+state.homeDir)
|
2023-10-23 10:54:10 +00:00
|
|
|
env = append(env, "CI_WORKSPACE="+state.workspaceDir)
|
2023-04-02 14:47:22 +00:00
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
switch step.Type {
|
|
|
|
case types.StepTypeClone:
|
|
|
|
return e.execClone(ctx, step, state, env)
|
|
|
|
case types.StepTypeCommands:
|
|
|
|
return e.execCommands(ctx, step, state, env)
|
2023-08-22 20:00:32 +00:00
|
|
|
case types.StepTypePlugin:
|
|
|
|
return e.execPlugin(ctx, step, state, env)
|
2023-08-07 13:39:58 +00:00
|
|
|
default:
|
|
|
|
return ErrUnsupportedStepType
|
|
|
|
}
|
|
|
|
}
|
2022-11-06 12:36:34 +00:00
|
|
|
|
2023-08-22 20:00:32 +00:00
|
|
|
// execCommands use step.Image as shell and run the commands in it
|
2023-08-07 13:39:58 +00:00
|
|
|
func (e *local) execCommands(ctx context.Context, step *types.Step, state *workflowState, env []string) error {
|
2023-10-28 11:38:47 +00:00
|
|
|
// Prepare commands
|
2024-01-19 04:34:02 +00:00
|
|
|
// TODO support `entrypoint` from pipeline config
|
2023-11-02 14:45:18 +00:00
|
|
|
args, err := e.genCmdByShell(step.Image, step.Commands)
|
2023-10-28 11:38:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert commands into args: %w", err)
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
// Use "image name" as run command (indicate shell)
|
2023-10-28 11:38:47 +00:00
|
|
|
cmd := exec.CommandContext(ctx, step.Image, args...)
|
2023-05-17 12:53:23 +00:00
|
|
|
cmd.Env = env
|
|
|
|
cmd.Dir = state.workspaceDir
|
2022-03-10 21:07:02 +00:00
|
|
|
|
|
|
|
// Get output and redirect Stderr to Stdout
|
2023-05-17 12:53:23 +00:00
|
|
|
e.output, _ = cmd.StdoutPipe()
|
|
|
|
cmd.Stderr = cmd.Stdout
|
2022-03-10 21:07:02 +00:00
|
|
|
|
2023-11-02 06:58:32 +00:00
|
|
|
if e.os == "windows" {
|
2023-10-28 11:38:47 +00:00
|
|
|
// we get non utf8 output from windows so just sanitize it
|
|
|
|
// TODO: remove hack
|
|
|
|
e.output = io.NopCloser(transform.NewReader(e.output, unicode.UTF8.NewDecoder().Transformer))
|
|
|
|
}
|
|
|
|
|
2024-01-09 04:43:03 +00:00
|
|
|
state.stepCMDs[step.UUID] = cmd
|
2023-05-17 12:53:23 +00:00
|
|
|
|
|
|
|
return cmd.Start()
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 20:00:32 +00:00
|
|
|
// execPlugin use step.Image as exec binary
|
|
|
|
func (e *local) execPlugin(ctx context.Context, step *types.Step, state *workflowState, env []string) error {
|
|
|
|
binary, err := exec.LookPath(step.Image)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("lookup plugin binary: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, binary)
|
|
|
|
cmd.Env = env
|
|
|
|
cmd.Dir = state.workspaceDir
|
|
|
|
|
|
|
|
// Get output and redirect Stderr to Stdout
|
|
|
|
e.output, _ = cmd.StdoutPipe()
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
|
|
|
2024-01-09 04:43:03 +00:00
|
|
|
state.stepCMDs[step.UUID] = cmd
|
2023-08-22 20:00:32 +00:00
|
|
|
|
|
|
|
return cmd.Start()
|
|
|
|
}
|
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
// WaitStep for the pipeline step to complete and returns
|
2022-03-10 21:07:02 +00:00
|
|
|
// the completion results.
|
2023-07-20 18:39:20 +00:00
|
|
|
func (e *local) WaitStep(_ context.Context, step *types.Step, taskUUID string) (*types.State, error) {
|
|
|
|
log.Trace().Str("taskUUID", taskUUID).Msgf("wait for step %s", step.Name)
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
state, err := e.getState(taskUUID)
|
2023-05-17 12:53:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-09 04:43:03 +00:00
|
|
|
cmd, ok := state.stepCMDs[step.UUID]
|
2023-05-17 12:53:23 +00:00
|
|
|
if !ok {
|
2024-01-09 04:43:03 +00:00
|
|
|
return nil, fmt.Errorf("step cmd for %s not found", step.UUID)
|
2023-05-17 12:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
2022-07-02 13:56:08 +00:00
|
|
|
ExitCode := 0
|
2023-02-01 23:08:02 +00:00
|
|
|
|
|
|
|
var execExitError *exec.ExitError
|
|
|
|
if errors.As(err, &execExitError) {
|
|
|
|
ExitCode = execExitError.ExitCode()
|
2022-10-18 01:24:12 +00:00
|
|
|
// Non-zero exit code is a pipeline failure, but not an agent error.
|
2022-07-02 13:56:08 +00:00
|
|
|
err = nil
|
|
|
|
}
|
2023-02-01 23:08:02 +00:00
|
|
|
|
2022-03-10 21:07:02 +00:00
|
|
|
return &types.State{
|
2022-07-02 13:56:08 +00:00
|
|
|
Exited: true,
|
|
|
|
ExitCode: ExitCode,
|
|
|
|
}, err
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
// TailStep the pipeline step logs.
|
|
|
|
func (e *local) TailStep(_ context.Context, step *types.Step, taskUUID string) (io.ReadCloser, error) {
|
|
|
|
log.Trace().Str("taskUUID", taskUUID).Msgf("tail logs of step %s", step.Name)
|
2022-03-10 21:07:02 +00:00
|
|
|
return e.output, nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 08:35:11 +00:00
|
|
|
func (e *local) DestroyStep(_ context.Context, _ *types.Step, _ string) error {
|
|
|
|
// WaitStep already waits for the command to finish, so there is nothing to do here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
// DestroyWorkflow the pipeline environment.
|
2023-08-07 13:39:58 +00:00
|
|
|
func (e *local) DestroyWorkflow(_ context.Context, _ *types.Config, taskUUID string) error {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Trace().Str("taskUUID", taskUUID).Msg("delete workflow environment")
|
2023-07-20 18:39:20 +00:00
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
state, err := e.getState(taskUUID)
|
2023-05-17 12:53:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(state.baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
e.deleteState(taskUUID)
|
2023-05-17 12:53:23 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
func (e *local) getState(taskUUID string) (*workflowState, error) {
|
|
|
|
state, ok := e.workflows.Load(taskUUID)
|
2023-05-17 12:53:23 +00:00
|
|
|
if !ok {
|
2023-08-07 13:39:58 +00:00
|
|
|
return nil, ErrWorkflowStateNotFound
|
2023-05-17 12:53:23 +00:00
|
|
|
}
|
2024-01-12 01:01:02 +00:00
|
|
|
|
|
|
|
s, ok := state.(*workflowState)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("could not parse state: %v", state)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2023-05-17 12:53:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
func (e *local) saveState(taskUUID string, state *workflowState) {
|
|
|
|
e.workflows.Store(taskUUID, state)
|
|
|
|
}
|
2023-05-17 12:53:23 +00:00
|
|
|
|
2023-08-07 13:39:58 +00:00
|
|
|
func (e *local) deleteState(taskUUID string) {
|
|
|
|
e.workflows.Delete(taskUUID)
|
2022-03-10 21:07:02 +00:00
|
|
|
}
|