exec.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /*
  15. Copyright 2015 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package exec
  27. import (
  28. "bytes"
  29. "fmt"
  30. "yunion.io/x/log"
  31. "yunion.io/x/pkg/errors"
  32. "yunion.io/x/onecloud/pkg/util/exec"
  33. "yunion.io/x/onecloud/pkg/util/ioutils"
  34. "yunion.io/x/onecloud/pkg/util/probe"
  35. )
  36. const (
  37. maxReadLength = 10 * 1 << 10 // 10KB
  38. )
  39. // Prober is an interface defining the Probe object for container readiness/liveness checks.
  40. type Prober interface {
  41. Probe(e exec.Cmd, info string) (probe.Result, string, error)
  42. }
  43. // New creates a Prober.
  44. func New() Prober {
  45. return execProber{}
  46. }
  47. type execProber struct{}
  48. // Probe executes a command to check the liveness/readiness of container
  49. // from executing a command. Returns the Result status, command output, and
  50. // errors if any.
  51. func (pr execProber) Probe(e exec.Cmd, info string) (probe.Result, string, error) {
  52. var dataBuffer bytes.Buffer
  53. writer := ioutils.LimitWriter(&dataBuffer, maxReadLength)
  54. e.SetStderr(writer)
  55. e.SetStdout(writer)
  56. err := e.Start()
  57. if err == nil {
  58. err = e.Wait()
  59. }
  60. data := dataBuffer.Bytes()
  61. log.Debugf("Exec probe response: %q, error: %v", string(data), err)
  62. if err != nil {
  63. exit, ok := errors.Cause(err).(exec.ExitError)
  64. if ok {
  65. if exit.ExitStatus() == 0 {
  66. return probe.Success, string(data), nil
  67. }
  68. return probe.Failure, fmt.Sprintf("%s, %s: %s", info, exit, data), nil
  69. }
  70. timeoutErr, ok := err.(*TimeoutError)
  71. if ok {
  72. log.Warningf("Exec probe timed out after %s", timeoutErr.Timeout())
  73. return probe.Failure, string(data), nil
  74. }
  75. return probe.Unknown, "", err
  76. }
  77. return probe.Success, string(data), nil
  78. }