hostlist 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +33 -0
- data/Rakefile +24 -0
- data/bin/hostlist +5 -0
- data/hostlist.gemspec +17 -0
- data/hostlist.yaml.sample +24 -0
- data/lib/hostlist/thor.rb +34 -0
- data/lib/hostlist.rb +93 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99786e75f67e7ee45170151e74536aff51cf468c
|
4
|
+
data.tar.gz: 451b40aa2f6eab1bf4d82ba1b4e29baea2a33fa6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65af6d55ceb088b9f4e9b7b73cdf038dee6aac0c7ff099aacd3b84951c79bb87fb849da2a095a555ebc5eb8fcaa3dafa1ade743c053922d823903051fd821726
|
7
|
+
data.tar.gz: aa3bb6bbdf9b441d3f83cf1938aa6b62b0933796e433952370f18f33c7a7b6ca8fdc7e0daa895c4b1c773c47af3861dde2c6815771931ac57dc1771e271a82e6
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
## Description
|
2
|
+
|
3
|
+
hostlist is a small script that keeps a text list of hosts with tags and allows you to search that list. The purpose of this script is to facilitate other scripts. For example, if you have a script that needs the hostnames of only production weblogic servers, you would query hostlist first and then loop through the output.
|
4
|
+
|
5
|
+
## Configuration
|
6
|
+
|
7
|
+
All FQDNs are entered into the configuration file. This file is in YAML format and uses brace expansion very similar to how BASH does brace expansion. A list of values would be comma separated (eg: proxy{1,2} expands to proxy1, proxy2). A range is represented with two periods (eg: proxy{1..3} expands to proxy1, proxy2, proxy3).
|
8
|
+
|
9
|
+
The default location for the configuration file is /etc/hostlist.yaml
|
10
|
+
|
11
|
+
```yaml
|
12
|
+
'extpxy00{1..4}.example.com':
|
13
|
+
tags: [ 'prd', 'pxy', 'extpxy']
|
14
|
+
'extpxy00{1,2}.pre.example.com':
|
15
|
+
tags: [ 'pre', 'pxy', 'extpxy']
|
16
|
+
'intpxy00{1,2}.example.com':
|
17
|
+
tags: [ 'prd', 'pxy', 'intpxy']
|
18
|
+
```
|
19
|
+
|
20
|
+
## Listing Hosts
|
21
|
+
|
22
|
+
You can list hosts by running hostlist with a list of tags you want to filter with. You can use any number of tags separated by a space. Using the above sample configuration, running "hostlist prd extpxy" would yield:
|
23
|
+
|
24
|
+
```
|
25
|
+
extpxy001.example.com
|
26
|
+
extpxy002.example.com
|
27
|
+
extpxy003.example.com
|
28
|
+
extpxy004.example.com
|
29
|
+
```
|
30
|
+
|
31
|
+
## Showing Tags
|
32
|
+
|
33
|
+
If you want to see a list of all the tags in the cached database, you can run "hostlist show". This list is not sorted in any way.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
task :default => [:build]
|
2
|
+
task :test => [:build, :install]
|
3
|
+
|
4
|
+
task :build do
|
5
|
+
system("gem build ./hostlist.gemspec")
|
6
|
+
end
|
7
|
+
|
8
|
+
task :install do
|
9
|
+
gem = Dir['*.gem'].last
|
10
|
+
system("sudo gem install #{gem}")
|
11
|
+
end
|
12
|
+
|
13
|
+
task :push do
|
14
|
+
gem = Dir['*.gem'].last
|
15
|
+
system("gem push #{gem}")
|
16
|
+
end
|
17
|
+
|
18
|
+
task :console do
|
19
|
+
exec "irb -I ./lib"
|
20
|
+
end
|
21
|
+
|
22
|
+
task :run do
|
23
|
+
ruby "-Ilib", 'bin/hostlist'
|
24
|
+
end
|
data/bin/hostlist
ADDED
data/hostlist.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = 'hostlist'
|
3
|
+
gem.version = '1.0.0'
|
4
|
+
gem.licenses = ['MIT']
|
5
|
+
gem.date = '2015-06-27'
|
6
|
+
gem.summary = 'Host List Generator'
|
7
|
+
gem.description = 'Generates list of hosts based on tags.'
|
8
|
+
gem.authors = ['Zan Loy']
|
9
|
+
gem.email = ['zan.loy@gmail.com']
|
10
|
+
gem.homepage = 'https://github.com/zanloy/hostlist'
|
11
|
+
gem.files = `git ls-files`.split("\n") - %w[.gitignore]
|
12
|
+
gem.executables = ['hostlist']
|
13
|
+
|
14
|
+
gem.add_runtime_dependency 'bracecomp', '~> 0.1', '>= 0.1.2'
|
15
|
+
gem.add_runtime_dependency 'daybreak', '~> 0.3', '>= 0.3.0'
|
16
|
+
gem.add_runtime_dependency 'thor', '~> 0.19', '>= 0.19.1'
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
'extpxy001.prd.vbms.vba.va.gov':
|
2
|
+
tags: [ 'prd', 'pxy', 'extpxy']
|
3
|
+
'extpxy002.prd.vbms.vba.va.gov':
|
4
|
+
tags: [ 'prd', 'pxy', 'extpxy']
|
5
|
+
'extpxy003.prd.vbms.vba.va.gov':
|
6
|
+
tags: [ 'prd', 'pxy', 'extpxy']
|
7
|
+
'extpxy004.prd.vbms.vba.va.gov':
|
8
|
+
tags: [ 'prd', 'pxy', 'extpxy']
|
9
|
+
'vbms000.prd.vbms.vba.va.gov':
|
10
|
+
tags: [ 'prd', 'weblogic', 'vbms' ]
|
11
|
+
'vbms001.prd.vbms.vba.va.gov':
|
12
|
+
tags: [ 'prd', 'weblogic', 'vbms' ]
|
13
|
+
'extpxy001.pre.vbms.vba.va.gov':
|
14
|
+
tags: [ 'pre', 'pxy', 'extpxy']
|
15
|
+
'extpxy002.pre.vbms.vba.va.gov':
|
16
|
+
tags: [ 'pre', 'pxy', 'extpxy']
|
17
|
+
'extpxy001.prdtst.vbms.vba.va.gov':
|
18
|
+
tags: [ 'prdtst', 'pxy', 'extpxy']
|
19
|
+
'extpxy002.prdtst.vbms.vba.va.gov':
|
20
|
+
tags: [ 'prdtst', 'pxy', 'extpxy']
|
21
|
+
'extpxy001.perf.vbms.vba.va.gov':
|
22
|
+
tags: [ 'perf', 'pxy', 'extpxy']
|
23
|
+
'extpxy002.perf.vbms.vba.va.gov':
|
24
|
+
tags: [ 'perf', 'pxy', 'extpxy']
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'hostlist'
|
2
|
+
require 'thor'
|
3
|
+
|
4
|
+
class HostListThor < Thor
|
5
|
+
|
6
|
+
default_task :list
|
7
|
+
|
8
|
+
class_option :yaml, type: :string, default: '/etc/hostlist.yaml'
|
9
|
+
class_option :db, type: :string, default: '~/.hostlist.db'
|
10
|
+
|
11
|
+
def method_missing(method, *args)
|
12
|
+
args = ['list', method.to_s] + args
|
13
|
+
HostListThor.start(args)
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'list', 'List hosts based on a tag.'
|
17
|
+
def list(*tags)
|
18
|
+
hostlist = HostList.new(yaml: options[:yaml], cache: options[:db])
|
19
|
+
hostlist.list(tags).each { |host| puts host }
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Show tags', 'Show all the tags in the cache database.'
|
23
|
+
def show
|
24
|
+
hostlist = HostList.new(yaml: options[:yaml], cache: options[:db])
|
25
|
+
hostlist.keys.each { |key| puts key }
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'print_db', 'Print the contents of the database for debugging purposes'
|
29
|
+
def print_db
|
30
|
+
hostlist = HostList.new(yaml: options[:yaml], cache: options[:db])
|
31
|
+
hostlist.print_db
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/hostlist.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'bracecomp'
|
2
|
+
require 'daybreak'
|
3
|
+
require 'digest'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
class HostList
|
7
|
+
|
8
|
+
def initialize(yaml: '/etc/hostlist.yaml', cache: '~/.hostlist.db')
|
9
|
+
@yaml = yaml
|
10
|
+
@db_filename = cache
|
11
|
+
end
|
12
|
+
|
13
|
+
def list(tags)
|
14
|
+
tags = [tags] if tags.is_a? String
|
15
|
+
# Verify we have the most recent data in our daybreak cache.
|
16
|
+
db = open_db
|
17
|
+
begin
|
18
|
+
if db.has_key? :md5
|
19
|
+
file_md5 = Digest::MD5.file @yaml
|
20
|
+
generate_db(db) unless file_md5 == db[:md5]
|
21
|
+
else
|
22
|
+
generate_db(db)
|
23
|
+
end
|
24
|
+
db.load
|
25
|
+
# Collect all the tags from the database
|
26
|
+
hosts = []
|
27
|
+
if tags.empty?
|
28
|
+
hosts = db[:all]
|
29
|
+
else
|
30
|
+
tags.each do |tag|
|
31
|
+
if db.has_key? tag
|
32
|
+
if hosts == []
|
33
|
+
hosts = db[tag]
|
34
|
+
else
|
35
|
+
hosts = hosts & db[tag]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
ensure
|
41
|
+
db.close
|
42
|
+
end
|
43
|
+
return hosts
|
44
|
+
end
|
45
|
+
|
46
|
+
# Return all the keys in the database.
|
47
|
+
def keys
|
48
|
+
db = open_db
|
49
|
+
begin
|
50
|
+
keys = db.keys
|
51
|
+
ensure
|
52
|
+
db.close
|
53
|
+
end
|
54
|
+
return keys
|
55
|
+
end
|
56
|
+
|
57
|
+
# Print the contents of the database for debugging purposes
|
58
|
+
def print_db
|
59
|
+
db = open_db
|
60
|
+
begin
|
61
|
+
db.keys.each do |key|
|
62
|
+
puts "db[#{key}] = #{db[key]}"
|
63
|
+
end
|
64
|
+
ensure
|
65
|
+
db.close
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def open_db
|
70
|
+
Daybreak::DB.new File.expand_path(@db_filename)
|
71
|
+
end
|
72
|
+
|
73
|
+
def generate_db(db)
|
74
|
+
db.clear
|
75
|
+
db[:all] = []
|
76
|
+
hosts = YAML.load_file(@yaml)
|
77
|
+
hosts.each do |key, value|
|
78
|
+
value['tags'].each do |tag|
|
79
|
+
if db.has_key? tag
|
80
|
+
db[tag] += key.expand
|
81
|
+
else
|
82
|
+
db[tag] = key.expand
|
83
|
+
end
|
84
|
+
end
|
85
|
+
# Add servers to :all
|
86
|
+
# TODO: Figure out why this isn't working. db[:all] is always an empty array.
|
87
|
+
key.expand.each { |host| db[:all] += [host] unless db[:all].include?(host) }
|
88
|
+
end
|
89
|
+
db[:md5] = Digest::MD5.file(@yaml).to_s
|
90
|
+
db.flush
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hostlist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zan Loy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bracecomp
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.1.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.1.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: daybreak
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.3'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 0.3.0
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.3'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 0.3.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: thor
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.19'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.19.1
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.19'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 0.19.1
|
73
|
+
description: Generates list of hosts based on tags.
|
74
|
+
email:
|
75
|
+
- zan.loy@gmail.com
|
76
|
+
executables:
|
77
|
+
- hostlist
|
78
|
+
extensions: []
|
79
|
+
extra_rdoc_files: []
|
80
|
+
files:
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/hostlist
|
84
|
+
- hostlist.gemspec
|
85
|
+
- hostlist.yaml.sample
|
86
|
+
- lib/hostlist.rb
|
87
|
+
- lib/hostlist/thor.rb
|
88
|
+
homepage: https://github.com/zanloy/hostlist
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Host List Generator
|
112
|
+
test_files: []
|