capistrano-sdb 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+
2
+ v0.1. 2008/10/27
3
+ * Basic functionality. Two precedence modes.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Jamis Buck
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ capistrano-sdb.gemspec
2
+ CHANGELOG
3
+ lib/capistrano/sdb.rb
4
+ Manifest
5
+ MIT-LICENSE
6
+ Rakefile
7
+ README
data/README ADDED
@@ -0,0 +1,51 @@
1
+ = Capistrano SDB
2
+
3
+ Allows you to use Amazon Simple DB as a repository for any capistrano config keys.
4
+
5
+ == INSTALLATION
6
+ In your Capfile, add:
7
+ require 'capistrano/sdb'
8
+
9
+ == DEPENDENCIES
10
+
11
+ * right_aws
12
+
13
+ == USAGE
14
+
15
+ In your deploy.rb, you must set:
16
+
17
+ set :aws_access_key_id, '<your access key>'
18
+ set :aws_secret_access_key, '<your secret>'
19
+
20
+ The SDB plugin works in two precedence modes; :local, and :remote
21
+
22
+ == PRECEDENCE MODES
23
+
24
+ You can set precedence modes using a directive in your deploy.rb: e.g.
25
+ set :sdb_precedence, :remote
26
+ or from the command line, e.g.
27
+ cap SDB_PRECEDENCE=remote deploy
28
+
29
+
30
+ === LOCAL (default)
31
+
32
+ It is assumed that anything stored in the local file(s)
33
+ overrides SDB configuration.
34
+
35
+ In this mode, calls to SDB are made only when keys are not found locally,
36
+ so you will not see any difference for keys that are found.
37
+
38
+ In this mode, you can slowly remove almost all config from your local filesystem,
39
+ and move it to SDB - thereby keeping your local files quite clean.
40
+
41
+ === REMOTE
42
+
43
+ Here we assume that any overrides are stored in SDB, and local files are used
44
+ as a backup.
45
+
46
+ The downside of this mode is that any task will first require a download of your
47
+ configuration
48
+
49
+ == CONFIGURATION OPTIONS
50
+
51
+ * sdb_domain : override the default sdb domain of "#{application}_cap_config"
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'echoe'
2
+ Echoe.new('capistrano-sdb') do |p|
3
+ p.author = "Hemant Bhanoo"
4
+ p.summary = "Allows you to use Amazon Simple DB as a repository for any capistrano config keys."
5
+ p.url = "http://code.google.com/p/capistrano-sdb/"
6
+ p.runtime_dependencies = ["right_aws >= 1.8.1", "json"]
7
+ p.install_message = <<-MSG
8
+ MSG
9
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{capistrano-sdb}
5
+ s.version = "0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Hemant Bhanoo"]
9
+ s.date = %q{2008-10-30}
10
+ s.description = %q{Allows you to use Amazon Simple DB as a repository for any capistrano config keys.}
11
+ s.email = %q{}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/capistrano/sdb.rb", "README"]
13
+ s.files = ["capistrano-sdb.gemspec", "CHANGELOG", "lib/capistrano/sdb.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://code.google.com/p/capistrano-sdb/}
16
+ s.post_install_message = %q{}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Capistrano-sdb", "--main", "README"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{capistrano-sdb}
20
+ s.rubygems_version = %q{1.3.0}
21
+ s.summary = %q{Allows you to use Amazon Simple DB as a repository for any capistrano config keys.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<right_aws>, [">= 0", "= 1.8.1"])
29
+ s.add_runtime_dependency(%q<json>, [">= 0"])
30
+ s.add_development_dependency(%q<echoe>, [">= 0"])
31
+ else
32
+ s.add_dependency(%q<right_aws>, [">= 0", "= 1.8.1"])
33
+ s.add_dependency(%q<json>, [">= 0"])
34
+ s.add_dependency(%q<echoe>, [">= 0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<right_aws>, [">= 0", "= 1.8.1"])
38
+ s.add_dependency(%q<json>, [">= 0"])
39
+ s.add_dependency(%q<echoe>, [">= 0"])
40
+ end
41
+ end
@@ -0,0 +1,235 @@
1
+ require 'rubygems'
2
+ require 'right_aws'
3
+ require 'json'
4
+
5
+ module SDB # :nodoc:all
6
+ class Logger < ::Logger
7
+ LEVEL_MAPPING = {
8
+ :debug => :trace,
9
+ :info => :debug,
10
+ :warn =>:info,
11
+ :error =>:important,
12
+ :fatal => :important
13
+ }
14
+
15
+ LEVEL_MAPPING.each do |k,v|
16
+ define_method( k ) do |*args|
17
+ @caplogger.send( v, *args )
18
+ end
19
+ end
20
+
21
+ def initialize( caplogger )
22
+ @caplogger = caplogger
23
+ end
24
+
25
+ end
26
+ end # module SDB
27
+
28
+ module Capistrano #:nodoc:
29
+ class Configuration #:nodoc:
30
+ module Variables #:nodoc:
31
+ module SDB
32
+ def self.included( base ) #:nodoc:
33
+ %w(initialize respond_to? method_missing).each do |m|
34
+ base_name = m[/^\w+/]
35
+ punct = m[/\W+$/]
36
+ base.send :alias_method, "#{base_name}_without_sdb_variables#{punct}", m
37
+ base.send :alias_method, m, "#{base_name}_with_sdb_variables#{punct}"
38
+ end # each m
39
+ end # self.included
40
+
41
+ def initialize_with_sdb_variables(*args) #:nodoc:
42
+ initialize_without_sdb_variables(*args)
43
+ end # def initialize_with_sdb_variables
44
+
45
+ def respond_to_with_sdb_variables?( sym ) #:nodoc:
46
+ return respond_to_without_sdb_variables?( sym ) if( @_sdb_booting )
47
+ if( sdb_local_override? )
48
+ return true if( respond_to_without_sdb_variables?( sym ) )
49
+ sdb_download_config
50
+ return sdb_has_key?( sym )
51
+ else
52
+ sdb_download_config
53
+ return sdb_has_key?( sym ) || respond_to_without_sdb_variables?( sym )
54
+ end
55
+ end # def respond_to_with_sdb_variables?
56
+
57
+ def method_missing_with_sdb_variables( sym, *args, &block ) #:nodoc:
58
+ return method_missing_without_sdb_variables( sym , *args, &block ) if( @_sdb_booting )
59
+ if( args.length == 0 && block.nil? )
60
+ if( sdb_local_override? )
61
+ if( respond_to_without_sdb_variables?( sym ) )
62
+ method_missing_without_sdb_variables( sym, *args, &block )
63
+ else
64
+ sdb_download_config
65
+ sdb_fetch( sym )
66
+ end
67
+ else # if sdb_local_override?
68
+ sdb_has_key?( sym ) ? sdb_fetch( sym ) : method_missing_without_sdb_variables( sym, *args, &block )
69
+ end # if sdb_local_override? else
70
+
71
+ else #
72
+ method_missing_without_sdb_variables( sym, *args, &block )
73
+ end # possible var
74
+
75
+ end # def method_missing_with_sdb_variables
76
+
77
+ # Boot the sdb system, including
78
+ # * creating a client
79
+ # * creating a domain for configuration
80
+ def require_sdb( opts = nil )
81
+ opts ||= {}
82
+ return if @booted
83
+ @_sdb_booting = true
84
+ raise "must specify aws_access_key_id and aws_secret_access_key" unless
85
+ ( exists?( :aws_access_key_id ) && exists?( :aws_secret_access_key ) )
86
+ @stage = respond_to?( :stage ) ? stage : 'default'
87
+ load_precedence_setting
88
+ raise "no stage specified" if @stage.nil?
89
+ logger.trace "booting sdb plugin with #{@precedence} precedence"
90
+ default_client_params = {
91
+ :protocol => 'http',
92
+ :port => 80,
93
+ :logger => ::SDB::Logger.new( logger )
94
+ }
95
+ override_client_params = respond_to?( :sdb_client_params ) ? sdb_client_params : {}
96
+ client_params = default_client_params.merge( override_client_params )
97
+ self._sdb_client = RightAws::SdbInterface.new( aws_access_key_id, aws_secret_access_key,
98
+ client_params )
99
+
100
+ default_domain = ENV['SDBSELF._SDB_DOMAIN'] || "#{application}_cap_config"
101
+ self._sdb_domain = default_domain
102
+ self._sdb_domain = sdb_domain if( exists?(:sdb_domain) )
103
+ self._sdb_client.create_domain( self._sdb_domain )
104
+ @_sdb_booting = false
105
+ @booted = true
106
+ end
107
+
108
+ # tests whether the sdb config defines a variable or not
109
+ # pass in :reload as the second param to force a re-download of config.
110
+ def sdb_has_key?( variable, reload = false )
111
+ sdb_download_config( reload )
112
+ return @sdb_vars.has_key?( variable.to_s )
113
+ end
114
+
115
+ # fetches a variable from sdb.
116
+ # pass in :reload as the second param to force a re-download of config.
117
+ def sdb_fetch( variable, reload = false )
118
+ logger.trace( "fetching #{variable} for #{@stage} from remote location" )
119
+ sdb_download_config( reload )
120
+ if( variable )
121
+ raise IndexError, "`#{variable} not found" if( !sdb_has_key?( variable ) )
122
+ return @sdb_vars[variable.to_s]
123
+ else
124
+ return @sdb_vars
125
+ end
126
+ end
127
+
128
+ # takes in a hash of variables to set.
129
+ def sdb_set( variables )
130
+ require_sdb
131
+ rval = self._sdb_client.put_attributes( self._sdb_domain, @stage, variables )
132
+ end
133
+
134
+ protected
135
+ attr_accessor :_sdb_client, :_sdb_domain
136
+
137
+ private
138
+
139
+ # dump internal variable state
140
+ def sdb_dump_vars( reload = false )
141
+ sdb_download_config( reload )
142
+ logger.debug "sdb config:\n #{@sdb_vars.inspect}"
143
+ end
144
+
145
+
146
+ def sdb_download_config( reload = false )
147
+ return @sdb_vars if( !reload && @sdb_vars )
148
+ require_sdb
149
+ logger.trace "downloading configuration from sdb"
150
+ # FIXME: we might still want to download data for all stages, just so
151
+ # that we can do a stage -> default fallback even for remote.
152
+ # @sdb_vars = Hash[ *(self._sdb_client.query( self._sdb_domain )[:items].collect{ |item|
153
+ # [item, self._sdb_client.get_attributes( self._sdb_domain, @stage )[:attributes]
154
+ # ]}.flatten) ]
155
+ @sdb_vars = self._sdb_client.get_attributes( self._sdb_domain, @stage )[:attributes]
156
+ end
157
+
158
+ def sdb_booted?
159
+ return @booted
160
+ end
161
+
162
+ def load_precedence_setting
163
+ if( @precedence.nil? )
164
+ if( ENV['SDB_PRECEDENCE'] )
165
+ @precedence = ENV['SDB_PRECEDENCE'].to_sym
166
+ else
167
+ wasbooting = @_sdb_booting
168
+ @_sdb_booting = true
169
+ @precedence = self.respond_to?( :sdb_precedence ) ? sdb_precedence : :local
170
+ @_sdb_booting = wasbooting
171
+ end
172
+ end
173
+ end
174
+
175
+ def sdb_local_override?
176
+ load_precedence_setting
177
+ :local == @precedence
178
+ end
179
+
180
+ def sdb_has_stage_info?( stage )
181
+ return @sdb_vars && @sdb_vars.has_key?( stage.to_s )
182
+ end
183
+
184
+ end # module SDB
185
+ end # module Variables
186
+ end # class Configuration
187
+ end # module Capistrano
188
+
189
+ Capistrano::Configuration.send( :include, Capistrano::Configuration::Variables::SDB )
190
+
191
+ Capistrano::Configuration.instance.load do
192
+ namespace :sdb do
193
+
194
+ desc <<-DESC
195
+ Store a variable in SDB.
196
+ Takes two forms
197
+ * single key/value set
198
+ $ cap KEY=msg VALUE=something sdb:set
199
+
200
+ * multiple kv pairs (JSON compliant string)
201
+ $ cap VARS='{"myarray" : [1,2,3]}'
202
+ DESC
203
+ task :set do
204
+ key, value = ENV['KEY'], ENV['VALUE']
205
+ varstring = ENV['VARS']
206
+ abort "Either specify a KEY and VALUE or a json VARS string. cap -e sdb:set for more info" if
207
+ ( varstring.nil? && ( key.nil? || value.nil? ) )
208
+ if( varstring )
209
+ vars = JSON.parse( varstring )
210
+ else
211
+ vars = { key => value }
212
+ end
213
+ sdb_set( vars )
214
+ end
215
+
216
+ desc <<-DESC
217
+ Dump the config stored in SDB.
218
+ Takes two forms:
219
+ * single key:
220
+ $ cap KEY=msg sdb:show
221
+ * full dump:
222
+ $ cap sdb:show
223
+ DESC
224
+ task :show do
225
+ key = ENV['KEY']
226
+ value = sdb_fetch( key )
227
+ if( key )
228
+ puts "`#{key} = #{value.inspect}"
229
+ else
230
+ logger.important( "all keys':\n #{value.inspect}" )
231
+ end
232
+ end
233
+
234
+ end # namespace sdb
235
+ end # Configuration.instance.load
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-sdb
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Hemant Bhanoo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: right_aws
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ - - "="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.1
27
+ version:
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ type: :runtime
31
+ version_requirement:
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: "0"
37
+ version:
38
+ - !ruby/object:Gem::Dependency
39
+ name: echoe
40
+ type: :development
41
+ version_requirement:
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ description: Allows you to use Amazon Simple DB as a repository for any capistrano config keys.
49
+ email: ""
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - CHANGELOG
56
+ - lib/capistrano/sdb.rb
57
+ - README
58
+ files:
59
+ - capistrano-sdb.gemspec
60
+ - CHANGELOG
61
+ - lib/capistrano/sdb.rb
62
+ - Manifest
63
+ - MIT-LICENSE
64
+ - Rakefile
65
+ - README
66
+ has_rdoc: true
67
+ homepage: http://code.google.com/p/capistrano-sdb/
68
+ post_install_message: ""
69
+ rdoc_options:
70
+ - --line-numbers
71
+ - --inline-source
72
+ - --title
73
+ - Capistrano-sdb
74
+ - --main
75
+ - README
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "1.2"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project: capistrano-sdb
93
+ rubygems_version: 1.3.0
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: Allows you to use Amazon Simple DB as a repository for any capistrano config keys.
97
+ test_files: []
98
+