|
| 1 | +package drivers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + log "github.com/Sirupsen/logrus" |
| 6 | + "github.com/docker/go-plugins-helpers/volume" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + CephOptions = "cephopts" |
| 13 | +) |
| 14 | + |
| 15 | +type cephDriver struct { |
| 16 | + volumeDriver |
| 17 | + username string |
| 18 | + password string |
| 19 | + context string |
| 20 | + cephmount string |
| 21 | + cephport string |
| 22 | + localmount string |
| 23 | + cephopts map[string]string |
| 24 | +} |
| 25 | + |
| 26 | +//var ( |
| 27 | +// EmptyMap = map[string]string{} |
| 28 | +//) |
| 29 | + |
| 30 | +func NewCephDriver(root string, username string, password string, context string, cephmount string, cephport string, localmount string, cephopts string) cephDriver { |
| 31 | + d := cephDriver{ |
| 32 | + volumeDriver: newVolumeDriver(root), |
| 33 | + username: username, |
| 34 | + password: password, |
| 35 | + context: context, |
| 36 | + cephmount: cephmount, |
| 37 | + cephport: cephport, |
| 38 | + localmount: localmount, |
| 39 | + cephopts: map[string]string{}, |
| 40 | + } |
| 41 | + if len(cephopts) > 0 { |
| 42 | + d.cephopts[CephOptions] = cephopts |
| 43 | + } |
| 44 | + |
| 45 | + return d |
| 46 | +} |
| 47 | + |
| 48 | +func (n cephDriver) Mount(r volume.Request) volume.Response { |
| 49 | + log.Debugf("Entering Mount: %v", r) |
| 50 | + n.m.Lock() |
| 51 | + defer n.m.Unlock() |
| 52 | + hostdir := mountpoint(n.root, r.Name) |
| 53 | + source := n.fixSource(r) |
| 54 | + if n.mountm.HasMount(r.Name) && n.mountm.Count(r.Name) > 0 { |
| 55 | + log.Infof("Using existing CEPH volume mount: %s", hostdir) |
| 56 | + n.mountm.Increment(r.Name) |
| 57 | + return volume.Response{Mountpoint: hostdir} |
| 58 | + } |
| 59 | + |
| 60 | + log.Infof("Mounting CEPH volume %s on %s", source, hostdir) |
| 61 | + if err := createDest(hostdir); err != nil { |
| 62 | + return volume.Response{Err: err.Error()} |
| 63 | + } |
| 64 | + |
| 65 | + if err := n.mountVolume(source, hostdir); err != nil { |
| 66 | + return volume.Response{Err: err.Error()} |
| 67 | + } |
| 68 | + n.mountm.Add(r.Name, hostdir) |
| 69 | + return volume.Response{Mountpoint: hostdir} |
| 70 | +} |
| 71 | + |
| 72 | +func (n cephDriver) Unmount(r volume.Request) volume.Response { |
| 73 | + log.Debugf("Entering Unmount: %v", r) |
| 74 | + |
| 75 | + n.m.Lock() |
| 76 | + defer n.m.Unlock() |
| 77 | + hostdir := mountpoint(n.root, r.Name) |
| 78 | + |
| 79 | + if n.mountm.HasMount(r.Name) { |
| 80 | + if n.mountm.Count(r.Name) > 1 { |
| 81 | + log.Printf("Skipping unmount for %s - in use by other containers", r.Name) |
| 82 | + n.mountm.Decrement(r.Name) |
| 83 | + return volume.Response{} |
| 84 | + } |
| 85 | + n.mountm.Decrement(r.Name) |
| 86 | + } |
| 87 | + |
| 88 | + log.Infof("Unmounting volume name %s from %s", r.Name, hostdir) |
| 89 | + |
| 90 | + if err := run(fmt.Sprintf("umount %s", hostdir)); err != nil { |
| 91 | + return volume.Response{Err: err.Error()} |
| 92 | + } |
| 93 | + |
| 94 | + n.mountm.DeleteIfNotManaged(r.Name) |
| 95 | + |
| 96 | + if err := os.RemoveAll(hostdir); err != nil { |
| 97 | + return volume.Response{Err: err.Error()} |
| 98 | + } |
| 99 | + |
| 100 | + return volume.Response{} |
| 101 | +} |
| 102 | + |
| 103 | +func (n cephDriver) fixSource(r volume.Request) string { |
| 104 | + if n.mountm.HasOption(r.Name, ShareOpt) { |
| 105 | + return n.mountm.GetOption(r.Name, ShareOpt) |
| 106 | + } |
| 107 | + source := strings.Split(r.Name, "/") |
| 108 | + source[0] = source[0] + ":" + n.cephport + ":" |
| 109 | + return strings.Join(source, "/") |
| 110 | +} |
| 111 | + |
| 112 | +func (n cephDriver) mountVolume(source, dest string) error { |
| 113 | + var cmd string |
| 114 | + |
| 115 | + options := n.mountOptions(n.mountm.GetOptions(dest)) |
| 116 | + opts := "" |
| 117 | + if val, ok := options[CephOptions]; ok { |
| 118 | + fmt.Println("opts = ", val) |
| 119 | + opts = "-o " + val |
| 120 | + } |
| 121 | + |
| 122 | + mountCmd := "mount" |
| 123 | + |
| 124 | + if log.GetLevel() == log.DebugLevel { |
| 125 | + mountCmd = mountCmd + " -t ceph" |
| 126 | + } |
| 127 | + |
| 128 | + //cmd = fmt.Sprintf("%s -t ceph %s:%s:/ -o %s,%s,%s %s %s", mountCmd, n.cephmount, n.cephport, n.context, n.username, n.password, opts, dest) |
| 129 | + cmd = fmt.Sprintf("%s -t ceph %s -o %s,%s,%s %s %s", mountCmd, source, n.context, n.username, n.password, opts, dest) |
| 130 | + |
| 131 | + log.Debugf("exec: %s\n", cmd) |
| 132 | + return run(cmd) |
| 133 | +} |
| 134 | + |
| 135 | +func (n cephDriver) mountOptions(src map[string]string) map[string]string { |
| 136 | + if len(n.cephopts) == 0 && len(src) == 0 { |
| 137 | + return EmptyMap |
| 138 | + } |
| 139 | + |
| 140 | + dst := map[string]string{} |
| 141 | + for k, v := range n.cephopts { |
| 142 | + dst[k] = v |
| 143 | + } |
| 144 | + for k, v := range src { |
| 145 | + dst[k] = v |
| 146 | + } |
| 147 | + return dst |
| 148 | +} |
0 commit comments