/
main.go
125 lines (100 loc) · 3.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
//-----------------------------------------------------------------------------
// This volume driver is meant to be used by docker >= 1.8.x
//
// 1- run the driver:
// sudo docker-volume-rsync
//
// 2- run the container:
// docker run -it --volume-driver rsync -v src.host.org/foo:/foo alpine sh
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Package membership:
//-----------------------------------------------------------------------------
package main
//-----------------------------------------------------------------------------
// Package factored import statement:
//-----------------------------------------------------------------------------
import (
// Standard library:
"flag"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
// Community:
"github.com/calavera/dkvolume"
)
//-----------------------------------------------------------------------------
// Package constant declarations factored into a block:
//-----------------------------------------------------------------------------
const (
id = "rsync"
socketAddress = "/var/run/docker/plugins/rsync.sock"
)
//-----------------------------------------------------------------------------
// Package variable declarations factored into a block:
//-----------------------------------------------------------------------------
var (
// Predefined defaults:
defVolRoot = filepath.Join(dkvolume.DefaultDockerRootDirectory, id)
defSshKey = sshKeyPath()
// Flags:
volRoot = flag.String("volroot", defVolRoot, "Docker volumes root directory")
archive = flag.Bool("archive", true, "Archive mode; equals -rlptgoD")
del = flag.Bool("delete", false, "Delete extraneous files from dest dirs")
compress = flag.Bool("compress", false, "Compress file data during the transfer")
sshKey = flag.String("ssh-private-key", defSshKey, "Path to the private SSH key")
)
//-----------------------------------------------------------------------------
// func sshKeyPath() returns the path to the current user's default private
// ssh key file.
//-----------------------------------------------------------------------------
func sshKeyPath() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return filepath.Join(usr.HomeDir, ".ssh/id_rsa")
}
//-----------------------------------------------------------------------------
// func init() is called after all the variable declarations in the package
// have evaluated their initializers, and those are evaluated only after all
// the imported packages have been initialized:
//-----------------------------------------------------------------------------
func init() {
// Check for mandatory argc:
if len(os.Args) < 1 {
usage()
}
// Parse commandline flags:
flag.Usage = usage
flag.Parse()
}
//-----------------------------------------------------------------------------
// func usage() reports the correct commandline usage for this driver:
//-----------------------------------------------------------------------------
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
//-----------------------------------------------------------------------------
// Function main of package main:
//-----------------------------------------------------------------------------
func main() {
// Initialize the driver struct:
d := rsyncDriver{
srcdst: make(map[string]string),
volRoot: *volRoot,
sshKey: *sshKey,
archive: *archive,
del: *del,
compress: *compress,
}
// Initializes the request handler with a driver implementation:
h := dkvolume.NewHandler(&d)
// Listen for requests in a unix socket:
log.Printf("Listening on %s\n", socketAddress)
fmt.Println(h.ServeUnix("root", socketAddress))
}