negroku 1.1.14 → 2.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +3 -201
  6. data/README.rdoc +6 -0
  7. data/Rakefile +44 -0
  8. data/bin/negroku +0 -8
  9. data/features/negroku.feature +8 -0
  10. data/features/step_definitions/negroku_steps.rb +6 -0
  11. data/features/support/env.rb +15 -0
  12. data/lib/negroku/cli/bootstrap.rb +139 -0
  13. data/lib/negroku/cli/commands/app.rb +39 -0
  14. data/lib/negroku/cli/commands/negroku.rb +15 -0
  15. data/lib/negroku/cli.rb +19 -206
  16. data/lib/negroku/deploy.rb +14 -36
  17. data/lib/negroku/helpers.rb +6 -83
  18. data/lib/negroku/i18n.rb +17 -0
  19. data/lib/negroku/tasks/bower.rake +12 -0
  20. data/lib/negroku/tasks/bundler.rake +12 -0
  21. data/lib/negroku/tasks/negroku.rake +48 -0
  22. data/lib/negroku/tasks/nginx.rake +24 -0
  23. data/lib/negroku/tasks/nodenv.rake +13 -0
  24. data/lib/negroku/tasks/rbenv.rake +56 -0
  25. data/lib/negroku/tasks/unicorn.rake +92 -0
  26. data/lib/negroku/templates/negroku/Capfile.erb +29 -0
  27. data/lib/negroku/templates/negroku/deploy.rb.erb +47 -0
  28. data/lib/negroku/templates/negroku/stage.rb.erb +13 -0
  29. data/lib/negroku/templates/tasks/unicorn_rails.rb.erb +40 -0
  30. data/lib/negroku/{tasks/templates/unicorn.erb → templates/tasks/unicorn_rails_activerecord.rb.erb} +5 -5
  31. data/lib/negroku/version.rb +1 -1
  32. data/lib/negroku.rb +4 -6
  33. data/negroku.gemspec +42 -0
  34. data/negroku.rdoc +5 -0
  35. data/test/default_test.rb +14 -0
  36. data/test/test_helper.rb +9 -0
  37. metadata +252 -51
  38. data/lib/negroku/capfile.rb +0 -32
  39. data/lib/negroku/config.rb +0 -36
  40. data/lib/negroku/tasks/base.rb +0 -29
  41. data/lib/negroku/tasks/bower.rb +0 -9
  42. data/lib/negroku/tasks/log.rb +0 -25
  43. data/lib/negroku/tasks/nginx.rb +0 -61
  44. data/lib/negroku/tasks/rbenv.rb +0 -27
  45. data/lib/negroku/tasks/templates/nginx.erb +0 -90
  46. data/lib/negroku/tasks/unicorn.rb +0 -47
  47. data/lib/negroku/templates/deploy.rb.erb +0 -24
  48. data/lib/negroku/templates/production.rb.erb +0 -14
  49. data/lib/negroku/templates/staging.rb.erb +0 -14
data/lib/negroku/cli.rb CHANGED
@@ -1,217 +1,30 @@
1
- require 'thor'
2
- require 'rainbow'
3
- require 'highline/import'
4
- require 'negroku'
1
+ require 'gli'
2
+ require 'inquirer'
3
+ require 'negroku/i18n'
4
+ require 'negroku/version'
5
+ require 'negroku/cli/bootstrap'
5
6
 
6
- trap('INT') { exit }
7
+ module Negroku::CLI
7
8
 
8
- class App < Thor
9
+ extend GLI::App
9
10
 
10
- say "\n\n#############################################".bright()
11
- say "## NEGROKU ##".bright()
12
- say "#############################################\n".bright()
11
+ # Set th version
12
+ version Negroku::VERSION
13
13
 
14
- desc "create", "Create application. Intialize the capfile, also create the deploy.rb file in the config folder"
15
- method_option :local_recipes, :type => :boolean, :aliases => "-l"
16
- method_option :path, :type => :string, :aliases => "-p"
17
- def create(name=nil)
18
- # Get configuration
19
- config = getConfig
14
+ # Negroku commands will manage the interactive cli ui
15
+ default_command :negroku
20
16
 
21
- # Test for config
22
- if config.empty?
23
- say "[WARNING]".foreground(:red)
24
- say "It's recommended that you add some custom settings to negroku before you create your app deployment\n\n"
25
- say "You can add your deployment servers urls using:\n"
26
- say "negroku target add my.deployment.com".foreground(:yellow)
27
- say "negroku target add 104.284.3.1\n".foreground(:yellow)
28
- unless agree "You have not added custom settings! Do you want to continue? [y/n]", true
29
- say "Goodbye"
30
- exit
31
- end
32
- end
17
+ # Don't show the negroku commands in the help
18
+ hide_commands_without_desc true
33
19
 
34
- say "We're about to create your application deploy setup\n".foreground(:green)
20
+ # Use the gemspec summary to describe the cli
21
+ program_desc Gem::Specification.find_by_name('negroku').summary
35
22
 
36
- # Hash to hold the app info
37
- data = {}
23
+ # Load all the commands in the negroku/cli/command folder
24
+ commands_from 'negroku/cli/commands'
38
25
 
39
- # Get the name
40
- if name.nil?
41
- data[:application_name] = ask "Give your application a name:".bright()
42
- else
43
- data[:application_name] = name
44
- end
45
-
46
- # The application code repository
47
- choose do |menu|
48
-
49
- say "\nREPOSITORIES"
50
- say "============"
51
- menu.prompt = "Please choose your repository?".bright()
52
- menu.select_by = :index
53
-
54
- # find local remote from git repo
55
- %x(git remote -v 2> /dev/null | awk '{print $2}' | uniq).split("\n").each do |url|
56
- menu.choice(url) do |server|
57
- say("Using #{server}")
58
- data[:repo] = server;
59
- end
60
- end
61
-
62
- # add other repo choice
63
- menu.choice(:other) {
64
- data[:repo] = ask "Type the url and username e.g. git@github.com:username/app-name.git: ".bright()
65
- }
66
- end
67
-
68
- # The application target deploy server
69
- say "\nTARGET SERVERS"
70
- say "==============\n"
71
- %w(staging production).each do |stage|
72
-
73
- # Stage title
74
- puts "\n#{stage.upcase}".foreground(:green)
75
-
76
- # Ask server
77
- choose do |menu|
78
- menu.prompt = "Please choose your #{stage} deployment server?".bright()
79
- menu.select_by = :index
80
-
81
- # Adds the targets in the config file if there is one
82
- if config[:target]
83
- config[:target].each do |val|
84
- menu.choice(val) do |server|
85
- say("Using #{server} for #{stage}")
86
- data[:"#{stage}_server"] = server;
87
- end
88
- end
89
- else
90
- say "[INFO] There are no target urls in the default settings".color(:yellow)
91
- end
92
-
93
- menu.choice(:other) {
94
- data[:"#{stage}_server"] = ask "Type the hostname or ip of the #{stage} server to deploy to:".bright()
95
- }
96
- end
97
-
98
- # Add custom domain
99
- case stage
100
- when "staging"
101
- domain = data[:"#{stage}_server"].gsub(/^([a-z\d]*)/, data[:application_name])
102
- when "production"
103
- domain = "www.#{data[:application_name]}.com"
104
- end
105
- if agree "Do you want to use #{domain}? [y/n]", true
106
- data[:"#{stage}_domains"] = domain
107
- else
108
- data[:"#{stage}_domains"] = ask "Please enter the domains separated by spaces"
109
- end
110
- end
111
-
112
- init(".", data)
113
-
114
- say "\n\nWhat to do next?\n".bright()
115
- say "Setup your app running " + "cap deploy:setup".color(:yellow) + " then " + "cap deploy:cold".color(:yellow)
116
- say "You can try with " + "cap -T".color(:yellow) +" to see the available tasks"
117
- say "Also you can check the following files, you may want to change some things there"
118
- say "config/deploy.rb".color(:yellow)
119
- say "config/deploy/staging.rb".color(:yellow)
120
- say "config/deploy/production.rb".color(:yellow)
121
- say "\nHAPPY DEPLOY".foreground(:green)
122
- end
123
- end
124
-
125
- class Target < Thor
126
- desc "add", "add new default target server"
127
- def add(host=nil)
128
- if host.nil?
129
- host = ask("Type the host or ip for the target machine")
130
- end
131
- saveConfig(:add, :target, host)
132
- say "[Negroku] Added #{host}"
133
- end
134
-
135
- desc "remove", "remove some target"
136
- def remove
137
- puts "I will remove a target"
138
- end
139
-
140
- desc "list", "show the targets"
141
- def list
142
- puts "I will list the target servers"
143
- end
144
- end
145
-
146
- class Konfig < Thor
147
- namespace "config"
148
-
149
- desc "show", "Show the current configuration file"
150
-
151
- def show
152
- showConfig
153
- end
26
+ # Initialize GLI app
27
+ exit Negroku::CLI.run(ARGV)
154
28
  end
155
29
 
156
- class RemoteEnv < Thor
157
- namespace "env"
158
- class_option :stage, :type => :string, :aliases => "-s", :desc => "Set the capistrano stage to be used", :banner => "STAGE"
159
-
160
- desc "show", "Show the current remote variables"
161
- def show
162
- %x(cap #{options[:stage]} rbenv:vars:show)
163
- end
164
-
165
- desc "add", "Adds or updates env variables to the remote server"
166
- method_option :local, :type => :boolean, :aliases => "-l"
167
- def add(key=nil, value=nil)
168
-
169
- # Check if the paramenter were sent in the call
170
- # Ask for the value if only the key was sent
171
- if !key.nil? && value.nil?
172
- value = ask("Please enter the value for #{key}:")
173
- # If nothing was sent
174
- elsif key.nil? && value.nil?
175
- # Check if the .rbenv-vars file exists and offer get the info from there
176
- if File.exist?(".rbenv-vars") && (agree "Do you want to add variables from your local .rbenv-vars file [y/n]", true)
177
- choose do |menu|
178
- menu.prompt = "Please choose variable you want to add?".bright()
179
- menu.select_by = :index
180
30
 
181
- File.readlines(".rbenv-vars").each do |line|
182
- menu.choice(line.gsub("\n","")) do |command|
183
- key = command.split("=")[0]
184
- value = command.split("=")[1]
185
- end
186
- end
187
- end
188
- else
189
- key = ask("Please enter the variable key:")
190
- value = ask("Please enter the value for #{key}:")
191
- end
192
- end
193
-
194
- %x(cap #{options[:stage]} rbenv:vars:add -s key="#{key}" -s value="#{value}")
195
- end
196
- end
197
-
198
- module Negroku
199
- class CLI < Thor
200
- class_option :version, :type => :boolean, :aliases => "-v"
201
-
202
- def initialize(*)
203
- super
204
-
205
- # Show the version
206
- if options[:version]
207
- puts Negroku::VERSION
208
- exit 0
209
- end
210
- end
211
-
212
- register(App, 'app', 'app [COMMAND]', 'Application')
213
- register(Target, 'target', 'target [COMMAND]', 'Target servers')
214
- register(Konfig, 'config', 'config [COMMAND]', 'Configuration')
215
- register(RemoteEnv, 'env', 'env [COMMAND]', 'Remote environmental variables')
216
- end
217
- end
@@ -1,39 +1,17 @@
1
- ##
2
- # Load Deployer Helpers
3
- require File.join(File.dirname(__FILE__), 'helpers')
1
+ # Base configuration
2
+ set :scm, :git
4
3
 
5
- # Base settings
6
- set :scm, 'git'
7
- set :deploy_via, :remote_cache
8
- set :use_sudo, false
4
+ # set :format, :pretty
5
+ # set :log_level, :debug
6
+ set :pty, true
9
7
 
10
- ##
11
- # Default Configuration
12
- set :remote, 'origin' unless respond_to?(:remote)
13
- set :branch, 'master' unless respond_to?(:branch)
8
+ set :keep_releases, 5
14
9
 
15
- # Default environment
16
- set :rails_env, 'production' unless respond_to?(:rails_env)
17
-
18
- ## Default path
19
- set :default_environment, {
20
- 'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$HOME/.nodenv/shims:$HOME/.nodenv/bin:$PATH"
21
- }
22
-
23
- # Run on Linux: `$ ssh-add` or on OSX: `$ ssh-add -K` for "forward_agent".
24
- ssh_options[:forward_agent] = true
25
- ssh_options[:port] = 22
26
- default_run_options[:pty] = true
27
-
28
- # Use the bundler capistrano task to deploy to the shared folder
29
- require "bundler/capistrano"
30
- set :bundle_flags, "--deployment --binstubs"
31
-
32
- ##
33
- # Load Deployment Tasks
34
- load_tasks('base')
35
- load_tasks('log')
36
- load_tasks('rbenv')
37
- load_tasks('bower')
38
- load_tasks('nginx')
39
- load_tasks('unicorn')
10
+ require 'negroku/helpers'
11
+ load_task "negroku"
12
+ load_task "rbenv" if was_required? 'capistrano/rbenv'
13
+ load_task "nodenv" if was_required? 'capistrano/nodenv'
14
+ load_task "bower" if was_required? 'capistrano/bower'
15
+ load_task "bundler" if was_required? 'capistrano/bundler'
16
+ load_task "nginx" if was_required? 'capistrano/nginx'
17
+ load_task "unicorn" if was_required? 'capistrano3/unicorn'
@@ -1,86 +1,9 @@
1
- def init(target=".", data)
2
-
3
- # Main locations
4
- target_path = File.expand_path(target)
5
- config_path = File.join(target_path, "config")
6
- deploy_path = File.join(target_path, "config", "deploy")
7
- capfile = File.join(target_path, "Capfile")
8
- deployfile = File.join(config_path, "deploy.rb")
9
- stagingfile = File.join(deploy_path, "staging.rb")
10
- productionfile = File.join(deploy_path, "production.rb")
11
-
12
- # Create the cap file if not found
13
- if Dir.entries(target_path).include?("Capfile")
14
- puts "[Negroku] => Found Capfile!"
15
- else
16
- puts "[Negroku] => Capifying!"
17
- `capify #{target_path}`
18
- end
19
-
20
- # Find or create config folder
21
- unless File.directory?(config_path)
22
- puts "[Negroku] => Could not find the \"config\" folder. Creating it now!"
23
- %x(mkdir #{config_path})
24
- end
25
-
26
- # Find or create deploy folder
27
- unless File.directory?(deploy_path)
28
- puts "[Negroku] => Could not find the \"deploy\" folder. Creating it now!"
29
- %x(mkdir #{deploy_path})
30
- end
31
-
32
- # replace and rename older deploy.rb
33
- if File.exist?(deployfile)
34
- puts "[Negroku] => Backing up deploy.rb"
35
- old_versions = Dir.entries(config_path).map {|entree| entree if entree =~ /deploy\.old\.(\d+)\.rb$/}.compact!
36
- if old_versions.empty?
37
- %x(mv #{deployfile} #{File.join(config_path, 'deploy.old.1.rb')})
38
- else
39
- version = old_versions.last.match('^deploy\.old\.(\d+)\.rb$')[1].to_i + 1
40
- %x(mv #{deployfile} #{File.join(config_path, "deploy.old.#{version}.rb")})
41
- end
42
- end
43
-
44
- # Create the new deploy
45
- puts "[Negroku] => Writing new deploy.rb."
46
- erb = getTemplate 'deploy.rb.erb'
47
- File.open(deployfile, 'w') do |f|
48
- f.write ERB.new(erb).result(binding)
49
- end
50
-
51
- # Create the new configuration stages
52
- puts "[Negroku] => Writing new deploy/staging.rb"
53
- erb = getTemplate 'staging.rb.erb'
54
- File.open(stagingfile, 'w') do |f|
55
- f.write ERB.new(erb).result(binding)
56
- end
57
- puts "[Negroku] => Writing new deploy/production.rb"
58
- erb = getTemplate 'production.rb.erb'
59
- File.open(productionfile, 'w') do |f|
60
- f.write ERB.new(erb).result(binding)
61
- end
62
-
63
- # Prepares the Capfile for negroku
64
- cfile = Capfile.new(capfile)
65
- cfile.assets()
66
- puts "[Negroku] => Enabling assets tasks."
67
- cfile.negroku()
68
- puts "[Negroku] => Adding Negroku Loader inside #{capfile}."
69
-
70
- end
71
-
72
- def showConfig()
73
- # Load the yaml file
74
- config = getConfig
75
- puts config
76
- end
77
-
78
- def getTemplate(template)
79
- File.read(File.join(File.dirname(__FILE__), 'templates', template))
1
+ # Find out if a specific library file was already required
2
+ def was_required?(file)
3
+ rex = Regexp.new("/#{Regexp.quote(file)}\.(so|o|sl|rb)?")
4
+ $LOADED_FEATURES.find { |f| f =~ rex }
80
5
  end
81
6
 
82
- ##
83
- # Helper Method that assists in loading in tasks from the tasks folder
84
- def load_tasks(tasks)
85
- load File.join(File.dirname(__FILE__), 'tasks', "#{tasks}.rb")
7
+ def load_task(name)
8
+ load File.join(File.dirname(__FILE__), 'tasks', "#{name}.rake")
86
9
  end
@@ -0,0 +1,17 @@
1
+ require 'i18n'
2
+
3
+ en = {
4
+ capified: 'Capified',
5
+ written_file: 'create %{file}',
6
+ application_name: 'Give your application a name',
7
+ other: 'Other',
8
+ choose_repo_url: 'Choose the repository url',
9
+ type_repo_url: 'Type the url and username e.g. git@github.com:username/app-name.git',
10
+ choose_stages: 'Select the stages you are going to use'
11
+ }
12
+
13
+ I18n.backend.store_translations(:en, { negroku: en })
14
+
15
+ if I18n.respond_to?(:enforce_available_locales=)
16
+ I18n.enforce_available_locales = true
17
+ end
@@ -0,0 +1,12 @@
1
+ ## bower.rb
2
+ #
3
+ # Adds capistrano/bower specific variables and tasks
4
+
5
+ namespace :load do
6
+ task :defaults do
7
+
8
+ # Add bower to :nodenv_map_bins
9
+ fetch(:nodenv_map_bins) << 'bower'
10
+
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ ## bundler.rb
2
+ #
3
+ # Adds capistrano/bundler specific properties and tasks
4
+
5
+ namespace :load do
6
+ task :defaults do
7
+
8
+ # Set bundler to run with the --deployment flag
9
+ set :bundle_flags, '--deployment'
10
+
11
+ end
12
+ end
@@ -0,0 +1,48 @@
1
+ require 'erb'
2
+ require 'pathname'
3
+
4
+ namespace :negroku do
5
+
6
+ desc 'Install Negroku, cap install STAGES=staging,production'
7
+ task :install do
8
+ envs = ENV['STAGES'] || 'staging,production'
9
+
10
+ tasks_dir = Pathname.new('lib/capistrano/tasks')
11
+ config_dir = Pathname.new('config')
12
+ deploy_dir = config_dir.join('deploy')
13
+
14
+ deploy_rb = File.expand_path("../../templates/deploy.rb.erb", __FILE__)
15
+ stage_rb = File.expand_path("../../templates/stage.rb.erb", __FILE__)
16
+ capfile = File.expand_path("../../templates/Capfile", __FILE__)
17
+
18
+ FileUtils.mkdir_p deploy_dir
19
+
20
+ template = File.read(deploy_rb)
21
+ file = config_dir.join('deploy.rb')
22
+ File.open(file, 'w+') do |f|
23
+ f.write(ERB.new(template).result(binding))
24
+ puts I18n.t(:written_file, scope: :negroku, file: file)
25
+ end
26
+
27
+ template = File.read(stage_rb)
28
+ envs.split(',').each do |stage|
29
+ file = deploy_dir.join("#{stage}.rb")
30
+ File.open(file, 'w+') do |f|
31
+ f.write(ERB.new(template).result(binding))
32
+ puts I18n.t(:written_file, scope: :negroku, file: file)
33
+ end
34
+ end
35
+
36
+ FileUtils.mkdir_p tasks_dir
37
+
38
+ FileUtils.cp(capfile, 'Capfile')
39
+
40
+
41
+ puts I18n.t :capified, scope: :negroku
42
+ end
43
+
44
+ task :test do
45
+ puts "si!!!"
46
+ puts I18n.t :capified, scope: :negroku
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ ## nginx.rb
2
+ #
3
+ # Adds capistrano3/nginx specific variables and tasks
4
+
5
+ namespace :load do
6
+ task :defaults do
7
+
8
+ end
9
+ end
10
+
11
+ # Adds some task on complement the capistrano3-nginx tasks
12
+ # This tasks are under the negroku namespace for easier identification
13
+ namespace :negroku do
14
+
15
+ namespace :nginx do
16
+ # Reload or restart unicorn after the application is published
17
+ after 'deploy:publishing', 'restart' do
18
+ invoke 'nginx:site:add'
19
+ invoke 'nginx:site:enable'
20
+ invoke 'nginx:reload'
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,13 @@
1
+ ## nodenv.rb
2
+ #
3
+ # Adds capistrano/nodenv specific variables and tasks
4
+
5
+ namespace :load do
6
+ task :defaults do
7
+
8
+ # Set the node version using the .node-version file
9
+ # Looks for the file in the project root
10
+ set :nodenv_node, File.read('.node-version').strip if File.exist?('.node-version')
11
+
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ ## rbenv.rb
2
+ #
3
+ # Adds capistrano/rbenv specific variables and tasks
4
+
5
+ namespace :load do
6
+ task :defaults do
7
+
8
+ # Set the ruby version using the .ruby-version file
9
+ # Looks for the file in the project root
10
+ set :rbenv_ruby, File.read('.ruby-version').strip if File.exist?('.ruby-version')
11
+
12
+ # Link .rbenv-vars file
13
+ set :linked_files, fetch(:linked_files, []) << '.rbenv-vars' if File.exist?('.rbenv-vars')
14
+ end
15
+ end
16
+
17
+ namespace :rbenv do
18
+ namespace :vars do
19
+ desc "Show current environmental variables"
20
+ task :show do
21
+ on release_roles :app do
22
+ within current_path do
23
+ execute :rbenv, 'vars'
24
+ end
25
+ end
26
+ end
27
+
28
+ desc "Add environmental variable"
29
+ task :add, [:key, :value] do |t, args|
30
+ key = args[:key]
31
+ value = args[:value]
32
+ on release_roles :app do
33
+ within shared_path do
34
+ unless test "[ ! -f .rbenv-vars ]"
35
+ execute :touch, ".rbenv-vars"
36
+ end
37
+ cmd = "if awk < #{shared_path}/.rbenv-vars -F= '{print $1}' | grep --quiet -w #{key}; then "
38
+ cmd += "sed -i 's/^#{key}=.*/#{key}=#{value.gsub("\/", "\\/")}/g' #{shared_path}/.rbenv-vars;"
39
+ cmd += "else echo '#{key}=#{value}' >> #{shared_path}/.rbenv-vars;"
40
+ cmd += "fi"
41
+ execute cmd
42
+ end
43
+ end
44
+ end
45
+
46
+ desc "Remove environmental variable"
47
+ task :remove, [:key] do |t, args|
48
+ on release_roles :app do
49
+ within shared_path do
50
+ execute :sed, "-i", "/^#{args[:key]}=/d", ".rbenv-vars"
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,92 @@
1
+ ## unicorn.rb
2
+ #
3
+ # Adds unicorn variables and tasks
4
+ # Unicorn main tasks are taken from the gem capistrano3-unicorn
5
+
6
+ # Here we are setting up the main negroku default to work with unicorn
7
+ namespace :load do
8
+ task :defaults do
9
+ # Add unicorn to :rbenv_map_bins
10
+ fetch(:rbenv_map_bins) << 'unicorn'
11
+
12
+ ###################################
13
+ ## capistrano3/unicorn variables
14
+
15
+ # Defines where the unicorn pid will live.
16
+ set :unicorn_pid, -> { "#{shared_path}/tmp/pids/unicorn.pid" }
17
+
18
+ set :unicorn_config_path, -> { "#{shared_path}/config/unicorn.rb" }
19
+
20
+ ###################################
21
+ ## unicorn.rb template variables
22
+
23
+ # Local path to look for custom config template
24
+ set :unicorn_template, -> { "config/deploy/#{fetch(:stage)}/unicorn.rb.erb" }
25
+
26
+ # The type of template to use
27
+ # rails, rails_activerecord
28
+ set :unicorn_template_type, -> { "rails_activerecord" }
29
+
30
+ # Number of workers (Rule of thumb is 2 per CPU)
31
+ # Just be aware that every worker needs to cache all classes and thus eat some
32
+ # of your RAM.
33
+ set :unicorn_workers, -> { 1 }
34
+
35
+ # Workers timeout in the amount of seconds below, when the master kills it and
36
+ # forks another one.
37
+ set :unicorn_workers_timeout, -> { 30 }
38
+
39
+ # The location of the unicorn socket
40
+ set :unicorn_socket, -> { "#{shared_path}/tmp/sockets/unicorn.sock" }
41
+
42
+ # Preload app for fast worker spawn
43
+ set :unicorn_preload, -> { true }
44
+
45
+ ###################################
46
+ ## capistrano3/nginx variables
47
+
48
+ # # Add bower to :nodenv_map_bins
49
+ set :app_server_socket, -> { fetch(:unicorn_socket) } if was_required? 'capistrano/nginx'
50
+
51
+ end
52
+ end
53
+
54
+ # Adds some task on complement the capistrano3-unicorn tasks
55
+ # This tasks are under the negroku namespace for easier identification
56
+ namespace :negroku do
57
+
58
+ namespace :unicorn do
59
+ desc "Upload unicorn configuration file"
60
+ task :setup do
61
+ on release_roles fetch(:unicorn_roles) do
62
+ within "#{shared_path}/config" do
63
+ config_file = fetch(:unicorn_template)
64
+ unless File.exists?(config_file)
65
+ config_file = File.expand_path("../../templates/tasks/unicorn_#{fetch(:unicorn_template_type)}.rb.erb", __FILE__)
66
+ end
67
+ config = ERB.new(File.read(config_file)).result(binding)
68
+ upload! StringIO.new(config), '/tmp/unicorn.rb'
69
+
70
+ execute :mv, '/tmp/unicorn.rb', 'unicorn.rb'
71
+ end
72
+ end
73
+ end
74
+
75
+ # Reload or restart unicorn after the application is published
76
+ after 'deploy:publishing', 'restart' do
77
+ invoke 'negroku:unicorn:setup'
78
+ invoke fetch(:unicorn_preload)? 'unicorn:restart' : 'unicorn:reload'
79
+ end
80
+
81
+ # Ensure the folders needed exist
82
+ after 'deploy:check', 'deploy:check:directories' do
83
+ on release_roles fetch(:unicorn_roles) do
84
+ execute :mkdir, '-pv', "#{shared_path}/config"
85
+ execute :mkdir, '-pv', "#{shared_path}/tmp/sockets"
86
+ execute :mkdir, '-pv', "#{shared_path}/tmp/pids"
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ end