Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alvin2ye committed Oct 16, 2009
0 parents commit 17ba0bf
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2009 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 18 additions & 0 deletions README.rdoc
@@ -0,0 +1,18 @@
DeployKit
=========

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2009 [name of plugin creator], released under the MIT license

Reference
========

http://github.com/gravelpup/backup_fu
23 changes: 23 additions & 0 deletions Rakefile
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the deploy_kit plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the deploy_kit plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'DeployKit'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
require 'deploy_kit'
1 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
# Install hook code here
11 changes: 11 additions & 0 deletions lib/deploy_kit.rb
@@ -0,0 +1,11 @@
require 'rubygems'
require 'uuid'
require 'logger'
require 'yaml'
require 'active_support'
require 'mime/types'
require 'erb'
require 'aws/s3'
require 'deploy_kit/deploy_kit'
require 'deploy_kit/backup_mysql'
require 'deploy_kit/backup_log'
14 changes: 14 additions & 0 deletions lib/deploy_kit/backup_log.rb
@@ -0,0 +1,14 @@
class BackupLog < DeployKit
def final_filename
File.join(backup_path, "backup_log_#{timestamp}.tar.gz")
end

def cmd
"tar -zcf %s log/*.log" % [final_filename]
end

def backup
puts cmd if @verbose
`#{cmd}`
end
end
18 changes: 18 additions & 0 deletions lib/deploy_kit/backup_mysql.rb
@@ -0,0 +1,18 @@
class BackupMysql < DeployKit
def final_filename
File.join(backup_path, "backup_mysql_#{timestamp}.gz")
end

def cmd
"""mysqldump -u#{@db_conf[:username]} -p#{@db_conf[:password]} \
--default-character-set=utf8 --opt --extended-insert=false \
--triggers -R --hex-blob --single-transaction #{@db_conf[:database]} | gzip \
> #{final_filename}
"""
end

def backup
puts cmd if @verbose
`#{cmd}`
end
end
70 changes: 70 additions & 0 deletions lib/deploy_kit/deploy_kit.rb
@@ -0,0 +1,70 @@
class DeployKitError < Exception; end
class DeployKitConfigError < Exception; end

class DeployKit
def initialize
db_conf = YAML.load_file(File.join(RAILS_ROOT, 'config', 'database.yml'))
@db_conf = db_conf[RAILS_ENV].symbolize_keys

raw_config = File.read(File.join(RAILS_ROOT, 'config', 'deploy_kit.yml'))
erb_config = ERB.new(raw_config).result
fu_conf = YAML.load(erb_config)
@fu_conf = fu_conf[RAILS_ENV].symbolize_keys

@s3_conf = YAML.load_file(File.join(RAILS_ROOT, 'config', 'amazon_s3.yml'))[RAILS_ENV].symbolize_keys
@fu_conf[:s3_bucket] ||= @s3_conf[:bucket_name]
@fu_conf[:aws_access_key_id] ||= @s3_conf[:access_key_id]
@fu_conf[:aws_secret_access_key] ||= @s3_conf[:secret_access_key]

@fu_conf[:mysqldump_options] ||= '--complete-insert --skip-extended-insert'
@verbose = !@fu_conf[:verbose].nil?
@fu_conf[:keep_files] ||= 5
check_conf
create_dirs
end

def check_conf
@fu_conf[:s3_bucket] = ENV['s3_bucket'] unless ENV['s3_bucket'].blank?
if @fu_conf[:app_name] == 'replace_me'
raise DeployKitConfigError, 'Application name (app_name) key not set in config/deploy_kit.yml.'
elsif @fu_conf[:s3_bucket] == 'some-s3-bucket'
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).'
else
# Check for access keys set as environment variables:
if ENV.keys.include?('AMAZON_ACCESS_KEY_ID') && ENV.keys.include?('AMAZON_SECRET_ACCESS_KEY')
@fu_conf[:aws_access_key_id] = ENV['AMAZON_ACCESS_KEY_ID']
@fu_conf[:aws_secret_access_key] = ENV['AMAZON_SECRET_ACCESS_KEY']
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')
raise DeployKitConfigError, 'AWS Access Key Id or AWS Secret Key not set in config/deploy_kit.yml.'
end
end
end

def timestamp
Time.current.strftime("%Y-%m-%d_%H%M%S")
end

def create_dirs
ensure_directory_exists(backup_path)
end

def ensure_directory_exists(dir)
FileUtils.mkdir_p(dir) unless File.exist?(dir)
end

def backup_path
@fu_conf[:dump_base_path] || File.join(RAILS_ROOT, 'backup')
end

def backup
raise 'Called abstract method: backup'
end

def cmd
raise 'Called abstract method: cmd'
end

def final_filename
raise 'Called abstract method: final_filename'
end
end
17 changes: 17 additions & 0 deletions tasks/deploy_kit_tasks.rake
@@ -0,0 +1,17 @@
require 'fileutils'
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'deploy_kit'

namespace :deploy do
desc "backup_mysql"
task :backup_mysql do
puts "backup mysql"
BackupMysql.new.backup
end

desc "backup_log"
task :backup_log do
puts "backup log"
BackupLog.new.backup
end
end
8 changes: 8 additions & 0 deletions test/deploy_kit_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class DeployKitTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
3 changes: 3 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,3 @@
require 'rubygems'
require 'active_support'
require 'active_support/test_case'
1 change: 1 addition & 0 deletions uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit 17ba0bf

Please sign in to comment.