session.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ansiblev2
  15. import (
  16. "context"
  17. "io"
  18. )
  19. type Session struct {
  20. PlaybookSessionBase
  21. playbook string
  22. requirements string
  23. files map[string][]byte
  24. }
  25. func NewSession() *Session {
  26. sess := &Session{
  27. PlaybookSessionBase: NewPlaybookSessionBase(),
  28. files: map[string][]byte{},
  29. }
  30. return sess
  31. }
  32. func (sess *Session) PrivateKey(s string) *Session {
  33. sess.privateKey = s
  34. return sess
  35. }
  36. func (sess *Session) Playbook(s string) *Session {
  37. sess.playbook = s
  38. return sess
  39. }
  40. func (sess *Session) Inventory(s string) *Session {
  41. sess.inventory = s
  42. return sess
  43. }
  44. func (sess *Session) Requirements(s string) *Session {
  45. sess.requirements = s
  46. return sess
  47. }
  48. func (sess *Session) AddFile(path string, data []byte) *Session {
  49. sess.files[path] = data
  50. return sess
  51. }
  52. func (sess *Session) RolePublic(public bool) *Session {
  53. sess.rolePublic = public
  54. return sess
  55. }
  56. func (sess *Session) Timeout(timeout int) *Session {
  57. sess.timeout = timeout
  58. return sess
  59. }
  60. func (sess *Session) RemoveFile(path string) []byte {
  61. data := sess.files[path]
  62. delete(sess.files, path)
  63. return data
  64. }
  65. func (sess *Session) Files(files map[string][]byte) *Session {
  66. sess.files = files
  67. return sess
  68. }
  69. func (sess *Session) OutputWriter(w io.Writer) *Session {
  70. sess.outputWriter = w
  71. return sess
  72. }
  73. func (sess *Session) KeepTmpdir(keep bool) *Session {
  74. sess.keepTmpdir = keep
  75. return sess
  76. }
  77. func (sess *Session) GetPlaybook() string {
  78. return sess.playbook
  79. }
  80. func (sess *Session) GetRequirements() string {
  81. return sess.requirements
  82. }
  83. func (sess *Session) GetFile() map[string][]byte {
  84. return sess.files
  85. }
  86. func (sess *Session) Run(ctx context.Context) (err error) {
  87. return runnable{sess}.Run(ctx)
  88. }