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
20
21
# File 'lib/osctl/exportfs/operations/export/add.rb', line 15

def initialize(server, export)
  super()
  @server = server
  @export = export
  @cfg = server.open_config
  @sys = OsCtl::Lib::Sys.new
end

Instance Attribute Details

#cfgObject (readonly, protected)

Returns the value of attribute cfg.



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

def cfg
  @cfg
end

#exportObject (readonly, protected)

Returns the value of attribute export.



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

def export
  @export
end

#serverObject (readonly, protected)

Returns the value of attribute server.



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

def server
  @server
end

#sysObject (readonly, protected)

Returns the value of attribute sys.



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

def sys
  @sys
end

Instance Method Details

#add_to_exportsObject (protected)

Add the export to the database



48
49
50
51
# File 'lib/osctl/exportfs/operations/export/add.rb', line 48

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



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
85
86
# File 'lib/osctl/exportfs/operations/export/add.rb', line 55

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



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

def execute
  unless Dir.exist?(export.dir)
    raise "dir #{export.dir} not found"
  end

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

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

  add_to_exports

  return unless server.running?

  enable_share(propagate: ex.nil?)
end