-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible-playbook.go
147 lines (126 loc) · 3.71 KB
/
ansible-playbook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
/*
* Addresses:
* Can be used in conjunction with below fix to start ansible with extra-vars parameter without falling over.
* https://github.com/mitchellh/vagrant/issues/6726
*
* References:
* http://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/
* http://www.darrencoxall.com/golang/executing-commands-in-go/
* https://groups.google.com/forum/#!msg/golang-nuts/cnG-N3KcoUU/4vR5MIuFDjYJ
*
*/
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"github.com/vaughan0/go-ini"
)
func main() {
var wrapperExecutableName = filepath.Base(os.Args[0])
var extension = filepath.Ext(wrapperExecutableName)
var executable = wrapperExecutableName[0 : len(wrapperExecutableName)-len(extension)]
f, err := os.OpenFile(executable+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating "+executable+" log file", err)
os.Exit(1)
}
defer f.Close()
log.SetOutput(f)
log.Println("*******************************************************")
log.Printf("Target executable: %s\n", executable)
wrapperExecutableDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Fprintln(os.Stderr, "Error getting path from "+os.Args[0], err)
os.Exit(1)
}
log.Printf("Wrapper dir: %s\n", wrapperExecutableDir)
pwd, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, "Error getting current working folder", err)
os.Exit(1)
}
log.Printf("Current dir: %s\n", pwd)
// default cygwin home
var cygHome = "c:/cygwin"
// now check if we need to override the path with env variable
var cygHomeEnv = os.Getenv("CYGWIN_HOME")
if cygHomeEnv != "" {
cygHome = cygHomeEnv
} else {
// try to lookup from ini file
var iniFile = filepath.ToSlash(wrapperExecutableDir + "/cygwin.ini")
file, err := ini.LoadFile(iniFile)
if err == nil {
p, ok := file.Get("cygwin", "home")
if ok {
cygHome = p
}
}
}
log.Printf("Cygwin Home: %s\n", cygHome)
// // environment variables
// log.Println("Environment variables:")
// for i, e := range os.Environ() {
// pair := strings.Split(e, "=")
// log.Printf(" > %d: %s=%s\n", i, pair[0], pair[1])
// }
// args provided
log.Println("System args:")
args := make([]string, len(os.Args[1:])+1, len(os.Args[1:])+1)
args[0] = "/bin/" + executable
for i, a := range os.Args[1:] {
log.Printf(" > %d: [%-10s] %s\n", i, reflect.TypeOf(a), a)
if strings.HasPrefix(a, "--extra-vars={") {
var sarg = strings.Split(a, "=")
args[i+1] = fmt.Sprintf("%s=%s", sarg[0], sarg[1])
log.Printf("extra vars set to: %s", args[i+1])
} else {
args[i+1] = a
}
}
var bashCmd = filepath.ToSlash(cygHome + "/bin/python2.7.exe")
log.Printf("execute\n %s %s", bashCmd, strings.Join(args[:], " "))
cmd := exec.Command(bashCmd, args...)
// echo stdin back to console
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating "+executable+" wrapper stdout pipeline", err)
os.Exit(1)
}
outScanner := bufio.NewScanner(cmdReader)
go func() {
for outScanner.Scan() {
fmt.Printf("%s\n", outScanner.Text())
}
}()
// echo stderr back to console
errReader, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating "+executable+" wrapper stderr pipeline", err)
os.Exit(1)
}
errScanner := bufio.NewScanner(errReader)
go func() {
for errScanner.Scan() {
fmt.Fprintln(os.Stderr, errScanner.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting "+executable+" wrapper", err)
os.Exit(1)
}
err = cmd.Wait()
if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for "+executable+" wrapper to finish", err)
os.Exit(1)
}
// all good can finish now
os.Exit(0)
}