cowtech-rails 2.8.1.0 → 2.8.2
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/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
@@ -4,76 +4,76 @@
|
|
4
4
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
5
|
#
|
6
6
|
module Cowtech
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
module RubyOnRails
|
8
|
+
class MongoUtils
|
9
|
+
@@log_compressor_command = "tar cjf"
|
10
|
+
@@log_compressed_extension = "tbz2"
|
11
11
|
|
12
|
-
|
12
|
+
def self.run_command(cmd); system(cmd) end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
# Get configuration
|
18
|
-
mongo_config = YAML.load_file(Rails.root + "config/mongoid.yml")
|
19
|
-
databases = mongo_config["backup_databases"].ensure_string.split(/\s*,\s*/)
|
20
|
-
return if databases.blank?
|
14
|
+
def self.backup(rake_args)
|
15
|
+
Cowtech::RubyOnRails::Scheduler.log "--- Backupping MongoDB ..."
|
21
16
|
|
22
|
-
|
23
|
-
|
17
|
+
# Get configuration
|
18
|
+
mongo_config = YAML.load_file(Rails.root + "config/mongoid.yml")
|
19
|
+
databases = mongo_config["backup_databases"].ensure_string.split(/\s*,\s*/)
|
20
|
+
return if databases.blank?
|
21
|
+
|
22
|
+
# Set output dir
|
23
|
+
dest_file = Rails.root + "backups/mongodb/mongo-#{Time.now.strftime("%Y%m%d-%H%M%S")}"
|
24
24
|
final_file = dest_file.to_s + "." + @@log_compressed_extension
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
# Create directory
|
27
|
+
dir = File.dirname(dest_file)
|
28
|
+
FileUtils.mkdir_p(dir) if !File.directory?(dir)
|
29
|
+
|
30
|
+
databases.each do |db|
|
31
|
+
Cowtech::RubyOnRails::Scheduler.log "\t\tBackupping DB #{db} ..."
|
32
32
|
|
33
|
-
|
34
|
-
|
33
|
+
dump_cmd = "mongodump"
|
34
|
+
dump_args = {"" => "-o \"#{dest_file}\"", "database" => "-d #{db}"}
|
35
|
+
|
36
|
+
# Execute command
|
37
|
+
Cowtech::RubyOnRails::Scheduler.log "\t\tDumping data ..."
|
38
|
+
Cowtech::RubyOnRails::MongoUtils.run_command(dump_cmd + " " + dump_args.values.join(" "))
|
39
|
+
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
Cowtech::RubyOnRails::Scheduler.log "\t\tCompressing backup ..."
|
43
|
-
Cowtech::RubyOnRails::MongoUtils.run_command(@@log_compressor_command + " " + final_file + " " + dest_file.to_s)
|
44
|
-
FileUtils.rm_rf(dest_file.to_s)
|
45
|
-
|
46
|
-
# Send file via mail
|
41
|
+
# Compress
|
42
|
+
Cowtech::RubyOnRails::Scheduler.log "\t\tCompressing backup ..."
|
43
|
+
Cowtech::RubyOnRails::MongoUtils.run_command(@@log_compressor_command + " " + final_file + " " + dest_file.to_s)
|
44
|
+
FileUtils.rm_rf(dest_file.to_s)
|
45
|
+
|
46
|
+
# Send file via mail
|
47
47
|
if (Rails.env == "production" || rake_args["force"].to_boolean) && rake_args["email_class"].present? then
|
48
48
|
Cowtech::RubyOnRails::Scheduler.log "\tForwarding backup file to requested email address..."
|
49
|
-
|
49
|
+
rake_args["email_class"].constantize.backup(final_file).deliver
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
|
52
|
+
Cowtech::RubyOnRails::Scheduler.log "Backup saved in #{dest_file}.#{@@log_compressed_extension}"
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
55
|
+
def self.backup_clean
|
56
|
+
Cowtech::RubyOnRails::Scheduler.log "--- Cleaning database backup files ..."
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
58
|
+
["backups/mysql/*.sql", "backups/mysql/backup/*.#{@@log_compressed_extension}"].each do |path|
|
59
|
+
Dir.glob(Rails.root + path) do |log_file|
|
60
|
+
Cowtech::RubyOnRails::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
|
61
|
+
File.delete(log_file)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
67
|
end
|
68
68
|
|
69
69
|
namespace :mongodb do
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
70
|
+
desc "Backups MongoDB collections"
|
71
|
+
task :backup, [:email_class, :force] => [:environment] do |task, args|
|
72
|
+
Cowtech::RubyOnRails::MongoUtils.backup(args)
|
73
|
+
end
|
74
|
+
|
75
|
+
desc "Clean every backup file"
|
76
|
+
task :backup_clean do |task|
|
77
|
+
Cowtech::RubyOnRails::MongoUtils.backup_clean
|
78
|
+
end
|
79
79
|
end
|
@@ -5,103 +5,103 @@
|
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
8
|
+
module RubyOnRails
|
9
|
+
class ServerUtils
|
10
|
+
@@yml_path = "config/server.yml"
|
11
|
+
@@config = nil
|
12
|
+
|
13
|
+
def self.thin_path; "config/thin.yml"; end
|
14
|
+
def self.default_environment; "development"; end
|
15
|
+
|
16
|
+
def self.load_config(force = false)
|
17
|
+
begin
|
18
|
+
@@config = YAML.load_file(Rails.root + @@yml_path) if @@config == nil || force == true
|
19
|
+
rescue
|
20
|
+
@@config = []
|
21
|
+
end
|
22
|
+
@@config
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.save_config(args = nil)
|
26
|
+
args = Cowtech::RubyOnRails::ServerUtils.load_config if !args
|
27
|
+
File.open(Rails.root + @@yml_path, "w") { |f| f.write(YAML::dump(args)) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.stringify_keys(hash)
|
31
|
+
rv = {}
|
32
|
+
hash.each { |k, v| rv[k.to_s] = v }
|
33
|
+
rv
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.merge(base, other)
|
37
|
+
rv = {}
|
38
|
+
base.each { |k, v| rv[k] = (other[k] ? other[k] : v) }
|
39
|
+
rv
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.run_command(cmd)
|
43
|
+
system(cmd)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Cowtech::RubyOnRails::ThinServer < Cowtech::RubyOnRails::ServerUtils
|
48
|
+
def self.prepare(rake_args)
|
49
|
+
self.load_config
|
50
|
+
Cowtech::RubyOnRails::ThinServer.merge(@@config["thin"], Cowtech::RubyOnRails::ThinServer.stringify_keys(rake_args))
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.start(rake_args)
|
54
|
+
args = self.prepare(rake_args)
|
55
|
+
@@config["thin"] = args
|
56
|
+
Cowtech::RubyOnRails::ThinServer.save_config
|
57
|
+
puts "--- Starting thin server with config file #{args["config"]} in environment #{args["environment"]}..."
|
58
|
+
Cowtech::RubyOnRails::ThinServer.execute("start", args["config"], args["environment"])
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.stop(rake_args)
|
62
|
+
args = self.prepare(rake_args)
|
63
|
+
puts "--- Stopping Thin server ..."
|
64
|
+
Cowtech::RubyOnRails::ThinServer.execute("stop", args["config"], args["environment"])
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.restart(rake_args)
|
68
|
+
args = self.prepare(rake_args)
|
69
|
+
puts "--- Restarting thin server with config file #{args["config"]} in environment #{args["environment"]}..."
|
70
|
+
Cowtech::RubyOnRails::ThinServer.execute("restart", args["config"], args["environment"])
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.execute(command, config, environment)
|
74
|
+
run_command("thin -C #{config} -e #{environment} #{command}")
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.test
|
78
|
+
puts "--- Testing thin server in the foreground"
|
79
|
+
run_command("thin start")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
83
|
end
|
84
84
|
|
85
85
|
namespace :server do
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
86
|
+
namespace :thin do
|
87
|
+
desc "Starts Thin server"
|
88
|
+
task :start, ["environment", "config"], do |task, rake_args|
|
89
|
+
Cowtech::RubyOnRails::ThinServer.start(rake_args.with_defaults("config" => Cowtech::RubyOnRails::ServerUtils.thin_path, "environment" => Cowtech::RubyOnRails::ServerUtils.default_environment))
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Stops Thin server"
|
93
|
+
task :stop, ["environment", "config"] do |task, rake_args|
|
94
|
+
Cowtech::RubyOnRails::ThinServer.stop(rake_args.with_defaults("config" => nil, "environment" => nil))
|
95
|
+
end
|
96
|
+
|
97
|
+
desc "Restarts Thin server"
|
98
|
+
task :restart, ["environment", "config"] do |task, rake_args|
|
99
|
+
Cowtech::RubyOnRails::ThinServer.restart(rake_args.with_defaults("config" => nil, "environment" => nil))
|
100
|
+
end
|
101
|
+
|
102
|
+
desc "Tests Thin server into the foreground"
|
103
|
+
task :test do |task|
|
104
|
+
Cowtech::RubyOnRails::ThinServer.test
|
105
|
+
end
|
106
|
+
end
|
107
107
|
end
|
data/lib/cowtech/tasks/sql.rake
CHANGED
@@ -5,116 +5,116 @@
|
|
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 MysqlUtils
|
10
|
+
@@log_compressor_command = "bzip2"
|
11
|
+
@@log_compressed_extension = "bz2"
|
12
|
+
|
13
|
+
def self.run_command(cmd); system(cmd) end
|
14
|
+
|
15
|
+
def self.mysql_execute(config, rake_args)
|
16
|
+
dest_file = Rails.root + "backups/mysql/mysql-#{Time.now.strftime("%Y%m%d-%H%M%S")}.sql"
|
17
|
+
final_file = dest_file.to_s + "." + @@log_compressed_extension
|
18
|
+
|
19
|
+
dump_cmd = "mysqldump"
|
20
|
+
dump_args = {"" => "-R -r \"#{dest_file}\"", "host" => "-h @@ARG@@", "username" => "-u @@ARG@@", "password" => "--password=\"@@ARG@@\"", "database" => "@@ARG@@"}
|
21
|
+
|
22
|
+
# Build command
|
23
|
+
args = dump_args.dup
|
24
|
+
args.keys.each do |k|
|
25
|
+
if k == "" || config[k] then
|
26
|
+
args[k].gsub!("@@ARG@@", config[k] || "")
|
27
|
+
else
|
28
|
+
args.delete(k)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Create directory
|
33
|
+
dir = File.dirname(dest_file)
|
34
|
+
FileUtils.mkdir_p(dir) if !File.directory?(dir)
|
35
|
+
|
36
|
+
# Execute command
|
37
|
+
Cowtech::RubyOnRails::Scheduler.log "\tDumping data ..."
|
38
|
+
Cowtech::RubyOnRails::MysqlUtils.run_command(dump_cmd + " " + dump_args.values.join(" "))
|
39
|
+
|
40
|
+
# Compress
|
41
|
+
Cowtech::RubyOnRails::Scheduler.log "\tCompressing backup ..."
|
42
|
+
Cowtech::RubyOnRails::MysqlUtils.run_command(@@log_compressor_command + " " + dest_file.to_s)
|
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 backup file to requested email address..."
|
47
|
-
|
47
|
+
rake_args["email_class"].constantize.backup(final_file).deliver
|
48
|
+
end
|
49
|
+
|
50
|
+
Cowtech::RubyOnRails::Scheduler.log "Backup saved in #{final_file}"
|
51
|
+
end
|
52
|
+
|
53
|
+
# ALIAS
|
54
|
+
class << self
|
55
|
+
alias_method :mysql2_execute, :mysql_execute
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class SqlUtils
|
60
|
+
@@log_compressor_command = "bzip2"
|
61
|
+
@@log_compressed_extension = "bz2"
|
62
|
+
|
63
|
+
def self.to_fixtures
|
64
|
+
Cowtech::RubyOnRails::Scheduler.log "--- Dumping database into fixtures ..."
|
65
|
+
sql = "SELECT * FROM %s"
|
66
|
+
skip_tables = ["schema_info"]
|
67
|
+
ActiveRecord::Base.establish_connection
|
68
|
+
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
|
69
|
+
Cowtech::RubyOnRails::Scheduler.log "--- --- Dumping table #{table_name} ..."
|
70
|
+
i = "01"
|
71
|
+
File.open("#{RAILS_ROOT}/test/fixture/#{table_name}.yml", 'w') do |file|
|
72
|
+
data = ActiveRecord::Base.connection.select_all(sql % table_name)
|
73
|
+
file.write data.inject({}) { |hash, record|
|
74
|
+
hash["#{table_name}_#{i.succ!}"] = record
|
75
|
+
hash
|
76
|
+
}.to_yaml
|
77
|
+
end
|
48
78
|
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.backup(rake_args)
|
82
|
+
Cowtech::RubyOnRails::Scheduler.log "--- Backupping database ..."
|
83
|
+
# OTTENIAMO LA CONFIGURAZIONE
|
84
|
+
db_config = YAML.load_file(Rails.root + "config/database.yml")
|
85
|
+
env = Rails.env
|
49
86
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
skip_tables = ["schema_info"]
|
67
|
-
ActiveRecord::Base.establish_connection
|
68
|
-
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
|
69
|
-
Cowtech::RubyOnRails::Scheduler.log "--- --- Dumping table #{table_name} ..."
|
70
|
-
i = "01"
|
71
|
-
File.open("#{RAILS_ROOT}/test/fixture/#{table_name}.yml", 'w') do |file|
|
72
|
-
data = ActiveRecord::Base.connection.select_all(sql % table_name)
|
73
|
-
file.write data.inject({}) { |hash, record|
|
74
|
-
hash["#{table_name}_#{i.succ!}"] = record
|
75
|
-
hash
|
76
|
-
}.to_yaml
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def self.backup(rake_args)
|
82
|
-
Cowtech::RubyOnRails::Scheduler.log "--- Backupping database ..."
|
83
|
-
# OTTENIAMO LA CONFIGURAZIONE
|
84
|
-
db_config = YAML.load_file(Rails.root + "config/database.yml")
|
85
|
-
env = Rails.env
|
86
|
-
|
87
|
-
# ESEGUIAMO
|
88
|
-
Cowtech::RubyOnRails::MysqlUtils.send("#{db_config[env]["adapter"]}_execute", db_config[env], rake_args)
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.backup_clean
|
92
|
-
Cowtech::RubyOnRails::Scheduler.log "--- Cleaning database backup files ..."
|
93
|
-
|
94
|
-
["backups/mysql/*.sql", "backups/mysql/backup/*.#{@@log_compressed_extension}"].each do |path|
|
95
|
-
Dir.glob(Rails.root + path) do |log_file|
|
96
|
-
Cowtech::RubyOnRails::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
|
97
|
-
File.delete(log_file)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
87
|
+
# ESEGUIAMO
|
88
|
+
Cowtech::RubyOnRails::MysqlUtils.send("#{db_config[env]["adapter"]}_execute", db_config[env], rake_args)
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.backup_clean
|
92
|
+
Cowtech::RubyOnRails::Scheduler.log "--- Cleaning database backup files ..."
|
93
|
+
|
94
|
+
["backups/mysql/*.sql", "backups/mysql/backup/*.#{@@log_compressed_extension}"].each do |path|
|
95
|
+
Dir.glob(Rails.root + path) do |log_file|
|
96
|
+
Cowtech::RubyOnRails::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
|
97
|
+
File.delete(log_file)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
103
|
end
|
104
104
|
|
105
105
|
namespace :mysql do
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
106
|
+
desc 'Converting data to fixtures'
|
107
|
+
task :to_fixtures => :environment do
|
108
|
+
Cowtech::RubyOnRails::SqlUtils.to_fixtures
|
109
|
+
end
|
110
|
+
|
111
|
+
desc "Backups database"
|
112
|
+
task :backup, [:email_class, :force] => [:environment] do |task, args|
|
113
|
+
Cowtech::RubyOnRails::SqlUtils.backup(args)
|
114
|
+
end
|
115
|
+
|
116
|
+
desc "Clean every backup file"
|
117
|
+
task :backup_clean do |task|
|
118
|
+
Cowtech::RubyOnRails::SqlUtils.backup_clean
|
119
|
+
end
|
120
120
|
end
|