|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/docker/machine/libmachine" |
| 11 | + "github.com/docker/machine/libmachine/log" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // TODO: possibly move this to ssh package |
| 16 | + baseSSHFSArgs = []string{ |
| 17 | + "-o", "StrictHostKeyChecking=no", |
| 18 | + "-o", "UserKnownHostsFile=/dev/null", |
| 19 | + "-o", "LogLevel=quiet", // suppress "Warning: Permanently added '[localhost]:2022' (ECDSA) to the list of known hosts." |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | +func cmdMount(c CommandLine, api libmachine.API) error { |
| 24 | + args := c.Args() |
| 25 | + if len(args) < 1 || len(args) > 2 { |
| 26 | + c.ShowHelp() |
| 27 | + return errWrongNumberArguments |
| 28 | + } |
| 29 | + |
| 30 | + src := args[0] |
| 31 | + dest := "" |
| 32 | + if len(args) > 1 { |
| 33 | + dest = args[1] |
| 34 | + } |
| 35 | + |
| 36 | + hostInfoLoader := &storeHostInfoLoader{api} |
| 37 | + |
| 38 | + cmd, err := getMountCmd(src, dest, c.Bool("unmount"), hostInfoLoader) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + cmd.Stdin = os.Stdin |
| 44 | + cmd.Stdout = os.Stdout |
| 45 | + cmd.Stderr = os.Stderr |
| 46 | + |
| 47 | + return cmd.Run() |
| 48 | +} |
| 49 | + |
| 50 | +func getMountCmd(src, dest string, unmount bool, hostInfoLoader HostInfoLoader) (*exec.Cmd, error) { |
| 51 | + var cmdPath string |
| 52 | + var err error |
| 53 | + if !unmount { |
| 54 | + cmdPath, err = exec.LookPath("sshfs") |
| 55 | + if err != nil { |
| 56 | + return nil, errors.New("You must have a copy of the sshfs binary locally to use the mount feature") |
| 57 | + } |
| 58 | + } else { |
| 59 | + cmdPath, err = exec.LookPath("fusermount") |
| 60 | + if err != nil { |
| 61 | + return nil, errors.New("You must have a copy of the fusermount binary locally to use the unmount option") |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + srcHost, srcUser, srcPath, srcOpts, err := getInfoForSshfsArg(src, hostInfoLoader) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + if dest == "" { |
| 71 | + dest = srcPath |
| 72 | + } |
| 73 | + |
| 74 | + sshArgs := baseSSHFSArgs |
| 75 | + if srcHost.GetSSHKeyPath() != "" { |
| 76 | + sshArgs = append(sshArgs, "-o", "IdentitiesOnly=yes") |
| 77 | + } |
| 78 | + |
| 79 | + // Append needed -i / private key flags to command. |
| 80 | + sshArgs = append(sshArgs, srcOpts...) |
| 81 | + |
| 82 | + // Append actual arguments for the sshfs command (i.e. docker@<ip>:/path) |
| 83 | + locationArg, err := generateLocationArg(srcHost, srcUser, srcPath) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + if !unmount { |
| 89 | + sshArgs = append(sshArgs, locationArg) |
| 90 | + sshArgs = append(sshArgs, dest) |
| 91 | + } else { |
| 92 | + sshArgs = []string{"-u"} |
| 93 | + sshArgs = append(sshArgs, dest) |
| 94 | + } |
| 95 | + |
| 96 | + cmd := exec.Command(cmdPath, sshArgs...) |
| 97 | + log.Debug(*cmd) |
| 98 | + return cmd, nil |
| 99 | +} |
| 100 | + |
| 101 | +func getInfoForSshfsArg(hostAndPath string, hostInfoLoader HostInfoLoader) (h HostInfo, user string, path string, args []string, err error) { |
| 102 | + // Path with hostname. e.g. "hostname:/usr/bin/cmatrix" |
| 103 | + var hostName string |
| 104 | + if parts := strings.SplitN(hostAndPath, ":", 2); len(parts) < 2 { |
| 105 | + hostName = defaultMachineName |
| 106 | + path = parts[0] |
| 107 | + } else { |
| 108 | + hostName = parts[0] |
| 109 | + path = parts[1] |
| 110 | + } |
| 111 | + if hParts := strings.SplitN(hostName, "@", 2); len(hParts) == 2 { |
| 112 | + user, hostName = hParts[0], hParts[1] |
| 113 | + } |
| 114 | + |
| 115 | + // Remote path |
| 116 | + h, err = hostInfoLoader.load(hostName) |
| 117 | + if err != nil { |
| 118 | + return nil, "", "", nil, fmt.Errorf("Error loading host: %s", err) |
| 119 | + } |
| 120 | + |
| 121 | + args = []string{} |
| 122 | + port, err := h.GetSSHPort() |
| 123 | + if err == nil && port > 0 { |
| 124 | + args = append(args, "-o", fmt.Sprintf("Port=%v", port)) |
| 125 | + } |
| 126 | + |
| 127 | + if h.GetSSHKeyPath() != "" { |
| 128 | + args = append(args, "-o", fmt.Sprintf("IdentityFile=%s", h.GetSSHKeyPath())) |
| 129 | + } |
| 130 | + |
| 131 | + if user == "" { |
| 132 | + user = h.GetSSHUsername() |
| 133 | + } |
| 134 | + |
| 135 | + return |
| 136 | +} |
0 commit comments