Class: VagrantPlugins::SyncedFolderNFSGuest::GuestLinux::Cap::NFSServer

Inherits:
Object
  • Object
show all
Extended by:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb

Class Method Summary collapse

Class Method Details

.nfs_apply_changes!(machine) ⇒ Object



47
48
49
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 47

def self.nfs_apply_changes!(machine)
  machine.guest.capability(:nfs_apply_command)
end

.nfs_apply_command(machine) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 10

def self.nfs_apply_command(machine)
  machine.communicate.sudo(
    "exportfs -r",
    error_class: Errors::GuestNFSError,
    error_key: :nfs_apply_failed
  )
end

.nfs_capable?(machine) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 43

def self.nfs_capable?(machine)
  machine.guest.capability(:nfs_test_command)
end

.nfs_check_command(machine) ⇒ Object



26
27
28
29
30
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 26

def self.nfs_check_command(machine)
  machine.communicate.test(
    "/etc/init.d/nfs-kernel-server status"
  )
end

.nfs_cleanup(machine) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 102

def self.nfs_cleanup(machine)
  return if !nfs_capable?(machine)

  id = machine.id
  user = Process.uid

  # Use sed to just strip out the block of code which was inserted
  # by Vagrant
  #
  machine.communicate.sudo(
    "sed -r -e '/^# VAGRANT(-NFS_GUEST)?-BEGIN/,/^# VAGRANT(-NFS_GUEST)?-END/ d' -ibak /etc/exports",
    error_class: Errors::GuestNFSError,
    error_key: :nfs_guest_clean
  )
end

.nfs_export(machine, ip, folders) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 59

def self.nfs_export(machine, ip, folders)
  if !nfs_capable?(machine)
      raise Errors::NFSServerMissing
  end

  if machine.guest.capability?(:nfs_setup_firewall)
    machine.ui.info I18n.t("vagrant_nfs_guest.guests.linux.nfs_setup_firewall")
    machine.guest.capability(:nfs_setup_firewall, ip)
  end

  nfs_exports_template = machine.guest.capability(:nfs_exports_template)

  nfs_opts_setup(machine, folders)

  output = Vagrant::Util::TemplateRenderer.render(
    nfs_exports_template,
    uuid: machine.id,
    ips: [ip],
    folders: folders,
    user: Process.uid)

  machine.ui.info I18n.t("vagrant_nfs_guest.guests.linux.nfs_export")
  sleep 0.5

  nfs_cleanup(machine)

  retryable(on: Errors::GuestNFSError, tries: 8, sleep: 3) do
    output.split("\n").each do |line|
      machine.communicate.sudo(
        %Q[echo '#{line}' >> /etc/exports],
        error_class: Errors::GuestNFSError,
        error_key: :nfs_update_exports_failed
      )
    end

    if nfs_running?(machine)
      nfs_apply_changes!(machine)
    else
      nfs_start!(machine)
    end
  end
end

.nfs_exports_template(machine) ⇒ Object



38
39
40
41
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 38

def self.nfs_exports_template(machine)
  VagrantPlugins::SyncedFolderNFSGuest.source_root.join(
    "templates/nfs_guest/guest_export_linux")
end

.nfs_opts_setup(machine, folders) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 118

def self.nfs_opts_setup(machine, folders)
  folders.each do |k, opts|
    if !opts[:linux__nfs_options]
      opts[:linux__nfs_options] ||= ["rw", "no_subtree_check", "all_squash", "insecure"]
    end

    # Only automatically set anonuid/anongid if they weren't
    # explicitly set by the user.
    hasgid = false
    hasuid = false
    opts[:linux__nfs_options].each do |opt|
      hasgid = !!(opt =~ /^anongid=/) if !hasgid
      hasuid = !!(opt =~ /^anonuid=/) if !hasuid
    end

    opts[:linux__nfs_options] << "anonuid=#{opts[:map_uid]}" if !hasuid
    opts[:linux__nfs_options] << "anongid=#{opts[:map_gid]}" if !hasgid
    opts[:linux__nfs_options] << "fsid=#{opts[:uuid]}"

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

    retryable(on: Errors::GuestNFSError, tries: 8, sleep: 3) do
      # Do the actual creating and mounting
      machine.communicate.sudo(
        "mkdir -p #{expanded_guest_path}",
        error_class: Errors::GuestNFSError,
        error_key: :nfs_create_mounts_failed
      )

      # Folder options
      opts[:owner] ||= machine.ssh_info[:username]
      opts[:group] ||= machine.ssh_info[:username]

      machine.communicate.sudo(
        "chown -R #{opts[:owner]}:#{opts[:group]} #{expanded_guest_path}",
        error_class: Errors::GuestNFSError,
        error_key: :nfs_create_mounts_failed
      )
      machine.communicate.sudo(
        "chmod 2775 #{expanded_guest_path}",
        error_class: Errors::GuestNFSError,
        error_key: :nfs_create_mounts_failed
      )
    end
  end
end

.nfs_running?(machine) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 55

def self.nfs_running?(machine)
  machine.guest.capability(:nfs_check_command)
end

.nfs_start!(machine) ⇒ Object



51
52
53
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 51

def self.nfs_start!(machine)
  machine.guest.capability(:nfs_start_command)
end

.nfs_start_command(machine) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 18

def self.nfs_start_command(machine)
  machine.communicate.sudo(
    "/etc/init.d/nfs-kernel-server start",
    error_class: Errors::GuestNFSError,
    error_key: :nfs_start_failed
  )
end

.nfs_test_command(machine) ⇒ Object



32
33
34
35
36
# File 'lib/vagrant-nfs_guest/guests/linux/cap/nfs_server.rb', line 32

def self.nfs_test_command(machine)
  machine.communicate.test(
    "which exportfs"
  )
end