Class: Banalize::Files
- Inherits:
-
Object
- Object
- Banalize::Files
- Includes:
- Singleton
- Defined in:
- lib/banalize/files.rb
Overview
Class for handling policies files: reading and loading.
Class Method Summary (collapse)
-
+ (Hash) files
Get list of all policy files installed in banalize.
-
+ (Object) policies
Load and populate policies list with configuration of each policy.
-
+ (Object) shell_config(bash)
Read configuration from Bash shell policy and return it as Hash.
Class Method Details
+ (Hash) files
Get list of all policy files installed in banalize.
Policies can come from Banalize distribution in
./lib/policies
directory or suppplied by user from
~/.banalize/policies
directory. This allows exteding Banalize
by creating own policies without need to repackage gem.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/banalize/files.rb', line 26 def self.files all = Dir.glob("#{File.dirname(File.dirname(__FILE__))}/policies/*") all += Dir.glob("#{Banalize::USER[:policies]}/*") if Dir.exists? Banalize::USER[:policies] all = all.grep(%r{/[^\.]}) ruby = all.dup.keep_if { |x| x=~ /\.rb$/ } @@files ||= { all: all, ruby: ruby, other: (all - ruby) } end |
+ (Object) policies
Load and populate policies list with configuration of each policy.
For Ruby policies it requries each file and then calls #config method for it.
47 48 49 50 51 52 53 |
# File 'lib/banalize/files.rb', line 47 def self.policies files[:ruby].each { |f| require f } @policies ||= (files[:other].map { |f| shell_config f }) + Banalize.policies.map(&:config) end |
+ (Object) shell_config(bash)
Read configuration from Bash shell policy and return it as Hash
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/banalize/files.rb', line 60 def self.shell_config bash yaml = %x{ #{bash} config 2>&1 } abort "Can not execute policy file #{bash}: \n ERROR #{yaml} " unless $?.exitstatus == 0 hash = YAML.load(yaml) rescue "Can not load YAML #{yaml}" abort "Loaded policy metdata is not Hash: #{hash.to_s}" unless hash.is_a? Hash hash.merge!({ path: bash, policy: File.basename(bash).to_sym }) Policy::DEFAULT.merge Hash[hash.map { |k, v| [k.to_sym, v] }] end |