ansibleplaybooks_output_recorder.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "yunion.io/x/log"
  17. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  18. )
  19. const (
  20. OutputMaxBytes = 64*1024*1024 - 1
  21. PlaybookMaxBytes = 64*1024 - 1
  22. )
  23. type ansiblePlaybookOutputRecorder interface {
  24. db.IModel
  25. getMaxOutputLength() int
  26. getOutput() string
  27. setOutput(string)
  28. }
  29. type ansiblePlaybookOutputWriter struct {
  30. rec ansiblePlaybookOutputRecorder
  31. }
  32. func (w *ansiblePlaybookOutputWriter) Write(p []byte) (n int, err error) {
  33. rec := w.rec
  34. _, err = db.Update(rec, func() error {
  35. cur := rec.getOutput()
  36. i := len(p) + len(cur) - rec.getMaxOutputLength()
  37. if i > 0 {
  38. // truncate to preserve the tail
  39. rec.setOutput(cur[i:] + string(p))
  40. } else {
  41. rec.setOutput(cur + string(p))
  42. }
  43. return nil
  44. })
  45. if err != nil {
  46. log.Errorf("%s %s(%s): record output: %v", rec.Keyword(), rec.GetName(), rec.GetId(), err)
  47. return 0, err
  48. }
  49. return len(p), nil
  50. }