Skip to content

Commit

Permalink
Merge pull request #22 from dustymabe/dusty-add-unmounting
Browse files Browse the repository at this point in the history
Add ability to unmount guest folders
  • Loading branch information
dustymabe committed Apr 18, 2016
2 parents 1df77f1 + e95cbc0 commit fc15257
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ section.
## Executing the `vagrant sshfs` Command

The Vagrant SSHFS plugin also supports execution of the `vagrant sshfs`
command from the command line. Executing this command will
iterate through the Vagrant file and attempt to mount (via SSHFS) any
folders that aren't already mounted in the Vagrant guest that is
associated with the current directory.
command from the command line. Executing this command with the `--mount`
option will iterate through the Vagrant file and attempt to mount (via
SSHFS) any folders that aren't already mounted in the Vagrant guest.
Executing with the `--unmount` option will unmount any mounted folders.

```
vagrant sshfs
vagrant sshfs [--mount|--unmount] [vm-name]
```

## Options
Expand Down
27 changes: 27 additions & 0 deletions lib/vagrant-sshfs/cap/linux/sshfs_mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ def self.sshfs_mount_folder(machine, opts)
end
end

def self.sshfs_unmount_folder(machine, opts)
# opts contains something like:
# { :type=>:sshfs,
# :guestpath=>"/sharedfolder",
# :hostpath=>"/guests/sharedfolder",
# :disabled=>false
# :ssh_host=>"192.168.1.1"
# :ssh_port=>"22"
# :ssh_username=>"username"
# :ssh_password=>"password"
# }

# expand the guest path so we can handle things like "~/vagrant"
expanded_guest_path = machine.guest.capability(
:shell_expand_guest_path, opts[:guestpath])

# Log some information
machine.ui.info(I18n.t("vagrant.sshfs.actions.unmounting_folder",
guestpath: expanded_guest_path))

# Build up the command and connect
error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSUnmountFailed
cmd = "umount #{expanded_guest_path}"
machine.communicate.sudo(
cmd, error_class: error_class, error_key: :unmount_failed)
end

protected

# Perform a mount by running an sftp-server on the vagrant host
Expand Down
20 changes: 16 additions & 4 deletions lib/vagrant-sshfs/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ def self.synopsis
end

def execute
options = {:unmount => false} # Default to mounting shares
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant sshfs"
o.banner = "Usage: vagrant sshfs [--mount|--unmount] [vm-name]"
o.separator ""
o.separator "Mount all sshfs synced folders into the vagrant box"
o.separator "Mount or unmount sshfs synced folders into the vagrant box"
o.separator ""

o.on("--mount", "Mount folders - the default") do
options[:unmount] = false
end
o.on("--unmount", "Unmount folders") do
options[:unmount] = true
end
end

# Parse the options and return if we don't have any target.
Expand All @@ -36,8 +44,12 @@ def execute
folders = synced_folders(machine, cached: false)[:sshfs]
next if !folders || folders.empty?

# Sync them!
SyncedFolder.new.enable(machine, folders, {})
# Mount or Unmount depending on the user's request
if options[:unmount]
SyncedFolder.new.disable(machine, folders, {})
else
SyncedFolder.new.enable(machine, folders, {})
end
end
return error ? 1 : 0
end
Expand Down
4 changes: 4 additions & 0 deletions lib/vagrant-sshfs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class SSHFSSlaveMountFailed < SSHFSError
error_key(:slave_mount_failed)
end

class SSHFSUnmountFailed < SSHFSError
error_key(:unmount_failed)
end

class SSHFSInstallFailed < SSHFSError
error_key(:install_failed)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/vagrant-sshfs/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Plugin < Vagrant.plugin("2")
SyncedFolder
end

command("sshfs", primary: false) do
command("sshfs", primary: true) do
require_relative "command"
Command::SSHFS
end
Expand All @@ -25,6 +25,11 @@ class Plugin < Vagrant.plugin("2")
VagrantPlugins::GuestLinux::Cap::MountSSHFS
end

guest_capability("linux", "sshfs_unmount_folder") do
require_relative "cap/linux/sshfs_mount"
VagrantPlugins::GuestLinux::Cap::MountSSHFS
end

guest_capability("linux", "sshfs_is_folder_mounted") do
require_relative "cap/linux/sshfs_mount"
VagrantPlugins::GuestLinux::Cap::MountSSHFS
Expand Down
29 changes: 29 additions & 0 deletions lib/vagrant-sshfs/synced_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ def enable(machine, folders, pluginopts)
end
end

# This is called to remove the synced folders from a running
# machine.
#
# This is not guaranteed to be called, but this should be implemented
# by every synced folder implementation.
#
# @param [Machine] machine The machine to modify.
# @param [Hash] folders The folders to remove. This will not contain
# any folders that should remain.
# @param [Hash] opts Any options for the synced folders.
def disable(machine, folders, opts)

# Iterate through the folders and mount if needed
folders.each do |id, opts|

# If not mounted then there is nothing to do
if ! machine.guest.capability(:sshfs_is_folder_mounted, opts)
machine.ui.info(
I18n.t("vagrant.sshfs.info.not_mounted",
folder: opts[:guestpath]))
next
end

# Do the Unmount
machine.ui.info(I18n.t("vagrant.sshfs.actions.unmounting"))
machine.guest.capability(:sshfs_unmount_folder, opts)
end
end

# This is called after destroying the machine during a
# `vagrant destroy` and also prior to syncing folders during
# a `vagrant up`.
Expand Down
21 changes: 20 additions & 1 deletion locales/synced_folder_sshfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ en:
sshfs:
actions:
installing: Installing SSHFS client...
mounting: Mounting SSHFS shared folders...
mounting: Mounting SSHFS shared folder...
unmounting: Unmounting SSHFS shared folder...
unmounting_folder: |-
Unmounting SSHFS shared folder mounted at %{guestpath}
slave_mounting_folder: |-
Mounting folder via SSHFS: %{hostpath} => %{guestpath}
normal_mounting_folder: |-
Expand All @@ -16,6 +19,8 @@ en:
Detected host IP address is '%{ip}'
already_mounted: |-
The folder %{folder} in the guest already mounted.
not_mounted: |-
The folder %{folder} in the guest is not mounted.
errors:
communicator_not_ready: |-
The machine is reporting that it is not ready to communicate via ssh. Verify
Expand Down Expand Up @@ -54,3 +59,17 @@ en:
Mounting SSHFS shared via slave SSHFS mount failed. Please look at your
terminal scrollback to look for any error messages from the processes that
were run.
unmount_failed: |-
Unmount the SSHFS mount failed.
The command and output are:
%{command}
Stdout from the command:
%{stdout}
Stderr from the command:
%{stderr}

0 comments on commit fc15257

Please sign in to comment.