backup_monitor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc3244e3fc514e8900ddd48ef4790e859ccaf522
4
+ data.tar.gz: aa5fc44a704cc6989ef0cc29e37933b65728ef9d
5
+ SHA512:
6
+ metadata.gz: 084a8f544d62ff76127d611874571223cf05199a827d43783b0670b9c4da0455ddc5ede37710ae005a2d93cd1944658cb3fd9ef6f61fcf4c517cf515f2c2ec91
7
+ data.tar.gz: a431ef968c778e166a6af23f1da3c5b82c8ef6863926e1d47a97a6a7eecf92f5790da69aa1c5ee883c074ffc4963e4d9ce3f50fa723d77561b19a2e2b7639ae4
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in backup_monitor.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aaron Ten Clay
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # BackupMonitor
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'backup_monitor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install backup_monitor
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/backup_monitor/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'bundler/setup'
3
+ require 'rake/testtask'
4
+
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = FileList['test/test_*.rb']
8
+ end
9
+
10
+
11
+ task default: :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path '../lib', __FILE__
3
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
4
+ require 'backup_monitor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'backup_monitor'
8
+ spec.version = BackupMonitor::VERSION
9
+ spec.authors = ['Aaron Ten Clay']
10
+ spec.email = ['backup_monitor_gem@aarontc.com']
11
+ spec.summary = %q{Checks the modification age of a directory hierarchy to locate stale backups.}
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/AaronTC/backup_monitor'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split "\x0"
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep %r{^(test|spec|features)/}
19
+ spec.require_paths = %w[lib]
20
+
21
+ %w[coalesce thor].each do |gem|
22
+ spec.add_dependency gem
23
+ end
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.6'
26
+ %w[fakefs minitest minitest-ci rake rr simplecov simplecov-rcov spork spork-minitest].each do |gem|
27
+ spec.add_development_dependency gem
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'backup_monitor/cli'
4
+ BackupMonitor::CLI.start
@@ -0,0 +1,3 @@
1
+ require 'backup_monitor/version'
2
+
3
+ require 'backup_monitor/checker'
@@ -0,0 +1,35 @@
1
+ require 'coalesce'
2
+ require 'pathname'
3
+ require 'backup_monitor/directory_hierarchy_modification_time'
4
+
5
+ module BackupMonitor
6
+ class Checker
7
+ def initialize(options = {})
8
+ @warning_threshold = options.delete(:warning_threshold)._? { raise ArgumentError, ':warning_threshold (seconds) must be supplied in options hash' }
9
+ end
10
+
11
+ def check(path)
12
+ pathname = real_path path
13
+ result = {}
14
+ Dir[pathname.join('*')].each do |d|
15
+ full_path = pathname.join d
16
+ next if %w[. ..].include? d
17
+ next unless File.directory? full_path
18
+ dir_mtime = DirectoryHierarchyModificationTime.modification_time full_path
19
+ if dir_mtime.nil?
20
+ # No files in the tree
21
+ result[full_path] = Time.at(0)
22
+ else
23
+ result[full_path] = dir_mtime if (Time.now - dir_mtime).to_i > @warning_threshold
24
+ end
25
+ end
26
+ result
27
+ end
28
+
29
+
30
+ # This method wrapper exists solely for testing due to some outstanding bugs in FakeFS
31
+ def real_path(path)
32
+ Pathname.new(path).realpath
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'thor'
2
+ require 'backup_monitor/checker'
3
+
4
+ module BackupMonitor
5
+ class CLI < Thor
6
+
7
+ desc 'check', 'Checks a directory of directories for modification times'
8
+ method_option :threshold, aliases: '-t', desc: 'Warning threshold, in seconds', type: :numeric, default: 300
9
+ def check(base_path = nil)
10
+ base_path ||= Dir.pwd
11
+
12
+ checker = Checker.new warning_threshold: options[:threshold].to_i
13
+ result = checker.check base_path
14
+ if result.empty?
15
+ STDOUT.puts 'No stale directories found'
16
+ else
17
+ STDERR.puts 'Found stale directories:'
18
+ result.each_pair do |key, value|
19
+ STDERR.puts "\t#{value} - #{key}"
20
+ end
21
+ end
22
+ end
23
+
24
+ default_task :check
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ module BackupMonitor
2
+ class DirectoryHierarchyModificationTime
3
+ # TODO: This is slow and kludgy
4
+ def self.modification_time(root_path)
5
+ newest_mtime = nil
6
+ newest_path = nil
7
+ Dir["#{root_path}/**"].each do |f|
8
+ next if File.directory? f
9
+ mtime = File.mtime f
10
+ if newest_mtime.nil? or mtime > newest_mtime
11
+ newest_mtime = mtime
12
+ newest_path = f
13
+ end
14
+ end
15
+ newest_mtime
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module BackupMonitor
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'simplecov'
2
+ require 'simplecov-rcov'
3
+
4
+ # Configure code coverage
5
+ SimpleCov.coverage_dir 'build/output/coverage'
6
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
7
+ SimpleCov.start unless SimpleCov.running
8
+
9
+ # Configure minitest
10
+ require 'minitest/pride'
11
+ require 'minitest/autorun'
12
+ require 'rr'
13
+
14
+ # Test dependencies
15
+ require 'fakefs/safe'
16
+
17
+ Minitest::Ci.clean = false
18
+ Minitest::Ci.report_dir = 'build/output/test'
@@ -0,0 +1,52 @@
1
+ require_relative 'helper'
2
+
3
+ require 'backup_monitor/checker'
4
+
5
+ class TestBackupMonitorChecker < Minitest::Test
6
+ def test_with_two_old_backups
7
+ mock(BackupMonitor::DirectoryHierarchyModificationTime).modification_time('/backups/host1.domain') { Time.new 2014, 7, 14, 16, 15, 22, '-07:00' }
8
+ mock(BackupMonitor::DirectoryHierarchyModificationTime).modification_time('/backups/host2.domain') { Time.new 2014, 8, 2, 1, 3, 5, '-07:00' }
9
+ mock(BackupMonitor::DirectoryHierarchyModificationTime).modification_time('/backups/host3.otherdomain') { Time.new 2014, 2, 4, 22, 45, 8, '-07:00' }
10
+ mock(FakeFS::Dir).[]('/backups/*') { %w[. .. host1.domain host2.domain host3.otherdomain].shuffle }
11
+ mock(FakeFS::File).directory?('/backups/host1.domain') { true }
12
+ mock(FakeFS::File).directory?('/backups/host2.domain') { true }
13
+ mock(FakeFS::File).directory?('/backups/host3.otherdomain') { true }
14
+ stub(Time).now { Time.new 2014, 8, 3, 16, 23, 26, '-07:00' }
15
+
16
+ actual = nil
17
+ FakeFS do
18
+ uut = BackupMonitor::Checker.new warning_threshold: (3 * 24 * 60 * 60)
19
+ stub(uut).real_path.with_any_args { stub!.join.with_any_args { |x| "/backups/#{x}" } }
20
+ actual = uut.check '/backups'
21
+ end
22
+
23
+ expected = {
24
+ '/backups/host1.domain' => Time.new(2014, 7, 14, 16, 15, 22, '-07:00'),
25
+ '/backups/host3.otherdomain' => Time.new(2014, 2, 4, 22, 45, 8, '-07:00')
26
+ }
27
+
28
+ assert_equal expected, actual, 'Checker did not return expected list of outdated directories'
29
+ end
30
+
31
+
32
+ def test_with_no_old_backups
33
+ mock(BackupMonitor::DirectoryHierarchyModificationTime).modification_time('/backups/other/host44.domain') { Time.new 2014, 8, 2, 7, 32, 16, '-07:00' }
34
+ mock(BackupMonitor::DirectoryHierarchyModificationTime).modification_time('/backups/other/host45.domain') { Time.new 2014, 8, 2, 2, 17, 52, '-07:00' }
35
+ mock(BackupMonitor::DirectoryHierarchyModificationTime).modification_time('/backups/other/host46.otherdomain') { Time.new 2014, 8, 2, 4, 3, 41, '-07:00' }
36
+ mock(FakeFS::Dir).[]('/backups/other/*') { %w[. .. host44.domain host45.domain host46.otherdomain].shuffle }
37
+ mock(FakeFS::File).directory?('/backups/other/host44.domain') { true }
38
+ mock(FakeFS::File).directory?('/backups/other/host45.domain') { true }
39
+ mock(FakeFS::File).directory?('/backups/other/host46.otherdomain') { true }
40
+ stub(Time).now { Time.new 2014, 8, 3, 16, 23, 26, '-07:00' }
41
+
42
+ actual = nil
43
+ FakeFS do
44
+ uut = BackupMonitor::Checker.new warning_threshold: (3 * 24 * 60 * 60)
45
+ stub(uut).real_path.with_any_args { stub!.join.with_any_args { |x| "/backups/other/#{x}" } }
46
+ actual = uut.check '/backups/other'
47
+ end
48
+
49
+ expected = {}
50
+ assert_equal expected, actual, 'Checker did not return empty hash with no old backups for #check'
51
+ end
52
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'helper'
2
+
3
+ require 'backup_monitor/directory_hierarchy_modification_time'
4
+
5
+ class TestDirectoryHierarchyModificationTime < Minitest::Test
6
+ def test_modification_time_with_some_files
7
+ actual = nil
8
+ expected = Time.new 2014, 8, 3, 15, 05, 01, '-07:00'
9
+
10
+ FakeFS do
11
+ # There is a bug in FakeFS, the following line should work but doesn't:
12
+ #FileUtils.mkdir_p '/my_directory/some_cool/files_here'
13
+
14
+ # This is the workaround:
15
+ FileUtils.mkdir '/my_directory'
16
+ FileUtils.mkdir '/my_directory/some_cool'
17
+ FileUtils.mkdir '/my_directory/some_cool/files_here'
18
+
19
+ FileUtils.touch '/my_directory/LICENSE.md', mtime: (expected - 1000)
20
+ FileUtils.touch '/my_directory/some_cool/files_here/my_file.txt', mtime: expected
21
+ FileUtils.touch '/my_directory/some_cool/README.md', mtime: (expected - 100)
22
+
23
+ actual = BackupMonitor::DirectoryHierarchyModificationTime.modification_time '/my_directory'
24
+ end
25
+
26
+ assert_equal expected, actual, 'The modification time returned was incorrect'
27
+ end
28
+
29
+
30
+ def test_modification_time_with_no_files
31
+ actual = nil
32
+
33
+ FakeFS do
34
+ # There is a bug in FakeFS, the following line should work but doesn't:
35
+ #FileUtils.mkdir_p '/my_directory/some_cool/files_here'
36
+
37
+ # This is the workaround:
38
+ FileUtils.mkdir '/my_directory2'
39
+ FileUtils.mkdir '/my_directory2/some_cool'
40
+ FileUtils.mkdir '/my_directory2/some_cool/files_here'
41
+
42
+ actual = BackupMonitor::DirectoryHierarchyModificationTime.modification_time '/my_directory2'
43
+ end
44
+
45
+ assert_nil actual, 'The modification time returned was not nil and should have been'
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backup_monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Ten Clay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coalesce
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
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fakefs
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-ci
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov-rcov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: spork
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: spork-minitest
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Checks the modification age of a directory hierarchy to locate stale
182
+ backups.
183
+ email:
184
+ - backup_monitor_gem@aarontc.com
185
+ executables:
186
+ - backup_monitor
187
+ extensions: []
188
+ extra_rdoc_files: []
189
+ files:
190
+ - .gitignore
191
+ - Gemfile
192
+ - LICENSE.txt
193
+ - README.md
194
+ - Rakefile
195
+ - backup_monitor.gemspec
196
+ - bin/backup_monitor
197
+ - lib/backup_monitor.rb
198
+ - lib/backup_monitor/checker.rb
199
+ - lib/backup_monitor/cli.rb
200
+ - lib/backup_monitor/directory_hierarchy_modification_time.rb
201
+ - lib/backup_monitor/version.rb
202
+ - test/helper.rb
203
+ - test/test_backup_monitor_checker.rb
204
+ - test/test_directory_hierarchy_modification_time.rb
205
+ homepage: https://github.com/AaronTC/backup_monitor
206
+ licenses:
207
+ - MIT
208
+ metadata: {}
209
+ post_install_message:
210
+ rdoc_options: []
211
+ require_paths:
212
+ - lib
213
+ required_ruby_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - '>='
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ required_rubygems_version: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - '>='
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ requirements: []
224
+ rubyforge_project:
225
+ rubygems_version: 2.0.14
226
+ signing_key:
227
+ specification_version: 4
228
+ summary: Checks the modification age of a directory hierarchy to locate stale backups.
229
+ test_files:
230
+ - test/helper.rb
231
+ - test/test_backup_monitor_checker.rb
232
+ - test/test_directory_hierarchy_modification_time.rb