Class: OsCtl::ExportFS::Operations::Export::Add

Inherits:
Base
  • Object
show all
Includes:
Lib::Utils::File, Lib::Utils::Log, Lib::Utils::System
Defined in:
lib/osctl/exportfs/operations/export/add.rb

Overview

Add export to a NFS server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

run

Constructor Details

#initialize(server, export) ⇒ Add

Returns a new instance of Add.

Parameters:



15
16
17
18
19
# File 'lib/osctl/exportfs/operations/export/add.rb', line 15

def initialize(server, export)
  @server = server
  @export = export
  @cfg = server.open_config
end

Instance Attribute Details

#cfgObject (readonly, protected)

Returns the value of attribute cfg.



42
43
44
# File 'lib/osctl/exportfs/operations/export/add.rb', line 42

def cfg
  @cfg
end

#exportObject (readonly, protected)

Returns the value of attribute export.



42
43
44
# File 'lib/osctl/exportfs/operations/export/add.rb', line 42

def export
  @export
end

#serverObject (readonly, protected)

Returns the value of attribute server.



42
43
44
# File 'lib/osctl/exportfs/operations/export/add.rb', line 42

def server
  @server
end

Instance Method Details

#add_to_exportsObject (protected)

Add the export to the database



45
46
47
48
# File 'lib/osctl/exportfs/operations/export/add.rb', line 45

def add_to_exports
  cfg.exports << export
  cfg.save
end

#enable_share(propagate: true) ⇒ Object (protected)

Propagate the directory from the host to the server using the shared directory and then export it



52
53
54
55
56
57
58
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
# File 'lib/osctl/exportfs/operations/export/add.rb', line 52

def enable_share(propagate: true)
  if propagate
    hash = Digest::SHA2.hexdigest(export.dir)
    shared = File.join(server.shared_dir, hash)

    Dir.mkdir(shared)
    Sys.bind_mount(export.dir, shared)
  end

  begin
    Operations::Server::Exec.run(server) do
      server.enter_ns

      if propagate
        FileUtils.mkdir_p(export.as)
        Sys.move_mount(
          File.join(RunState::CURRENT_SERVER, 'shared', hash),
          export.as
        )

        Operations::Exportfs::Generate.run(server)
      end

      syscmd("exportfs -i -o \"#{export.options}\" \"#{export.host}:#{export.as}\"")
    end

  ensure
    if propagate
      Sys.unmount(shared)
      Dir.rmdir(shared)
    end
  end
end

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/osctl/exportfs/operations/export/add.rb', line 21

def execute
  if !Dir.exist?(export.dir)
    fail "dir #{export.dir} not found"
  end

  ex = cfg.exports.find_by_as(export.as)

  if ex && ex.dir != export.dir
    fail "source directory mismatch: expected '#{ex.dir}', got '#{export.dir}'"
  elsif ex && ex.host == export.host
    fail "export of #{export.as} to #{export.host} already exists"
  end

  add_to_exports

  if server.running?
    enable_share(propagate: ex.nil?)
  end
end