/
mac_sync.go
243 lines (219 loc) · 6.34 KB
/
mac_sync.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
package crane
import (
"crypto/md5"
"errors"
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
)
type MacSync interface {
ContainerName() string
Volume() string
Autostart() bool
Exists() bool
Running() bool
Start(debug bool)
Stop()
Status() string
}
type macSync struct {
RawVolume string
RawFlags []string `json:"flags" yaml:"flags"`
RawImage string `json:"image" yaml:"image"`
Uid int `json:"uid" yaml:"uid"`
Gid int `json:"gid" yaml:"gid"`
RawAutostart bool `json:"autostart" yaml:"autostart"`
configPath string
cName string
volume string
}
func (s *macSync) ContainerName() string {
if s.cName == "" {
syncIdentifierParts := []string{
s.configPath,
s.Volume(),
s.image(),
strings.Join(s.flags(), " "),
strconv.Itoa(s.Uid),
strconv.Itoa(s.Gid),
}
syncIdentifier := []byte(strings.Join(syncIdentifierParts, ":"))
digest := fmt.Sprintf("%x", md5.Sum(syncIdentifier))
s.cName = "crane_unison_" + digest
}
return s.cName
}
func (s *macSync) Volume() string {
if s.volume == "" {
v := expandEnv(s.RawVolume)
parts := strings.Split(v, ":")
if !path.IsAbs(parts[0]) {
parts[0] = s.configPath + "/" + parts[0]
}
s.volume = strings.Join(parts, ":")
}
return s.volume
}
func (s *macSync) Autostart() bool {
return s.RawAutostart
}
func (s *macSync) Exists() bool {
return containerID(s.ContainerName()) != ""
}
func (s *macSync) Running() bool {
return s.serverRunning() && s.clientRunning()
}
func (s *macSync) Status() string {
status := "-"
if s.Exists() {
status = "stopped"
if s.Running() {
status = "running"
}
}
return status
}
func (s *macSync) Start(debug bool) {
unisonArgs := []string{}
// Start sync container if needed
if s.Exists() {
if s.serverRunning() {
verboseLog("Unison sync server for " + s.hostDir() + " already running")
} else {
verboseLog("Starting unison sync server for " + s.hostDir())
dockerArgs := []string{"start", s.ContainerName()}
executeHiddenCommand("docker", dockerArgs)
}
} else {
checkUnisonRequirements()
verboseLog("Starting unison sync server for " + s.hostDir())
dockerArgs := []string{
"run",
"--name", s.ContainerName(),
"-d",
"-P",
"-e", "UNISON_DIR=" + s.containerDir(),
"-e", "UNISON_UID=" + strconv.Itoa(s.Uid),
"-e", "UNISON_GID=" + strconv.Itoa(s.Gid),
"-v", s.containerDir(),
s.image(),
}
executeHiddenCommand("docker", dockerArgs)
fmt.Printf("Doing initial snyc for %s ...\n", s.hostDir())
unisonArgs = s.unisonArgs()
initialSyncArgs := []string{}
for _, a := range unisonArgs {
if !strings.HasPrefix(a, "-repeat") {
initialSyncArgs = append(initialSyncArgs, a)
}
}
// Wait a bit for the Unison server to start
time.Sleep(3 * time.Second)
if debug {
executeCommand("unison", initialSyncArgs, os.Stdout, os.Stderr)
} else {
executeCommand("unison", initialSyncArgs, nil, nil)
}
}
// Start unison in background if not already running
if s.clientRunning() {
verboseLog("Unison sync client for " + s.hostDir() + " already running")
} else {
verboseLog("Starting unison sync client for " + s.hostDir())
unisonArgs = s.unisonArgs()
if !debug { // Line will be logged later within executeCommand anyway
verboseLog("unison " + strings.Join(unisonArgs, " "))
}
if !isDryRun() {
// Wait a bit for the Unison server to start
time.Sleep(3 * time.Second)
if debug {
executeCommand("unison", unisonArgs, os.Stdout, os.Stderr)
} else {
cmd := exec.Command("unison", unisonArgs...)
cmd.Dir = cfg.Path()
cmd.Stdout = nil
cmd.Stderr = nil
cmd.Stdin = nil
cmd.Start()
}
}
}
}
func (s *macSync) Stop() {
verboseLog("Stopping unison sync for " + s.hostDir())
// stop container (also stops Unison sync)
dockerArgs := []string{"kill", s.ContainerName()}
executeHiddenCommand("docker", dockerArgs)
}
func (s *macSync) serverRunning() bool {
return s.Exists() && inspectBool(s.ContainerName(), "{{.State.Running}}")
}
func (s *macSync) clientRunning() bool {
args := []string{"-f", "unison " + s.hostDir() + " socket://localhost:" + s.publishedPort()}
_, err := commandOutput("pgrep", args)
return err == nil
}
func (s *macSync) unisonArgs() []string {
unisonArgs := []string{s.hostDir(), "socket://localhost:" + s.publishedPort() + "/"}
return append(unisonArgs, s.flags()...)
}
func (s *macSync) image() string {
if len(s.RawImage) > 0 {
return expandEnv(s.RawImage)
}
allowedVersions := []string{"2.48.4"}
versionOut, err := commandOutput("unison", []string{"-version"})
if err != nil {
return "michaelsauter/unison:2.48.4"
}
// `unison -version` returns sth like "unison version 2.48.4"
versionParts := strings.Split(versionOut, " ")
installedVersion := versionParts[len(versionParts)-1]
if !includes(allowedVersions, installedVersion) {
panic(StatusError{errors.New("Unison version " + installedVersion + " is not supported. You need to install: " + strings.Join(allowedVersions, ", ")), 69})
}
return "michaelsauter/unison:" + installedVersion
}
func (s *macSync) flags() []string {
if len(s.RawFlags) > 0 {
f := []string{}
for _, rawFlag := range s.RawFlags {
f = append(f, expandEnv(rawFlag))
}
return f
}
return []string{"-auto", "-batch", "-ignore=Name {.git}", "-confirmbigdel=false", "-prefer=newer", "-repeat=watch"}
}
func (s *macSync) hostDir() string {
parts := strings.Split(s.Volume(), ":")
return parts[0]
}
func (s *macSync) containerDir() string {
parts := strings.Split(s.Volume(), ":")
return parts[1]
}
func (s *macSync) publishedPort() string {
args := []string{"port", s.ContainerName(), "5000/tcp"}
published, err := commandOutput("docker", args)
if err != nil {
printErrorf("Could not detect port of container %s. Sync will not work properly.", s.ContainerName())
return ""
}
parts := strings.Split(published, ":")
return parts[1]
}
func checkUnisonRequirements() {
_, err := commandOutput("which", []string{"unison"})
if err != nil {
panic(StatusError{errors.New("`unison` is not installed or not in your $PATH.\nSee https://github.com/michaelsauter/crane/wiki/Unison-installation."), 69})
}
_, err = commandOutput("which", []string{"unison-fsmonitor"})
if err != nil {
panic(StatusError{errors.New("`unison-fsmonitor` is not installed or not in your $PATH.\nSee https://github.com/michaelsauter/crane/wiki/Unison-installation."), 69})
}
}