boned 0.2.6 → 0.3.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.
data/lib/boned.rb CHANGED
@@ -1,178 +1,46 @@
1
- require 'rack/auth/digest/md5'
2
-
3
- require 'redis'
4
- require 'redis/namespace'
5
- require 'storable'
6
- require 'attic'
7
- require 'gibbler/aliases'
8
- require 'sysinfo'
9
- require 'socket'
10
- require 'uri'
11
- require 'rye'
12
1
 
13
2
  unless defined?(BONED_HOME)
14
3
  BONED_HOME = File.expand_path(File.join(File.dirname(__FILE__), '..') )
15
4
  end
16
5
 
17
- module Boned
18
- unless defined?(APIVERSION)
19
- APIVERSION = 'v1'.freeze
20
- BONED_REDIS = "redis://127.0.0.1:8045/1".freeze
21
- end
22
-
6
+ local_libs = %w{bone}
7
+ local_libs.each { |dir|
8
+ a = File.join(BONED_HOME, '..', '..', 'opensource', dir, 'lib')
9
+ $:.unshift a
10
+ }
11
+
12
+ module Boned
23
13
  module VERSION
24
- MAJOR = 0
25
- MINOR = 2
26
- TINY = 6
27
- PATCH = '001'
28
- def self.inspect; [to_s, PATCH].join('.'); end
29
- def self.to_s; [MAJOR, MINOR, TINY].join('.'); end
30
- def self.to_f; self.to_s.to_f; end
31
- def self.patch; PATCH; end
32
- end
33
-
34
- class Problem < RuntimeError; end
35
- class BadBone < Problem
36
- def message() "No such bone: #{super}" end
37
- end
38
-
39
- @debug = false
40
- @conf = nil
41
- @redis = nil
42
- @redis_thread = nil
43
- @sysinfo = nil
44
- class << self
45
- attr_accessor :debug
46
- attr_reader :conf
47
- def debug?() @debug == true end
48
- def enable_debug() @debug = true end
49
- def disable_debug() @debug = false end
50
- def sysinfo
51
- @sysinfo = SysInfo.new.freeze if @sysinfo.nil?
52
- @sysinfo
14
+ def self.to_s
15
+ load_config
16
+ [@version[:MAJOR], @version[:MINOR], @version[:PATCH]].join('.')
53
17
  end
54
- def redis()
55
- @redis || Boned.connect
18
+ alias_method :inspect, :to_s
19
+ def self.load_config
20
+ require 'yaml'
21
+ @version ||= YAML.load_file(File.join(BONED_HOME, '..', 'VERSION.yml'))
56
22
  end
57
23
  end
58
-
59
- # Connect to Redis and Mongo.
60
- def self.connect(start_redis=true)
61
- update_redis_client_config
62
- ld "CONNECT: start_redis(#{start_redis})"
63
- if start_redis
64
- self.start_redis
65
- abort "No Redis" unless redis_available?
66
- end
67
- @redis = Redis.new @conf[:redis]
68
- end
69
-
70
- def self.start_redis
71
- return if redis_available?
72
- conf_path = File.join(BONED_HOME, 'config', 'redis-server.conf')
73
- ld "REDIS SERVER CONF: #{conf_path}"
74
- @redis_thread = Thread.new do
75
- Rye.shell 'redis-server', conf_path
76
- end
77
- sleep 2 # Give redis time to start.
78
- end
79
-
80
- def self.stop_redis
81
- ld "SHUTDOWN REDIS #{redis_available?}"
82
- ld @redis.inspect
83
- # Shutdown command returns "-ERR operation not permitted" ??
84
- @redis.shutdown if !@redis.nil? && redis_available? rescue nil
85
- return if @redis_thread.nil? || !@redis_thread.alive?
86
- end
87
-
88
- # <tt>require</tt> a library from the vendor directory.
89
- # The vendor directory should be organized such
90
- # that +name+ and +version+ can be used to create
91
- # the path to the library.
92
- #
93
- # e.g.
94
- #
95
- # vendor/httpclient-2.1.5.2/httpclient
96
- #
97
- def self.require_vendor(name, version)
98
- path = File.join(BONED_HOME, 'vendor', "#{name}-#{version}", 'lib')
99
- $:.unshift path
100
- Boned.ld "REQUIRE VENDOR: ", path
101
- require name
102
- end
103
-
104
- def self.require_glob(*path)
105
- path = [BONED_HOME, 'lib', path].flatten
106
- libs = Dir.glob(File.join(*path))
107
- Boned.ld "REQUIRE GLOB: ", libs
108
- libs.each do |lib|
109
- next if lib == __FILE__
110
- require lib if File.file? lib
111
- end
112
- end
113
-
114
- # Checks whether something is listening to a socket.
115
- # * +host+ A hostname
116
- # * +port+ The port to check
117
- # * +wait+ The number of seconds to wait for before timing out.
118
- #
119
- # Returns true if +host+ allows a socket connection on +port+.
120
- # Returns false if one of the following exceptions is raised:
121
- # Errno::EAFNOSUPPORT, Errno::ECONNREFUSED, SocketError, Timeout::Error
122
- #
123
- def self.service_available?(host, port, wait=3)
124
- a = Socket.getaddrinfo @conf[:redis][:host], @conf[:redis][:port]
125
- ip_addr = a[0][3]
126
- ld "SERVICE: #{host} (#{ip_addr}) #{port}"
127
- if Boned.sysinfo.vm == :java
128
- begin
129
- iadd = Java::InetSocketAddress.new host, port
130
- socket = Java::Socket.new
131
- socket.connect iadd, wait * 1000 # milliseconds
132
- success = !socket.isClosed && socket.isConnected
133
- rescue NativeException => ex
134
- puts ex.message, ex.backtrace if Boned.debug?
135
- false
136
- end
137
- else
138
- begin
139
- status = Timeout::timeout(wait) do
140
- socket = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
141
- sockaddr = Socket.pack_sockaddr_in( port, host )
142
- socket.connect( sockaddr )
143
- end
144
- true
145
- rescue Errno::EAFNOSUPPORT, Errno::ECONNREFUSED, SocketError, Timeout::Error => ex
146
- puts ex.class, ex.message, ex.backtrace if Boned.debug?
147
- false
148
- end
24
+ end
25
+
26
+
27
+ require 'bone'
28
+
29
+ Bone.source = 'redis://localhost:8045'
30
+ Bone.debug = false
31
+
32
+ module Boned
33
+ @allow_register = false
34
+ class << self
35
+ attr_reader :allow_register
36
+ # Disabled the API method for regsitering given tokens.
37
+ # The value is automatically frozen so it will only
38
+ # work once per instance of Ruby.
39
+ def allow_register= v
40
+ @allow_register = v.freeze unless @allow_register.frozen?
149
41
  end
150
42
  end
151
-
152
- def self.redis_available?
153
- service_available? @conf[:redis][:host], @conf[:redis][:port]
154
- end
155
-
156
- def self.update_redis_client_config(uri=nil)
157
- uri ||= ENV['BONED_REDIS'] || BONED_REDIS
158
- uri = URI.parse(uri)
159
- @conf ||= {}
160
- @conf[:redis] = {
161
- :host => uri.host,
162
- :port => uri.port || 8045,
163
- :database => uri.path.sub(/\D/, "").to_i || 1,
164
- :password => uri.user || nil
165
- }
166
- ld "REDIS: #{@conf[:redis].inspect}"
167
- end
168
-
169
- def self.ld(*msg)
170
- return unless Boned.debug
171
- prefix = "D(#{Thread.current.object_id}): "
172
- puts "#{prefix}" << msg.join("#{$/}#{prefix}")
173
- end
174
-
175
- update_redis_client_config # parse ENV['BONED_REDIS']
176
43
  end
177
44
 
178
- require 'boned/models'
45
+
46
+
@@ -0,0 +1,37 @@
1
+ # try try/10_basics_try.rb
2
+
3
+ require 'boned'
4
+ #Bone.debug = true
5
+ Bone.source = "http://localhost:3073"
6
+ Bone.credentials = 'atoken:crystal'
7
+
8
+ ## Bone.register
9
+ Bone.register Bone.token, Bone.secret
10
+ #=> 'atoken'
11
+
12
+ ## Bone.register is not possible for existing key
13
+ Bone.register Bone.token, Bone.secret
14
+ #=> nil
15
+
16
+ ## Bone.generate
17
+ @token, @secret = *Bone.generate
18
+ @token.size
19
+ #=> 24
20
+
21
+ ## Can destroy a token
22
+ Bone.destroy @token
23
+ #=> true
24
+
25
+ ## Cannot destroy a token that doesn't exist
26
+ Bone.destroy 'bogus'
27
+ #=> false
28
+
29
+ ## Bone.get returns nil for bad key
30
+ Bone['bogus']
31
+ #=> nil
32
+
33
+ ## Bone.keys returns empty array
34
+ Bone.keys
35
+ #=> []
36
+
37
+ Bone.destroy Bone.token
@@ -0,0 +1,11 @@
1
+ require 'boned'
2
+ #Bone.debug = true
3
+ Bone.source = "http://localhost:3073"
4
+ Bone.credentials = "11_signature:crystal"
5
+
6
+ ## Bone.register
7
+ Bone.register Bone.token, Bone.secret
8
+ #=> '11_signature'
9
+
10
+
11
+ Bone.destroy Bone.token
@@ -0,0 +1,34 @@
1
+ require 'boned'
2
+ #Bone.debug = true
3
+ Bone.source = "http://localhost:3073"
4
+ Bone.credentials = '20_keys_try:crystal'
5
+
6
+ ## Bone.credentials
7
+ [Bone.token, Bone.secret]
8
+ #=> ['20_keys_try', 'crystal']
9
+
10
+ ## Bone.register
11
+ Bone.register Bone.token, Bone.secret
12
+ #=> '20_keys_try'
13
+
14
+ ## Bone.set
15
+ Bone['akey1'] = 'value1'
16
+ Bone['akey1']
17
+ #=> 'value1'
18
+
19
+ ## Bone.set
20
+ Bone['akey2'] = 'value2'
21
+ Bone['akey2']
22
+ #=> 'value2'
23
+
24
+ ## Bone.get
25
+ Bone['akey1']
26
+ #=> 'value1'
27
+
28
+ ## Bone.keys
29
+ Bone.keys.sort
30
+ #=> ['akey1', 'akey2']
31
+
32
+ ## Bone.destroy
33
+ Bone.destroy Bone.token
34
+ #=> true
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boned
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Delano Mandelbaum
@@ -9,83 +15,119 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-05-17 00:00:00 -04:00
13
- default_executable:
18
+ date: 2010-12-17 00:00:00 -05:00
19
+ default_executable: boned
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: bone
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 0
34
+ version: 0.3.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: sinatra
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 0
50
+ version: 1.0.0
17
51
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: thin
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
20
58
  requirements:
21
- - - "="
59
+ - - ">="
22
60
  - !ruby/object:Gem::Version
23
- version: 0.2.6
24
- version:
25
- description: The bone daemon
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description: HTTP server companion for Bone (remote environment variables)
26
68
  email: delano@solutious.com
27
69
  executables:
28
70
  - boned
29
71
  extensions: []
30
72
 
31
73
  extra_rdoc_files:
32
- - README.md
33
74
  - LICENSE.txt
34
- - CHANGES.txt
75
+ - README.md
35
76
  files:
77
+ - .gitignore
36
78
  - CHANGES.txt
37
79
  - LICENSE.txt
38
80
  - README.md
39
81
  - Rakefile
82
+ - VERSION.yml
40
83
  - api/api-set.rb
41
84
  - api/api.rb
85
+ - app.ru
42
86
  - bin/boned
43
87
  - boned.gemspec
44
- - config.ru
45
88
  - config/redis-server.conf
46
89
  - lib/boned.rb
47
90
  - lib/boned/api.rb
48
- - lib/boned/api/debug.rb
49
- - lib/boned/api/redis.rb
50
- - lib/boned/api/service.rb
51
- - lib/boned/cli.rb
52
- - lib/boned/models.rb
53
- - lib/boned/models/bone.rb
54
- - lib/boned/server.rb
91
+ - lib/boned/api/base.rb
92
+ - lib/boned/app.rb
55
93
  - public/index.html
56
- - try/10_bone_model.rb
94
+ - try/10_basics_try.rb
95
+ - try/11_signature_try.rb
96
+ - try/20_keys_try.rb
57
97
  - views/redisviewer/keys.erb
58
98
  has_rdoc: true
59
- homepage: http://github.com/solutious/boned
99
+ homepage: https://github.com/solutious/boned
60
100
  licenses: []
61
101
 
62
102
  post_install_message:
63
103
  rdoc_options:
64
- - --line-numbers
65
- - --title
66
- - The bone daemon
67
- - --main
68
- - README.md
104
+ - --charset=UTF-8
69
105
  require_paths:
70
106
  - lib
71
107
  required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
72
109
  requirements:
73
110
  - - ">="
74
111
  - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
75
115
  version: "0"
76
- version:
77
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
78
118
  requirements:
79
119
  - - ">="
80
120
  - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
81
124
  version: "0"
82
- version:
83
125
  requirements: []
84
126
 
85
127
  rubyforge_project: boned
86
- rubygems_version: 1.3.5
128
+ rubygems_version: 1.3.7
87
129
  signing_key:
88
130
  specification_version: 3
89
- summary: The bone daemon
131
+ summary: HTTP server companion for Bone (remote environment variables)
90
132
  test_files: []
91
133
 
data/config.ru DELETED
@@ -1,31 +0,0 @@
1
- # Rackup - GetBones.com
2
- # 2009-12-11
3
-
4
- ENV['RACK_ENV'] ||= 'production'
5
- ENV['APP_ROOT'] = ::File.expand_path(::File.join(::File.dirname(__FILE__)))
6
- $:.unshift(::File.join(ENV['APP_ROOT'], 'lib'))
7
-
8
- require 'sinatra'
9
-
10
- require 'boned/api'
11
-
12
- disable :run # disable sinatra's auto-application starting
13
-
14
- configure :production do
15
- Boned.debug = false
16
-
17
- map("/bone/#{Boned::APIVERSION}/") { run Boned::API::Service }
18
-
19
- end
20
-
21
- configure :development do
22
- use Rack::ShowExceptions
23
-
24
- require 'boned/api/debug'
25
- require 'boned/api/redis'
26
-
27
- map("/bone/#{Boned::APIVERSION}/") { run Boned::API::Service }
28
- map("/debug") { run Boned::API::Debug }
29
- map("/redis") { run Boned::API::RedisViewer }
30
-
31
- end
@@ -1,21 +0,0 @@
1
-
2
- class Boned::API::Debug < Boned::API
3
- set :public => 'public/debug/'
4
- set :views => 'views/debug/'
5
-
6
- not_found do
7
- 'not found'
8
- end
9
-
10
- get '/env/?' do
11
- content_type 'text/plain'
12
- env.to_yaml
13
- end
14
-
15
- get '/slideshow' do
16
- content_type 'text/html'
17
- erb :slideshow
18
- end
19
- end
20
-
21
-
@@ -1,44 +0,0 @@
1
-
2
-
3
- class Boned::API::RedisViewer < Boned::API
4
-
5
- set :public => 'public/'
6
- set :views => 'views/redisviewer/'
7
-
8
- before do
9
- content_type 'text/html'
10
- end
11
-
12
- get '/list/:name' do
13
- Boned.redis.lrange(params[:name], 0, -1).to_yaml
14
- end
15
-
16
- get '/smembers/:name' do
17
- Boned.redis.smembers(params[:name]).to_yaml
18
- end
19
-
20
- get '/opts' do
21
- Boned.redis_opts.to_yaml
22
- end
23
-
24
- get '/get/:name' do
25
- '%s=%s' % [params[:name], Boned.redis.get(params[:name])]
26
- end
27
-
28
- get '/:key' do
29
- @keys = Boned.redis.keys("*#{params[:key]}*")
30
- erb :keys
31
- end
32
-
33
- get '/?' do
34
- @keys = Boned.redis.keys("*")
35
- erb :keys
36
- end
37
-
38
-
39
- helpers do
40
- def key_kind(key)
41
- Boned.redis.type key
42
- end
43
- end
44
- end
@@ -1,86 +0,0 @@
1
- require 'pp'
2
-
3
- # /get/bashrc?path=/Users/delano/&env=development&role=fe&token=1901484c41b8752d61863e323c743abe2c6e90f8841bbdfa789e1d70bc6f4899
4
- # /set/bashrc?value=1000&path=/Users/delano/&env=development&role=fe&token=1901484c41b8752d61863e323c743abe2c6e90f8841bbdfa789e1d70bc6f4899
5
- #
6
- class Boned::API::Service < Boned::API
7
- set :public => 'public/'
8
- set :views => 'views/'
9
-
10
- error do
11
- "Bad bone rising"
12
- end
13
-
14
- get '/' do
15
- 'Throw me a fricken bone'
16
- end
17
-
18
- get '/rev/?' do
19
- Boned::VERSION.inspect
20
- end
21
-
22
- get '/get/:key/?' do
23
- carefully do
24
- assert_params :key
25
- assert_exists current_token, "No token"
26
- assert_sha256 current_token
27
- bone = Bone.get current_token, params[:key], params
28
- bone.value
29
- end
30
- end
31
-
32
- post '/set/:key/?' do
33
- carefully do
34
- assert_params :key, :value
35
- assert_exists current_token, "No token"
36
- assert_sha256 current_token
37
- bone = Bone.new current_token, params[:key], params[:value], params
38
- bone.save
39
- end
40
- params[:value]
41
- end
42
-
43
- delete "/del/:key/?" do
44
- carefully do
45
- assert_params :key
46
- assert_exists current_token, "No token"
47
- assert_sha256 current_token
48
- bone = Bone.del current_token, params[:key], params
49
- bone.value
50
- end
51
- end
52
-
53
- get '/keys/:key' do
54
- carefully do
55
- assert_params :key
56
- assert_exists current_token, "No token"
57
- assert_sha256 current_token
58
- keys = Bone.keys current_token, params[:key]
59
- keys.join($/)
60
- end
61
- end
62
-
63
- get '/keys/?' do
64
- carefully do
65
- assert_exists current_token, "No token"
66
- assert_sha256 current_token
67
- keys = Bone.keys current_token, '*'
68
- keys.join($/)
69
- end
70
- end
71
-
72
- #post '/set/:key/?' do
73
- # carefully do
74
- # assert_params :key
75
- # assert_sha256 current_token
76
- # #assert_exists current_token
77
- # #key, filter, path = params.values_at :key, :filter, :path
78
- # #Boned::Object.set current_token, key
79
- # pp request
80
- # pp env['rack.input'].read
81
- # end
82
- #end
83
-
84
- end
85
-
86
-