diff --git a/lib/capistrano/tasks/unicorn.rake b/lib/capistrano/tasks/unicorn.rake index dd68c23..1ced9fa 100644 --- a/lib/capistrano/tasks/unicorn.rake +++ b/lib/capistrano/tasks/unicorn.rake @@ -22,6 +22,8 @@ namespace :load do set :unicorn_logrotate_enabled, false # by default, don't use logrotate to rotate unicorn logs set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids') + set :detect_os_script_name, 'detect_os.sh' + set :detect_os_remote_script_path, "#{fetch(:tmp_dir)}/#{fetch(:application)}/detect_os.sh" end end @@ -34,11 +36,16 @@ namespace :unicorn do end desc 'Setup Unicorn initializer' - task :setup_initializer do + task :setup_initializer => :upload_os_detection_script do on roles :app do sudo_upload! template('unicorn_init.erb'), unicorn_initd_file execute :chmod, '+x', unicorn_initd_file - sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults' + case capture(fetch(:detect_os_remote_script_path)).downcase + when 'centos' + sudo 'chkconfig', fetch(:unicorn_service), 'on' + when 'ubuntu' + sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults' + end end end @@ -68,6 +75,15 @@ namespace :unicorn do end end + desc 'Upload OS detection script.' + task :upload_os_detection_script do + on roles :app do + execute :mkdir, '-p', "#{fetch(:tmp_dir)}/#{fetch(:application)}/" + upload! File.join(File.dirname(__FILE__), "../../scripts/#{fetch(:detect_os_script_name)}"), fetch(:detect_os_remote_script_path) + execute :chmod, '+x', fetch(:detect_os_remote_script_path) + end + end + before :setup_initializer, :defaults before :setup_logrotate, :defaults diff --git a/lib/scripts/detect_os.sh b/lib/scripts/detect_os.sh new file mode 100644 index 0000000..fce8619 --- /dev/null +++ b/lib/scripts/detect_os.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +#Refer to https://github.com/puppetlabs/facter/blob/master/lib/facter/operatingsystem/linux.rb if additional +#operating systems need to be supported or more specific information about the operating system is required. + +if [[ $(lsb_release -is 2>/dev/null) == "Ubuntu" ]]; then + echo "Ubuntu" + exit 0 +else + if [[ -e "/etc/redhat-release" ]]; then + echo "$(cat /etc/redhat-release | cut -d " " -f1)" + exit 0 + fi +fi + +exit 1 \ No newline at end of file