backup-agoddard 3.0.27
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of backup-agoddard might be problematic. Click here for more details.
- data/.gitignore +8 -0
- data/.travis.yml +10 -0
- data/Gemfile +28 -0
- data/Guardfile +23 -0
- data/LICENSE.md +24 -0
- data/README.md +478 -0
- data/backup.gemspec +32 -0
- data/bin/backup +11 -0
- data/lib/backup.rb +133 -0
- data/lib/backup/archive.rb +117 -0
- data/lib/backup/binder.rb +22 -0
- data/lib/backup/cleaner.rb +121 -0
- data/lib/backup/cli/helpers.rb +93 -0
- data/lib/backup/cli/utility.rb +255 -0
- data/lib/backup/compressor/base.rb +35 -0
- data/lib/backup/compressor/bzip2.rb +50 -0
- data/lib/backup/compressor/custom.rb +53 -0
- data/lib/backup/compressor/gzip.rb +50 -0
- data/lib/backup/compressor/lzma.rb +52 -0
- data/lib/backup/compressor/pbzip2.rb +59 -0
- data/lib/backup/config.rb +174 -0
- data/lib/backup/configuration.rb +33 -0
- data/lib/backup/configuration/helpers.rb +130 -0
- data/lib/backup/configuration/store.rb +24 -0
- data/lib/backup/database/base.rb +53 -0
- data/lib/backup/database/mongodb.rb +230 -0
- data/lib/backup/database/mysql.rb +160 -0
- data/lib/backup/database/postgresql.rb +144 -0
- data/lib/backup/database/redis.rb +136 -0
- data/lib/backup/database/riak.rb +67 -0
- data/lib/backup/dependency.rb +108 -0
- data/lib/backup/encryptor/base.rb +29 -0
- data/lib/backup/encryptor/gpg.rb +760 -0
- data/lib/backup/encryptor/open_ssl.rb +72 -0
- data/lib/backup/errors.rb +124 -0
- data/lib/backup/hooks.rb +68 -0
- data/lib/backup/logger.rb +152 -0
- data/lib/backup/model.rb +409 -0
- data/lib/backup/notifier/base.rb +81 -0
- data/lib/backup/notifier/campfire.rb +155 -0
- data/lib/backup/notifier/hipchat.rb +93 -0
- data/lib/backup/notifier/mail.rb +206 -0
- data/lib/backup/notifier/prowl.rb +65 -0
- data/lib/backup/notifier/pushover.rb +88 -0
- data/lib/backup/notifier/twitter.rb +70 -0
- data/lib/backup/package.rb +47 -0
- data/lib/backup/packager.rb +100 -0
- data/lib/backup/pipeline.rb +110 -0
- data/lib/backup/splitter.rb +75 -0
- data/lib/backup/storage/base.rb +99 -0
- data/lib/backup/storage/cloudfiles.rb +87 -0
- data/lib/backup/storage/cycler.rb +117 -0
- data/lib/backup/storage/dropbox.rb +178 -0
- data/lib/backup/storage/ftp.rb +119 -0
- data/lib/backup/storage/local.rb +82 -0
- data/lib/backup/storage/ninefold.rb +116 -0
- data/lib/backup/storage/rsync.rb +149 -0
- data/lib/backup/storage/s3.rb +94 -0
- data/lib/backup/storage/scp.rb +99 -0
- data/lib/backup/storage/sftp.rb +108 -0
- data/lib/backup/syncer/base.rb +46 -0
- data/lib/backup/syncer/cloud/base.rb +247 -0
- data/lib/backup/syncer/cloud/cloud_files.rb +78 -0
- data/lib/backup/syncer/cloud/s3.rb +68 -0
- data/lib/backup/syncer/rsync/base.rb +49 -0
- data/lib/backup/syncer/rsync/local.rb +55 -0
- data/lib/backup/syncer/rsync/pull.rb +36 -0
- data/lib/backup/syncer/rsync/push.rb +116 -0
- data/lib/backup/template.rb +46 -0
- data/lib/backup/version.rb +43 -0
- data/spec-live/.gitignore +6 -0
- data/spec-live/README +7 -0
- data/spec-live/backups/config.rb +83 -0
- data/spec-live/backups/config.yml.template +46 -0
- data/spec-live/backups/models.rb +184 -0
- data/spec-live/compressor/custom_spec.rb +30 -0
- data/spec-live/compressor/gzip_spec.rb +30 -0
- data/spec-live/encryptor/gpg_keys.rb +239 -0
- data/spec-live/encryptor/gpg_spec.rb +287 -0
- data/spec-live/notifier/mail_spec.rb +121 -0
- data/spec-live/spec_helper.rb +151 -0
- data/spec-live/storage/dropbox_spec.rb +151 -0
- data/spec-live/storage/local_spec.rb +83 -0
- data/spec-live/storage/scp_spec.rb +193 -0
- data/spec-live/syncer/cloud/s3_spec.rb +124 -0
- data/spec/archive_spec.rb +335 -0
- data/spec/cleaner_spec.rb +312 -0
- data/spec/cli/helpers_spec.rb +301 -0
- data/spec/cli/utility_spec.rb +411 -0
- data/spec/compressor/base_spec.rb +52 -0
- data/spec/compressor/bzip2_spec.rb +217 -0
- data/spec/compressor/custom_spec.rb +106 -0
- data/spec/compressor/gzip_spec.rb +217 -0
- data/spec/compressor/lzma_spec.rb +123 -0
- data/spec/compressor/pbzip2_spec.rb +165 -0
- data/spec/config_spec.rb +321 -0
- data/spec/configuration/helpers_spec.rb +247 -0
- data/spec/configuration/store_spec.rb +39 -0
- data/spec/configuration_spec.rb +62 -0
- data/spec/database/base_spec.rb +63 -0
- data/spec/database/mongodb_spec.rb +510 -0
- data/spec/database/mysql_spec.rb +411 -0
- data/spec/database/postgresql_spec.rb +353 -0
- data/spec/database/redis_spec.rb +334 -0
- data/spec/database/riak_spec.rb +176 -0
- data/spec/dependency_spec.rb +51 -0
- data/spec/encryptor/base_spec.rb +40 -0
- data/spec/encryptor/gpg_spec.rb +909 -0
- data/spec/encryptor/open_ssl_spec.rb +148 -0
- data/spec/errors_spec.rb +306 -0
- data/spec/hooks_spec.rb +35 -0
- data/spec/logger_spec.rb +367 -0
- data/spec/model_spec.rb +694 -0
- data/spec/notifier/base_spec.rb +104 -0
- data/spec/notifier/campfire_spec.rb +217 -0
- data/spec/notifier/hipchat_spec.rb +211 -0
- data/spec/notifier/mail_spec.rb +316 -0
- data/spec/notifier/prowl_spec.rb +138 -0
- data/spec/notifier/pushover_spec.rb +123 -0
- data/spec/notifier/twitter_spec.rb +153 -0
- data/spec/package_spec.rb +61 -0
- data/spec/packager_spec.rb +213 -0
- data/spec/pipeline_spec.rb +259 -0
- data/spec/spec_helper.rb +60 -0
- data/spec/splitter_spec.rb +120 -0
- data/spec/storage/base_spec.rb +166 -0
- data/spec/storage/cloudfiles_spec.rb +254 -0
- data/spec/storage/cycler_spec.rb +247 -0
- data/spec/storage/dropbox_spec.rb +480 -0
- data/spec/storage/ftp_spec.rb +271 -0
- data/spec/storage/local_spec.rb +259 -0
- data/spec/storage/ninefold_spec.rb +343 -0
- data/spec/storage/rsync_spec.rb +362 -0
- data/spec/storage/s3_spec.rb +245 -0
- data/spec/storage/scp_spec.rb +233 -0
- data/spec/storage/sftp_spec.rb +244 -0
- data/spec/syncer/base_spec.rb +109 -0
- data/spec/syncer/cloud/base_spec.rb +515 -0
- data/spec/syncer/cloud/cloud_files_spec.rb +181 -0
- data/spec/syncer/cloud/s3_spec.rb +174 -0
- data/spec/syncer/rsync/base_spec.rb +98 -0
- data/spec/syncer/rsync/local_spec.rb +149 -0
- data/spec/syncer/rsync/pull_spec.rb +98 -0
- data/spec/syncer/rsync/push_spec.rb +333 -0
- data/spec/version_spec.rb +21 -0
- data/templates/cli/utility/archive +25 -0
- data/templates/cli/utility/compressor/bzip2 +4 -0
- data/templates/cli/utility/compressor/custom +11 -0
- data/templates/cli/utility/compressor/gzip +4 -0
- data/templates/cli/utility/compressor/lzma +10 -0
- data/templates/cli/utility/compressor/pbzip2 +10 -0
- data/templates/cli/utility/config +32 -0
- data/templates/cli/utility/database/mongodb +18 -0
- data/templates/cli/utility/database/mysql +21 -0
- data/templates/cli/utility/database/postgresql +17 -0
- data/templates/cli/utility/database/redis +16 -0
- data/templates/cli/utility/database/riak +11 -0
- data/templates/cli/utility/encryptor/gpg +27 -0
- data/templates/cli/utility/encryptor/openssl +9 -0
- data/templates/cli/utility/model.erb +23 -0
- data/templates/cli/utility/notifier/campfire +12 -0
- data/templates/cli/utility/notifier/hipchat +15 -0
- data/templates/cli/utility/notifier/mail +22 -0
- data/templates/cli/utility/notifier/prowl +11 -0
- data/templates/cli/utility/notifier/pushover +11 -0
- data/templates/cli/utility/notifier/twitter +13 -0
- data/templates/cli/utility/splitter +7 -0
- data/templates/cli/utility/storage/cloud_files +22 -0
- data/templates/cli/utility/storage/dropbox +20 -0
- data/templates/cli/utility/storage/ftp +12 -0
- data/templates/cli/utility/storage/local +7 -0
- data/templates/cli/utility/storage/ninefold +9 -0
- data/templates/cli/utility/storage/rsync +11 -0
- data/templates/cli/utility/storage/s3 +19 -0
- data/templates/cli/utility/storage/scp +11 -0
- data/templates/cli/utility/storage/sftp +11 -0
- data/templates/cli/utility/syncer/cloud_files +46 -0
- data/templates/cli/utility/syncer/rsync_local +12 -0
- data/templates/cli/utility/syncer/rsync_pull +17 -0
- data/templates/cli/utility/syncer/rsync_push +17 -0
- data/templates/cli/utility/syncer/s3 +43 -0
- data/templates/general/links +11 -0
- data/templates/general/version.erb +2 -0
- data/templates/notifier/mail/failure.erb +9 -0
- data/templates/notifier/mail/success.erb +7 -0
- data/templates/notifier/mail/warning.erb +9 -0
- data/templates/storage/dropbox/authorization_url.erb +6 -0
- data/templates/storage/dropbox/authorized.erb +4 -0
- data/templates/storage/dropbox/cache_file_written.erb +10 -0
- metadata +277 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Compressor
|
5
|
+
class Lzma < Base
|
6
|
+
|
7
|
+
##
|
8
|
+
# Tells Backup::Compressor::Lzma to compress
|
9
|
+
# better (-9) rather than faster when set to true
|
10
|
+
attr_accessor :best
|
11
|
+
|
12
|
+
##
|
13
|
+
# Tells Backup::Compressor::Lzma to compress
|
14
|
+
# faster (-1) rather than better when set to true
|
15
|
+
attr_accessor :fast
|
16
|
+
|
17
|
+
##
|
18
|
+
# Creates a new instance of Backup::Compressor::Lzma
|
19
|
+
def initialize(&block)
|
20
|
+
load_defaults!
|
21
|
+
|
22
|
+
@best ||= false
|
23
|
+
@fast ||= false
|
24
|
+
|
25
|
+
instance_eval(&block) if block_given?
|
26
|
+
|
27
|
+
@cmd = "#{ utility(:lzma) }#{ options }"
|
28
|
+
@ext = '.lzma'
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
##
|
33
|
+
# Yields to the block the compressor command and filename extension.
|
34
|
+
def compress_with
|
35
|
+
Backup::Logger.warn(
|
36
|
+
"[DEPRECATION WARNING]\n" +
|
37
|
+
" Compressor::Lzma is being deprecated as of backup v.3.0.24\n" +
|
38
|
+
" and will soon be removed. Please see the Compressors wiki page at\n" +
|
39
|
+
" https://github.com/meskyanichi/backup/wiki/Compressors"
|
40
|
+
)
|
41
|
+
super
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def options
|
47
|
+
(' --best' if @best) || (' --fast' if @fast)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Compressor
|
5
|
+
class Pbzip2 < Base
|
6
|
+
|
7
|
+
##
|
8
|
+
# Tells Backup::Compressor::Lzma to compress
|
9
|
+
# better (-9) rather than faster when set to true
|
10
|
+
attr_accessor :best
|
11
|
+
|
12
|
+
##
|
13
|
+
# Tells Backup::Compressor::Lzma to compress
|
14
|
+
# faster (-1) rather than better when set to true
|
15
|
+
attr_accessor :fast
|
16
|
+
|
17
|
+
##
|
18
|
+
# Tells Backup::Compressor::Pbzip2 how many processors to use.
|
19
|
+
# Autodetects the number of active CPUs by default.
|
20
|
+
attr_accessor :processors
|
21
|
+
|
22
|
+
##
|
23
|
+
# Creates a new instance of Backup::Compressor::Pbzip2
|
24
|
+
def initialize(&block)
|
25
|
+
load_defaults!
|
26
|
+
|
27
|
+
@best ||= false
|
28
|
+
@fast ||= false
|
29
|
+
@processors ||= false
|
30
|
+
|
31
|
+
instance_eval(&block) if block_given?
|
32
|
+
|
33
|
+
@cmd = "#{ utility(:pbzip2) }#{ options }"
|
34
|
+
@ext = '.bz2'
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Yields to the block the compressor command and filename extension.
|
39
|
+
def compress_with
|
40
|
+
Backup::Logger.warn(
|
41
|
+
"[DEPRECATION WARNING]\n" +
|
42
|
+
" Compressor::Pbzip2 is being deprecated as of backup v.3.0.24\n" +
|
43
|
+
" and will soon be removed. Please see the Compressors wiki page at\n" +
|
44
|
+
" https://github.com/meskyanichi/backup/wiki/Compressors"
|
45
|
+
)
|
46
|
+
super
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def options
|
52
|
+
level = (' --best' if @best) || (' --fast' if @fast)
|
53
|
+
cpus = " -p#{ @processors }" if @processors
|
54
|
+
"#{ level }#{ cpus }"
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Config
|
5
|
+
DEFAULTS = {
|
6
|
+
:config_file => 'config.rb',
|
7
|
+
:data_path => 'data',
|
8
|
+
:log_path => 'log',
|
9
|
+
:cache_path => '.cache',
|
10
|
+
:tmp_path => '.tmp'
|
11
|
+
}
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_reader :user, :root_path, :config_file,
|
15
|
+
:data_path, :log_path, :cache_path, :tmp_path
|
16
|
+
|
17
|
+
##
|
18
|
+
# Setup required paths based on the given options
|
19
|
+
def update(options = {})
|
20
|
+
root_path = options[:root_path].to_s.strip
|
21
|
+
new_root = root_path.empty? ? false : set_root_path(root_path)
|
22
|
+
|
23
|
+
DEFAULTS.each do |name, ending|
|
24
|
+
set_path_variable(name, options[name], ending, new_root)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Tries to find and load the configuration file
|
30
|
+
def load_config!
|
31
|
+
unless File.exist?(@config_file)
|
32
|
+
raise Errors::Config::NotFoundError,
|
33
|
+
"Could not find configuration file: '#{@config_file}'."
|
34
|
+
end
|
35
|
+
|
36
|
+
module_eval(File.read(@config_file), @config_file)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
##
|
42
|
+
# Sets the @root_path to the given +path+ and returns it.
|
43
|
+
# Raises an error if the given +path+ does not exist.
|
44
|
+
def set_root_path(path)
|
45
|
+
# allows #reset! to set the default @root_path,
|
46
|
+
# then use #update to set all other paths,
|
47
|
+
# without requiring that @root_path exist.
|
48
|
+
return @root_path if path == @root_path
|
49
|
+
|
50
|
+
path = File.expand_path(path)
|
51
|
+
unless File.directory?(path)
|
52
|
+
raise Errors::Config::NotFoundError, <<-EOS
|
53
|
+
Root Path Not Found
|
54
|
+
When specifying a --root-path, the path must exist.
|
55
|
+
Path was: #{ path }
|
56
|
+
EOS
|
57
|
+
end
|
58
|
+
@root_path = path
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_path_variable(name, path, ending, root_path)
|
62
|
+
# strip any trailing '/' in case the user supplied this as part of
|
63
|
+
# an absolute path, so we can match it against File.expand_path()
|
64
|
+
path = path.to_s.sub(/\/\s*$/, '').lstrip
|
65
|
+
new_path = false
|
66
|
+
if path.empty?
|
67
|
+
new_path = File.join(root_path, ending) if root_path
|
68
|
+
else
|
69
|
+
new_path = File.expand_path(path)
|
70
|
+
unless path == new_path
|
71
|
+
new_path = File.join(root_path, path) if root_path
|
72
|
+
end
|
73
|
+
end
|
74
|
+
instance_variable_set(:"@#{name}", new_path) if new_path
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# Set default values for accessors
|
79
|
+
def reset!
|
80
|
+
@user = ENV['USER'] || Etc.getpwuid.name
|
81
|
+
@root_path = File.join(File.expand_path(ENV['HOME'] || ''), 'Backup')
|
82
|
+
update(:root_path => @root_path)
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# List the available database, storage, syncer, compressor, encryptor
|
87
|
+
# and notifier constants. These are used to dynamically define these
|
88
|
+
# constant names inside Backup::Config to provide a nicer configuration
|
89
|
+
# file DSL syntax to the users. Adding existing constants to the arrays
|
90
|
+
# below will enable the user to use a constant instead of a string.
|
91
|
+
# Nested namespaces are represented using Hashs. Deep nesting supported.
|
92
|
+
#
|
93
|
+
# Example, instead of:
|
94
|
+
# database "MySQL" do |mysql|
|
95
|
+
# sync_with "RSync::Local" do |rsync|
|
96
|
+
#
|
97
|
+
# You can do:
|
98
|
+
# database MySQL do |mysql|
|
99
|
+
# sync_with RSync::Local do |rsync|
|
100
|
+
#
|
101
|
+
def add_dsl_constants!
|
102
|
+
create_modules(
|
103
|
+
self,
|
104
|
+
[ # Databases
|
105
|
+
['MySQL', 'PostgreSQL', 'MongoDB', 'Redis', 'Riak'],
|
106
|
+
# Storages
|
107
|
+
['S3', 'CloudFiles', 'Ninefold', 'Dropbox', 'FTP',
|
108
|
+
'SFTP', 'SCP', 'RSync', 'Local'],
|
109
|
+
# Compressors
|
110
|
+
['Gzip', 'Bzip2', 'Custom', 'Pbzip2', 'Lzma'],
|
111
|
+
# Encryptors
|
112
|
+
['OpenSSL', 'GPG'],
|
113
|
+
# Syncers
|
114
|
+
[
|
115
|
+
{ 'Cloud' => ['CloudFiles', 'S3'] },
|
116
|
+
{ 'RSync' => ['Push', 'Pull', 'Local'] }
|
117
|
+
],
|
118
|
+
# Notifiers
|
119
|
+
['Mail', 'Twitter', 'Campfire', 'Prowl', 'Hipchat', 'Pushover']
|
120
|
+
]
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
def create_modules(scope, names)
|
125
|
+
names.flatten.each do |name|
|
126
|
+
if name.is_a?(Hash)
|
127
|
+
name.each do |key, val|
|
128
|
+
create_modules(get_or_create_empty_module(scope, key), [val])
|
129
|
+
end
|
130
|
+
else
|
131
|
+
get_or_create_empty_module(scope, name)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def get_or_create_empty_module(scope, const)
|
137
|
+
if scope.const_defined?(const)
|
138
|
+
scope.const_get(const)
|
139
|
+
else
|
140
|
+
scope.const_set(const, Module.new)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# Add the DSL constants and set default values for accessors when loaded.
|
147
|
+
add_dsl_constants!
|
148
|
+
reset!
|
149
|
+
end
|
150
|
+
|
151
|
+
##
|
152
|
+
# Warn user of deprecated Backup::CONFIG_FILE constant reference
|
153
|
+
# in older config.rb files and return the proper Config.config_file value.
|
154
|
+
class << self
|
155
|
+
def const_missing(const)
|
156
|
+
if const.to_s == 'CONFIG_FILE'
|
157
|
+
Logger.warn Errors::ConfigError.new(<<-EOS)
|
158
|
+
Configuration File Upgrade Needed
|
159
|
+
Your configuration file, located at #{ Config.config_file }
|
160
|
+
needs to be upgraded for this version of Backup.
|
161
|
+
The reference to 'Backup::CONFIG_FILE' in your current config.rb file
|
162
|
+
has been deprecated and needs to be replaced with 'Config.config_file'.
|
163
|
+
You may update this reference in your config.rb manually,
|
164
|
+
or generate a new config.rb using 'backup generate:config'.
|
165
|
+
* Note: if you have global configuration defaults set in config.rb,
|
166
|
+
be sure to transfer them to your new config.rb, should you choose
|
167
|
+
to generate a new config.rb file.
|
168
|
+
EOS
|
169
|
+
return Config.config_file
|
170
|
+
end
|
171
|
+
super
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
%w[helpers store].each do |file|
|
4
|
+
require File.expand_path("../configuration/#{file}", __FILE__)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Temporary measure for deprecating the use of Configuration
|
8
|
+
# namespaced classes for setting pre-configured defaults.
|
9
|
+
module Backup
|
10
|
+
module Configuration
|
11
|
+
extend self
|
12
|
+
|
13
|
+
##
|
14
|
+
# Pass calls on to the proper class and log a warning
|
15
|
+
def defaults(&block)
|
16
|
+
klass = eval(self.to_s.sub('Configuration::', ''))
|
17
|
+
Logger.warn Errors::ConfigurationError.new <<-EOS
|
18
|
+
[DEPRECATION WARNING]
|
19
|
+
#{ self }.defaults is being deprecated.
|
20
|
+
To set pre-configured defaults for #{ klass }, use:
|
21
|
+
#{ klass }.defaults
|
22
|
+
EOS
|
23
|
+
klass.defaults(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def const_missing(const)
|
29
|
+
const_set(const, Module.new { extend Configuration })
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
def self.included(klass)
|
8
|
+
klass.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
##
|
14
|
+
# Returns or yields the Configuration::Store
|
15
|
+
# for storing pre-configured defaults for the class.
|
16
|
+
def defaults
|
17
|
+
@configuration ||= Configuration::Store.new
|
18
|
+
|
19
|
+
if block_given?
|
20
|
+
yield @configuration
|
21
|
+
else
|
22
|
+
@configuration
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Used only within the specs
|
28
|
+
def clear_defaults!
|
29
|
+
defaults.reset!
|
30
|
+
end
|
31
|
+
|
32
|
+
def deprecations
|
33
|
+
@deprecations ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def log_deprecation_warning(name, deprecation)
|
37
|
+
msg = "#{ self }##{ name } has been deprecated as of " +
|
38
|
+
"backup v.#{ deprecation[:version] }"
|
39
|
+
msg << "\n#{ deprecation[:message] }" if deprecation[:message]
|
40
|
+
Logger.warn Backup::Errors::ConfigurationError.new <<-EOS
|
41
|
+
[DEPRECATION WARNING]
|
42
|
+
#{ msg }
|
43
|
+
EOS
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
##
|
49
|
+
# Method to deprecate an attribute.
|
50
|
+
#
|
51
|
+
# :version
|
52
|
+
# Must be set to the backup version which will first
|
53
|
+
# introduce the deprecation.
|
54
|
+
#
|
55
|
+
# :action
|
56
|
+
# If set, this Proc will be called with a reference to the
|
57
|
+
# class instance and the value set on the deprecated accessor.
|
58
|
+
# e.g. deprecation[:action].call(klass, value)
|
59
|
+
# This should perform whatever action is neccessary, such as
|
60
|
+
# transferring the value to a new accessor.
|
61
|
+
#
|
62
|
+
# :message
|
63
|
+
# If set, this will be appended to #log_deprecation_warning
|
64
|
+
#
|
65
|
+
# Note that this replaces the `attr_accessor` method, or other
|
66
|
+
# method previously used to set the accessor being deprecated.
|
67
|
+
# #method_missing will handle any calls to `name=`.
|
68
|
+
#
|
69
|
+
def attr_deprecate(name, args = {})
|
70
|
+
deprecations[name] = {
|
71
|
+
:version => nil,
|
72
|
+
:message => nil,
|
73
|
+
:action => nil
|
74
|
+
}.merge(args)
|
75
|
+
end
|
76
|
+
|
77
|
+
end # ClassMethods
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
##
|
82
|
+
# Sets any pre-configured default values.
|
83
|
+
# If a default value was set for an invalid accessor,
|
84
|
+
# this will raise a NameError.
|
85
|
+
def load_defaults!
|
86
|
+
configuration = self.class.defaults
|
87
|
+
configuration._attributes.each do |name|
|
88
|
+
send(:"#{ name }=", configuration.send(name))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
##
|
93
|
+
# Check missing methods for deprecated attribute accessors.
|
94
|
+
#
|
95
|
+
# If a value is set on an accessor that has been deprecated
|
96
|
+
# using #attr_deprecate, a warning will be issued and any
|
97
|
+
# :action (Proc) specified will be called with a reference to
|
98
|
+
# the class instance and the value set on the deprecated accessor.
|
99
|
+
# See #attr_deprecate and #log_deprecation_warning
|
100
|
+
#
|
101
|
+
# Note that OpenStruct (used for setting defaults) does not allow
|
102
|
+
# multiple arguments when assigning values for members.
|
103
|
+
# So, we won't allow it here either, even though an attr_accessor
|
104
|
+
# will accept and convert them into an Array. Therefore, setting
|
105
|
+
# an option value using multiple values, whether as a default or
|
106
|
+
# directly on the class' accessor, should not be supported.
|
107
|
+
# i.e. if an option will accept being set as an Array, then it
|
108
|
+
# should be explicitly set as such. e.g. option = [val1, val2]
|
109
|
+
#
|
110
|
+
def method_missing(name, *args)
|
111
|
+
deprecation = nil
|
112
|
+
if method = name.to_s.chomp!('=')
|
113
|
+
if (len = args.count) != 1
|
114
|
+
raise ArgumentError,
|
115
|
+
"wrong number of arguments (#{ len } for 1)", caller(1)
|
116
|
+
end
|
117
|
+
deprecation = self.class.deprecations[method.to_sym]
|
118
|
+
end
|
119
|
+
|
120
|
+
if deprecation
|
121
|
+
self.class.log_deprecation_warning(method, deprecation)
|
122
|
+
deprecation[:action].call(self, args[0]) if deprecation[:action]
|
123
|
+
else
|
124
|
+
super
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|