maxaf-fluke 0.0.1

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/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 0.0.1 2009-09-05
2
+
3
+ * Initial release: implemented minimum set of core features.
4
+ * Threaded operation, isolated watchers.
5
+ * Amazon S3 integration and AR migrations allow setup with minimum
6
+ configuration/setup.
data/Manifest.txt ADDED
@@ -0,0 +1,19 @@
1
+ bin/fluke
2
+ db/migrate/0001_create_watchers.rb
3
+ db/migrate/0002_create_results.rb
4
+ History.txt
5
+ lib/fluke/cli.rb
6
+ lib/fluke/monkey_patch.rb
7
+ lib/fluke.rb
8
+ lib/fluke/result.rb
9
+ lib/fluke/watcher.rb
10
+ Manifest.txt
11
+ Rakefile
12
+ README.rdoc
13
+ script/console
14
+ script/destroy
15
+ script/generate
16
+ tasks/ar.rake
17
+ test/test_fluke_cli.rb
18
+ test/test_fluke.rb
19
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = fluke
2
+
3
+ * http://github.com/maxaf/fluke
4
+
5
+ == DESCRIPTION:
6
+
7
+ A simple resource observer designed to detect change over time.
8
+
9
+ == SYNOPSIS:
10
+
11
+ $ fluke --help
12
+
13
+ == REQUIREMENTS:
14
+
15
+ * Ruby 1.8.6+ || JRuby 1.3.1+
16
+ * ActiveRecord (tested with 2.3+)
17
+ * Some database
18
+ * github.com/marcel/aws-s3
19
+
20
+ == INSTALL:
21
+
22
+ # gem install fluke
23
+
24
+ == LICENSE:
25
+
26
+ (The MIT License)
27
+
28
+ Copyright (c) 2009 Max Afonov
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ 'Software'), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/fluke'
6
+
7
+ Hoe.plugin :newgem
8
+
9
+ $hoe = Hoe.spec 'fluke' do
10
+ self.developer 'Max Afonov', 'max@bumnetworks.com'
11
+ self.rubyforge_name = self.name
12
+ self.extra_deps = [
13
+ ['activerecord', '>=2.3'],
14
+ ['aws-s3', '>=0.6.2'],
15
+ ['httpclient', '>=2.1.5.2'],
16
+ ]
17
+ end
18
+
19
+ require 'newgem/tasks'
20
+ Dir['tasks/**/*.rake'].each { |t| load t }
data/bin/fluke ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/fluke")
5
+ require "fluke/cli"
6
+
7
+ Fluke::CLI.execute(STDOUT, STDERR, ARGV)
@@ -0,0 +1,18 @@
1
+ class CreateWatchers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :watchers do |t|
4
+ t.string :name
5
+ t.text :url
6
+ t.decimal :delay
7
+ t.timestamps
8
+ end
9
+
10
+ change_table :watchers do |t|
11
+ t.index([:name], :unique => true)
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :watchers
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ class CreateResults < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :results do |t|
4
+ t.references :watcher
5
+ t.string :checksum
6
+ t.timestamps
7
+ end
8
+
9
+ change_table :results do |t|
10
+ t.index([:checksum], :unique => false)
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :results
16
+ end
17
+ end
data/lib/fluke/cli.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'optparse'
2
+
3
+ module Fluke
4
+ class CLI
5
+ def self.execute(stdout, stderr, arguments=[])
6
+ p = {
7
+ :stdout => stdout, :stderr => stderr
8
+ }
9
+
10
+ parser = OptionParser.new do |opts|
11
+ opts.banner = <<-BANNER.gsub(/^ /,'')
12
+ A simple resource observer designed to detect change over time.
13
+
14
+ Usage: #{File.basename($0)} [options]
15
+ # run all watchers
16
+
17
+ -OR-
18
+
19
+ #{File.basename($0)} [options] watcher1..watcherN
20
+ # run particular watchers
21
+ Options are:
22
+ BANNER
23
+ opts.separator ""
24
+ opts.on("-c", "--conf", String,
25
+ "Config file location",
26
+ "Default: #{Fluke::DEFAULT_CONF_PATH}") { |arg| p[:conf_path] = arg || nil }
27
+ opts.on("-v", "--verbose",
28
+ "Be chatty.",
29
+ "Default: false") { |arg| Fluke.verbose = arg }
30
+ opts.on("-h", "--help",
31
+ "Show this help message.") { stderr.puts opts; exit }
32
+ opts.parse!(arguments)
33
+ end
34
+
35
+ Fluke.configure! p
36
+
37
+ if arguments.size > 0 then
38
+ arguments.uniq.each do |name|
39
+ Fluke.watch name
40
+ end
41
+ else
42
+ Watcher.find(:all).each do |watcher|
43
+ Fluke.watch watcher
44
+ end
45
+ end
46
+
47
+ Fluke.run!
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ module Fluke
2
+ module Delays
3
+ def seconds
4
+ return self*1000
5
+ end
6
+
7
+ def minutes
8
+ return self.seconds*60
9
+ end
10
+
11
+ def hours
12
+ return self.minutes*60
13
+ end
14
+
15
+ def days
16
+ return self.hours*24
17
+ end
18
+ end
19
+ end
20
+
21
+ class Fixnum
22
+ include Fluke::Delays
23
+ end
24
+
25
+ class Float
26
+ include Fluke::Delays
27
+ end
@@ -0,0 +1,45 @@
1
+ require 'digest/sha1'
2
+ require 'aws/s3'
3
+
4
+ module Fluke
5
+ class Result < ActiveRecord::Base
6
+ belongs_to :watcher
7
+
8
+ class Body < AWS::S3::S3Object
9
+ end
10
+
11
+ def body_key
12
+ "#{self.watcher.name}/#{self.checksum}"
13
+ end
14
+
15
+ def body
16
+ begin
17
+ return Body.find self.body_key
18
+ rescue AWS::S3::NoSuchKey => nsk
19
+ return nil
20
+ end
21
+ end
22
+
23
+ def body?
24
+ Body.exists? self.body_key
25
+ end
26
+
27
+ def self.from_response(watcher, content)
28
+ checksum = Digest::SHA1.hexdigest(content)
29
+ result = Result.new :checksum => checksum
30
+ result.watcher = watcher
31
+ result.save
32
+
33
+ Thread.new do
34
+ if result.body?
35
+ Fluke.log { "#{result.watcher} exists: #{result.body_key}" }
36
+ else
37
+ Fluke.log { "#{result.watcher} store: #{result.body_key}" }
38
+ Body.store(result.body_key, content.dup)
39
+ end
40
+ end
41
+
42
+ result
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ require 'fluke/result'
4
+ require 'httpclient'
5
+
6
+ module Fluke
7
+ class Watcher < ActiveRecord::Base
8
+ has_many :results
9
+ attr_accessor :thread
10
+
11
+ def run
12
+ hc = HTTPClient.new
13
+ http_method = self.meth.downcase.to_sym
14
+ unless hc.respond_to?(http_method)
15
+ Fluke::log { "#{self}: invalid method '#{http_method}', converting to :get" }
16
+ http_method = :get
17
+ end
18
+ res = hc.send(http_method, self.url)
19
+ result = Result.from_response(self, res.body.dump.dup)
20
+ end
21
+
22
+ def wait
23
+ sleep self.delay/1000.000
24
+ end
25
+
26
+ def to_s
27
+ "Watcher<#{name}>"
28
+ end
29
+ end
30
+ end
data/lib/fluke.rb ADDED
@@ -0,0 +1,85 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'yaml'
6
+ require 'fluke/watcher'
7
+ require 'fluke/monkey_patch'
8
+ require 'activerecord'
9
+ require 'aws/s3'
10
+
11
+ module Fluke
12
+ VERSION = '0.0.1'
13
+ ROOT = Dir.new(File.expand_path(File.dirname(__FILE__) + "/../"))
14
+ DEFAULT_CONF_PATH = "~/.fluke.yml"
15
+ @@verbose = false
16
+ @@watchers = {}
17
+
18
+ def self.verbose?
19
+ @@verbose
20
+ end
21
+
22
+ def self.verbose=(v = false)
23
+ @@verbose = v ? true : false
24
+ end
25
+
26
+ def self.watchers
27
+ @@watchers
28
+ end
29
+
30
+ def self.conf
31
+ @@conf.dup
32
+ end
33
+
34
+ def self.log
35
+ return unless verbose? and block_given?
36
+ @@log_here.puts yield
37
+ end
38
+
39
+ def self.watch(watcher, &init)
40
+ unless watcher.is_a?(Watcher)
41
+ watcher = Watcher.find_by_name(watcher.to_s)
42
+ end
43
+ watchers[watcher.name] = watcher
44
+ if init
45
+ watcher.instance_eval &init
46
+ end
47
+ end
48
+
49
+ def self.configure!(opts = {})
50
+ p = {
51
+ :conf_path => File.expand_path(DEFAULT_CONF_PATH),
52
+ :stdout => STDOUT,
53
+ :stderr => STDERR,
54
+ :log_here => :stderr
55
+ }
56
+ p.merge! opts
57
+ @@log_here = p[p[:log_here]] || p[:stderr]
58
+ begin
59
+ @@conf = YAML::load(File.open(p[:conf_path]))
60
+ ActiveRecord::Base.colorize_logging = false
61
+ ActiveRecord::Base.logger = Logger.new(@@log_here) if verbose?
62
+ ActiveRecord::Base.establish_connection(@@conf[:db])
63
+ AWS::S3::Base.establish_connection!(@@conf[:s3][:auth])
64
+ Result::Body.set_current_bucket_to @@conf[:s3][:bucket]
65
+ rescue => e
66
+ STDERR.puts e.inspect
67
+ return false
68
+ end
69
+ return true
70
+ end
71
+
72
+ def self.run!
73
+ watchers.each do |n,watcher|
74
+ watcher.thread = Thread.new do
75
+ while true
76
+ watcher.run
77
+ watcher.wait
78
+ end
79
+ end
80
+ end
81
+
82
+ sleep while true
83
+ end
84
+
85
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/fluke.rb'}"
9
+ puts "Loading fluke gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/tasks/ar.rake ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ require 'yaml'
4
+
5
+ namespace :db do
6
+ task :migrate_setup do
7
+ Fluke.verbose = true
8
+ ActiveRecord::Migration.verbose = Fluke.verbose?
9
+ Fluke.configure!
10
+ end
11
+ task(:migrate => :migrate_setup) do
12
+ ActiveRecord::Migrator.migrate("db/migrate")
13
+ end
14
+ task(:rollback => :migrate_setup) do
15
+ ActiveRecord::Migrator.rollback("db/migrate")
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestFluke < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper.rb")
2
+ require 'fluke/cli'
3
+
4
+ class TestFlukeCli < Test::Unit::TestCase
5
+ def setup
6
+ Fluke::CLI.execute(@stdout_io = StringIO.new, [])
7
+ @stdout_io.rewind
8
+ @stdout = @stdout_io.read
9
+ end
10
+
11
+ def test_print_default_output
12
+ assert_match(/To update this executable/, @stdout)
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/fluke'
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maxaf-fluke
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max Afonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-06 00:00:00 -07:00
13
+ default_executable: fluke
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.3"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: aws-s3
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: httpclient
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.1.5.2
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: pvande-differ
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.1.1
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.3.3
64
+ version:
65
+ description: A simple resource observer designed to detect change over time.
66
+ email:
67
+ - max@bumnetworks.com
68
+ executables:
69
+ - fluke
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - History.txt
74
+ - Manifest.txt
75
+ files:
76
+ - bin/fluke
77
+ - db/migrate/0001_create_watchers.rb
78
+ - db/migrate/0002_create_results.rb
79
+ - History.txt
80
+ - lib/fluke/cli.rb
81
+ - lib/fluke/monkey_patch.rb
82
+ - lib/fluke.rb
83
+ - lib/fluke/result.rb
84
+ - lib/fluke/watcher.rb
85
+ - Manifest.txt
86
+ - Rakefile
87
+ - README.rdoc
88
+ - script/console
89
+ - script/destroy
90
+ - script/generate
91
+ - tasks/ar.rake
92
+ - test/test_fluke_cli.rb
93
+ - test/test_fluke.rb
94
+ - test/test_helper.rb
95
+ has_rdoc: false
96
+ homepage: http://github.com/maxaf/fluke
97
+ post_install_message:
98
+ rdoc_options:
99
+ - --main
100
+ - README.rdoc
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: "0"
108
+ version:
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ version:
115
+ requirements: []
116
+
117
+ rubyforge_project: fluke
118
+ rubygems_version: 1.2.0
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A simple resource observer designed to detect change over time.
122
+ test_files:
123
+ - test/test_fluke_cli.rb
124
+ - test/test_helper.rb
125
+ - test/test_fluke.rb