Module: Hookit::Helper::NFS

Defined in:
lib/hookit/helper/nfs.rb

Instance Method Summary collapse

Instance Method Details

#clean_writables(dirs) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hookit/helper/nfs.rb', line 37

def clean_writables(dirs)
  begin
    dirs = dirs.map{|i| i.to_s}
  rescue
    dirs = [dirs].map{|i| i.to_s}
  end
  dirs = remove_empty(dirs)
  dirs = filter_offensive(dirs)
  dirs = strip_leading_slash(dirs)
  dirs = strip_trailing_slash(dirs)
  dirs = remove_nested(dirs)
  dirs.uniq
end

#filter_offensive(dirs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hookit/helper/nfs.rb', line 58

def filter_offensive(dirs)
  dirs.inject([]) do |res, elem|
    if elem[0] != '.'
      # ensure not going up a directory
      # ensure spaces are intended
      # ensure directory is not . or /
      unless elem =~ /(\*|\.?\.\/|(?<!\\)\s)|^\.$|^\/$/
        res << elem
      end
    end
    res
  end       
end

#net_dirs(payload) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hookit/helper/nfs.rb', line 15

def net_dirs(payload)
  key     = payload[:storage].keys.first
  boxfile = payload[:boxfile]

  if boxfile[:shared_writable_dirs]
    {
      key => boxfile[:shared_writable_dirs]
    }
  elsif boxfile[:network_dirs].is_a? String
    {
      key => [boxfile[:network_dirs]]
    }
  elsif boxfile[:network_dirs].is_a? Array
    {
      key => boxfile[:network_dirs]
    }
  else
    boxfile[:network_dirs] ||= {}
  end

end

#remove_empty(dirs) ⇒ Object



51
52
53
54
55
56
# File 'lib/hookit/helper/nfs.rb', line 51

def remove_empty(dirs)
  dirs.inject([]) do |res, elem|
    res << elem if elem && elem != ""
    res
  end
end

#remove_nested(dirs) ⇒ Object

this removes nested mounts like: tmp/ tmp/cache/ tmp/assets/

and keeps tmp/



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hookit/helper/nfs.rb', line 96

def remove_nested(dirs)
  dirs.sort!
  dirs.inject([]) do |res, elem|
    overlap = false
    # now make sure parents dont contain children
    res.each do |parent|
      if elem =~ /^#{parent}\//
        overlap = true
      end
    end
    res << elem if not overlap
    res
  end
end

#sanitize_network_dirs(payload) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/hookit/helper/nfs.rb', line 5

def sanitize_network_dirs(payload)
  net_dirs = net_dirs(payload)

  net_dirs.each do |component, dirs|
    net_dirs[component] = clean_writables(dirs)
  end

  net_dirs
end

#strip_leading_slash(dirs) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/hookit/helper/nfs.rb', line 72

def strip_leading_slash(dirs)
  dirs.inject([]) do |res, elem|
    if elem[0] == '/'
      elem.slice!(0)
    end 
    res << elem
  end
end

#strip_trailing_slash(dirs) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/hookit/helper/nfs.rb', line 81

def strip_trailing_slash(dirs)
  dirs.inject([]) do |res, elem|
    if elem[-1] == '/'
      elem.slice!(-1)
    end
    res << elem
  end
end