public
Description: Automate your wordpress deploys with capistrano and git
Homepage: http://jestro.com
Clone URL: git://github.com/jestro/wordpress-capistrano.git
jnewland (author)
Mon Nov 24 11:55:16 -0800 2008
commit  827c9366c6133c338f417e17ac5cf21d5d3a79a5
tree    4e4b09b9fd30541b06c15f5238224a561aa552e6
parent  045539400448bd5c4bc5c6b33bec05359a0ee3e9
wordpress-capistrano / config / deploy / wordpress.rb
100644 184 lines (158 sloc) 5.695 kb
1
2
3
4
5
6
7
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'erb'
Capistrano::Configuration.instance.load do
  default_run_options[:pty] = true
  set :deploy_to, "/var/www/apps/#{application}"
  set :scm, "git"
  set :user, "wordpress"
  set :runner, user
  set :deploy_via, :remote_cache
  set :branch, "master"
  set :git_enable_submodules, 1
 
  #allow deploys w/o having git installed locally
  set(:real_revision) do
    output = ""
    invoke_command("git ls-remote #{repository} #{branch} | cut -f 1", :once => true) do |ch, stream, data|
      case stream
      when :out
        if data =~ /\(yes\/no\)\?/ # first time connecting via ssh, add to known_hosts?
          ch.send_data "yes\n"
        elsif data =~ /Warning/
        elsif data =~ /yes/
          #
        else
          output << data
        end
      when :err then warn "[err :: #{ch[:server]}] #{data}"
      end
    end
    output.gsub(/\\/, '').chomp
  end
  #no need for system, log, and pids directory
  set :shared_children, %w()
 
  role :app, domain
  role :web, domain
  role :db, domain, :primary => true
 
  before "deploy:setup", "puppet:initial_setup"
  before "deploy:setup", "setup:users"
  after "deploy:setup", "setup:fix_permissions"
 
  namespace :deploy do
    desc "Override deploy restart to not do anything"
    task :restart do
      #
    end
 
    task :finalize_update, :except => { :no_release => true } do
      run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
      #link the themes, plugins, and config
      run <<-CMD
rm -rf #{latest_release}/wordpress/wp-content/themes #{latest_release}/wordpress/wp-content/plugins &&
ln -s #{latest_release}/themes #{latest_release}/wordpress/wp-content/themes &&
ln -s #{latest_release}/plugins #{latest_release}/wordpress/wp-content/plugins &&
ln -s #{latest_release}/config/wp-config.php #{latest_release}/wordpress/wp-config.php
CMD
      puppet.update_from_release
    end
 
    task :cold do
      update
      apache.configure
    end
  end
 
  namespace :setup do
 
    task :users do
      set :user, 'root'
      reset_password
      set :password_user, 'wordpress'
      run "groupadd -f wheel"
      run "useradd -g wheel wordpress || echo"
      reset_password
      teardown_connections_to(sessions.keys)
      set :user, 'wordpress'
      generate_ssh_keys
    end
 
    task :generate_ssh_keys do
      run "mkdir -p /home/wordpress/.ssh"
      run "chmod 700 /home/wordpress/.ssh"
      run "ssh-keygen -q -f /home/wordpress/.ssh/id_rsa -N ''"
      pubkey = capture("cat /home/wordpress/.ssh/id_rsa.pub")
      puts "Below is a freshly generated SSH public key for your server."
      puts "Please add this as a 'deploy key' to your github project."
      puts ""
      puts pubkey
      puts ""
    end
 
    task :reset_password do
      password_user = fetch(:password_user, 'root')
      puts "Changing password for user #{password_user}"
      root_password = Capistrano::CLI.password_prompt "New UNIX password:"
      root_password_confirmation = Capistrano::CLI.password_prompt "Retype new UNIX password:"
      if root_password != ''
        if root_password == root_password_confirmation
          run "echo \"#{ root_password }\" | sudo passwd --stdin #{password_user}"
        else
          puts "Passwords did not match"
          exit
        end
      else
        puts "Not resetting password, none provided"
      end
    end
 
    task :fix_permissions do
      sudo "chown -R #{user}:wheel #{deploy_to}"
    end
  end
 
  namespace :apache do
    task :configure do
      aliases = []
      aliases << "www.#{domain}"
      aliases.concat fetch(:server_aliases, [])
      set :server_aliases_array, aliases
 
      file = File.join(File.dirname(__FILE__), "..", "vhost.conf.erb")
      template = File.read(file)
      buffer = ERB.new(template).result(binding)
 
      put buffer, "#{shared_path}/#{application}.conf", :mode => 0444
      sudo "mv #{shared_path}/#{application}.conf /etc/httpd/conf.d/"
      sudo "/etc/init.d/httpd restart"
    end
  end
 
  namespace :puppet do
 
    task :initial_setup do
      set :user, 'root'
      run "groupadd -f puppet"
      run "useradd -g puppet puppet || echo"
      install_dependencies
      download
      manually_update_node_definition
      update
      set :user, 'wordpress'
    end
 
    task :install_dependencies do
      #install ruby and curl
      run "yum install -y ruby ruby-devel ruby-libs ruby-rdoc ruby-ri curl which"
 
      #install rubygems
      run "cd /tmp && curl -OL http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz"
      run "cd /tmp && tar xfz rubygems-1.3.1.tgz"
      run "cd /tmp/rubygems-1.3.1 && sudo ruby setup.rb"
 
      #install puppet
      run "gem install facter puppet --no-rdoc --no-ri"
 
      #setup puppet dir
      run "mkdir -p /var/puppet"
    end
 
    task :download do
      run "cd /tmp && curl -L http://github.com/jestro/puppet-wordpress/tarball/master | tar xz"
      run "rm -rf /etc/puppet"
      run "mv /tmp/jestro-puppet-wordpress* /etc/puppet"
      run "rm -rf jestro-puppet-wordpress-*"
    end
 
    task :manually_update_node_definition do
      put(File.read(File.join(File.dirname(__FILE__), '..', 'puppet.pp')),"/etc/puppet/manifests/site.pp")
    end
 
    task :update_from_release do
      sudo "rm -rf /etc/puppet"
      sudo "ln -s #{latest_release}/config/puppet /etc/puppet"
      sudo "rm -rf /etc/puppet/manifests/site.pp"
      sudo "ln -s #{latest_release}/config/puppet.pp /etc/puppet/manifests/site.pp"
    end
 
    task :update do
      run "sudo sh -c 'PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin puppet /etc/puppet/manifests/site.pp'"
    end
 
  end
 
end