deploy_kit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
File without changes
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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/Manifest ADDED
@@ -0,0 +1,19 @@
1
+ CHANGELOG
2
+ MIT-LICENSE
3
+ Manifest
4
+ README.rdoc
5
+ Rakefile
6
+ config/amazon_s3.yml.sample
7
+ config/deploy_kit.yml.sample
8
+ deploy_kit.gemspec
9
+ init.rb
10
+ lib/deploy_kit.rb
11
+ lib/deploy_kit/backup_log.rb
12
+ lib/deploy_kit/backup_mysql.rb
13
+ lib/deploy_kit/clear_file.rb
14
+ lib/deploy_kit/deploy_kit.rb
15
+ lib/deploy_kit/log_monitor.rb
16
+ lib/deploy_kit/s3_storage.rb
17
+ tasks/deploy_kit_tasks.rake
18
+ test/deploy_kit_test.rb
19
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ ==DeployKit
2
+
3
+ Introduction
4
+
5
+ sudo gem install deploy_kit --source http://gemcutter.org
6
+
7
+ or
8
+
9
+ sudo gem install gemcutter
10
+ sudo gem tumble
11
+ sudo gem install deploy_kit
12
+
13
+
14
+ Add gem require to config/environment.rb
15
+
16
+ config.gem "deploy_kit", :source => 'http://gemcutter.org'
17
+
18
+
19
+ Add below codes to your rails's Rakefile
20
+
21
+ Dir["#{Gem.searcher.find('deploy_kit').full_gem_path}/tasks/*.rake"].each { |ext| load ext }
22
+
23
+
24
+ ==Example
25
+
26
+ rake -T deploy # View deploy tasks
27
+
28
+ rake deploy:backup_log
29
+ rake deploy:backup_mysql
30
+ rake deploy:clear
31
+ rake deploy:setup
32
+
33
+
34
+ ==Reference
35
+
36
+ http://github.com/gravelpup/backup_fu
37
+
38
+ ==Requirement
39
+
40
+ http://github.com/marcel/aws-s3
41
+
42
+ Copyright (c) 2009 [alvin@agideo], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('deploy_kit', '0.1.0') do |p|
6
+ p.description = "Generate a deploy kit"
7
+ p.url = "http://github.com/jimjin/deploy_kit"
8
+ p.author = "Jim Jin"
9
+ p.email = "jim.jin2006@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,4 @@
1
+ production:
2
+ bucket_name: <app name>
3
+ access_key_id: xxx
4
+ secret_access_key: xxx
@@ -0,0 +1,6 @@
1
+ production:
2
+ app_name: <app name>
3
+ allow_time_ms: 1000
4
+ from_mail: xxx@gmail.com
5
+ to_mail: xxx@googlegroups.com
6
+ default_clear_days: 7
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{deploy_kit}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jim Jin"]
9
+ s.date = %q{2009-11-18}
10
+ s.description = %q{Generate a deploy kit}
11
+ s.email = %q{jim.jin2006@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/deploy_kit.rb", "lib/deploy_kit/backup_log.rb", "lib/deploy_kit/backup_mysql.rb", "lib/deploy_kit/clear_file.rb", "lib/deploy_kit/deploy_kit.rb", "lib/deploy_kit/log_monitor.rb", "lib/deploy_kit/s3_storage.rb", "tasks/deploy_kit_tasks.rake"]
13
+ s.files = ["CHANGELOG", "MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "config/amazon_s3.yml.sample", "config/deploy_kit.yml.sample", "deploy_kit.gemspec", "init.rb", "lib/deploy_kit.rb", "lib/deploy_kit/backup_log.rb", "lib/deploy_kit/backup_mysql.rb", "lib/deploy_kit/clear_file.rb", "lib/deploy_kit/deploy_kit.rb", "lib/deploy_kit/log_monitor.rb", "lib/deploy_kit/s3_storage.rb", "tasks/deploy_kit_tasks.rake", "test/deploy_kit_test.rb", "test/test_helper.rb"]
14
+ s.homepage = %q{http://github.com/jimjin/deploy_kit}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Deploy_kit", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{deploy_kit}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Generate a deploy kit}
20
+ s.test_files = ["test/test_helper.rb", "test/deploy_kit_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'deploy_kit'
data/lib/deploy_kit.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'logger'
3
+ require 'yaml'
4
+ require 'active_support'
5
+ require 'mime/types'
6
+ require 'erb'
7
+ require 'aws/s3'
8
+ require 'deploy_kit/deploy_kit'
9
+ require 'deploy_kit/backup_mysql'
10
+ require 'deploy_kit/backup_log'
11
+ require 'deploy_kit/s3_storage'
12
+ require 'deploy_kit/log_monitor'
13
+ require 'deploy_kit/clear_file'
@@ -0,0 +1,15 @@
1
+ class BackupLog < DeployKit
2
+ def final_filename
3
+ File.join(backup_path, "#{@fu_conf[:app_name]}_backup_log_#{timestamp}.tar.gz")
4
+ end
5
+
6
+ def cmd
7
+ "tar -zcf %s log/*.log" % [final_filename]
8
+ end
9
+
10
+ def backup(store)
11
+ puts cmd if @verbose
12
+ `#{cmd}`
13
+ S3storage.new.put(final_filename) if store == "s3"
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ class BackupMysql < DeployKit
2
+ def final_filename
3
+ File.join(backup_path, "#{@fu_conf[:app_name]}_backup_mysql_#{timestamp}.gz")
4
+ end
5
+
6
+ def cmd
7
+ """mysqldump -u\"#{@db_conf[:username]}\" -p\"#{@db_conf[:password]}\" \
8
+ --default-character-set=utf8 --opt --extended-insert=false \
9
+ --triggers -R --hex-blob --single-transaction #{options} #{@db_conf[:database]} | gzip \
10
+ > #{final_filename}
11
+ """
12
+ end
13
+
14
+ def backup(store)
15
+ puts cmd if @verbose
16
+ `#{cmd}`
17
+ S3storage.new.put(final_filename) if store == "s3"
18
+ end
19
+
20
+ def options
21
+ options = []
22
+
23
+ if !@db_conf[:socket].blank?
24
+ options << "--socket=#{@db_conf[:socket]}"
25
+ end
26
+
27
+ options.join(" ")
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ class ClearFile < DeployKit
2
+ def final_filename
3
+ File.join(backup_path, "*.gz")
4
+ end
5
+
6
+ def get_clear_days
7
+ @fu_conf[:default_clear_days].to_i
8
+ end
9
+
10
+ def cmd
11
+ "find %s -type f -mtime +%s -exec rm {} \\;"
12
+ end
13
+
14
+ def clear(path, days)
15
+ full_cmd = cmd % [path || final_filename, days || get_clear_days]
16
+ puts full_cmd if @verbose
17
+ `#{full_cmd}`
18
+ end
19
+ end
@@ -0,0 +1,69 @@
1
+ class DeployKitConfigError < Exception; end
2
+
3
+ class DeployKit
4
+ def initialize
5
+ db_conf = YAML.load_file(File.join(RAILS_ROOT, 'config', 'database.yml'))
6
+ @db_conf = db_conf[RAILS_ENV].symbolize_keys
7
+
8
+ raw_config = File.read(File.join(RAILS_ROOT, 'config', 'deploy_kit.yml'))
9
+ erb_config = ERB.new(raw_config).result
10
+ fu_conf = YAML.load(erb_config)
11
+ @fu_conf = fu_conf[RAILS_ENV].symbolize_keys
12
+
13
+ @s3_conf = YAML.load_file(File.join(RAILS_ROOT, 'config', 'amazon_s3.yml'))[RAILS_ENV].symbolize_keys
14
+ @fu_conf[:s3_bucket] ||= @s3_conf[:bucket_name]
15
+ @fu_conf[:aws_access_key_id] ||= @s3_conf[:access_key_id]
16
+ @fu_conf[:aws_secret_access_key] ||= @s3_conf[:secret_access_key]
17
+
18
+ @fu_conf[:mysqldump_options] ||= '--complete-insert --skip-extended-insert'
19
+ @verbose = !@fu_conf[:verbose].nil?
20
+ @fu_conf[:keep_files] ||= 5
21
+ check_conf
22
+ create_dirs
23
+ end
24
+
25
+ def check_conf
26
+ @fu_conf[:s3_bucket] = ENV['s3_bucket'] unless ENV['s3_bucket'].blank?
27
+ if @fu_conf[:app_name] == 'replace_me'
28
+ raise DeployKitConfigError, 'Application name (app_name) key not set in config/deploy_kit.yml.'
29
+ elsif @fu_conf[:s3_bucket] == 'some-s3-bucket'
30
+ raise DeployKitConfigError, 'S3 bucket (s3_bucket) not set in config/deploy_kit.yml. This bucket must be created using an external S3 tool like S3 Browser for OS X, or JetS3t (Java-based, cross-platform).'
31
+ else
32
+ # Check for access keys set as environment variables:
33
+ if ENV.keys.include?('AMAZON_ACCESS_KEY_ID') && ENV.keys.include?('AMAZON_SECRET_ACCESS_KEY')
34
+ @fu_conf[:aws_access_key_id] = ENV['AMAZON_ACCESS_KEY_ID']
35
+ @fu_conf[:aws_secret_access_key] = ENV['AMAZON_SECRET_ACCESS_KEY']
36
+ elsif @fu_conf[:aws_access_key_id].blank? || @fu_conf[:aws_access_key_id].include?('--replace me') || @fu_conf[:aws_secret_access_key].include?('--replace me')
37
+ raise DeployKitConfigError, 'AWS Access Key Id or AWS Secret Key not set in config/deploy_kit.yml.'
38
+ end
39
+ end
40
+ end
41
+
42
+ def timestamp
43
+ @timestamp ||= Time.now.strftime("%Y-%m-%d_%H%M%S")
44
+ end
45
+
46
+ def create_dirs
47
+ ensure_directory_exists(backup_path)
48
+ end
49
+
50
+ def ensure_directory_exists(dir)
51
+ FileUtils.mkdir_p(dir) unless File.exist?(dir)
52
+ end
53
+
54
+ def backup_path
55
+ @fu_conf[:dump_base_path] || File.join(RAILS_ROOT, 'backup')
56
+ end
57
+
58
+ def backup(store)
59
+ raise 'Called abstract method: backup'
60
+ end
61
+
62
+ def cmd
63
+ raise 'Called abstract method: cmd'
64
+ end
65
+
66
+ def final_filename
67
+ raise 'Called abstract method: final_filename'
68
+ end
69
+ end
@@ -0,0 +1,51 @@
1
+ class LogMonitor < DeployKit
2
+ def check_warning(will_sent = nil)
3
+ cmd = "cat #{log_path} | grep \"200 OK\" | awk '{print $3 \" \" $0}' | sort -nr | head -n 10"
4
+ puts cmd if @verbose
5
+ lines = `#{cmd}`
6
+ return if !need_send?(lines)
7
+
8
+ if !will_sent.blank?
9
+ send_mail(lines)
10
+ else
11
+ puts lines
12
+ end
13
+ end
14
+
15
+ def allow_time
16
+ @fu_conf[:allow_time_ms].to_i
17
+ end
18
+
19
+ def log_path
20
+ File.join(RAILS_ROOT, "log", "production.log")
21
+ end
22
+
23
+ def need_send?(lines)
24
+ result = nil
25
+ time = lines.to_s.split(" ").first
26
+
27
+ return false if time.blank?
28
+ if is_ms?(time)
29
+ if time.to_f > allow_time
30
+ result = true
31
+ end
32
+ else
33
+ if time.to_f > (allow_time / 1000)
34
+ result = true
35
+ end
36
+ end
37
+
38
+ result
39
+ end
40
+
41
+ def send_mail(lines)
42
+ cmd = "echo \"#{lines}\" | mail #{@fu_conf[:to_mail]} -s \"#{@timestamp} #{@fu_conf[:app_name]} log \" -a \"From: #{@fu_conf[:from_mail]}\""
43
+ puts cmd if @verbose
44
+ `#{cmd}`
45
+ end
46
+
47
+ private
48
+ def is_ms?(time)
49
+ time.index("ms")
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ class S3storage < DeployKit
2
+ def put(file)
3
+ s3_connection
4
+ file_name = File.basename(file)
5
+ AWS::S3::S3Object.store(
6
+ file_name, open(file), @fu_conf[:s3_bucket]
7
+ )
8
+ end
9
+
10
+ def s3_connection
11
+ @s3 ||= AWS::S3::Base.establish_connection!(
12
+ :access_key_id => @fu_conf[:aws_access_key_id],
13
+ :secret_access_key => @fu_conf[:aws_secret_access_key]
14
+ )
15
+ end
16
+
17
+ def list
18
+ s3_connection
19
+ AWS::S3::Bucket.find(@fu_conf[:s3_bucket]).objects.each do |file|
20
+ puts "#{file.key.to_s.ljust(50)} #{file.size}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ require 'fileutils'
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+ require 'deploy_kit'
4
+
5
+ namespace :deploy do
6
+ desc "rake deploy:setup copy amazon_s3.yml.sample, deploy_kit.yml.sample"
7
+ task :setup do
8
+ ['amazon_s3.yml.sample', 'deploy_kit.yml.sample'].each do |file|
9
+ srcfile = File.join(File.dirname(__FILE__), '..', 'config', file)
10
+ destfile = File.join(RAILS_ROOT, 'config', file)
11
+
12
+ if File.exist?(destfile)
13
+ puts "\nTarget file: #{destfile}\n ... already exists. Aborting.\n\n"
14
+ else
15
+ FileUtils.cp(srcfile, destfile)
16
+ puts "cp config/#{file} config/#{file.gsub('.sample', '')}"
17
+ end
18
+ end
19
+ end
20
+
21
+ desc "rake deploy:backup_mysql or rake deploy:backup_mysql STORE=s3"
22
+ task :backup_mysql do
23
+ BackupMysql.new.backup(ENV["STORE"])
24
+ end
25
+
26
+ desc "rake deploy:backup_log or rake deploy:backup_log STORE=s3"
27
+ task :backup_log do
28
+ BackupLog.new.backup(ENV["STORE"])
29
+ end
30
+
31
+ desc "Clear files in date, rake deploy:clear or rake deploy:clear BACKUP_PATH=/tmp/*.log DAYS=10 defaut BACKUP_PATH: RAILS_ROOT/config/deploy_kit.yml key dump_base_path or 'RAILS_ROOT/backup/*.gz' DAYS: config key default_clear_days"
32
+ task :clear do
33
+ ClearFile.new.clear(ENV["BACKUP_PATH"], ENV["DAYS"])
34
+ end
35
+ end
36
+
37
+ namespace :s3 do
38
+ desc "list"
39
+ task :list do
40
+ S3storage.new.list
41
+ end
42
+
43
+ desc "store file use ENV like: rake s3:put FILE=/tmp/xx.tar.gz"
44
+ task :put do
45
+ S3storage.new.put(ENV["FILE"])
46
+ end
47
+
48
+ desc "download file like: rake s3:get KEY=xx.tar.gz"
49
+ task :get do
50
+ puts "..." # TODO impl
51
+ end
52
+ end
53
+
54
+ namespace :monitor do
55
+ desc "or rake monitor:slow_log SEND_MAIL=true"
56
+ task :slow_log do
57
+ LogMonitor.new.check_warning(ENV["SEND_MAIL"])
58
+ end
59
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class DeployKitTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deploy_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jim Jin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-18 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generate a deploy kit
17
+ email: jim.jin2006@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - README.rdoc
25
+ - lib/deploy_kit.rb
26
+ - lib/deploy_kit/backup_log.rb
27
+ - lib/deploy_kit/backup_mysql.rb
28
+ - lib/deploy_kit/clear_file.rb
29
+ - lib/deploy_kit/deploy_kit.rb
30
+ - lib/deploy_kit/log_monitor.rb
31
+ - lib/deploy_kit/s3_storage.rb
32
+ - tasks/deploy_kit_tasks.rake
33
+ files:
34
+ - CHANGELOG
35
+ - MIT-LICENSE
36
+ - Manifest
37
+ - README.rdoc
38
+ - Rakefile
39
+ - config/amazon_s3.yml.sample
40
+ - config/deploy_kit.yml.sample
41
+ - deploy_kit.gemspec
42
+ - init.rb
43
+ - lib/deploy_kit.rb
44
+ - lib/deploy_kit/backup_log.rb
45
+ - lib/deploy_kit/backup_mysql.rb
46
+ - lib/deploy_kit/clear_file.rb
47
+ - lib/deploy_kit/deploy_kit.rb
48
+ - lib/deploy_kit/log_monitor.rb
49
+ - lib/deploy_kit/s3_storage.rb
50
+ - tasks/deploy_kit_tasks.rake
51
+ - test/deploy_kit_test.rb
52
+ - test/test_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/jimjin/deploy_kit
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --line-numbers
60
+ - --inline-source
61
+ - --title
62
+ - Deploy_kit
63
+ - --main
64
+ - README.rdoc
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "1.2"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: deploy_kit
82
+ rubygems_version: 1.3.5
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Generate a deploy kit
86
+ test_files:
87
+ - test/test_helper.rb
88
+ - test/deploy_kit_test.rb