@@ -20,8 +20,10 @@ import (
20
20
"io/ioutil"
21
21
"os"
22
22
"path/filepath"
23
+ "strings"
23
24
24
25
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
26
+ "github.com/spf13/afero"
25
27
26
28
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
27
29
)
@@ -60,6 +62,35 @@ func (i *InputDir) addFile(path string, content []byte) error {
60
62
return err
61
63
}
62
64
65
+ // copyInventory copies a file or directory from src to dst
66
+ func (i * InputDir ) copyInventory (src string , dst string ) error {
67
+ fs := afero .NewOsFs ()
68
+ return afero .Walk (fs , src ,
69
+ func (path string , info os.FileInfo , err error ) error {
70
+ if err != nil {
71
+ return err
72
+ }
73
+ fullDst := strings .Replace (path , src , dst , 1 )
74
+ if info .IsDir () {
75
+ if err = fs .MkdirAll (fullDst , info .Mode ()); err != nil {
76
+ return err
77
+ }
78
+ } else {
79
+ f , err := fs .Open (path )
80
+ if err != nil {
81
+ return err
82
+ }
83
+ if err = afero .WriteReader (fs , fullDst , f ); err != nil {
84
+ return err
85
+ }
86
+ if err = fs .Chmod (fullDst , info .Mode ()); err != nil {
87
+ return err
88
+ }
89
+ }
90
+ return nil
91
+ })
92
+ }
93
+
63
94
// Stdout reads the stdout from the ansible artifact that corresponds to the
64
95
// given ident and returns it as a string.
65
96
func (i * InputDir ) Stdout (ident string ) (string , error ) {
@@ -101,16 +132,39 @@ func (i *InputDir) Write() error {
101
132
return err
102
133
}
103
134
104
- // If ansible-runner is running in a python virtual environment, propagate
105
- // that to ansible.
106
- venv := os .Getenv ("VIRTUAL_ENV" )
107
- hosts := "localhost ansible_connection=local"
108
- if venv != "" {
109
- hosts = fmt .Sprintf ("%s ansible_python_interpreter=%s" , hosts , filepath .Join (venv , "bin/python" ))
110
- }
111
- err = i .addFile ("inventory/hosts" , []byte (hosts ))
112
- if err != nil {
113
- return err
135
+ // ANSIBLE_INVENTORY takes precendence over our generated hosts file
136
+ // so if the envvar is set we don't bother making it, we just copy
137
+ // the inventory into our runner directory
138
+ ansible_inventory := os .Getenv ("ANSIBLE_INVENTORY" )
139
+ if ansible_inventory == "" {
140
+ // If ansible-runner is running in a python virtual environment, propagate
141
+ // that to ansible.
142
+ venv := os .Getenv ("VIRTUAL_ENV" )
143
+ hosts := "localhost ansible_connection=local"
144
+ if venv != "" {
145
+ hosts = fmt .Sprintf ("%s ansible_python_interpreter=%s" , hosts , filepath .Join (venv , "bin/python" ))
146
+ }
147
+ err = i .addFile ("inventory/hosts" , []byte (hosts ))
148
+ if err != nil {
149
+ return err
150
+ }
151
+ } else {
152
+ fi , err := os .Stat (ansible_inventory )
153
+ if err != nil {
154
+ return err
155
+ }
156
+ switch mode := fi .Mode (); {
157
+ case mode .IsDir ():
158
+ err = i .copyInventory (ansible_inventory , filepath .Join (i .Path , "inventory" ))
159
+ if err != nil {
160
+ return err
161
+ }
162
+ case mode .IsRegular ():
163
+ err = i .copyInventory (ansible_inventory , filepath .Join (i .Path , "inventory/hosts" ))
164
+ if err != nil {
165
+ return err
166
+ }
167
+ }
114
168
}
115
169
116
170
if i .PlaybookPath != "" {
0 commit comments