Class: Nfsadmin::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/nfsadmin/tasks.rb

Class Method Summary collapse

Class Method Details

.create_share(exportsfile, location, address, options, createlocation) ⇒ Object



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
101
102
103
104
105
106
# File 'lib/nfsadmin/tasks.rb', line 76

def self.create_share(exportsfile, location, address, options, 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 options.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 => 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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nfsadmin/tasks.rb', line 108

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



123
124
125
126
127
128
129
# File 'lib/nfsadmin/tasks.rb', line 123

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nfsadmin/tasks.rb', line 7

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nfsadmin/tasks.rb', line 56

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


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nfsadmin/tasks.rb', line 131

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_configObject



166
167
168
# File 'lib/nfsadmin/tasks.rb', line 166

def self.reload_config
  `/usr/sbin/service nfs reload`
end

.restart_serviceObject



162
163
164
# File 'lib/nfsadmin/tasks.rb', line 162

def self.restart_service
  `/usr/sbin/service nfd restart`
end

.show_statusObject



150
151
152
# File 'lib/nfsadmin/tasks.rb', line 150

def self.show_status
  `/usr/sbin/service nfs status`
end

.start_serviceObject



154
155
156
# File 'lib/nfsadmin/tasks.rb', line 154

def self.start_service
  `/usr/sbin/service nfs start`
end

.stop_serviceObject



158
159
160
# File 'lib/nfsadmin/tasks.rb', line 158

def self.stop_service
  `/usr/sbin/service nfs stop`
end

.write_exports(exportsfile, exports) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nfsadmin/tasks.rb', line 38

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