Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate danman01's work into donnoman:master #3

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
47fb37e
adding hook after setup to install
danman01 Jun 25, 2013
088ebb8
adding after setup hook
danman01 Jun 25, 2013
4d899f9
changing out utilities usage to use sudo
danman01 Jun 25, 2013
99b4814
expanding --group
danman01 Jun 25, 2013
79a655f
trying utilities sudo
danman01 Jun 25, 2013
2dc7d87
adding sudo to addgroup
danman01 Jun 25, 2013
2aea39f
taking out custom adduser and addgroup code, as i edited utilities mo…
danman01 Jun 25, 2013
ed4d252
Create postgresql.rb
danman01 Jun 25, 2013
7014a04
changing hooks to work with deploy:setup
danman01 Jun 25, 2013
68ec3aa
adding set default method
danman01 Jun 25, 2013
5301fbf
Update postgresql.rb
danman01 Jun 25, 2013
67ba75a
Update postgresql.rb
danman01 Jun 25, 2013
f9df481
adding set default method
danman01 Jun 25, 2013
d032775
Update postgresql.rb
danman01 Jun 25, 2013
cb4ef6d
wrapped everything up
danman01 Jun 25, 2013
a3a22a4
Update postgresql.rb
danman01 Jun 25, 2013
91ef792
removed set default and just using set
danman01 Jun 25, 2013
eee7f52
setting values to blocks
danman01 Jun 25, 2013
9517319
Update postgresql.rb
danman01 Jun 25, 2013
174bb8d
Update postgresql.rb
danman01 Jun 25, 2013
13ccfa9
Update postgresql.rb
danman01 Jun 25, 2013
cc6ea8e
Update postgresql.rb
danman01 Jun 25, 2013
ec486cc
Update postgresql.rb
danman01 Jun 25, 2013
7c27909
Update postgresql.rb
danman01 Jun 25, 2013
cbd1164
commenting postgresql setup to not generate database yml
danman01 Jun 25, 2013
0f2aa5b
adding full list of available install recipes to readme
danman01 Jun 25, 2013
e0cf30b
Update postgresql.rb
danman01 Jun 25, 2013
9d1f5ec
adding after setup hook
danman01 Jun 26, 2013
f754f64
adding some rollback tasks
danman01 Jun 26, 2013
910b75b
commenting out postgresql ssetup for now...
danman01 Jun 26, 2013
46a8445
adding deploy:provision back into the mix
danman01 Jun 26, 2013
7d37e51
relying on deploy:provision once more
danman01 Jun 26, 2013
0498f4f
removing deploy:setup install hook
danman01 Jun 26, 2013
4d2b068
taking out hooks
danman01 Jun 26, 2013
e457cb6
Update hooks.rb
danman01 Jun 26, 2013
4127a7a
took out deploy setup hook
danman01 Jun 26, 2013
bc7dbcd
adding provision task
danman01 Jun 26, 2013
1084117
Update manage.rb
danman01 Jun 26, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,63 @@ for common issues which can then be used by the community.
Feel free to improve upon this recipe collection by adding your own!

Currently included recipes in this gem:

* Ruby Setup
* Rubygems Management
* Apache Server
* Phusion Passenger
* Apparmor
* Aptitude
* Autossh
* Backgroundrb
* Bprobe
* Bundler Setup
* Cassandra
* Chef Client
* Chef Server
* Csgo_ds
* DelayedJob
* Datadog
* Denyhosts
* Dovecot
* Dphys_swapfile
* Erlang
* Gitosis Git Repository Hosting
* God
* Graphite
* Haproxy
* Hids
* Juggernaut Daemon
* Leap_second
* Logrotat
* Memcached Management
* MongoDB Management
* Munin
* Mysql
* Mysql_master
* newrelic_rpm
* newrelic_sysmond
* nginx
* nginx_passenger
* nginx_unicorn
* Postgresql
* Phusion Passenger
* Provision
* Rails
* Redis
* Ree
* Resque
* Ruby Setup
* Rubygems Management
* s3cmd
* s3fs
* s3sync
* sdagent
* ssh
* ssmtp
* statsd
* teelogger
* ThinkingSphinx Daemon
* Juggernaut Daemon
* Backgroundrb Server
* DelayedJob Worker
* Ufw
* Unicorn
* Whenever Cron Scheduling
* MongoDB Management
* Aptitude Package Management
* Gitosis Git Repository Hosting
* Xtrabackup

Check out the USAGE section below for specific tasks included in the recipes.

Expand All @@ -34,6 +77,8 @@ To install the gem, execute:

@$ sudo gem install cap-recipes --source http://gemcutter.org@

gem install cap-recipes gives a smaller collection of recipes, so make sure you fork from the gem you want

Then, include into your @deploy.rb@ configuration file for Capistrano:

<pre><code>
Expand Down Expand Up @@ -515,4 +560,4 @@ 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.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion lib/cap_recipes/tasks/monit/hooks.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Capistrano::Configuration.instance(true).load do
after "deploy:provision", "monit:install"
after "deploy:restart", "monit:enable", "monit:restart"
end
end
149 changes: 149 additions & 0 deletions lib/cap_recipes/tasks/postgresql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# @author Donovan Bray <[email protected]>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These top comments obviously came from the nginx recipe, and should be removed from the postgresql recipe.

I suggest you update the author tag with your email. But you can omit it if you want. I just think it's good to know who was the brainchild behind each recipe.

require File.expand_path(File.dirname(__FILE__) + '/utilities')

# This Nginx is targeted for the :app role meant to be acting as a front end
# to a unicorn based application

# Additions
# https://github.com/newobj/nginx-x-rid-header
# https://github.com/yaoweibin/nginx_syslog_patch

# Possible Future Additions
# https://support.newrelic.com/kb/features/tracking-front-end-time


Capistrano::Configuration.instance(true).load do


set :postgresql_host, "localhost"
set :postgresql_user, application
set(:postgresql_password) {Capistrano::CLI.password_prompt "PostgreSQL Password: "}
set(:postgresql_database) {"#{application}_production"}
set(:postgresql_dump_path) {"#{current_path}/tmp"}
set(:postgresql_dump_file) {"#{application}_dump.sql"}
set(:postgresql_local_dump_path) {File.expand_path("../../../tmp", __FILE__) }
set(:postgresql_pid) {"/var/run/postgresql/9.2-main.pid"}

namespace :postgresql do
desc "Install the latest stable release of PostgreSQL."
task :install, roles: :db, only: {primary: true} do
run "#{sudo} add-apt-repository -y ppa:pitti/postgresql"
run "#{sudo} apt-get -y update"
run "#{sudo} apt-get -y install postgresql libpq-dev"
end
after "postgresql:install", "postgresql:create_database"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callbacks belong in the hooks, so others requiring the recipe can choose to use your hooks or not.

This becomes important in some orchestration because once a hook is defined it can't be removed.


desc "Create a database for this application."
task :create_database, roles: :db, only: {primary: true} do
run %Q{#{sudo} -u postgres psql -c "create user #{postgresql_user} with password '#{postgresql_password}';"}
run %Q{#{sudo} -u postgres psql -c "create database #{postgresql_database} owner #{postgresql_user};"}
end
after "deploy:provision", "postgresql:install"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same move all callback definitions to hooks.rb


desc "Generate the database.yml configuration file."
task :setup, roles: :app do
run "mkdir -p #{shared_path}/config"
template "postgresql.yml.erb", "#{shared_path}/config/database.yml"
end
after "postgresql:create_database", "postgresql:setup"

desc "Symlink the database.yml file into latest release"
task :symlink, roles: :app do
puts "Symlinking database yml file"
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "postgresql:symlink"

desc "database console"
task :console do
auth = capture "cat #{shared_path}/config/database.yml"
puts "PASSWORD::: #{auth.match(/password: (.*$)/).captures.first}"
hostname = find_servers_for_task(current_task).first
exec "ssh #{hostname} -t 'source ~/.zshrc && psql -U #{application} #{postgresql_database}'"
end


namespace :local do
desc "Download remote database to tmp/"
task :download do
dumpfile = "#{postgresql_local_dump_path}/#{postgresql_dump_file}.gz"
get "#{postgresql_dump_path}/#{postgresql_dump_file}.gz", dumpfile
end

desc "Restores local database from temp file"
task :restore do
auth = YAML.load_file(File.expand_path('../../database.yml', __FILE__))
dev = auth['development']
user, pass, database, host = dev['username'], dev['password'], dev['database'], dev['host']
dumpfile = "#{postgresql_local_dump_path}/#{postgresql_dump_file}"
system "gzip -cd #{dumpfile}.gz > #{dumpfile} && cat #{dumpfile} | psql -U #{user} -h #{host} #{database}"
end

desc "Dump remote database and download it locally"
task :localize do
remote.dump
download
end

desc "Dump remote database, download it locally and restore local database"
task :sync do
localize
restore
end
end

namespace :remote do
desc "Dump remote database"
task :dump do
dbyml = capture "cat #{shared_path}/config/database.yml"
info = YAML.load dbyml
db = info[stage.to_s]
user, pass, database, host = db['username'], db['password'], db['database'], db['host']
commands = <<-CMD
pg_dump -U #{user} -h #{host} #{database} | \
gzip > #{postgresql_dump_path}/#{postgresql_dump_file}.gz
CMD
run commands do |ch, stream, data|
if data =~ /Password/
ch.send_data("#{pass}\n")
end
end
end

desc "Uploads local sql.gz file to remote server"
task :upload do
dumpfile = "#{postgresql_local_dump_path}/#{postgresql_dump_file}.gz"
upfile = "#{postgresql_dump_path}/#{postgresql_dump_file}.gz"
put File.read(dumpfile), upfile
end

desc "Restores remote database"
task :restore do
dumpfile = "#{postgresql_dump_path}/#{postgresql_dump_file}"
gzfile = "#{dumpfile}.gz"
dbyml = capture "cat #{shared_path}/config/database.yml"
info = YAML.load dbyml
db = info['production']
user, pass, database, host = db['username'], db['password'], db['database'], db['host']

commands = <<-CMD
gzip -cd #{gzfile} > #{dumpfile} && \
cat #{dumpfile} | \
psql -U #{user} -h #{host} #{database}
CMD

run commands do |ch, stream, data|
if data =~ /Password/
ch.send_data("#{pass}\n")
end
end
end

desc "Uploads and restores remote database"
task :sync do
upload
restore
end
end
end
end
10 changes: 5 additions & 5 deletions lib/cap_recipes/tasks/rails/hooks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Capistrano::Configuration.instance(true).load do
after "deploy:update_code", "rails:symlink_db_config" # copy database.yml file to release path
after "deploy:update_code", "rails:sweep:cache" # clear cache after updating code
after "deploy:restart" , "rails:repair_permissions" # fix the permissions to work properly
after "deploy:restart" , "rails:ping" # ping passenger to start the rails instance
end
#after "deploy:update_code", "rails:symlink_db_config" # copy database.yml file to release path
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain your reasoning for removing?

Is this a change for everybody or just you.

because you could chose to do

require 'cap-recipes/tasks/rails/manage.rb

instead of

require 'cap-recipes/tasks/rails'

in your deploy.rb and let this file remain unmodified for those who still depend on it.

#after "deploy:update_code", "rails:sweep:cache" # clear cache after updating code
#after "deploy:restart" , "rails:repair_permissions" # fix the permissions to work properly
#after "deploy:restart" , "rails:ping" # ping passenger to start the rails instance
end
22 changes: 22 additions & 0 deletions lib/cap_recipes/tasks/rails/manage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@
run "cd #{release_path} && #{try_sudo} bundle exec rake tmp:cache:clear RAILS_ENV=#{stage_or_production}"
end
end
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => { :no_release => true } do
set :branch, "HEAD@{1}"
deploy.default
end

desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
task :cleanup, :except => { :no_release => true } do
run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
end

desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end

end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end

# ===============================================================
Expand Down
2 changes: 1 addition & 1 deletion lib/cap_recipes/tasks/redis/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def ipaddress(eth)
desc "install redis-server"
task :install, :roles => [:redis,:redis_slave] do
utilities.apt_install %w[build-essential wget]
utilities.addgroup "redis", :system => true
utilities.addgroup "redis", :system => true # needs sudo
utilities.adduser "redis" , :nohome => true, :group => "redis", :system => true, :disabled_login => true
sudo "mkdir -p #{redis_base_path}/bin #{redis_base_path}/src /var/log/redis"
run "cd #{redis_base_path}/src && #{sudo} wget --tries=2 -c --progress=bar:force #{redis_src} && #{sudo} tar xzf #{redis_ver}.tar.gz"
Expand Down
6 changes: 5 additions & 1 deletion lib/cap_recipes/tasks/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def addgroup(group,options={})
switches = ''
switches += " --system" if options[:system]
switches += " --gid #{options[:gid]} " if options[:gid]
invoke_command "/usr/sbin/addgroup #{switches} #{group}", :via => run_method
invoke_command "sudo /usr/sbin/addgroup #{switches} #{group}", :via => run_method
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be in the form of the capistrano sudo helper?

ie:

invoke_command "#{sudo} /usr/sbin/addgroup  #{switches} #{group}", :via => run_method

end

#utilities.delgroup('deploy')
Expand Down Expand Up @@ -339,6 +339,10 @@ def handle_command_with_input(local_run_method, shell_command, input_query, resp
def deprecated(name,replacement=nil)
raise Capistrano::Error, "#{name} is deprecated, #{replacement ? "see: #{replacment}" : "no replacement" }."
end

def set_default(name, *args, &block)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you document an example and it's usage?

does this duplicate the _cset capistrano internal method?

set(name, *args, &block) unless exists?(name)
end



Expand Down