Class: HostList
- Inherits:
-
Object
- Object
- HostList
- Defined in:
- lib/hostlist.rb
Instance Method Summary collapse
-
#export_ansible(filename = '/etc/ansible/hosts') ⇒ Object
Export the host list as an ansible hosts file.
-
#generate_db(db) ⇒ Object
Read in yaml file and cache to database.
-
#initialize(yaml: '/etc/hostlist.yaml', cache: '~/.hostlist.db') ⇒ HostList
constructor
A new instance of HostList.
-
#keys ⇒ Object
Return all the keys in the database.
-
#list(tags) ⇒ Object
Return array based on tags.
-
#open_db ⇒ Object
Open cache database.
-
#print_db ⇒ Object
Print the contents of the database for debugging purposes.
Constructor Details
#initialize(yaml: '/etc/hostlist.yaml', cache: '~/.hostlist.db') ⇒ HostList
Returns a new instance of HostList.
8 9 10 11 |
# File 'lib/hostlist.rb', line 8 def initialize(yaml: '/etc/hostlist.yaml', cache: '~/.hostlist.db') @yaml = yaml @db_filename = cache end |
Instance Method Details
#export_ansible(filename = '/etc/ansible/hosts') ⇒ Object
Export the host list as an ansible hosts file.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/hostlist.rb', line 97 def export_ansible(filename = '/etc/ansible/hosts') db = open_db output = [] begin db.keys.each do |key| next if key == 'md5' output << "[#{key}]" db[key].each do |host| output << host end end output = output.join("\n") if filename and File.writable? filename File.open(filename, 'w') { |f| f.write(output) } else puts output end ensure db.close end end |
#generate_db(db) ⇒ Object
Read in yaml file and cache to database
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/hostlist.rb', line 76 def generate_db(db) db.clear db[:all] = [] hosts = YAML.load_file(@yaml) hosts.each do |key, value| value['tags'].each do |tag| if db.has_key? tag db[tag] += key. else db[tag] = key. end end # Add servers to :all # TODO: Figure out why this isn't working. db[:all] is always an empty array. key..each { |host| db[:all] += [host] unless db[:all].include?(host) } end db[:md5] = Digest::MD5.file(@yaml).to_s db.flush end |
#keys ⇒ Object
Return all the keys in the database.
41 42 43 44 45 46 47 48 49 |
# File 'lib/hostlist.rb', line 41 def keys db = open_db begin keys = db.keys ensure db.close end return keys end |
#list(tags) ⇒ Object
Return array based on tags. Tags can be a string or array of strings.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hostlist.rb', line 14 def list() = [] if .is_a? String # Verify we have the most recent data in our daybreak cache. db = open_db begin # Collect all the tags from the database hosts = [] if .empty? hosts = db[:all] else .each do |tag| if db.has_key? tag if hosts == [] hosts = db[tag] else hosts = hosts & db[tag] end end end end ensure db.close end return hosts end |
#open_db ⇒ Object
Open cache database
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hostlist.rb', line 64 def open_db db = Daybreak::DB.new File.(@db_filename) if db.has_key? :md5 file_md5 = Digest::MD5.file @yaml generate_db(db) unless file_md5 == db[:md5] else generate_db(db) end return db end |
#print_db ⇒ Object
Print the contents of the database for debugging purposes
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hostlist.rb', line 52 def print_db db = open_db begin db.keys.each do |key| puts "db[#{key}] = #{db[key]}" end ensure db.close end end |