-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdcs.go
200 lines (164 loc) · 4.55 KB
/
dcs.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
)
func main() {
// Get the arguments passed to the program
args := os.Args[1:]
// Check if the user passed any arguments
if len(args) == 0 {
invalidCommand()
}
// Check if the required environment variables are set
if os.Getenv("VAULT_ADDR") == "" || os.Getenv("VAULT_TOKEN") == "" {
log.Fatal("VAULT_ADDR and VAULT_TOKEN must be set in environment")
}
// Check which command the user passed
switch args[0] {
case "start":
start(false)
case "stop":
stop()
case "restart":
start(true)
case "update":
update()
default:
invalidCommand()
}
}
func invalidCommand() {
fmt.Println("Invalid command, please use one of the following:")
fmt.Println(" start")
fmt.Println(" stop")
fmt.Println(" restart")
fmt.Println(" update")
fmt.Println("\nExample: dcs start")
os.Exit(1)
}
func start(restart bool) {
// Get the address of the Vault server from the environment
server := os.Getenv("VAULT_ADDR")
// Get the folder of the current working directory
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
path := filepath.Base(dir)
// Get the token from the environment
token := os.Getenv("VAULT_TOKEN")
// Print the settings to the user
fmt.Println("Retrieving secrets from Vault:")
fmt.Println(" Server:", server)
fmt.Println(" Path:", path)
fmt.Println(" Token:", token)
fmt.Println()
// Initialize the request to the Vault server with the correct path
req, _ := http.NewRequest("GET", server+"/v1/secret/data/"+path, nil)
// Add the authentication token header to the request
req.Header.Add("X-Vault-Token", token)
// Send the request to the Vault server
res, _ := http.DefaultClient.Do(req)
// Close the response body when we're done
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(res.Body)
// Decode the response body
body, _ := io.ReadAll(res.Body)
// Unmarshal and parse the JSON response into a map
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
log.Fatal(err)
}
// Set the command to run depending on if we're restarting or not
var cmd *exec.Cmd
if restart {
cmd = exec.Command("docker", "compose", "up", "-d", "--force-recreate")
} else {
cmd = exec.Command("docker", "compose", "up", "-d")
}
// secrets := result["data"].(map[string]interface{})["data"].(map[string]interface{})
// Extract the secrets from the response and handle errors
s1 := result["data"]
if s1 != nil {
s2 := s1.(map[string]interface{})
if s2 != nil {
s3 := s2["data"]
if s3 != nil {
s4 := s3.(map[string]interface{})
if s4 != nil {
fmt.Println("Injecting secrets into process:")
// Pass all OS environment variables to the command
cmd.Env = os.Environ()
// Inject all secrets into the command as environment variables and print them to the user
for k, v := range s4 {
cmd.Env = append(cmd.Environ(), fmt.Sprintf("%s=%s", k, v))
fmt.Printf(" %s: %s\n", k, v)
}
} else {
fmt.Println("No secrets found for \"" + path + "\", continuing without secrets")
}
} else {
fmt.Println("No secrets found for \"" + path + "\", continuing without secrets")
}
} else {
fmt.Println("No secrets found for \"" + path + "\", continuing without secrets")
}
} else {
fmt.Println("No secrets found for \"" + path + "\", continuing without secrets")
}
fmt.Println()
fmt.Println("Starting docker compose:")
// Write the output of the command to the terminal
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run the command
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func stop() {
// Set the command to run
cmd := exec.Command("docker", "compose", "down", "--remove-orphans")
// Pass all OS environment variables to the command
cmd.Env = os.Environ()
fmt.Println("Stopping docker compose:")
// Write the output of the command to the terminal
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run the command
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func update() {
// Set the command to run
cmd := exec.Command("docker", "compose", "pull")
// Pass all OS environment variables to the command
cmd.Env = os.Environ()
fmt.Println("Pulling latest docker images:")
// Write the output of the command to the terminal
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run the command
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println()
// Restart the docker compose after updating the images
start(true)
}