-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
716 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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. | ||
|
||
package log | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
|
||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc" | ||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/shared" | ||
) | ||
|
||
// LineWriter sends logs to the client. | ||
type LineWriter struct { | ||
sync.Mutex | ||
|
||
peer rpc.Peer | ||
stepUUID string | ||
num int | ||
startTime time.Time | ||
replacer *strings.Replacer | ||
} | ||
|
||
// NewLineWriter returns a new line reader. | ||
func NewLineWriter(peer rpc.Peer, stepUUID string, secret ...string) io.WriteCloser { | ||
lw := &LineWriter{ | ||
peer: peer, | ||
stepUUID: stepUUID, | ||
startTime: time.Now().UTC(), | ||
replacer: shared.NewSecretsReplacer(secret), | ||
} | ||
return lw | ||
} | ||
|
||
func (w *LineWriter) Write(p []byte) (n int, err error) { | ||
data := string(p) | ||
if w.replacer != nil { | ||
data = w.replacer.Replace(data) | ||
} | ||
log.Trace().Str("step-uuid", w.stepUUID).Msgf("grpc write line: %s", data) | ||
|
||
line := &rpc.LogEntry{ | ||
Data: []byte(strings.TrimSuffix(data, "\n")), // remove trailing newline | ||
StepUUID: w.stepUUID, | ||
Time: int64(time.Since(w.startTime).Seconds()), | ||
Type: rpc.LogEntryStdout, | ||
Line: w.num, | ||
} | ||
|
||
w.num++ | ||
|
||
if err := w.peer.Log(context.Background(), line); err != nil { | ||
return 0, err | ||
} | ||
|
||
return len(data), nil | ||
} | ||
|
||
func (w *LineWriter) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2019 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. | ||
|
||
package log_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/log" | ||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc" | ||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc/mocks" | ||
) | ||
|
||
func TestLineWriter(t *testing.T) { | ||
peer := mocks.NewPeer(t) | ||
peer.On("Log", mock.Anything, mock.Anything).Return(nil) | ||
|
||
secrets := []string{"world"} | ||
lw := log.NewLineWriter(peer, "e9ea76a5-44a1-4059-9c4a-6956c478b26d", secrets...) | ||
defer lw.Close() | ||
|
||
_, err := lw.Write([]byte("hello world\n")) | ||
assert.NoError(t, err) | ||
_, err = lw.Write([]byte("the previous line had no newline at the end")) | ||
assert.NoError(t, err) | ||
|
||
peer.AssertCalled(t, "Log", mock.Anything, &rpc.LogEntry{ | ||
StepUUID: "e9ea76a5-44a1-4059-9c4a-6956c478b26d", | ||
Time: 0, | ||
Type: rpc.LogEntryStdout, | ||
Line: 0, | ||
Data: []byte("hello ********"), | ||
}) | ||
|
||
peer.AssertCalled(t, "Log", mock.Anything, &rpc.LogEntry{ | ||
StepUUID: "e9ea76a5-44a1-4059-9c4a-6956c478b26d", | ||
Time: 0, | ||
Type: rpc.LogEntryStdout, | ||
Line: 1, | ||
Data: []byte("the previous line had no newline at the end"), | ||
}) | ||
|
||
peer.AssertExpectations(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 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. | ||
|
||
package log | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"io" | ||
) | ||
|
||
func writeChunks(dst io.WriteCloser, data []byte, size int) error { | ||
if len(data) <= size { | ||
_, err := dst.Write(data) | ||
return err | ||
} | ||
|
||
for len(data) > size { | ||
if _, err := dst.Write(data[:size]); err != nil { | ||
return err | ||
} | ||
data = data[size:] | ||
} | ||
|
||
if len(data) > 0 { | ||
_, err := dst.Write(data) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func CopyLineByLine(dst io.WriteCloser, src io.Reader, maxSize int) error { | ||
r := bufio.NewReader(src) | ||
defer dst.Close() | ||
|
||
for { | ||
// TODO: read til newline or maxSize directly | ||
line, err := r.ReadBytes('\n') | ||
if errors.Is(err, io.EOF) { | ||
return writeChunks(dst, line, maxSize) | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
err = writeChunks(dst, line, maxSize) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} |
Oops, something went wrong.