Skip to content

Commit

Permalink
synced_folder: add in functionality for unmount folders
Browse files Browse the repository at this point in the history
  • Loading branch information
dustymabe committed Apr 15, 2016
1 parent b188343 commit 8f64fce
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
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
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
5 changes: 5 additions & 0 deletions lib/vagrant-sshfs/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions locales/synced_folder_sshfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ en:
actions:
installing: Installing SSHFS client...
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 8f64fce

Please sign in to comment.