-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
299 lines (262 loc) · 7.79 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
)
const VERSION = "1.0.0"
func runContainer() {
// Path to the current directory
currentDir, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current directory:", err)
return
}
// Create a Docker client
cli, err := client.NewClientWithOpts()
if err != nil {
fmt.Println("Error creating Docker client:", err)
return
}
ctx := context.Background()
cli.NegotiateAPIVersion(ctx)
imageName := os.Getenv("DOP_AVM_IMAGE_NAME")
if imageName == "" {
imageName = "devopspass/ansible:latest"
}
// Check if the Docker image exists locally
_, _, err = cli.ImageInspectWithRaw(ctx, imageName)
if client.IsErrNotFound(err) {
// Image does not exist locally, pull it
fmt.Printf("Image %s not found locally, pulling...\n", imageName)
pullResp, err := cli.ImagePull(ctx, imageName, image.PullOptions{})
if err != nil {
fmt.Println("Error pulling Docker image:", err)
return
}
defer pullResp.Close()
// Stream the pull response if needed
io.Copy(os.Stdout, pullResp)
}
// Docker volume mappings
volumeMappings := []string{
fmt.Sprintf("%s:/ansible", currentDir),
"/var/run/docker.sock:/var/run/docker.sock",
}
// List of directories to check
directories := []string{
filepath.Join(getHomeDir(), ".ssh"),
filepath.Join(getHomeDir(), ".aws"),
filepath.Join(getHomeDir(), ".azure"),
filepath.Join(getHomeDir(), ".ansible"),
}
// Ansible home dir
ansibleDir := filepath.Join(getHomeDir(), ".ansible")
if _, err := os.Stat(ansibleDir); os.IsNotExist(err) {
// Create .ansible directory
if err := os.Mkdir(ansibleDir, 0755); err != nil {
fmt.Printf("Error creating directory %s: %v\n", ansibleDir, err)
} else {
fmt.Printf("Created directory %s\n", ansibleDir)
// Append to volume mappings
volumeMappings = append(volumeMappings, fmt.Sprintf("%s:/%s", ansibleDir, ".ansible"))
}
}
// Check if directories exist and add them as volume mappings
for _, dir := range directories {
if _, err := os.Stat(dir); err == nil {
baseDir := filepath.Base(dir)
volumeMappings = append(volumeMappings, fmt.Sprintf("%s:/root/%s", dir, baseDir))
}
}
// Docker environment variables
var envs []string
for _, env := range os.Environ() {
if strings.HasPrefix(env, "ANSIBLE_") ||
strings.HasPrefix(env, "MOLECULE_") ||
strings.HasPrefix(env, "GALAXY_") ||
strings.HasPrefix(env, "AWS_") {
envs = append(envs, env)
}
}
// SSH agent
switch runtime.GOOS {
case "windows", "darwin":
volumeMappings = append(volumeMappings, "/run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock")
envs = append(envs, "SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock")
default:
if os.Getenv("SSH_AUTH_SOCK") != "" {
volumeMappings = append(volumeMappings, fmt.Sprintf("%s:%s", os.Getenv("SSH_AUTH_SOCK"), "/tmp/ssh-auth.sock"))
envs = append(envs, "SSH_AUTH_SOCK=/tmp/ssh-auth.sock")
}
}
// Add GOOGLE_APPLICATION_CREDENTIALS volume if the environment variable is set
credentialsPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credentialsPath == "" {
// Use default path based on the operating system
switch runtime.GOOS {
case "windows":
credentialsPath = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming", "gcloud", "application_default_credentials.json")
case "darwin":
credentialsPath = filepath.Join(getHomeDir(), ".config", "gcloud", "application_default_credentials.json")
default:
credentialsPath = filepath.Join(getHomeDir(), ".config", "gcloud", "application_default_credentials.json")
}
}
// Add the credentialsPath to the volume mappings if it's not empty
if credentialsPath != "" {
if _, err := os.Stat(credentialsPath); err == nil {
volumeMappings = append(volumeMappings, fmt.Sprintf("%s:%s", credentialsPath, "/root/.config/gcloud/application_default_credentials.json"))
}
}
// Get the program binary name
binaryName := filepath.Base(os.Args[0])
if runtime.GOOS == "windows" {
binaryName = strings.Replace(binaryName, ".exe", "", -1)
}
// Create container options
config := &container.Config{
Image: imageName,
Cmd: append([]string{binaryName}, os.Args[1:]...),
Env: envs,
Tty: true,
AttachStdout: true,
AttachStderr: true,
}
hostConfig := &container.HostConfig{
Binds: volumeMappings,
AutoRemove: true,
}
// Create the container
resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, "ansible")
if err != nil {
// Check if the error is due to Docker not running
if client.IsErrConnectionFailed(err) {
fmt.Println("ERROR: DOP Ansible Version Manager relies on Docker, so please ensure that it's installed and running.")
fmt.Println("Error creating Docker container:", err)
} else {
fmt.Println("Error creating Docker container:", err)
}
return
}
// Start the container
err = cli.ContainerStart(ctx, resp.ID, container.StartOptions{})
if err != nil {
fmt.Println("Error starting Docker container:", err)
return
}
// Stream container logs
logsOptions := container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
}
out, err := cli.ContainerLogs(ctx, resp.ID, logsOptions)
if err != nil {
fmt.Println("Error streaming container logs:", err)
return
}
defer out.Close()
// Print container logs
_, err = io.Copy(os.Stdout, out)
if err != nil {
fmt.Println("Error printing container logs:", err)
return
}
}
func printHelp() {
fmt.Printf("DevOps Pass AI - Ansible Version Manager v%s", VERSION)
fmt.Println("Run 'dop-avm setup' to setup Ansible binaries.")
}
func main() {
currentBinaryPath, err := os.Executable()
if err != nil {
fmt.Printf("Error getting current binary path: %v", err)
return
}
if strings.HasSuffix(currentBinaryPath, "dop-avm") || strings.HasSuffix(currentBinaryPath, "dop-avm.exe") {
if len(os.Args) > 1 && os.Args[1] == "setup" {
copyBinaryToNames()
} else {
printHelp()
}
} else {
runContainer()
}
}
// Function to get the home directory
func getHomeDir() string {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting home directory:", err)
os.Exit(1)
}
return home
}
func copyBinaryToNames() error {
// Get the path to the current binary
currentBinaryPath, err := os.Executable()
if err != nil {
return fmt.Errorf("error getting current binary path: %v", err)
}
// Determine the target file names
targetNames := []string{
"ansible",
"ansible-playbook",
"ansible-galaxy",
"ansible-vault",
"ansible-doc",
"ansible-config",
"ansible-console",
"ansible-inventory",
"ansible-adhoc",
"ansible-lint",
"molecule",
}
// Append ".exe" for Windows
if runtime.GOOS == "windows" {
for i, name := range targetNames {
targetNames[i] = name + ".exe"
}
}
// Copy the binary to each target name
for _, name := range targetNames {
targetPath := filepath.Join(".", name)
if err := copyFile(currentBinaryPath, targetPath); err != nil {
return fmt.Errorf("error copying binary to %s: %v", targetPath, err)
}
fmt.Printf("Binary copied to %s\n", targetPath)
// Set executable permission for macOS and Linux
if runtime.GOOS != "windows" {
if err := os.Chmod(targetPath, 0755); err != nil {
return fmt.Errorf("error setting executable permission for %s: %v", targetPath, err)
}
fmt.Printf("Executable permission set for %s\n", targetPath)
}
}
return nil
}
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return err
}
return nil
}