knife-hosts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-hosts.gemspec
4
+ gemspec
@@ -0,0 +1,16 @@
1
+
2
+ This software is licensed under the Apache 2 license, quoted below.
3
+
4
+ Copyright 2013 Matt Greensmith <matt@mattgreensmith.net>
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
+ use this file except in compliance with the License. You may obtain a copy of
8
+ the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
+ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
+ License for the specific language governing permissions and limitations under
16
+ the License.
@@ -0,0 +1,29 @@
1
+ # Knife::Hosts
2
+
3
+ Knife plugin to print node names and IPs formatted for inclusion in a hosts file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'knife-hosts'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install knife-hosts
18
+
19
+ ## Usage
20
+
21
+ `knife hosts [QUERY]`
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "knife-hosts/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "knife-hosts"
7
+ gem.version = KnifeHosts::VERSION
8
+ gem.authors = ["Matt Greensmith"]
9
+ gem.email = ["matt@mattgreensmith.net"]
10
+ gem.description = %q{Knife plugin to print node names and IPs formatted for inclusion in a hosts file.}
11
+ gem.summary = %q{Knife plugin to format nodes for a hosts file.}
12
+ gem.homepage = "http://github.com/mgreensmith/knife-hosts"
13
+
14
+ gem.add_development_dependency "bundler", ">= 1.0.0"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,92 @@
1
+ # Hosts is a knife plugin to create /etc/hosts listings for your nodes.
2
+ # Author: Matt Greensmith
3
+ #
4
+ # Usage: 'knife hosts' and then copy the output to your /etc/hosts
5
+ # You can also use a search query to limit the output.
6
+ #
7
+ # It adds friendly aliases a couple of ways:
8
+ #
9
+ # 1. It strips trailing domain elements (default 2) from the end of node names and adds an alias, eg
10
+ # 10.1.1.1 foo.bar.com foo
11
+ # You can override the number of domain elements stripped with the -d --drop-elements option,
12
+ # and disable it completely with -d 0.
13
+ #
14
+ # 2. Rackspace prefaces the hostname of physical nodes with an identifying number, eg 000000-foo.bar.com
15
+ # We strip this number and add the leftover host name as an alias, eg:
16
+ # 10.1.1.1 000000-foo.bar.com foo.bar.com
17
+ # If you happen to name your nodes with a leading number and then a hyphen, you may want to disable
18
+ # this behavior with -i --ignore-strip-rackspace option.
19
+
20
+ require 'chef/knife'
21
+
22
+ # A quick helper
23
+ class String
24
+ def numeric?
25
+ Float(self) != nil rescue false
26
+ end
27
+ end
28
+
29
+ module KnifeHosts
30
+
31
+ class Hosts < Chef::Knife
32
+
33
+ deps do
34
+ require 'chef/search/query'
35
+ end
36
+
37
+ banner "knife hosts QUERY (options)"
38
+
39
+ option :drop_elements,
40
+ :short => '-d NUM_ELEMENTS',
41
+ :long => '--drop-elements NUM_ELEMENTS',
42
+ :description => "Number of trailing elements (dot-separated) to drop from a node name. (Default: 2)",
43
+ :default => 2,
44
+ :proc => Proc.new { |d| d.to_i }
45
+
46
+ option :ignore_strip_rackspace,
47
+ :short => '-i',
48
+ :long => '--ignore-strip-rackspace',
49
+ :description => "Do not strip leading rackspace identifier (Do Not: nnnnnn-foo.bar.com -> foo.bar.com)",
50
+ :boolean => true
51
+
52
+ def run
53
+ all_nodes = []
54
+ q = Chef::Search::Query.new
55
+ query = @name_args[0] || "*:*"
56
+ q.search(:node, query) do |node|
57
+ all_nodes << node
58
+ end
59
+
60
+ all_nodes.each do |node|
61
+ if node.has_key?("ec2")
62
+ ipaddress = node['ec2']['public_ipv4']
63
+ else
64
+ ipaddress = node['ipaddress']
65
+ end
66
+
67
+ if ipaddress
68
+ output_line = Array.new
69
+ output_line << ipaddress
70
+ output_line << node.name
71
+
72
+ working_name = node.name
73
+
74
+ unless config[:ignore_strip_rackspace]
75
+ n = working_name.split(/-/)
76
+ if n[0].numeric?
77
+ working_name = n.drop(1).join("-")
78
+ output_line << working_name
79
+ end
80
+ end
81
+
82
+ if config[:drop_elements] != 0
83
+ n = working_name.split(/\./)
84
+ output_line << n.first(n.length - config[:drop_elements]).join(".")
85
+ end
86
+
87
+ puts output_line.join(" ")
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,4 @@
1
+ module KnifeHosts
2
+ VERSION = "0.0.1"
3
+ MAJOR, MINOR, TINY = VERSION.split('.')
4
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-hosts
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Matt Greensmith
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-01-25 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 1
28
+ - 0
29
+ - 0
30
+ version: 1.0.0
31
+ name: bundler
32
+ requirement: *id001
33
+ prerelease: false
34
+ description: Knife plugin to print node names and IPs formatted for inclusion in a hosts file.
35
+ email:
36
+ - matt@mattgreensmith.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - knife-hosts.gemspec
50
+ - lib/chef/knife/hosts.rb
51
+ - lib/knife-hosts/version.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/mgreensmith/knife-hosts
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.6
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Knife plugin to format nodes for a hosts file.
82
+ test_files: []
83
+