2023-03-19 21:42:21 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
// Copyright 2011 Drone.IO Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-03-12 08:46:59 +00:00
|
|
|
"strings"
|
2017-03-05 07:56:08 +00:00
|
|
|
"time"
|
2021-11-23 14:36:52 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-01-08 19:39:52 +00:00
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/shared"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Identifies the type of line in the logs.
|
|
|
|
const (
|
|
|
|
LineStdout int = iota
|
|
|
|
LineStderr
|
|
|
|
LineExitCode
|
|
|
|
LineMetadata
|
|
|
|
LineProgress
|
|
|
|
)
|
|
|
|
|
|
|
|
// Line is a line of console output.
|
|
|
|
type Line struct {
|
2022-10-28 15:38:53 +00:00
|
|
|
Step string `json:"step,omitempty"`
|
2017-03-05 07:56:08 +00:00
|
|
|
Time int64 `json:"time,omitempty"`
|
|
|
|
Type int `json:"type,omitempty"`
|
2021-10-09 00:43:44 +00:00
|
|
|
Pos int `json:"pos,omitempty"`
|
2017-03-05 07:56:08 +00:00
|
|
|
Out string `json:"out,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) String() string {
|
|
|
|
switch l.Type {
|
|
|
|
case LineExitCode:
|
2022-10-28 15:38:53 +00:00
|
|
|
return fmt.Sprintf("[%s] exit code %s", l.Step, l.Out)
|
2017-03-05 07:56:08 +00:00
|
|
|
default:
|
2022-10-28 15:38:53 +00:00
|
|
|
return fmt.Sprintf("[%s:L%v:%vs] %s", l.Step, l.Pos, l.Time, l.Out)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineWriter sends logs to the client.
|
|
|
|
type LineWriter struct {
|
2017-04-01 11:17:04 +00:00
|
|
|
peer Peer
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
num int
|
|
|
|
now time.Time
|
|
|
|
rep *strings.Replacer
|
|
|
|
lines []*Line
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLineWriter returns a new line reader.
|
2017-03-12 08:46:59 +00:00
|
|
|
func NewLineWriter(peer Peer, id, name string, secret ...string) *LineWriter {
|
2022-01-08 19:39:52 +00:00
|
|
|
return &LineWriter{
|
|
|
|
peer: peer,
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
now: time.Now().UTC(),
|
|
|
|
rep: shared.NewSecretsReplacer(secret),
|
|
|
|
lines: nil,
|
2017-03-12 08:46:59 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LineWriter) Write(p []byte) (n int, err error) {
|
2017-03-12 08:46:59 +00:00
|
|
|
out := string(p)
|
|
|
|
if w.rep != nil {
|
|
|
|
out = w.rep.Replace(out)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2022-01-08 19:39:52 +00:00
|
|
|
log.Trace().Str("name", w.name).Str("ID", w.id).Msgf("grpc write line: %s", out)
|
2017-03-12 08:46:59 +00:00
|
|
|
|
|
|
|
line := &Line{
|
|
|
|
Out: out,
|
2022-10-28 15:38:53 +00:00
|
|
|
Step: w.name,
|
2017-03-12 08:46:59 +00:00
|
|
|
Pos: w.num,
|
|
|
|
Time: int64(time.Since(w.now).Seconds()),
|
|
|
|
Type: LineStdout,
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := w.peer.Log(context.Background(), w.id, line); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("fail to write pipeline log to peer '%s'", w.id)
|
|
|
|
}
|
2017-03-12 08:46:59 +00:00
|
|
|
w.num++
|
|
|
|
|
|
|
|
// for _, part := range bytes.Split(p, []byte{'\n'}) {
|
|
|
|
// line := &Line{
|
|
|
|
// Out: string(part),
|
2022-10-28 15:38:53 +00:00
|
|
|
// Step: w.name,
|
2017-03-12 08:46:59 +00:00
|
|
|
// Pos: w.num,
|
|
|
|
// Time: int64(time.Since(w.now).Seconds()),
|
|
|
|
// Type: LineStdout,
|
|
|
|
// }
|
|
|
|
// w.peer.Log(context.Background(), w.id, line)
|
|
|
|
// w.num++
|
|
|
|
// }
|
2017-04-01 11:17:04 +00:00
|
|
|
w.lines = append(w.lines, line)
|
2017-03-05 07:56:08 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
|
|
|
|
// Lines returns the line history
|
|
|
|
func (w *LineWriter) Lines() []*Line {
|
|
|
|
return w.lines
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear clears the line history
|
|
|
|
func (w *LineWriter) Clear() {
|
|
|
|
w.lines = w.lines[:0]
|
|
|
|
}
|