-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
190 lines (162 loc) · 4.36 KB
/
utils.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
package merry
import (
"embed"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/compose-spec/compose-go/loader"
)
//go:embed resources/docker-compose.yml
//go:embed resources/bitcoin.conf
//go:embed resources/nginx/nginx.conf
var f embed.FS
var DefaultComposePath string
func defaultComposePath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user's home directory: %v", err)
}
return filepath.Join(home, ".merry", "docker-compose.yml"), nil
}
// runDockerCompose runs docker-compose with the given arguments
func runDockerCompose(composePath string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
_, err := exec.LookPath("docker-compose")
if err != nil {
cmd = exec.Command("docker", append([]string{"compose", "-f", composePath}, args...)...)
} else {
cmd = exec.Command("docker-compose", append([]string{"-f", composePath}, args...)...)
}
return cmd
}
// getServices extracts the services from the docker compose file
func getServices(composePath string) (map[string]string, error) {
composeBytes, err := os.ReadFile(composePath)
if err != nil {
return nil, err
}
parsed, err := loader.ParseYAML(composeBytes)
if err != nil {
return nil, err
}
if _, ok := parsed["services"]; !ok {
return nil, errors.New("missing services in compose")
}
serviceMap := parsed["services"].(map[string]interface{})
services := map[string]string{}
for k, v := range serviceMap {
m := v.(map[string]interface{})
i, ok := m["ports"].([]interface{})
if !ok {
continue
}
for _, j := range i {
port := j.(string)
exposedPorts := strings.Split(port, ":")
endpoint := "localhost:" + exposedPorts[0]
services[k] = endpoint
}
}
return services, nil
}
func retry(f func() error) {
for {
err := f()
if err == nil {
return
}
fmt.Printf("failed with %v, retrying after 5 seconds\n", err)
time.Sleep(5 * time.Second)
}
}
// Provisioning Nigiri reosurces
func (m *Merry) provisionResourcesToDatadir(datadir string) error {
if m.Ready {
return nil
}
// create folders in volumes/{bitcoin,elements} for node datadirs
if err := makeDirectoryIfNotExists(filepath.Join(datadir, "volumes", "bitcoin")); err != nil {
return err
}
// copy docker compose into the Nigiri data directory
if err := copyFromResourcesToDatadir(
filepath.Join("resources", "docker-compose.yml"),
filepath.Join(datadir, "docker-compose.yml"),
); err != nil {
return err
}
// copy bitcoin.conf into the Nigiri data directory
if err := copyFromResourcesToDatadir(
filepath.Join("resources", "bitcoin.conf"),
filepath.Join(datadir, "volumes", "bitcoin", "bitcoin.conf"),
); err != nil {
return err
}
if err := makeDirectoryIfNotExists(filepath.Join(datadir, "nginx")); err != nil {
return err
}
// copy nginx/nginx.conf into the Nigiri data directory
if err := copyFromResourcesToDatadir(
filepath.Join("resources", "nginx", "nginx.conf"),
filepath.Join(datadir, "nginx", "nginx.conf"),
); err != nil {
return err
}
m.Ready = true
data, err := json.Marshal(m)
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(datadir, "merry.config.json"), data, 0777); err != nil {
return err
}
return nil
}
func makeDirectoryIfNotExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, os.ModeDir|0755)
}
return nil
}
func copyFromResourcesToDatadir(src string, dest string) error {
data, err := f.ReadFile(src)
if err != nil {
return fmt.Errorf("read embed: %w", err)
}
err = os.WriteFile(dest, data, 0777)
if err != nil {
return fmt.Errorf("write %s to %s: %w", src, dest, err)
}
return nil
}
func getPortsForService(composeFile string, serviceName string) ([]string, error) {
composeBytes, err := os.ReadFile(composeFile)
if err != nil {
return nil, err
}
parsed, err := loader.ParseYAML(composeBytes)
if err != nil {
return nil, err
}
if _, ok := parsed["services"]; !ok {
return nil, errors.New("missing services in compose")
}
serviceMap := parsed["services"].(map[string]interface{})
var ports []string
for k, v := range serviceMap {
if k == serviceName {
m := v.(map[string]interface{})
i := m["ports"].([]interface{})
for _, j := range i {
port := j.(string)
ports = append(ports, port)
}
}
}
return ports, nil
}