atech_cloud 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,11 @@ Capistrano::Configuration.instance(:must_exist).load do
4
4
  set :user, `whoami`.chomp
5
5
  set :ssh_options, {:forward_agent => true, :port => 22}
6
6
 
7
+ ## Return the deployment path
8
+ def deploy_to
9
+ fetch(:deploy_to, nil) || "/opt/apps/#{fetch(:application)}"
10
+ end
11
+
7
12
  ## Deployment namespace
8
13
  namespace :deploy do
9
14
  desc 'Deploy the latest revision of the application'
@@ -19,34 +24,37 @@ Capistrano::Configuration.instance(:must_exist).load do
19
24
  end
20
25
 
21
26
  task :update_code, :roles => :app do
22
- path = fetch(:deploy_to)
23
27
  ## Create a branch for previous (pre-deployment)
24
- run "cd #{path} && git branch -d rollback && git branch rollback"
28
+ run "cd #{deploy_to} && git branch -d rollback && git branch rollback"
25
29
  ## Update remote repository and merge deploy branch into current branch
26
- run "cd #{path} && git fetch origin && git reset --hard origin/#{fetch(:branch)}"
30
+ run "cd #{deploy_to} && git fetch origin && git reset --hard origin/#{fetch(:branch)}"
27
31
  finalise
28
32
  end
29
33
 
30
34
  task :finalise, :roles => :app do
31
35
  execute = Array.new
32
- execute << "cd #{fetch(:deploy_to)}"
36
+ execute << "cd #{deploy_to}"
33
37
  execute << "git submodule init"
34
38
  execute << "git submodule sync"
35
39
  execute << "git submodule update --recursive"
36
40
  run execute.join(' && ')
37
41
 
38
- run "cd #{fetch(:deploy_to)} && bundle --deployment --quiet"
42
+ run "cd #{deploy_to} && bundle --deployment --quiet"
39
43
  migrate if fetch(:run_migrations, false)
40
44
  end
41
45
 
42
46
  desc 'Setup the repository on the remote server for the first time'
43
47
  task :setup, :roles => :app do
44
- path = fetch(:deploy_to)
45
- run "git clone -n #{fetch(:repository)} #{path} --branch #{fetch(:branch)}"
46
- run "cd #{path} && git branch rollback && git checkout -b deploy && git branch -d #{fetch(:branch)}"
47
- run "sed 's/socket: \\\/tmp\\\/mysql.sock/host: db-a.cloud.atechmedia.net/' #{path}/config/database.yml.example > #{path}/config/database.yml ; true"
48
+ run "git clone -n #{fetch(:repository)} #{deploy_to} --branch #{fetch(:branch)}"
49
+ run "cd #{deploy_to} && git branch rollback && git checkout -b deploy && git branch -d #{fetch(:branch)}"
50
+ upload_db_config
48
51
  update_code
49
52
  end
53
+
54
+ desc 'Upload the database configuration file'
55
+ task :upload_db_config, :roles => :app do
56
+ put "[production]\n adapter: mysql2\n encoding: utf8\n reconnect: false\n database: #{fetch(:application, 'databasename')}\n pool: 5\n username: #{fetch(:application, 'dbusernmae')}\n password: #{ENV['DBPASS'] || 'xxxx'}\n host: #{fetch(:database_host, 'db-a.cloud.atechmedia.net')}\n", File.join(deploy_to, 'config', 'database.yml')
57
+ end
50
58
  end
51
59
 
52
60
  ## ==================================================================
@@ -54,7 +62,7 @@ Capistrano::Configuration.instance(:must_exist).load do
54
62
  ## ==================================================================
55
63
  desc 'Run database migrations on the remote'
56
64
  task :migrate, :roles => :app, :only => {:database_ops => true} do
57
- run "cd #{fetch(:deploy_to)} && RAILS_ENV=#{fetch(:environment)} bundle exec rake db:migrate"
65
+ run "cd #{deploy_to} && RAILS_ENV=#{fetch(:environment)} bundle exec rake db:migrate"
58
66
  end
59
67
 
60
68
  ## ==================================================================
@@ -62,7 +70,7 @@ Capistrano::Configuration.instance(:must_exist).load do
62
70
  ## ==================================================================
63
71
  desc 'Rollback to the previous deployment'
64
72
  task :rollback, :roles => :app do
65
- run "cd #{fetch(:deploy_to)} && git reset --hard rollback"
73
+ run "cd #{deploy_to} && git reset --hard rollback"
66
74
  deploy.finalise
67
75
  deploy.restart
68
76
  end
@@ -101,15 +109,26 @@ Capistrano::Configuration.instance(:must_exist).load do
101
109
  ## ==================================================================
102
110
  namespace :unicorn do
103
111
  task :start, :roles => :app do
104
- run "sudo -u app sh -c \"cd #{fetch(:deploy_to)} && bundle exec unicorn_rails -E #{fetch(:environment)} -c #{fetch(:deploy_to)}/config/unicorn.rb -D\""
112
+ upload_config
113
+ run "sudo -u app sh -c \"cd #{deploy_to} && bundle exec unicorn_rails -E #{fetch(:environment)} -c #{deploy_to}/config/unicorn.rb -D\""
105
114
  end
106
115
 
107
116
  task :stop, :roles => :app do
108
- run "sudo -u app sh -c \"kill `cat #{fetch(:deploy_to)}/tmp/pids/unicorn.pid`\""
117
+ run "sudo -u app sh -c \"kill `cat #{deploy_to}/tmp/pids/unicorn.pid`\""
109
118
  end
110
119
 
111
120
  task :restart, :roles => :app do
112
- run "sudo -u app sh -c \"kill -USR2 `cat #{fetch(:deploy_to)}/tmp/pids/unicorn.pid`\""
121
+ upload_config
122
+ run "sudo -u app sh -c \"kill -USR2 `cat #{deploy_to}/tmp/pids/unicorn.pid`\""
123
+ end
124
+
125
+ task :upload_config, :roles => :app do
126
+ unless fetch(:skip_unicorn_config, false)
127
+ template_config = File.read(File.expand_path('../unicorn.rb', __FILE__))
128
+ template_config.gsub!('$WORKER_PROCESSES', fetch(:unicorn_workers, 4).to_s)
129
+ template_config.gsub!('$TIMEOUT', fetch(:unicorn_timeout, 30).to_s)
130
+ put template_config, File.join(deploy_to, 'config', 'unicorn.rb')
131
+ end
113
132
  end
114
133
  end
115
134
 
@@ -0,0 +1,52 @@
1
+ ## Directory to use for stuff
2
+ rails_root = File.expand_path('../../', __FILE__)
3
+ FileUtils.mkdir_p(File.join(rails_root, 'tmp', 'sockets'))
4
+ FileUtils.mkdir_p(File.join(rails_root, 'tmp', 'pids'))
5
+ FileUtils.mkdir_p(File.join(rails_root, 'log'))
6
+
7
+ ## Set the number of worker processes which can be spawned
8
+ worker_processes $WORKER_PROCESSES
9
+
10
+ ## Preload the application into master for fast worker spawn
11
+ ## times.
12
+ preload_app true
13
+
14
+ ## Restart any workers which haven't responded for 30 seconds.
15
+ timeout $TIMEOUT
16
+
17
+ ## Store the pid file safely away in the pids folder
18
+ logger Logger.new(File.join(rails_root, 'log', 'unicorn.log'))
19
+ pid File.join(rails_root, 'tmp', 'pids', 'unicorn.pid')
20
+
21
+ ## Listen on a unix data socket
22
+ listen File.join(rails_root, 'tmp', 'sockets', 'unicorn.sock')
23
+
24
+ before_fork do |server, worker|
25
+ # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
26
+ # immediately start loading up a new version of itself (loaded with a new
27
+ # version of our app). When this new Unicorn is completely loaded
28
+ # it will begin spawning workers. The first worker spawned will check to
29
+ # see if an .oldbin pidfile exists. If so, this means we've just booted up
30
+ # a new Unicorn and need to tell the old one that it can now die. To do so
31
+ # we send it a QUIT.
32
+ #
33
+ # Using this method we get 0 downtime deploys.
34
+
35
+ old_pid = File.join(rails_root, 'tmp', 'pids', 'unicorn.pid.oldbin')
36
+ if File.exists?(old_pid) && server.pid != old_pid
37
+ begin
38
+ Process.kill("QUIT", File.read(old_pid).to_i)
39
+ rescue Errno::ENOENT, Errno::ESRCH
40
+ # someone else did our job for us
41
+ end
42
+ end
43
+ end
44
+
45
+ after_fork do |server, worker|
46
+ # Unicorn master loads the app then forks off workers - because of the way
47
+ # Unix forking works, we need to make sure we aren't using any of the parent's
48
+ # sockets, e.g. db connection
49
+ ActiveRecord::Base.establish_connection
50
+ srand
51
+ end
52
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atech_cloud
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Cooke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-28 00:00:00 +01:00
18
+ date: 2011-04-06 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,6 +29,7 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - lib/atech_cloud/deploy.rb
32
+ - lib/atech_cloud/unicorn.rb
32
33
  - lib/atech_cloud.rb
33
34
  has_rdoc: true
34
35
  homepage: http://atechmedia.com