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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nfsadmin/tasks.rb', line 75

def self.create_share(exportsfile, location, address, options)
  if location.nil?
    fail 'Location must be specified'
  end
  if address.nil?
    fail 'Address must be specified'
  end
  if options.nil?
    fail 'Options must be specified'
  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)
  else
    puts 'Share already exists. Use nfsadmin export modify to change it. Skipping'
  end
end

.delete_share(exportsfile, location) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/nfsadmin/tasks.rb', line 101

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 }
  shares.delete(share)
  write_exports(exportsfile, shares)
end

.get_share(exportsfile, location, output_type) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/nfsadmin/tasks.rb', line 111

def self.get_share(exportsfile, location, output_type)
  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 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

.get_shares(exportsfile) ⇒ Object



6
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
# File 'lib/nfsadmin/tasks.rb', line 6

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



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

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

.reload_configObject



147
148
149
# File 'lib/nfsadmin/tasks.rb', line 147

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

.restart_serviceObject



143
144
145
# File 'lib/nfsadmin/tasks.rb', line 143

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

.show_statusObject



131
132
133
# File 'lib/nfsadmin/tasks.rb', line 131

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

.start_serviceObject



135
136
137
# File 'lib/nfsadmin/tasks.rb', line 135

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

.stop_serviceObject



139
140
141
# File 'lib/nfsadmin/tasks.rb', line 139

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

.write_exports(exportsfile, exports) ⇒ Object



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

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