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, user, group, mode) ⇒ 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, user, group, mode) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/nfsadmin/tasks.rb', line 95 def self.create_share(exportsfile, location, address, , createlocation, user, group, mode) 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) FileUtils::chown user, nil, location unless user.nil? FileUtils::chown nil, group, location unless group.nil? FileUtils::chmod mode, location unless mode.nil? 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/nfsadmin/tasks.rb', line 130 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
145 146 147 148 149 150 151 |
# File 'lib/nfsadmin/tasks.rb', line 145 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
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 49 50 51 52 53 |
# File 'lib/nfsadmin/tasks.rb', line 20 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 stat = File.stat(location) share[:location] = location share[:owner] = Etc.getpwuid(stat.uid).name share[:group] = Etc.getgrgid(stat.gid).name share[:mode] = stat.mode.to_s(8)[-3,3] share[:acl] = acl shares << share end file.close return shares end |
.list_shares(exportsfile, output_type) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nfsadmin/tasks.rb', line 73 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(share[:owner]) unless share[:owner].nil? print(':' + share[:group] + ' ') unless share[:group].nil? print(share[:mode] + "\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
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/nfsadmin/tasks.rb', line 153 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
188 189 190 |
# File 'lib/nfsadmin/tasks.rb', line 188 def self.reload_config system "#{@service_cmd} nfs reload" end |
.restart_service ⇒ Object
184 185 186 |
# File 'lib/nfsadmin/tasks.rb', line 184 def self.restart_service system "#{@service_cmd} nfs restart" end |
.show_status ⇒ Object
172 173 174 |
# File 'lib/nfsadmin/tasks.rb', line 172 def self.show_status system "#{@service_cmd} nfs status" end |
.start_service ⇒ Object
176 177 178 |
# File 'lib/nfsadmin/tasks.rb', line 176 def self.start_service system "#{@service_cmd} nfs start" end |
.stop_service ⇒ Object
180 181 182 |
# File 'lib/nfsadmin/tasks.rb', line 180 def self.stop_service system "#{@service_cmd} nfs stop" end |
.write_exports(exportsfile, exports) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/nfsadmin/tasks.rb', line 55 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 |