cowtech-rails 2.8.1.0 → 2.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/app/helpers/cowtech/ruby_on_rails/helpers/application_helper.rb +98 -98
- data/app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb +293 -293
- data/app/helpers/cowtech/ruby_on_rails/helpers/browser_helper.rb +62 -62
- data/app/helpers/cowtech/ruby_on_rails/helpers/format_helper.rb +31 -31
- data/app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb +222 -222
- data/app/helpers/cowtech/ruby_on_rails/helpers/validation_helper.rb +60 -60
- data/app/models/cowtech/ruby_on_rails/models/e_mail.rb +55 -49
- data/lib/cowtech.rb +15 -15
- data/lib/cowtech/extensions.rb +34 -34
- data/lib/cowtech/monkey_patches.rb +54 -54
- data/lib/cowtech/scheduler.rb +33 -33
- data/lib/cowtech/tasks/app.rake +102 -102
- data/lib/cowtech/tasks/log.rake +73 -73
- data/lib/cowtech/tasks/mongodb.rake +56 -56
- data/lib/cowtech/tasks/server.rake +96 -96
- data/lib/cowtech/tasks/sql.rake +105 -105
- data/lib/cowtech/version.rb +8 -9
- metadata +4 -4
data/lib/cowtech/scheduler.rb
CHANGED
@@ -5,24 +5,24 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
8
|
+
module RubyOnRails
|
9
|
+
class Scheduler
|
10
10
|
attr_accessor :application
|
11
11
|
attr_accessor :logger
|
12
12
|
attr_accessor :pid
|
13
13
|
attr_accessor :definitions
|
14
14
|
attr_accessor :scheduler
|
15
|
-
|
15
|
+
|
16
16
|
def self.start(application_class, log_file, pid_file, &definitions)
|
17
17
|
self.new(application_class, log_file, pid_file, definitions).execute
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def self.log(msg)
|
21
21
|
puts msg
|
22
22
|
#@@class_logger ||= Logger.new(Rails.root.to_s + "/log/scheduler.log")
|
23
23
|
#@@class_logger.debug(msg)
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def initialize(application_class, log_file, pid_file, block = nil, &definitions)
|
27
27
|
@application = application_class
|
28
28
|
@logger = Logger.new(log_file)
|
@@ -34,20 +34,20 @@ module Cowtech
|
|
34
34
|
|
35
35
|
def log_string(message, options = {})
|
36
36
|
rv = ""
|
37
|
-
|
37
|
+
|
38
38
|
rv += "[#{Time.now.strftime("%Y-%m-%d %T")}] " if !options[:no_time]
|
39
39
|
rv += options[:prefix].ensure_array.collect { |p| "[" + p.center(6, " ") + "]" }.join(" ") + " " if options[:prefix].present?
|
40
40
|
rv += message
|
41
|
-
|
41
|
+
|
42
42
|
rv
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def log(message, options = {})
|
46
46
|
msg = self.log_string(message, options)
|
47
47
|
type = options[:type] || :info
|
48
48
|
(options[:logger] || @logger).send(type, msg)
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
def execute_rake_task(label, name, args)
|
52
52
|
begin
|
53
53
|
args = args.symbolize_keys
|
@@ -64,7 +64,7 @@ module Cowtech
|
|
64
64
|
self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
|
65
65
|
end
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
def execute_inline_task(label, name)
|
69
69
|
begin
|
70
70
|
self.log(label + " ...", {:prefix => ["INLINE", "START", name]})
|
@@ -79,44 +79,44 @@ module Cowtech
|
|
79
79
|
self.log("Scheduler started.", {:prefix => "MAIN"})
|
80
80
|
self.handle_plain
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
def handle_phusion_passenger
|
84
84
|
if defined?(PhusionPassenger) then
|
85
85
|
File.delete(@pid) if FileTest.exists?(@pid)
|
86
|
-
|
87
|
-
|
88
|
-
|
86
|
+
|
87
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
88
|
+
if forked && !FileTest.exists?(@pid) then
|
89
89
|
self.log("Starting process with PID #{$$}", {:prefix => ["WORKER", "START"]})
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
90
|
+
File.open(@pid, "w") { |f| f.write($$) }
|
91
|
+
self.handle_plain
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
PhusionPassenger.on_event(:stopping_worker_process) do
|
96
|
+
if FileTest.exists?(@pid) then
|
97
|
+
if File.open(@pid, "r") { |f| pid = f.read.to_i} == $$ then
|
98
98
|
self.log("Stopped process with PID #{$$}", {:prefix => ["WORKER", "STOP"]})
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
99
|
+
File.delete(@pid)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
103
|
else
|
104
104
|
self.handle_plain
|
105
105
|
end
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
def handle_plain
|
109
109
|
if !@rake_loaded then
|
110
110
|
@application.load_tasks
|
111
111
|
@rake_loaded = true
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
@definitions.call(self)
|
115
|
-
end
|
116
|
-
|
117
|
-
def method_missing(method, *args, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
def method_missing(method, *args, &block)
|
118
118
|
self.scheduler.send(method, *args, &block)
|
119
119
|
end
|
120
|
-
|
121
|
-
|
120
|
+
end
|
121
|
+
end
|
122
122
|
end
|
data/lib/cowtech/tasks/app.rake
CHANGED
@@ -5,113 +5,113 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
8
|
+
module RubyOnRails
|
9
|
+
class AppUtils
|
10
|
+
def self.run_command(cmd)
|
11
|
+
IO.popen(cmd, "r") { |f| puts f.gets }
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.get_version(as_string = true)
|
15
|
+
info = YAML.load_file(Rails.root + "config/application_info.yml")
|
16
|
+
(as_string ? "Current application info --- Version #{info["version"]} - Release date: #{info["release-date"]}" : info)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.set_version(info)
|
20
|
+
puts "--- Setting application version ..."
|
21
21
|
version = Cowtech::RubyOnRails::AppUtils.get_version(false)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
22
|
+
File.open(Rails.root + "config/application_info.yml", "w") { |f| f.write(version.merge(info.stringify_keys).to_yaml) }
|
23
|
+
puts Cowtech::RubyOnRails::AppUtils.get_version
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.tag
|
27
|
+
info = Cowtech::RubyOnRails::AppUtils.get_version(false)
|
28
|
+
tag = "v-#{info["version"]}"
|
29
|
+
puts "--- Tagging current version as tag #{tag}"
|
30
|
+
Cowtech::RubyOnRails::AppUtils.run_command("git tag -f #{tag}")
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.commit(msg)
|
34
|
+
# Commit data
|
35
|
+
puts "--- Adding data to repository..."
|
36
|
+
Cowtech::RubyOnRails::AppUtils.run_command("git add .")
|
37
|
+
|
38
|
+
puts "--- Commiting changes ..."
|
39
|
+
Cowtech::RubyOnRails::AppUtils.run_command("git commit -a --allow-empty-message -m \"#{msg}\"")
|
40
|
+
puts "--- Checking repository status ..."
|
41
|
+
Cowtech::RubyOnRails::AppUtils.run_command("git status")
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.push
|
45
|
+
puts "--- Pushing to server ..."
|
46
|
+
run_command("git push server")
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.clear_cache
|
50
|
+
puts "--- Clearing rails cache ..."
|
51
|
+
Rails.cache.clear
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
55
|
end
|
56
56
|
|
57
57
|
namespace :app do
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
58
|
+
namespace :version do
|
59
|
+
desc "Get application info"
|
60
|
+
task :get do |task|
|
61
|
+
puts Cowtech::RubyOnRails::AppUtils.get_version
|
62
|
+
end
|
63
|
+
|
64
|
+
desc "Set application info"
|
65
|
+
task :set, :version do |task, args|
|
66
|
+
if args[:version] then
|
67
|
+
Cowtech::RubyOnRails::AppUtils.set_version({"version" => args[:version], "release-date" => Time.now.strftime("%Y-%m-%d")})
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
desc "Clears all Rails cache"
|
73
|
+
task :clear_cache => :environment do |task|
|
74
|
+
Cowtech::RubyOnRails::AppUtils.clear_cache
|
75
|
+
end
|
76
|
+
|
77
|
+
desc "Set application info, add all files and commits to git"
|
78
|
+
task :commit, :message, :version do |task, args|
|
79
|
+
args[:message] ||= ""
|
80
|
+
|
81
|
+
Cowtech::RubyOnRails::AppUtils.set_version({"version" => args[:version], "release-date" => Time.now.strftime("%Y-%m-%d")}) if args[:version].present?
|
82
|
+
Cowtech::RubyOnRails::AppUtils.commit(args[:message])
|
83
|
+
Cowtech::RubyOnRails::AppUtils.tag if args[:version].present?
|
84
|
+
end
|
85
|
+
|
86
|
+
desc "Commit application and then push it to server"
|
87
|
+
task :push, :message, :version do |task, args|
|
88
|
+
Cowtech::RubyOnRails::AppUtils.set_version({"version" => args[:version], "release-date" => Time.now.strftime("%Y-%m-%d")}) if args[:version].present?
|
89
|
+
Cowtech::RubyOnRails::AppUtils.commit(args[:message])
|
90
|
+
Cowtech::RubyOnRails::AppUtils.tag if args[:version].present?
|
91
|
+
Cowtech::RubyOnRails::AppUtils.push
|
92
|
+
end
|
93
|
+
|
94
|
+
desc "Tags current application version in git"
|
95
|
+
task :tag do |task|
|
96
|
+
Cowtech::RubyOnRails::AppUtils.tag
|
97
|
+
end
|
98
98
|
end
|
99
99
|
|
100
100
|
namespace :css do
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
101
|
+
desc "Regenerating CSS..."
|
102
|
+
task :regenerate => :environment do |task|
|
103
|
+
puts "Regenerating CSS..."
|
104
|
+
|
105
|
+
if defined?(Less) then # MORE
|
106
|
+
puts "Using More"
|
107
|
+
Rake::Task["more:clean"].execute
|
108
|
+
Rake::Task["more:generate"].execute
|
109
|
+
elsif defined?(Sass) # SASS
|
110
|
+
Sass::Plugin.on_updating_stylesheet do |template, css|
|
111
|
+
puts "[SCSS] Compiling #{template} to #{css} ..."
|
112
|
+
end
|
113
|
+
|
114
|
+
Sass::Plugin.force_update_stylesheets
|
115
|
+
end
|
116
|
+
end
|
117
117
|
end
|
data/lib/cowtech/tasks/log.rake
CHANGED
@@ -5,84 +5,84 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
8
|
+
module RubyOnRails
|
9
|
+
class LogUtils
|
10
|
+
@@log_compressor_command = "bzip2"
|
11
|
+
@@log_compressed_extension = "bz2"
|
12
|
+
|
13
|
+
def self.generate_new_name(base, i = 0)
|
14
|
+
(Rails.root + "backups/logs/#{base}#{i > 0 ? "-#{i}" : ""}").to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.run_command(cmd)
|
18
|
+
IO.popen(cmd) { |f| print f.gets }
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.rotate(rake_args)
|
22
|
+
Cowtech::RubyOnRails::Scheduler.log "Rotating log files..."
|
23
|
+
|
24
|
+
# Get timestamp
|
25
|
+
tstamp = Time.now.strftime("%Y%m%d")
|
26
|
+
|
27
|
+
# For each log file
|
28
|
+
Dir.glob(Rails.root + "log/*.log") do |log_file|
|
29
|
+
Cowtech::RubyOnRails::Scheduler.log "\tRotating #{log_file} ..."
|
30
|
+
new_name = "#{File.basename(log_file)}-#{tstamp}" # CREIAMO IL NOME
|
31
|
+
|
32
|
+
# Resolv duplicates
|
33
|
+
i = 0
|
34
|
+
new_file = catch(:name) do
|
35
|
+
i += 1
|
36
|
+
name = generate_new_name(new_name, i)
|
37
|
+
redo if File.exists?(name . + "." + @@log_compressed_extension)
|
38
|
+
throw :name, name
|
39
|
+
end
|
40
|
+
|
41
|
+
dir = File.dirname(new_file)
|
42
|
+
FileUtils.mkdir_p(dir) if !File.directory?(dir)
|
43
|
+
|
44
|
+
# Send file via mail
|
45
45
|
if (Rails.env == "production" || rake_args["force"].to_boolean) && rake_args["email_class"].present? then
|
46
46
|
Cowtech::RubyOnRails::Scheduler.log "\tForwarding log file to requested email address..."
|
47
|
-
|
47
|
+
rake_args["email_class"].constantize.log_report(log_file).deliver
|
48
48
|
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
50
|
+
# Copy file
|
51
|
+
FileUtils.cp(log_file, new_file)
|
52
|
+
|
53
|
+
# BZIPPIAMO IL FILE
|
54
|
+
system(@@log_compressor_command, new_file)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Truncate files
|
58
|
+
Cowtech::RubyOnRails::Scheduler.log "Truncating current log files ..."
|
59
|
+
Dir.glob(Rails.root + "log/*.log") do |log_file|
|
60
|
+
File.open(log_file, "w").close
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.clean
|
65
|
+
Cowtech::RubyOnRails::Scheduler.log "Cleaning log files..."
|
66
|
+
|
67
|
+
["backups/logs/*.log", "backups/logs/*.#{@@log_compressed_extension}"].each do |path|
|
68
|
+
Dir.glob(Rails.root + path) do |log_file|
|
69
|
+
Cowtech::RubyOnRails::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
|
70
|
+
File.delete(log_file)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
76
|
end
|
77
77
|
|
78
78
|
namespace :log do
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
79
|
+
desc "Rotates log files"
|
80
|
+
task :rotate, [:email_class, :force] => [:environment] do |task, args|
|
81
|
+
Cowtech::RubyOnRails::LogUtils.rotate(args)
|
82
|
+
end
|
83
|
+
|
84
|
+
desc "Clean every log file"
|
85
|
+
task :clean do |task|
|
86
|
+
Cowtech::RubyOnRails::LogUtils.clean
|
87
|
+
end
|
88
88
|
end
|