Class: DnsOne::Util
Constant Summary collapse
- DOM_REGEX =
/^[a-z0-9]+([\-\.][a-z0-9]+)*\.[a-z]{2,32}$/i
Class Method Summary collapse
- .const_underscore(name) ⇒ Object
- .die(msg) ⇒ Object
- .ensure_sytemd ⇒ Object
- .has_systemd? ⇒ Boolean
- .hash_to_ostruct_deep(hash) ⇒ Object
- .init_logger(logdev, level = Logger::WARN, shift_age = 10, shift_size = 2**20) ⇒ Object
- .last_mod(constant) ⇒ Object
- .match_root(stat) ⇒ Object
- .run(cmd) ⇒ Object
Class Method Details
.const_underscore(name) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/dns_one/util.rb', line 36 def const_underscore name name = name.to_s.dup name.gsub!('::', '/') name.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') name.gsub!(/([a-z\d])([A-Z])/,'\1_\2') name.tr!("-", "_") name.downcase! name end |
.die(msg) ⇒ Object
6 7 8 9 |
# File 'lib/dns_one/util.rb', line 6 def die msg Global.logger.fatal msg exit 1 end |
.ensure_sytemd ⇒ Object
21 22 23 24 25 26 |
# File 'lib/dns_one/util.rb', line 21 def ensure_sytemd unless has_systemd? STDERR.puts "Systemd not available. Aborting." exit 1 end end |
.has_systemd? ⇒ Boolean
16 17 18 19 |
# File 'lib/dns_one/util.rb', line 16 def has_systemd? File.exist?(`which systemctl`.strip) && File.writable?('/lib/systemd/system') end |
.hash_to_ostruct_deep(hash) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dns_one/util.rb', line 46 def hash_to_ostruct_deep hash os = OpenStruct.new hash.each_pair{ |k, v| if v.is_a? Hash os[k] = hash_to_ostruct_deep v else os[k] = v end } os end |
.init_logger(logdev, level = Logger::WARN, shift_age = 10, shift_size = 2**20) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dns_one/util.rb', line 58 def init_logger logdev, level = Logger::WARN, shift_age = 10, shift_size = 2**20 if logdev.is_a? String begin if File.exists? logdev File.writable? logdev else FileUtils.mkdir_p File.dirname(logdev) end rescue => e $stderr.puts "#{e.desc}\nCannot open log file #{logdev}. Will use STDOUT." logdev = $stdout end end l = Logger.new logdev, shift_age, shift_size l.level = level l end |
.last_mod(constant) ⇒ Object
32 33 34 |
# File 'lib/dns_one/util.rb', line 32 def last_mod constant constant.to_s.split('::').last end |
.match_root(stat) ⇒ Object
28 29 30 |
# File 'lib/dns_one/util.rb', line 28 def match_root stat stat.uid == 0 && stat.gid == 0 end |
.run(cmd) ⇒ Object
11 12 13 14 |
# File 'lib/dns_one/util.rb', line 11 def run cmd puts "Running #{cmd}..." system cmd end |