auto_cron 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +18 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/auto_cron.gemspec +63 -0
- data/generators/auto_cron/auto_cron_generator.rb +14 -0
- data/generators/auto_cron/templates/header.erb +9 -0
- data/generators/auto_cron/templates/publish.erb +1 -0
- data/install.rb +6 -0
- data/lib/auto_cron.rb +65 -0
- data/lib/capistrano/auto_cron.rb +22 -0
- data/lib/tasks/auto_cron_tasks.rake +12 -0
- data/test/helper.rb +18 -0
- data/test/test_auto_cron.rb +7 -0
- metadata +129 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
# gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2)
|
10
|
+
rcov (0.9.10)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
bundler (~> 1.0.0)
|
17
|
+
jeweler (~> 1.6.4)
|
18
|
+
rcov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Alex Farrill
|
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/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= auto_cron
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to auto_cron
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Alex Farrill. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "auto_cron"
|
18
|
+
gem.homepage = "http://github.com/alexfarrill/auto_cron"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Generate a crontab}
|
21
|
+
gem.description = %Q{Generate a crontab}
|
22
|
+
gem.email = "alex.farrill@gmail.com"
|
23
|
+
gem.authors = ["Alex Farrill"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "auto_cron #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/auto_cron.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{auto_cron}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Alex Farrill"]
|
12
|
+
s.date = %q{2011-09-04}
|
13
|
+
s.description = %q{Generate a crontab}
|
14
|
+
s.email = %q{alex.farrill@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"auto_cron.gemspec",
|
28
|
+
"generators/auto_cron/auto_cron_generator.rb",
|
29
|
+
"generators/auto_cron/templates/header.erb",
|
30
|
+
"generators/auto_cron/templates/publish.erb",
|
31
|
+
"install.rb",
|
32
|
+
"lib/auto_cron.rb",
|
33
|
+
"lib/capistrano/auto_cron.rb",
|
34
|
+
"lib/tasks/auto_cron_tasks.rake",
|
35
|
+
"test/helper.rb",
|
36
|
+
"test/test_auto_cron.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/alexfarrill/auto_cron}
|
39
|
+
s.licenses = ["MIT"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.7}
|
42
|
+
s.summary = %q{Generate a crontab}
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
51
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
55
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class AutoCronGenerator < Rails::Generator::Base
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
dest_template_dir = "config/auto_cron"
|
5
|
+
m.directory dest_template_dir
|
6
|
+
Dir.entries( File.dirname(__FILE__) + "/templates" ).each do |entry|
|
7
|
+
unless %w( . .. ).include?( entry )
|
8
|
+
dest_template_file = File.join( dest_template_dir, entry )
|
9
|
+
m.file entry, dest_template_file
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
# THIS CRON IS AUTO-GENERATED
|
3
|
+
# -----------------------------------------------------------------------------
|
4
|
+
# This crontab should not be edited directly; any such changes will be
|
5
|
+
# overwritten with the next deploy.
|
6
|
+
#
|
7
|
+
# To edit this crontab, edit the *.erb files in the config/auto_cron/ directory
|
8
|
+
# of the main project.
|
9
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
### publish crons, should only be on one machine per environment ###
|
data/install.rb
ADDED
data/lib/auto_cron.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
class AutoCron
|
2
|
+
def initialize( template, application )
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
@auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
|
6
|
+
@template = template
|
7
|
+
@application = application
|
8
|
+
end
|
9
|
+
|
10
|
+
def updated_crontab
|
11
|
+
# Check for unopened or unclosed identifier blocks
|
12
|
+
if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
|
13
|
+
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
|
14
|
+
exit(1)
|
15
|
+
elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
16
|
+
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
|
17
|
+
exit(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
# If an existing identier block is found, replace it with the new cron entries
|
21
|
+
if read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
22
|
+
read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
|
23
|
+
else # Otherwise, append the new cron entries after any existing ones
|
24
|
+
[read_crontab, auto_cron_wrapped].join("\n\n")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def auto_cron_wrapped
|
31
|
+
@auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
|
32
|
+
end
|
33
|
+
|
34
|
+
def auto_cron_inner
|
35
|
+
header_template = File.join( @auto_cron_dir, 'header.erb' )
|
36
|
+
header = if File.exist?( header_template )
|
37
|
+
ERB.new( File.read( header_template ) ).result
|
38
|
+
else
|
39
|
+
''
|
40
|
+
end
|
41
|
+
full_template_path = File.join( @auto_cron_dir, "#{ @template }.erb" )
|
42
|
+
header + "\n\n" + ERB.new( File.read( full_template_path ) ).result
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_crontab
|
46
|
+
return @current_crontab if @current_crontab
|
47
|
+
|
48
|
+
command = ['crontab -l']
|
49
|
+
command_results = %x[#{command.join(' ')} 2> /dev/null]
|
50
|
+
@current_crontab = $?.exitstatus.zero? ? command_results : ''
|
51
|
+
end
|
52
|
+
|
53
|
+
def comment_base
|
54
|
+
"auto_cron generated tasks for: #{ @application }"
|
55
|
+
end
|
56
|
+
|
57
|
+
def comment_open
|
58
|
+
"# Begin #{ comment_base }"
|
59
|
+
end
|
60
|
+
|
61
|
+
def comment_close
|
62
|
+
"# End #{ comment_base }"
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Capistrano::Configuration.instance.load do
|
2
|
+
namespace :auto_cron do
|
3
|
+
task :publish, :roles => :publish do
|
4
|
+
run "cd #{release_path} && rake TEMPLATE=publish RAILS_ENV=#{rails_env} auto_cron"
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Connects to each box individual and installs the specifed cron
|
8
|
+
USAGE:: role :publish_cron, 'prilb', :crontab => 'cron' "
|
9
|
+
task :publish_crons, :roles => :publish_cron do
|
10
|
+
find_servers_for_task( current_task ).each do |sd|
|
11
|
+
establish_connections_to( sd )
|
12
|
+
targets = []
|
13
|
+
targets.push sessions[sd]
|
14
|
+
|
15
|
+
Command.process( "cd #{release_path} && rake TEMPLATE=#{sd.options[:crontab]} RAILS_ENV=#{rails_env} auto_cron",
|
16
|
+
targets, options.merge(:logger => logger), &block)
|
17
|
+
|
18
|
+
logger.debug "creating crontab:#{sd.options[:crontab]} on #{sd.host}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../auto_cron"
|
2
|
+
|
3
|
+
desc "Generates the auto-cron and saves it to the crontab"
|
4
|
+
task :auto_cron do
|
5
|
+
require 'tempfile'
|
6
|
+
cron_body = AutoCron.auto_cron ENV['TEMPLATE']
|
7
|
+
tmp_cron_path = Tempfile.new( 'auto_cron' ).path
|
8
|
+
File.open( tmp_cron_path, File::WRONLY | File::APPEND ) do |file|
|
9
|
+
file << cron_body
|
10
|
+
end
|
11
|
+
sh "crontab #{ tmp_cron_path }"
|
12
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
# require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'auto_cron'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: auto_cron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alex Farrill
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-04 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 23
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 1.0.0
|
33
|
+
type: :development
|
34
|
+
name: bundler
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 7
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 6
|
47
|
+
- 4
|
48
|
+
version: 1.6.4
|
49
|
+
type: :development
|
50
|
+
name: jeweler
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
name: rcov
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: *id003
|
67
|
+
description: Generate a crontab
|
68
|
+
email: alex.farrill@gmail.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.rdoc
|
76
|
+
files:
|
77
|
+
- .document
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.rdoc
|
82
|
+
- Rakefile
|
83
|
+
- VERSION
|
84
|
+
- auto_cron.gemspec
|
85
|
+
- generators/auto_cron/auto_cron_generator.rb
|
86
|
+
- generators/auto_cron/templates/header.erb
|
87
|
+
- generators/auto_cron/templates/publish.erb
|
88
|
+
- install.rb
|
89
|
+
- lib/auto_cron.rb
|
90
|
+
- lib/capistrano/auto_cron.rb
|
91
|
+
- lib/tasks/auto_cron_tasks.rake
|
92
|
+
- test/helper.rb
|
93
|
+
- test/test_auto_cron.rb
|
94
|
+
has_rdoc: true
|
95
|
+
homepage: http://github.com/alexfarrill/auto_cron
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
hash: 3
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
requirements: []
|
122
|
+
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.3.7
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Generate a crontab
|
128
|
+
test_files: []
|
129
|
+
|