frosty 0.0.1.alpha1

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ N2Y3YjU1MjM3YWM2MmM3ZDdiZGE0NjUzMzg4MzEyOWVhNzRlNTcyZA==
5
+ data.tar.gz: !binary |-
6
+ MzgxNWM0YjM5MjEzNjkyMjEwNWUxYzQ5N2U1YTMwNzcxYTZiNDY1Mg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OWEyNzhlNzJmNDZlYzM0OWEwNzQwMGYwYmIwMWQ2N2U3Y2FmZTZhZWYxZTQ4
10
+ ZjMwMzRiODFkMDBmMDQ3ODY5NTZlODFmMmExM2VhZDNiZDFjM2QwNWJkYzZl
11
+ NDA4NWY5YTYwZmQ3MmMyMDkwMzZmNWE0NDRlN2FjNmYyMGY3NTY=
12
+ data.tar.gz: !binary |-
13
+ MDgwYTkzMmI3NmI1NzhmY2M5YTJjN2I0NWVlYWU0NjI3MjhhZTQwMTY3Mzkz
14
+ YzViYmZhOWE3ZTBiMjUxYTk1ZWYxMzI5ODQ1MzFiNzZjNmMyYjg5NjhjNDVk
15
+ YWNkMzQwNzMxZTNmNTM5YzZhNjU4MTJjMzRkZGIzYmViNjIwYmY=
@@ -0,0 +1,61 @@
1
+ require 'frosty/service'
2
+ require 'frosty/node'
3
+ require 'frosty/etcd'
4
+ require 'etcd'
5
+
6
+ class Frosty
7
+ include Frosty::Etcd
8
+
9
+ attr_accessor :environment, :server, :port
10
+ def initialize(server = '127.0.0.1', port = '4001', options ={})
11
+ @server = server
12
+ @port = port
13
+ @environment = options['environment'] || 'default'
14
+ end
15
+
16
+ def can_connect?
17
+ connect
18
+ ret = true
19
+ begin
20
+ @connection.get('/')
21
+ rescue
22
+ ret = false
23
+ end
24
+ ret
25
+ end
26
+
27
+ def connect
28
+ @connection = ::Etcd.client(:host => @server,
29
+ :port => @port)
30
+ end
31
+
32
+ def services
33
+ array_of_keys("/#{@environment}/services/")
34
+ end
35
+
36
+ def nodes
37
+ array_of_keys("/#{@environment}/nodes/")
38
+ end
39
+
40
+ # This probably needs to be re-worked
41
+ def array_of_keys(key)
42
+ n = query(@connection, key)
43
+ if n.nil?
44
+ []
45
+ else
46
+ a = []
47
+ n.each do |value|
48
+ a.push(value.key.gsub(key, ''))
49
+ end
50
+ a
51
+ end
52
+ end
53
+
54
+ def service(name)
55
+ Frosty::Service.new(name, @environment, @connection)
56
+ end
57
+
58
+ def node(name)
59
+ Frosty::Node.new(name, @environment, @connection)
60
+ end
61
+ end
@@ -0,0 +1,29 @@
1
+ class Frosty
2
+ module Etcd
3
+ def query(connection, key)
4
+ begin
5
+ connection.get(key)
6
+ rescue
7
+ nil
8
+ end
9
+ end
10
+
11
+ def write(connection, key, value)
12
+ begin
13
+ connection.set(key, value)
14
+ rescue
15
+ nil
16
+ end
17
+ end
18
+
19
+ def hashify(responses)
20
+ h = {}
21
+ responses.each do |response|
22
+ unless response.value.nil?
23
+ h[response.key.split('/').pop] = response.value
24
+ end
25
+ end
26
+ h
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ require 'frosty/etcd'
2
+
3
+ class Frosty
4
+ class Node
5
+ include Frosty::Etcd
6
+
7
+ def initialize(ip, environment, connection)
8
+ @ip = ip
9
+ @connection = connection
10
+ @base_path = "/#{environment}/nodes/#{ip}/"
11
+ refresh
12
+ end
13
+
14
+ def exists?
15
+ if query(@connection, @base_path).nil?
16
+ false
17
+ else
18
+ true
19
+ end
20
+ end
21
+
22
+ def fetch_services
23
+ ret = []
24
+ if exists?
25
+ services = query(@connection, @base_path + 'services')
26
+ unless services.nil?
27
+ ret = services.value.split(',')
28
+ end
29
+ end
30
+ ret
31
+ end
32
+
33
+ def refresh
34
+ @services = fetch_services
35
+ end
36
+
37
+ def add_service(name)
38
+ @services.push name
39
+ end
40
+
41
+ def services
42
+ @services
43
+ end
44
+
45
+ def save
46
+ services = @services.join(',')
47
+ write(@connection, @base_path + 'services', services)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,105 @@
1
+ require_relative 'service/endpoint'
2
+ require 'frosty/etcd'
3
+
4
+ class Frosty
5
+ class Service
6
+ include Frosty::Etcd
7
+
8
+ attr_accessor :role, :status
9
+ def initialize(name, environment, connection)
10
+ @name = name
11
+ @connection = connection
12
+ @base_path = "/#{environment}/services/#{name}/"
13
+ @endpoints = fetch_endpoints
14
+ end
15
+
16
+ def exists?
17
+ if query(@connection, @base_path).nil?
18
+ false
19
+ else
20
+ true
21
+ end
22
+ end
23
+
24
+ def has_master?
25
+ ret = false
26
+ if self.exists?
27
+ @endpoints.each do |ep|
28
+ if ep.role == 'master'
29
+ ret = true
30
+ break
31
+ end
32
+ end
33
+ end
34
+ ret
35
+ end
36
+
37
+ # return ip of master
38
+ def masters
39
+ m = []
40
+ if self.has_master?
41
+ @endpoints.each do |ep|
42
+ if ep.role == 'master'
43
+ m.push ep.ip
44
+ end
45
+ end
46
+ end
47
+ m
48
+ end
49
+
50
+ def refresh
51
+ @endpoints = fetch_endpoints
52
+ end
53
+
54
+ def fetch_endpoints
55
+ ret = []
56
+ endpoints = query(@connection, @base_path + 'endpoints')
57
+ unless endpoints.nil?
58
+ endpoints.each do |e|
59
+ ret.push endpoint(e.key.split('/').pop)
60
+ end
61
+ end
62
+ ret
63
+ end
64
+
65
+ def endpoints
66
+ ret = []
67
+ @endpoints.each do |endpoint|
68
+ ret.push endpoint.ip
69
+ end
70
+ ret
71
+ end
72
+
73
+ def endpoint(ip)
74
+ resp = query(@connection, @base_path + "endpoints/#{ip}")
75
+
76
+ ep = Frosty::Service::Endpoint.new(ip)
77
+ if resp.nil?
78
+ if has_master?
79
+ ep.role = 'slave'
80
+ else
81
+ ep.role = 'master'
82
+ end
83
+ else
84
+ info = hashify(resp)
85
+ ep.port = info['port']
86
+ ep.role = info['role']
87
+ ep.status = info['status']
88
+ end
89
+ ep
90
+ end
91
+
92
+ def save_endpoint(ep)
93
+ if ep.port.nil?
94
+ raise TypeError, "Endpoint port must be either a string or an integer"
95
+ end
96
+ path = @base_path + "endpoints/#{ep.ip}/"
97
+ write(@connection, path + "port", ep.port)
98
+ write(@connection, path + "role", ep.role)
99
+ unless ep.status.nil?
100
+ write(@connection, path + "status", ep.status)
101
+ end
102
+ refresh
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,11 @@
1
+ class Frosty
2
+ class Service
3
+ class Endpoint
4
+ attr_accessor :status, :port, :role
5
+ attr_reader :ip
6
+ def initialize(ip)
7
+ @ip = ip
8
+ end
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frosty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - Jim Rosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: etcd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Provides Libraries to query and save to etcd based on the enigma project
28
+ email: jarosser06@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/frosty.rb
34
+ - lib/frosty/node.rb
35
+ - lib/frosty/service.rb
36
+ - lib/frosty/etcd.rb
37
+ - lib/frosty/service/endpoint.rb
38
+ homepage: https://github.com/enigmaproject/frosty
39
+ licenses:
40
+ - mit
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>'
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.1
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.0.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Etcd with Chef
62
+ test_files: []