forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mount command for mounting machine directories
This comes in handy, when wanting to use volumes with drivers that don't support folder sharing (such as KVM, as opposed to VirtualBox) You will need FUSE and SSHFS installed, in order to use this feature. The machine will also need a "sftp" binary, but most of them have it. Signed-off-by: Anders F Björklund <[email protected]> (cherry picked from commit 30de5bb)
- Loading branch information
1 parent
b6d7614
commit a6a5995
Showing
4 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/docker/machine/libmachine" | ||
"github.com/docker/machine/libmachine/log" | ||
) | ||
|
||
var ( | ||
// TODO: possibly move this to ssh package | ||
baseSSHFSArgs = []string{ | ||
"-o", "StrictHostKeyChecking=no", | ||
"-o", "UserKnownHostsFile=/dev/null", | ||
"-o", "LogLevel=quiet", // suppress "Warning: Permanently added '[localhost]:2022' (ECDSA) to the list of known hosts." | ||
} | ||
) | ||
|
||
func cmdMount(c CommandLine, api libmachine.API) error { | ||
args := c.Args() | ||
if len(args) < 1 || len(args) > 2 { | ||
c.ShowHelp() | ||
return errWrongNumberArguments | ||
} | ||
|
||
src := args[0] | ||
dest := "" | ||
if len(args) > 1 { | ||
dest = args[1] | ||
} | ||
|
||
hostInfoLoader := &storeHostInfoLoader{api} | ||
|
||
cmd, err := getMountCmd(src, dest, c.Bool("unmount"), hostInfoLoader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Run() | ||
} | ||
|
||
func getMountCmd(src, dest string, unmount bool, hostInfoLoader HostInfoLoader) (*exec.Cmd, error) { | ||
var cmdPath string | ||
var err error | ||
if !unmount { | ||
cmdPath, err = exec.LookPath("sshfs") | ||
if err != nil { | ||
return nil, errors.New("You must have a copy of the sshfs binary locally to use the mount feature") | ||
} | ||
} else { | ||
cmdPath, err = exec.LookPath("fusermount") | ||
if err != nil { | ||
return nil, errors.New("You must have a copy of the fusermount binary locally to use the unmount option") | ||
} | ||
} | ||
|
||
srcHost, srcUser, srcPath, srcOpts, err := getInfoForSshfsArg(src, hostInfoLoader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if dest == "" { | ||
dest = srcPath | ||
} | ||
|
||
sshArgs := baseSSHFSArgs | ||
if srcHost.GetSSHKeyPath() != "" { | ||
sshArgs = append(sshArgs, "-o", "IdentitiesOnly=yes") | ||
} | ||
|
||
// Append needed -i / private key flags to command. | ||
sshArgs = append(sshArgs, srcOpts...) | ||
|
||
// Append actual arguments for the sshfs command (i.e. docker@<ip>:/path) | ||
locationArg, err := generateLocationArg(srcHost, srcUser, srcPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !unmount { | ||
sshArgs = append(sshArgs, locationArg) | ||
sshArgs = append(sshArgs, dest) | ||
} else { | ||
sshArgs = []string{"-u"} | ||
sshArgs = append(sshArgs, dest) | ||
} | ||
|
||
cmd := exec.Command(cmdPath, sshArgs...) | ||
log.Debug(*cmd) | ||
return cmd, nil | ||
} | ||
|
||
func getInfoForSshfsArg(hostAndPath string, hostInfoLoader HostInfoLoader) (h HostInfo, user string, path string, args []string, err error) { | ||
// Path with hostname. e.g. "hostname:/usr/bin/cmatrix" | ||
var hostName string | ||
if parts := strings.SplitN(hostAndPath, ":", 2); len(parts) < 2 { | ||
hostName = defaultMachineName | ||
path = parts[0] | ||
} else { | ||
hostName = parts[0] | ||
path = parts[1] | ||
} | ||
if hParts := strings.SplitN(hostName, "@", 2); len(hParts) == 2 { | ||
user, hostName = hParts[0], hParts[1] | ||
} | ||
|
||
// Remote path | ||
h, err = hostInfoLoader.load(hostName) | ||
if err != nil { | ||
return nil, "", "", nil, fmt.Errorf("Error loading host: %s", err) | ||
} | ||
|
||
args = []string{} | ||
port, err := h.GetSSHPort() | ||
if err == nil && port > 0 { | ||
args = append(args, "-o", fmt.Sprintf("Port=%v", port)) | ||
} | ||
|
||
if h.GetSSHKeyPath() != "" { | ||
args = append(args, "-o", fmt.Sprintf("IdentityFile=%s", h.GetSSHKeyPath())) | ||
} | ||
|
||
if user == "" { | ||
user = h.GetSSHUsername() | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package commands | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetMountCmd(t *testing.T) { | ||
hostInfoLoader := MockHostInfoLoader{MockHostInfo{ | ||
ip: "12.34.56.78", | ||
sshPort: 234, | ||
sshUsername: "root", | ||
sshKeyPath: "/fake/keypath/id_rsa", | ||
}} | ||
|
||
path, err := exec.LookPath("sshfs") | ||
if err != nil { | ||
t.Skip("sshfs not found (install sshfs ?)") | ||
} | ||
cmd, err := getMountCmd("myfunhost:/home/docker/foo", "/tmp/foo", false, &hostInfoLoader) | ||
|
||
expectedArgs := append( | ||
baseSSHFSArgs, | ||
"-o", | ||
"IdentitiesOnly=yes", | ||
"-o", | ||
"Port=234", | ||
"-o", | ||
"IdentityFile=/fake/keypath/id_rsa", | ||
"[email protected]:/home/docker/foo", | ||
"/tmp/foo", | ||
) | ||
expectedCmd := exec.Command(path, expectedArgs...) | ||
|
||
assert.Equal(t, expectedCmd, cmd) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGetMountCmdWithoutSshKey(t *testing.T) { | ||
hostInfoLoader := MockHostInfoLoader{MockHostInfo{ | ||
ip: "1.2.3.4", | ||
sshUsername: "user", | ||
}} | ||
|
||
path, err := exec.LookPath("sshfs") | ||
if err != nil { | ||
t.Skip("sshfs not found (install sshfs ?)") | ||
} | ||
cmd, err := getMountCmd("myfunhost:/home/docker/foo", "", false, &hostInfoLoader) | ||
|
||
expectedArgs := append( | ||
baseSSHFSArgs, | ||
"[email protected]:/home/docker/foo", | ||
"/home/docker/foo", | ||
) | ||
expectedCmd := exec.Command(path, expectedArgs...) | ||
|
||
assert.Equal(t, expectedCmd, cmd) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGetMountCmdUnmount(t *testing.T) { | ||
hostInfoLoader := MockHostInfoLoader{MockHostInfo{ | ||
ip: "1.2.3.4", | ||
sshUsername: "user", | ||
}} | ||
|
||
path, err := exec.LookPath("fusermount") | ||
if err != nil { | ||
t.Skip("fusermount not found (install fuse ?)") | ||
} | ||
cmd, err := getMountCmd("myfunhost:/home/docker/foo", "/tmp/foo", true, &hostInfoLoader) | ||
|
||
expectedArgs := []string{ | ||
"-u", | ||
"/tmp/foo", | ||
} | ||
expectedCmd := exec.Command(path, expectedArgs...) | ||
|
||
assert.Equal(t, expectedCmd, cmd) | ||
assert.NoError(t, err) | ||
} |