executor.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 procutils
  15. import (
  16. "context"
  17. "io"
  18. "os/exec"
  19. "syscall"
  20. "yunion.io/x/executor/client"
  21. )
  22. var (
  23. execInstance Executor
  24. localExecutor = new(defaultExecutor)
  25. _remoteExecutor = new(remoteExecutor)
  26. )
  27. func init() {
  28. execInstance = localExecutor
  29. }
  30. func SetRemoteExecutor() {
  31. execInstance = _remoteExecutor
  32. }
  33. type Cmd interface {
  34. StdinPipe() (io.WriteCloser, error)
  35. StdoutPipe() (io.ReadCloser, error)
  36. StderrPipe() (io.ReadCloser, error)
  37. CombinedOutput() ([]byte, error)
  38. Start() error
  39. Wait() error
  40. Run() error
  41. Kill() error
  42. SetEnv([]string)
  43. }
  44. type Executor interface {
  45. CommandContext(ctx context.Context, name string, args ...string) Cmd
  46. Command(name string, args ...string) Cmd
  47. GetExitStatus(err error) (int, bool)
  48. }
  49. type defaultCmd struct {
  50. *exec.Cmd
  51. }
  52. func (c *defaultCmd) Kill() error {
  53. return c.Process.Kill()
  54. }
  55. func (c *defaultCmd) SetEnv(envs []string) {
  56. c.Env = append(c.Env, envs...)
  57. }
  58. type defaultExecutor struct{}
  59. func (e *defaultExecutor) Command(name string, args ...string) Cmd {
  60. cmd := exec.Command(name, args...)
  61. cmdSetSid(cmd)
  62. cmdSetEnv(cmd)
  63. return &defaultCmd{cmd}
  64. }
  65. func (e *defaultExecutor) CommandContext(ctx context.Context, name string, args ...string) Cmd {
  66. cmd := exec.CommandContext(ctx, name, args...)
  67. cmdSetSid(cmd)
  68. cmdSetEnv(cmd)
  69. return &defaultCmd{cmd}
  70. }
  71. func (e *defaultExecutor) GetExitStatus(err error) (int, bool) {
  72. if exiterr, ok := err.(*exec.ExitError); ok {
  73. ws := exiterr.Sys().(syscall.WaitStatus)
  74. return ws.ExitStatus(), true
  75. } else {
  76. return 0, false
  77. }
  78. }
  79. type remoteCmd struct {
  80. *client.Cmd
  81. }
  82. func (c *remoteCmd) SetEnv(envs []string) {
  83. c.Env = append(c.Env, envs...)
  84. }
  85. type remoteExecutor struct{}
  86. func (e *remoteExecutor) Command(name string, args ...string) Cmd {
  87. cmd := client.Command(name, args...)
  88. remoteCmdSetEnv(cmd)
  89. return &remoteCmd{cmd}
  90. }
  91. func (e *remoteExecutor) CommandContext(ctx context.Context, name string, args ...string) Cmd {
  92. cmd := client.CommandContext(ctx, name, args...)
  93. remoteCmdSetEnv(cmd)
  94. return &remoteCmd{cmd}
  95. }
  96. func (e *remoteExecutor) GetExitStatus(err error) (int, bool) {
  97. if exiterr, ok := err.(*client.ExitError); ok {
  98. ws := exiterr.Sys().(syscall.WaitStatus)
  99. return ws.ExitStatus(), true
  100. } else {
  101. return 0, false
  102. }
  103. }