Class: Nfsadmin::Tasks
- Inherits:
-
Object
- Object
- Nfsadmin::Tasks
- Defined in:
- lib/nfsadmin/tasks.rb
Class Method Summary collapse
- .create_share(exportsfile, location, address, options, createlocation) ⇒ Object
- .delete_share(exportsfile, location) ⇒ Object
- .get_share(exportsfile, location) ⇒ Object
- .get_shares(exportsfile) ⇒ Object
- .list_shares(exportsfile, output_type) ⇒ Object
- .print_share(exportsfile, location, output_type) ⇒ Object
- .reload_config ⇒ Object
- .restart_service ⇒ Object
- .show_status ⇒ Object
- .start_service ⇒ Object
- .stop_service ⇒ Object
- .write_exports(exportsfile, exports) ⇒ Object
Class Method Details
.create_share(exportsfile, location, address, options, createlocation) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/nfsadmin/tasks.rb', line 88 def self.create_share(exportsfile, location, address, , createlocation) if location.nil? fail 'Location must be specified' else fail 'Location must be an absolute path' unless (Pathname.new(location)).absolute? end if address.nil? fail 'Address must be specified' end if .nil? fail 'Options must be specified' end if createlocation FileUtils::mkdir_p location unless File.directory?(location) end share = { :location => location, :acl => [{ :address => address, :options => }] } shares = get_shares(exportsfile) existingshare = shares.find { |s| s[:location] == location } if existingshare.nil? shares << share write_exports(exportsfile, shares) puts 'Share created' else puts 'Share already exists. Use nfsadmin export modify to change it. Skipping' end end |
.delete_share(exportsfile, location) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/nfsadmin/tasks.rb', line 120 def self.delete_share(exportsfile, location) if location.nil? fail 'Location must be specified with -l or --location=' end shares = get_shares(exportsfile) share = shares.find { |share| share[:location] == location } if share.nil? fail "#{location} not found" else shares.delete(share) write_exports(exportsfile, shares) puts "Deleted #{location}" end end |
.get_share(exportsfile, location) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/nfsadmin/tasks.rb', line 135 def self.get_share(exportsfile, location) if location.nil? fail 'Location must be specified with -l or --location=' end shares = get_shares(exportsfile) shares.find { |share| share[:location] == location } end |
.get_shares(exportsfile) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nfsadmin/tasks.rb', line 19 def self.get_shares(exportsfile) shares = [] location = '' begin file = File.open(exportsfile, 'r') rescue fail 'No exports configuration could be found' end file.readlines.each do |line| share = {} acl = [] parts = line.split parts.each do |part| entry = {} if (Pathname.new(part)).absolute? location = part else subparts = part.split('(') entry[:address] = subparts[0] entry[:options] = subparts[1].sub!(/\)/, '') acl << entry end end share[:location] = location share[:acl] = acl shares << share end file.close return shares end |
.list_shares(exportsfile, output_type) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nfsadmin/tasks.rb', line 68 def self.list_shares(exportsfile, output_type) exports = get_shares(exportsfile) if output_type.downcase == 'text' # Output plain text exports.each do |share| printf('%-40s', share[:location]) share[:acl].each do |acl| print acl[:address] + '(' + acl[:options] + ')' + ' ' end print "\n" end STDOUT.flush elsif output_type.downcase == 'json' # Output JSON puts JSON.generate({ :exports => exports }) else fail "#{output_type} is an invalid output format. Must one of: text, json" end end |
.print_share(exportsfile, location, output_type) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/nfsadmin/tasks.rb', line 143 def self.print_share(exportsfile, location, output_type) share = get_share(exportsfile, location) if share.nil? fail 'share not found' end if output_type.downcase == 'text' printf('%-40s', share[:location]) share[:acl].each do |acl| print acl[:address] + '(' + acl[:options] + ')' + ' ' end print "\n" STDOUT.flush elsif output_type.downcase == 'json' puts JSON.generate(share) else fail "#{output_type} is an invalid output format. Must one of: text, json" end end |
.reload_config ⇒ Object
178 179 180 |
# File 'lib/nfsadmin/tasks.rb', line 178 def self.reload_config system "#{@service_cmd} nfs reload" end |
.restart_service ⇒ Object
174 175 176 |
# File 'lib/nfsadmin/tasks.rb', line 174 def self.restart_service system "#{@service_cmd} nfs restart" end |
.show_status ⇒ Object
162 163 164 |
# File 'lib/nfsadmin/tasks.rb', line 162 def self.show_status system "#{@service_cmd} nfs status" end |
.start_service ⇒ Object
166 167 168 |
# File 'lib/nfsadmin/tasks.rb', line 166 def self.start_service system "#{@service_cmd} nfs start" end |
.stop_service ⇒ Object
170 171 172 |
# File 'lib/nfsadmin/tasks.rb', line 170 def self.stop_service system "#{@service_cmd} nfs stop" end |
.write_exports(exportsfile, exports) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/nfsadmin/tasks.rb', line 50 def self.write_exports(exportsfile, exports) begin file = File.open(exportsfile, File::WRONLY|File::CREAT) file.truncate(0) rescue fail "Could not write to exports configuration #{exportsfile}" end exports.each do |share| file.print(share[:location] + ' ') share[:acl].each do |acl| file.print acl[:address].to_s + '(' + acl[:options].to_s + ')' + ' ' end file.print "\n" end file.flush file.close end |