procutils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "strings"
  19. "time"
  20. "yunion.io/x/log"
  21. )
  22. var (
  23. Timeout = 3 * time.Second
  24. )
  25. type Command struct {
  26. path string
  27. args []string
  28. cmd Cmd
  29. remoteCmd bool
  30. }
  31. func NewCommand(name string, args ...string) *Command {
  32. return &Command{
  33. path: name,
  34. args: args,
  35. cmd: localExecutor.Command(name, args...),
  36. }
  37. }
  38. func NewCommandContext(ctx context.Context, name string, args ...string) *Command {
  39. return &Command{
  40. path: name,
  41. args: args,
  42. cmd: localExecutor.CommandContext(ctx, name, args...),
  43. }
  44. }
  45. // exec remote command as far as possible
  46. func NewRemoteCommandAsFarAsPossible(name string, args ...string) *Command {
  47. return &Command{
  48. path: name,
  49. args: args,
  50. cmd: execInstance.Command(name, args...),
  51. remoteCmd: true,
  52. }
  53. }
  54. func NewRemoteCommandContextAsFarAsPossible(ctx context.Context, name string, args ...string) *Command {
  55. return &Command{
  56. path: name,
  57. args: args,
  58. cmd: execInstance.CommandContext(ctx, name, args...),
  59. remoteCmd: true,
  60. }
  61. }
  62. func (c *Command) StdinPipe() (io.WriteCloser, error) {
  63. return c.cmd.StdinPipe()
  64. }
  65. func (c *Command) StdoutPipe() (io.ReadCloser, error) {
  66. return c.cmd.StdoutPipe()
  67. }
  68. func (c *Command) StderrPipe() (io.ReadCloser, error) {
  69. return c.cmd.StderrPipe()
  70. }
  71. func (c *Command) Run() error {
  72. log.Debugf("Exec command: %s %v", c.path, c.args)
  73. err := c.cmd.Run()
  74. if err != nil {
  75. log.Debugf("Execute command %q , error: %v", c, err)
  76. }
  77. return err
  78. }
  79. func (c *Command) Output() ([]byte, error) {
  80. log.Debugf("Exec command: %s %v", c.path, c.args)
  81. output, err := c.cmd.CombinedOutput()
  82. if err != nil {
  83. log.Debugf("Execute command %q , error: %v , output: %s", c, err, string(output))
  84. }
  85. return output, err
  86. }
  87. func (c *Command) Start() error {
  88. log.Debugf("Exec command: %s %v", c.path, c.args)
  89. return c.cmd.Start()
  90. }
  91. func (c *Command) SetEnv(envs []string) {
  92. c.cmd.SetEnv(envs)
  93. }
  94. func (c *Command) Wait() error {
  95. return c.cmd.Wait()
  96. }
  97. func (c *Command) String() string {
  98. ss := []string{c.path}
  99. ss = append(ss, c.args...)
  100. return strings.Join(ss, " ")
  101. }
  102. func (c *Command) Kill() error {
  103. return c.cmd.Kill()
  104. }
  105. func (c *Command) GetExitStatus(err error) (int, bool) {
  106. if c.remoteCmd {
  107. return _remoteExecutor.GetExitStatus(err)
  108. } else {
  109. return localExecutor.GetExitStatus(err)
  110. }
  111. }