lita-enhance 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE +19 -0
  6. data/README.md +95 -0
  7. data/Rakefile +6 -0
  8. data/lib/lita-enhance.rb +7 -0
  9. data/lib/lita/handlers/enhance.rb +209 -0
  10. data/lib/lita/handlers/enhance/chef_indexer.rb +133 -0
  11. data/lib/lita/handlers/enhance/enhancer.rb +58 -0
  12. data/lib/lita/handlers/enhance/enhancers/hostname_enhancer.rb +72 -0
  13. data/lib/lita/handlers/enhance/enhancers/instance_id_enhancer.rb +40 -0
  14. data/lib/lita/handlers/enhance/enhancers/ip_enhancer.rb +40 -0
  15. data/lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb +42 -0
  16. data/lib/lita/handlers/enhance/node.rb +57 -0
  17. data/lib/lita/handlers/enhance/node_index.rb +41 -0
  18. data/lib/lita/handlers/enhance/session.rb +77 -0
  19. data/lita-enhance.gemspec +23 -0
  20. data/locales/en.yml +16 -0
  21. data/spec/data/box01.json +214 -0
  22. data/spec/data/box02.json +163 -0
  23. data/spec/data/box03.json +123 -0
  24. data/spec/data/stg-web01.json +89 -0
  25. data/spec/data/web01.json +89 -0
  26. data/spec/lita/handlers/enhance/chef_indexer_spec.rb +18 -0
  27. data/spec/lita/handlers/enhance/enhancer_example.rb +16 -0
  28. data/spec/lita/handlers/enhance/enhancers/hostname_enhancer_spec.rb +58 -0
  29. data/spec/lita/handlers/enhance/enhancers/instance_id_enhancer_spec.rb +31 -0
  30. data/spec/lita/handlers/enhance/enhancers/ip_enhancer_spec.rb +48 -0
  31. data/spec/lita/handlers/enhance/enhancers/mac_address_enhancer_spec.rb +32 -0
  32. data/spec/lita/handlers/enhance/node_index_spec.rb +33 -0
  33. data/spec/lita/handlers/enhance/node_spec.rb +51 -0
  34. data/spec/lita/handlers/enhance/session_spec.rb +48 -0
  35. data/spec/lita/handlers/enhance_spec.rb +136 -0
  36. data/spec/spec_helper.rb +64 -0
  37. metadata +168 -0
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "stg-web01",
3
+ "chef_environment": "_default",
4
+ "run_list": [
5
+ "role[web]"
6
+ ],
7
+ "normal": {
8
+ "tags": [
9
+
10
+ ]
11
+ },
12
+ "default": {
13
+ },
14
+ "override": {
15
+ },
16
+ "automatic": {
17
+ "ipaddress": "10.254.74.122",
18
+ "macaddress": "22:00:0A:FE:4A:80",
19
+ "ec2": {
20
+ "ami_id": "ami-f6ec60c6",
21
+ "ami_launch_index": "0",
22
+ "ami_manifest_path": "ubuntu-us-west-2/images/ubuntu-lucid-10.04-amd64-server-20120403.manifest.xml",
23
+ "block_device_mapping_ami": "sda1",
24
+ "block_device_mapping_ephemeral0": "sda2",
25
+ "block_device_mapping_root": "/dev/sda1",
26
+ "block_device_mapping_swap": "sda3",
27
+ "hostname": "ip-10-254-74-122.us-west-2.compute.internal",
28
+ "instance_action": "none",
29
+ "instance_id": "i-fe4cddcc",
30
+ "instance_type": "m1.small",
31
+ "kernel_id": "aki-ace26f9c",
32
+ "local_hostname": "ip-10-254-74-122.us-west-2.compute.internal",
33
+ "local_ipv4": "10.254.74.122",
34
+ "mac": "22:00:0A:FE:4A:80",
35
+ "metrics_vhostmd": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
36
+ "network_interfaces_macs": {
37
+ "22:00:0a:fe:4a:80": {
38
+ "device_number": "0",
39
+ "local_hostname": "ip-10-254-74-122.us-west-2.compute.internal",
40
+ "local_ipv4s": "10.254.74.122",
41
+ "mac": "22:00:0a:fe:4a:80",
42
+ "owner_id": "622089341825",
43
+ "public_hostname": "ec2-54-214-188-38.us-west-2.compute.amazonaws.com",
44
+ "public_ipv4s": "54.214.188.38"
45
+ }
46
+ },
47
+ "placement_availability_zone": "us-west-2b",
48
+ "profile": "default-paravirtual",
49
+ "public_hostname": "ec2-54-214-188-38.us-west-2.compute.amazonaws.com",
50
+ "public_ipv4": "54.214.188.38",
51
+ "reservation_id": "r-994854ac",
52
+ "security_groups": [
53
+ "web"
54
+ ],
55
+ "userdata": null
56
+ },
57
+ "cloud": {
58
+ "public_ips": [
59
+ "54.214.188.38"
60
+ ],
61
+ "private_ips": [
62
+ "10.254.74.122"
63
+ ],
64
+ "public_ipv4": "54.214.188.38",
65
+ "public_hostname": "ec2-54-214-188-38.us-west-2.compute.amazonaws.com",
66
+ "local_ipv4": "10.254.74.122",
67
+ "local_hostname": "ip-10-254-74-122.us-west-2.compute.internal",
68
+ "provider": "ec2"
69
+ },
70
+ "cloud_v2": {
71
+ "public_ipv4_addrs": [
72
+ "54.214.188.38"
73
+ ],
74
+ "local_ipv4_addrs": [
75
+ "10.254.74.122"
76
+ ],
77
+ "provider": "ec2",
78
+ "public_hostname": "ec2-54-214-188-38.us-west-2.compute.amazonaws.com",
79
+ "local_hostname": "ip-10-254-74-122.us-west-2.compute.internal",
80
+ "public_ipv4": "54.214.188.38",
81
+ "local_ipv4": "10.254.74.122"
82
+ },
83
+ "hostname": "stg-web01",
84
+ "machinename": "stg-web01",
85
+ "fqdn": "stg-web01.example.com",
86
+ "domain": "example.com"
87
+ }
88
+ }
89
+
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "web01",
3
+ "chef_environment": "_default",
4
+ "run_list": [
5
+ "role[web]"
6
+ ],
7
+ "normal": {
8
+ "tags": [
9
+
10
+ ]
11
+ },
12
+ "default": {
13
+ },
14
+ "override": {
15
+ },
16
+ "automatic": {
17
+ "ipaddress": "10.254.74.123",
18
+ "macaddress": "22:00:0A:FE:4A:81",
19
+ "ec2": {
20
+ "ami_id": "ami-f6ec60c6",
21
+ "ami_launch_index": "0",
22
+ "ami_manifest_path": "ubuntu-us-west-2/images/ubuntu-lucid-10.04-amd64-server-20120403.manifest.xml",
23
+ "block_device_mapping_ami": "sda1",
24
+ "block_device_mapping_ephemeral0": "sda2",
25
+ "block_device_mapping_root": "/dev/sda1",
26
+ "block_device_mapping_swap": "sda3",
27
+ "hostname": "ip-10-254-74-123.us-west-2.compute.internal",
28
+ "instance_action": "none",
29
+ "instance_id": "i-fe4cddcd",
30
+ "instance_type": "m1.small",
31
+ "kernel_id": "aki-ace26f9c",
32
+ "local_hostname": "ip-10-254-74-123.us-west-2.compute.internal",
33
+ "local_ipv4": "10.254.74.123",
34
+ "mac": "22:00:0A:FE:4A:81",
35
+ "metrics_vhostmd": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
36
+ "network_interfaces_macs": {
37
+ "22:00:0a:fe:4a:81": {
38
+ "device_number": "0",
39
+ "local_hostname": "ip-10-254-74-123.us-west-2.compute.internal",
40
+ "local_ipv4s": "10.254.74.123",
41
+ "mac": "22:00:0a:fe:4a:81",
42
+ "owner_id": "622089341825",
43
+ "public_hostname": "ec2-54-214-188-39.us-west-2.compute.amazonaws.com",
44
+ "public_ipv4s": "54.214.188.39"
45
+ }
46
+ },
47
+ "placement_availability_zone": "us-west-2b",
48
+ "profile": "default-paravirtual",
49
+ "public_hostname": "ec2-54-214-188-39.us-west-2.compute.amazonaws.com",
50
+ "public_ipv4": "54.214.188.39",
51
+ "reservation_id": "r-994854ac",
52
+ "security_groups": [
53
+ "web"
54
+ ],
55
+ "userdata": null
56
+ },
57
+ "cloud": {
58
+ "public_ips": [
59
+ "54.214.188.39"
60
+ ],
61
+ "private_ips": [
62
+ "10.254.74.123"
63
+ ],
64
+ "public_ipv4": "54.214.188.39",
65
+ "public_hostname": "ec2-54-214-188-39.us-west-2.compute.amazonaws.com",
66
+ "local_ipv4": "10.254.74.123",
67
+ "local_hostname": "ip-10-254-74-123.us-west-2.compute.internal",
68
+ "provider": "ec2"
69
+ },
70
+ "cloud_v2": {
71
+ "public_ipv4_addrs": [
72
+ "54.214.188.39"
73
+ ],
74
+ "local_ipv4_addrs": [
75
+ "10.254.74.123"
76
+ ],
77
+ "provider": "ec2",
78
+ "public_hostname": "ec2-54-214-188-39.us-west-2.compute.amazonaws.com",
79
+ "local_hostname": "ip-10-254-74-123.us-west-2.compute.internal",
80
+ "public_ipv4": "54.214.188.39",
81
+ "local_ipv4": "10.254.74.123"
82
+ },
83
+ "hostname": "web01",
84
+ "machinename": "web01",
85
+ "fqdn": "web01.example.com",
86
+ "domain": "example.com"
87
+ }
88
+ }
89
+
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Enhance::ChefIndexer do
4
+ include_context 'mocks'
5
+ include_context 'redis'
6
+
7
+ let(:indexer) { described_class.new(redis, {}) }
8
+
9
+ it 'should be able to create Node from a Chef::Node' do
10
+ node = indexer.node_from_chef_node(west2_chef_node)
11
+
12
+ expect(node.name).to eq('box01')
13
+ expect(node.dc).to eq('us-west-2b')
14
+ expect(node.environment).to eq('_default')
15
+ expect(node.fqdn).to eq('box01.example.com')
16
+ expect(node.last_seen_at.to_f).to be_within(5).of(Time.now.to_f)
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.shared_examples 'an enhancer' do
4
+ it 'should be able to render a node' do
5
+ expect(enhancer.render(nodes.first, 1)).to eq('*box01*')
6
+ expect(enhancer.render(nodes.first, 2)).to eq('*box01 (us-west-2b)*')
7
+ end
8
+
9
+ it 'should render an old node slightly differently' do
10
+ node = nodes.first
11
+ node.last_seen_at = Time.now - 24 * 60 * 60
12
+
13
+ expect(enhancer.render(node, 1)).to eq('¿box01?')
14
+ expect(enhancer.render(node, 2)).to eq('¿box01 (us-west-2b)?')
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ require 'lita/handlers/enhance/enhancer_example'
4
+
5
+ describe Lita::Handlers::Enhance::HostnameEnhancer do
6
+ include_context 'indexed'
7
+
8
+ let(:enhancer) { Lita::Handlers::Enhance::HostnameEnhancer.new(redis) }
9
+ let(:sub_klass) { Lita::Handlers::Enhance::Substitution }
10
+
11
+ it_should_behave_like 'an enhancer'
12
+
13
+ it 'should enhance a string with an EC2 public hostname in it' do
14
+ message = 'before ec2-54-214-188-37.us-west-2.compute.amazonaws.com after'
15
+ substitutions = enhancer.enhance!(message, 1)
16
+ expect(substitutions).to eq [sub_klass.new(7...56, '*box01*')]
17
+ end
18
+
19
+ it 'should enhance a string with an EC2 short public hostname in it' do
20
+ message = 'before ec2-54-214-188-37 after'
21
+ substitutions = enhancer.enhance!(message, 1)
22
+ expect(substitutions).to eq [sub_klass.new(7...24, '*box01*')]
23
+ end
24
+
25
+ it 'should enhance a string with an EC2 localhost hostname in it' do
26
+ message = 'before ip-10-254-74-121.us-west-2.compute.internal after'
27
+ substitutions = enhancer.enhance!(message, 1)
28
+ expect(substitutions).to eq [sub_klass.new(7...50, '*box01*')]
29
+ end
30
+
31
+ it 'should enhance a string with an EC2 localhost hostname in it' do
32
+ message = 'before ip-10-254-74-121 after'
33
+ substitutions = enhancer.enhance!(message, 1)
34
+ expect(substitutions).to eq [sub_klass.new(7...23, '*box01*')]
35
+ end
36
+
37
+ it 'should enhance a string with a custom long hostname in it' do
38
+ message = 'before box01.example.com after'
39
+ substitutions = enhancer.enhance!(message, 1)
40
+ expect(substitutions).to eq [sub_klass.new(7...24, '*box01*')]
41
+ end
42
+
43
+ it 'should enhance a string with a custom short hostname in it' do
44
+ message = 'before box01 after'
45
+ substitutions = enhancer.enhance!(message, 1)
46
+ expect(substitutions).to eq [sub_klass.new(7...12, '*box01*')]
47
+ end
48
+
49
+ it 'should not enhance a string with an unknown hostname' do
50
+ message = 'foo.bar.com'
51
+ substitutions = enhancer.enhance!(message, 1)
52
+ expect(substitutions).to be_empty
53
+ end
54
+
55
+ it 'should return a custom response to to_s' do
56
+ expect(enhancer.to_s).to match /short hostnames.*long hostnames indexed/
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ require 'lita/handlers/enhance/enhancer_example'
4
+
5
+ describe Lita::Handlers::Enhance::InstanceIdEnhancer do
6
+ include_context 'indexed'
7
+
8
+ let(:enhancer) { Lita::Handlers::Enhance::InstanceIdEnhancer.new(redis) }
9
+ let(:sub_klass) { Lita::Handlers::Enhance::Substitution }
10
+
11
+ it_should_behave_like 'an enhancer'
12
+
13
+ it 'should enhance a string with EC2 instance IDs in it' do
14
+ message = 'before i-fe4cddcb i-f4ff6aff after'
15
+ substitutions = enhancer.enhance!(message, 1)
16
+ expect(substitutions).to contain_exactly(
17
+ sub_klass.new(7...17, '*box01*'),
18
+ sub_klass.new(18...28, '*box02*')
19
+ )
20
+ end
21
+
22
+ it 'should not enhance an unrecognized EC2 instance ID' do
23
+ message = 'i-f00bac12'
24
+ substitutions = enhancer.enhance!(message, 1)
25
+ expect(substitutions).to be_empty
26
+ end
27
+
28
+ it 'should return a custom response to to_s' do
29
+ expect(enhancer.to_s).to match /instance IDs indexed/
30
+ end
31
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ require 'lita/handlers/enhance/enhancer_example'
4
+
5
+ describe Lita::Handlers::Enhance::IpEnhancer do
6
+ include_context 'indexed'
7
+
8
+ let(:enhancer) { Lita::Handlers::Enhance::IpEnhancer.new(redis) }
9
+ let(:sub_klass) { Lita::Handlers::Enhance::Substitution }
10
+
11
+ it_should_behave_like 'an enhancer'
12
+
13
+ it 'should enhance a string with EC2 external IPs in it' do
14
+ message = 'before 54.214.188.37 184.169.229.1 after'
15
+ substitutions = enhancer.enhance!(message, 1)
16
+ expect(substitutions).to contain_exactly(
17
+ sub_klass.new(7...20, '*box01*'),
18
+ sub_klass.new(21...34, '*box02*')
19
+ )
20
+ end
21
+
22
+ it 'should enhance a string with EC internal IPs in it' do
23
+ message = 'before 10.254.74.121 10.196.75.1 after'
24
+ substitutions = enhancer.enhance!(message, 1)
25
+ expect(substitutions).to contain_exactly(
26
+ sub_klass.new(7...20, '*box01*'),
27
+ sub_klass.new(21...32, '*box02*')
28
+ )
29
+ end
30
+
31
+ it 'should enhance a string with Linode IPs in it' do
32
+ message = 'before 192.155.85.2 after'
33
+ substitutions = enhancer.enhance!(message, 1)
34
+ expect(substitutions).to contain_exactly(
35
+ sub_klass.new(7...19, '*box03*')
36
+ )
37
+ end
38
+
39
+ it 'should not enhance an unrecognized IP' do
40
+ message = '127.0.0.1'
41
+ substitutions = enhancer.enhance!(message, 1)
42
+ expect(substitutions).to be_empty
43
+ end
44
+
45
+ it 'should return a custom response to to_s' do
46
+ expect(enhancer.to_s).to match /IPs indexed/
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ require 'lita/handlers/enhance/enhancer_example'
4
+
5
+ describe Lita::Handlers::Enhance::MacAddressEnhancer do
6
+ include_context 'indexed'
7
+
8
+ let(:enhancer) { Lita::Handlers::Enhance::MacAddressEnhancer.new(redis) }
9
+ let(:sub_klass) { Lita::Handlers::Enhance::Substitution }
10
+
11
+ it_should_behave_like 'an enhancer'
12
+
13
+ it 'should enhance a string with MAC addresses in it' do
14
+ message = 'before 22:00:0A:FE:4A:79 F2:3C:91:56:A2:00 after'
15
+ substitutions = enhancer.enhance!(message, 1)
16
+ expect(substitutions).to contain_exactly(
17
+ sub_klass.new(7...24, '*box01*'),
18
+ sub_klass.new(25...42, '*box03*')
19
+ )
20
+ end
21
+
22
+ it 'should not enhance an unrecognized MAC address' do
23
+ message = '00:00:00:00:00:00'
24
+ substitutions = enhancer.enhance!(message, 1)
25
+ expect(substitutions).to be_empty
26
+ end
27
+
28
+ it 'should return a custom response for to_s' do
29
+ expect(enhancer.to_s).to match /MAC addresses indexed/
30
+ end
31
+ end
32
+
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ require 'lita/handlers/enhance/node_index'
4
+
5
+ describe Lita::Handlers::Enhance::NodeIndex do
6
+ include_context 'mocks'
7
+ include_context 'redis'
8
+
9
+ let(:index) { Lita::Handlers::Enhance::NodeIndex.new(redis, 'fqdn') }
10
+ let(:node) { nodes.first }
11
+
12
+ it 'should be able to add a node to the index' do
13
+ index['127.0.0.1'] = node
14
+ end
15
+
16
+ it 'should be able to find a node in the index' do
17
+ index['127.0.0.1'] = node
18
+ new_node = index['127.0.0.1']
19
+ expect(new_node.name).to eq(node.name)
20
+ end
21
+
22
+ it 'should return nil if no node is found in the index' do
23
+ expect(index['127.0.0.5']).to be_nil
24
+ end
25
+
26
+ it 'should be able to return its size' do
27
+ expect(index.size).to eq(0)
28
+ nodes.each do |node|
29
+ index[node.name] = node
30
+ end
31
+ expect(index.size).to eq(nodes.length)
32
+ end
33
+ end
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Enhance::Node do
4
+ include_context 'mocks'
5
+ include_context 'redis'
6
+
7
+ let(:node) do
8
+ described_class.new.tap do |n|
9
+ n.name = 'box01'
10
+ n.dc = 'us-west-2b'
11
+ n.environment = '_default'
12
+ n.fqdn = 'box01.example.com'
13
+ n.last_seen_at = Time.now
14
+ end
15
+ end
16
+
17
+ it "should be able to save it's state in a JSON blob" do
18
+ node_json = node.as_json
19
+ expect(node_json).to include(
20
+ name: 'box01',
21
+ dc: 'us-west-2b',
22
+ environment: '_default',
23
+ fqdn: 'box01.example.com'
24
+ )
25
+ expect(node_json[:last_seen_at]).to_not be_nil
26
+ end
27
+
28
+ it "should be able to store and load itself" do
29
+ node.store!(redis)
30
+
31
+ new_node = Lita::Handlers::Enhance::Node.load(redis, 'box01')
32
+ expect(new_node).to_not be_nil
33
+ expect(new_node.name).to eq(node.name)
34
+ expect(new_node.last_seen_at).to be_kind_of(Time)
35
+ end
36
+
37
+ it 'should be able to render itself at differing levels of detail' do
38
+ expect(node.render(1)).to eq('box01')
39
+ expect(node.render(2)).to eq('box01 (us-west-2b)')
40
+ expect(node.render(3)).to eq('box01 (us-west-2b, _default)')
41
+ expect(node.render(4)).to match /box01 \(us-west-2b, _default, last seen .*\)/
42
+ expect(node.render(5)).to match /box01\.example\.com \(us-west-2b, _default, last seen .*\)/
43
+ end
44
+
45
+ it 'should know if it is old' do
46
+ expect(node.old?).to be(false)
47
+
48
+ node.last_seen_at = Time.now - 24 * 60 * 60
49
+ expect(node.old?).to be(true)
50
+ end
51
+ end