chef-extensions 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -3,3 +3,9 @@ require 'rdiscount'
3
3
  guard 'rocco', :run_on => [:start, :change], :dir => 'docs' do
4
4
  watch(%r{^lib/.*\.rb$})
5
5
  end
6
+
7
+ guard 'minitest', :notify => false do
8
+ watch(%r|^test/(.*)_test\.rb|)
9
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
10
+ watch(%r|^test/test_helper\.rb|) { "test" }
11
+ end
data/Rakefile CHANGED
@@ -1,9 +1,18 @@
1
1
  require 'bundler/gem_tasks'
2
+
2
3
  require 'rake/clean'
4
+ require 'rake/testtask'
5
+
3
6
  require 'rdiscount'
4
7
  require 'rocco/tasks'
5
8
  Rocco::make 'docs/'
6
9
 
10
+ Rake::TestTask.new do |t|
11
+ t.pattern = 'test/**/*_test.rb'
12
+ end
13
+
14
+ task :default => [:test]
15
+
7
16
  task :version do
8
17
  require 'chef'
9
18
  puts Chef::Extensions::VERSION
@@ -18,8 +18,12 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_development_dependency "chef"
22
+ s.add_development_dependency 'guard-minitest'
23
+ s.add_development_dependency "guard-rocco"
24
+ s.add_development_dependency 'minitest'
25
+ s.add_development_dependency 'pry'
21
26
  s.add_development_dependency "rdiscount"
22
27
  s.add_development_dependency "rocco"
23
- s.add_development_dependency "guard-rocco"
24
- s.add_development_dependency "chef"
28
+ s.add_development_dependency 'turn'
25
29
  end
@@ -1,108 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta http-equiv="content-type" content="text/html;charset=utf-8">
5
- <title>chef-extensions.rb</title>
6
- <link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
7
- </head>
8
- <body>
9
- <div id='container'>
10
- <div id="background"></div>
11
- <div id="jump_to">
12
- Jump To &hellip;
13
- <div id="jump_wrapper">
14
- <div id="jump_page">
15
- <a class="source" href="chef-extensions.html">chef-extensions.rb</a>
16
- <a class="source" href="chef-extensions/version.html">version.rb</a>
17
- </div>
18
- </div>
19
- </div>
20
- <table cellspacing=0 cellpadding=0>
21
- <thead>
22
- <tr>
23
- <th class=docs><h1>chef-extensions.rb</h1></th>
24
- <th class=code></th>
25
- </tr>
26
- </thead>
27
- <tbody>
28
- <tr id='section-1'>
29
- <td class=docs>
30
- <div class="pilwrap">
31
- <a class="pilcrow" href="#section-1">&#182;</a>
32
- </div>
33
-
34
- </td>
35
- <td class=code>
36
- <div class='highlight'><pre><span class="nb">require</span> <span class="s1">&#39;chef-extensions/version&#39;</span>
37
- <span class="nb">require</span> <span class="s1">&#39;securerandom&#39;</span>
38
-
39
- <span class="k">module</span> <span class="nn">Chef</span>
40
- <span class="k">module</span> <span class="nn">Extensions</span>
41
- <span class="kp">extend</span> <span class="nb">self</span></pre></div>
42
- </td>
43
- </tr>
44
- <tr id='section-2'>
45
- <td class=docs>
46
- <div class="pilwrap">
47
- <a class="pilcrow" href="#section-2">&#182;</a>
48
- </div>
49
- <p><strong>Ping Google&rsquo;s DNS.</strong> <br />
50
- If it doesn&rsquo;t hear back within 1s, it&rsquo;s safe to
51
- assume that we don&rsquo;t have internet connectivity</p>
52
- </td>
53
- <td class=code>
54
- <div class='highlight'><pre> <span class="k">def</span> <span class="nf">wan_up?</span>
55
- <span class="sb">`ping -c 1 8.8.8.8 -W 2`</span><span class="o">.</span><span class="n">include?</span> <span class="s2">&quot;1 received&quot;</span>
56
- <span class="k">end</span></pre></div>
57
- </td>
58
- </tr>
59
- <tr id='section-3'>
60
- <td class=docs>
61
- <div class="pilwrap">
62
- <a class="pilcrow" href="#section-3">&#182;</a>
63
- </div>
64
- <p><strong>Checks if vagrant user exists.</strong> <br />
65
- If it does, we&rsquo;re running in the context of a VirtualBox VM, provisioned
66
- through Vagrant.</p>
67
- </td>
68
- <td class=code>
69
- <div class='highlight'><pre> <span class="k">def</span> <span class="nf">vagrant?</span>
70
- <span class="vi">@vagrant</span> <span class="o">||=</span> <span class="sb">`id vagrant 2&gt;&amp;1`</span><span class="o">.</span><span class="n">index</span> <span class="s1">&#39;uid&#39;</span>
71
- <span class="k">end</span></pre></div>
72
- </td>
73
- </tr>
74
- <tr id='section-4'>
75
- <td class=docs>
76
- <div class="pilwrap">
77
- <a class="pilcrow" href="#section-4">&#182;</a>
78
- </div>
79
- <p><strong>Get this instance&rsquo;s ID.</strong> <br />
80
- If within 1s it gets a response which starts with &lsquo;i-&rsquo;, it&rsquo;s safe to
81
- assume that we&rsquo;re running within the context of an EC2 instance.</p>
82
- </td>
83
- <td class=code>
84
- <div class='highlight'><pre> <span class="k">def</span> <span class="nf">ec2?</span>
85
- <span class="vi">@ec2</span> <span class="o">||=</span> <span class="sb">`curl --connect-timeout 1 http://169.254.169.254/2011-01-01/meta-data/instance-id 2&gt;&amp;1`</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="s1">&#39;i-&#39;</span><span class="p">)</span>
86
- <span class="k">end</span></pre></div>
87
- </td>
88
- </tr>
89
- <tr id='section-5'>
90
- <td class=docs>
91
- <div class="pilwrap">
92
- <a class="pilcrow" href="#section-5">&#182;</a>
93
- </div>
94
- <p><strong>Generate base64 password using SecureRandom.</strong> <br />
95
- Trims the ending ==.</p>
96
-
97
- </td>
98
- <td class=code>
99
- <div class='highlight'><pre> <span class="k">def</span> <span class="nf">password</span><span class="p">(</span><span class="n">length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
100
- <span class="no">SecureRandom</span><span class="o">.</span><span class="n">base64</span><span class="p">(</span><span class="n">length</span><span class="o">+</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">tr</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
101
- <span class="k">end</span>
102
- <span class="k">end</span>
103
- <span class="k">end</span></pre></div>
104
- </td>
105
- </tr>
106
- </table>
107
- </div>
108
- </body>
@@ -0,0 +1,68 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html;charset=utf-8">
5
+ <title>ip.rb</title>
6
+ <link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
7
+ </head>
8
+ <body>
9
+ <div id='container'>
10
+ <div id="background"></div>
11
+ <div id="jump_to">
12
+ Jump To &hellip;
13
+ <div id="jump_wrapper">
14
+ <div id="jump_page">
15
+ <a class="source" href="../chef-extensions.html">chef-extensions.rb</a>
16
+ <a class="source" href="ec2.html">ec2.rb</a>
17
+ <a class="source" href="ip.html">ip.rb</a>
18
+ <a class="source" href="version.html">version.rb</a>
19
+ </div>
20
+ </div>
21
+ </div>
22
+ <table cellspacing=0 cellpadding=0>
23
+ <thead>
24
+ <tr>
25
+ <th class=docs><h1>ip.rb</h1></th>
26
+ <th class=code></th>
27
+ </tr>
28
+ </thead>
29
+ <tbody>
30
+ <tr id='section-1'>
31
+ <td class=docs>
32
+ <div class="pilwrap">
33
+ <a class="pilcrow" href="#section-1">&#182;</a>
34
+ </div>
35
+
36
+
37
+ </td>
38
+ <td class=code>
39
+ <div class='highlight'><pre><span class="nb">require</span> <span class="s1">&#39;socket&#39;</span>
40
+
41
+ <span class="k">class</span> <span class="nc">Chef</span>
42
+ <span class="k">module</span> <span class="nn">Extensions</span>
43
+ <span class="k">module</span> <span class="nn">IP</span>
44
+ <span class="kp">extend</span> <span class="nb">self</span>
45
+
46
+ <span class="k">def</span> <span class="nf">all</span>
47
+ <span class="vi">@all</span> <span class="o">||=</span> <span class="no">Socket</span><span class="o">.</span><span class="n">ip_address_list</span>
48
+ <span class="k">end</span>
49
+
50
+ <span class="k">def</span> <span class="nf">ipv4</span>
51
+ <span class="n">all</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:ipv4?</span><span class="p">)</span>
52
+ <span class="k">end</span>
53
+
54
+ <span class="k">def</span> <span class="nf">ipv6</span>
55
+ <span class="n">all</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:ipv6?</span><span class="p">)</span>
56
+ <span class="k">end</span>
57
+
58
+ <span class="k">def</span> <span class="nf">addresses</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span>
59
+ <span class="nb">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:ip_address</span><span class="p">)</span>
60
+ <span class="k">end</span>
61
+ <span class="k">end</span>
62
+ <span class="k">end</span>
63
+ <span class="k">end</span></pre></div>
64
+ </td>
65
+ </tr>
66
+ </table>
67
+ </div>
68
+ </body>
@@ -1,4 +1,6 @@
1
1
  require 'chef-extensions/version'
2
+ require 'chef-extensions/ec2'
3
+ require 'chef-extensions/ip'
2
4
  require 'securerandom'
3
5
 
4
6
  class Chef
@@ -19,11 +21,11 @@ class Chef
19
21
  @vagrant ||= `id vagrant 2>&1`.index 'uid'
20
22
  end
21
23
 
22
- # **Get this instance's ID.** <br />
23
- # If within 1s it gets a response which starts with 'i-', it's safe to
24
- # assume that we're running within the context of an EC2 instance.
24
+ # **Check if this instance ID is a valid one.** <br />
25
+ # If it starts with 'i-', it's safe to assume that we're running within
26
+ # the context of an EC2 instance.
25
27
  def ec2?
26
- @ec2 ||= `curl --connect-timeout 1 http://169.254.169.254/2011-01-01/meta-data/instance-id 2>&1`.include?('i-')
28
+ @ec2 ||= EC2.instance_id.include?('i-')
27
29
  end
28
30
 
29
31
  # **Generate base64 password using SecureRandom.** <br />
@@ -0,0 +1,13 @@
1
+ class Chef
2
+ module Extensions
3
+ module EC2
4
+ extend self
5
+
6
+ # **Get the instance ID from AWS's metadata service**
7
+ # If we don't hear back within 1 second, time out.
8
+ def instance_id
9
+ `curl --connect-timeout 1 http://169.254.169.254/2011-01-01/meta-data/instance-id 2>&1`.chomp
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ require 'socket'
2
+
3
+ class Chef
4
+ module Extensions
5
+ module IP
6
+ extend self
7
+
8
+ def all
9
+ @all ||= Socket.ip_address_list
10
+ end
11
+
12
+ def ipv4
13
+ all.select(&:ipv4?)
14
+ end
15
+
16
+ def ipv6
17
+ all.select(&:ipv6?)
18
+ end
19
+
20
+ def addresses(type)
21
+ self.send(type).map(&:ip_address)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,6 +1,6 @@
1
1
  class Chef
2
2
  module Extensions
3
3
  # Gem version
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'chef-extensions/ip'
3
+
4
+ class Chef
5
+ module Extensions
6
+ describe IP do
7
+ describe "all" do
8
+ it "returns all local ips" do
9
+ IP.all.each do |ip|
10
+ ip.must_be_instance_of Addrinfo
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "ipv4" do
16
+ it "only returns IPv4 ips" do
17
+ IP.ipv4.each do |ip|
18
+ ip.must_be :ipv4?
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "ipv6" do
24
+ it "only returns IPv6 ips" do
25
+ IP.ipv6.each do |ip|
26
+ ip.must_be :ipv6?
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "addresses" do
32
+ it "returns ips of a certain type as strings" do
33
+ IP.addresses(:ipv4).each do |ip|
34
+ ip.must_match /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ require 'minitest/unit'
6
+ require 'minitest/spec'
7
+
8
+ require 'turn/colorize'
9
+ require 'turn/controller'
10
+ require 'turn/runners/minirunner'
11
+
12
+ Turn.config do |config|
13
+ config.format = :pretty
14
+ end
15
+
16
+ MiniTest::Unit.runner = Turn::MiniRunner.new
17
+ MiniTest::Unit.autorun
18
+
19
+ require 'pry'
20
+
21
+ $:.push File.expand_path("../../lib", __FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-12 00:00:00.000000000Z
12
+ date: 2012-01-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rdiscount
16
- requirement: &2157561920 !ruby/object:Gem::Requirement
15
+ name: chef
16
+ requirement: &2164269240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2157561920
24
+ version_requirements: *2164269240
25
25
  - !ruby/object:Gem::Dependency
26
- name: rocco
27
- requirement: &2157561320 !ruby/object:Gem::Requirement
26
+ name: guard-minitest
27
+ requirement: &2164268620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2157561320
35
+ version_requirements: *2164268620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-rocco
38
- requirement: &2157560900 !ruby/object:Gem::Requirement
38
+ requirement: &2160320740 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,54 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2157560900
46
+ version_requirements: *2160320740
47
47
  - !ruby/object:Gem::Dependency
48
- name: chef
49
- requirement: &2157560480 !ruby/object:Gem::Requirement
48
+ name: minitest
49
+ requirement: &2160319940 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2160319940
58
+ - !ruby/object:Gem::Dependency
59
+ name: pry
60
+ requirement: &2160319280 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2160319280
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdiscount
71
+ requirement: &2160318580 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2160318580
80
+ - !ruby/object:Gem::Dependency
81
+ name: rocco
82
+ requirement: &2160317500 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *2160317500
91
+ - !ruby/object:Gem::Dependency
92
+ name: turn
93
+ requirement: &2160315680 !ruby/object:Gem::Requirement
50
94
  none: false
51
95
  requirements:
52
96
  - - ! '>='
@@ -54,7 +98,7 @@ dependencies:
54
98
  version: '0'
55
99
  type: :development
56
100
  prerelease: false
57
- version_requirements: *2157560480
101
+ version_requirements: *2160315680
58
102
  description: Commands useful for checking internet connectivity, VM presence etc.
59
103
  email:
60
104
  - gerhard@lazu.co.uk
@@ -71,9 +115,14 @@ files:
71
115
  - chef-extensions.gemspec
72
116
  - docs/index.html
73
117
  - docs/lib/chef-extensions.html
118
+ - docs/lib/chef-extensions/ip.html
74
119
  - docs/lib/chef-extensions/version.html
75
120
  - lib/chef-extensions.rb
121
+ - lib/chef-extensions/ec2.rb
122
+ - lib/chef-extensions/ip.rb
76
123
  - lib/chef-extensions/version.rb
124
+ - test/chef-extensions/ip_test.rb
125
+ - test/test_helper.rb
77
126
  homepage: http://gchef.github.com/chef-extensions
78
127
  licenses: []
79
128
  post_install_message:
@@ -98,5 +147,7 @@ rubygems_version: 1.8.10
98
147
  signing_key:
99
148
  specification_version: 3
100
149
  summary: Chef extensions, can be used stand-alone
101
- test_files: []
150
+ test_files:
151
+ - test/chef-extensions/ip_test.rb
152
+ - test/test_helper.rb
102
153
  has_rdoc: