Capistrano 3 change ssh_options inside task - ruby

I trying to run capistrano v.3 task in same stage with diferent ssh_options.
my production.rb say:
set :stage, :production
set :user, 'deploy'
set :ssh_options, { user: 'deploy' }
With this configuration capistrano connect with user deploy which is correct for the rest of taks. But I need connect it for one specific task with an_other_user wich is well configured in server.
Then my recipe say:
...
tasks with original user
...
task :my_task_with_an_other_user do
set :user, 'an_other_user'
set :ssh_options, { user: 'an_other_user' }
on roles(:all) do |host|
execute :mkdir, '-p', 'mydir'
end
end
...
other tasks with original user
...
When execute:
cap production namespace:my_task_with_an_other_user
capistrano make ssh conexion with original :user "deploy" (the user declared in production.rb).
How can I change the user and/or ssh_options it inside task?

Capistrano 3
I had hard time finding out a solution. But the solution is much nicer than version 2. Cap team has done a great job. Make sure you have updated Capistrano to 3.2.x+ Here's the trick:
# in config/deploy/production.rb, or config/deploy/staging.rb
# These roles are used for deployment that works with Cap hooks
role :app, %w{deploy#myserver.com}
role :web, %w{deploy#myserver.com}
role :db, %w{deploy#myserver.com}
# Use additional roles to run side job out side Capistrano hooks
# 'foo' is another ssh user for none-release purpose tasks (mostly root tasks).
# e.g. user 'deploy' does not have root permission, but 'foo' has root permission.
# 'no_release' flag is important to flag this user will skip some standard hooks
# (e.g. scm/git/svn checkout )
role :foo_role, %w{foo#myserver.com}, no_release: true
Make sure both 'deploy' & 'foo' user can ssh into the box. Within your tasks, use the on keyword:
task :restart do
on roles(:foo_role) do
sudo "service nginx restart"
end
end
task :other_standard_deployment_tasks do
on release_roles(:all) do
# ...
end
end
Other gotchas:
Make sure some Capistrano tasks skips the additional no release role you added. Otherwise, it might cause file permission issues during deployment. E.g. capistrano/bundler extension need to override the default bundler_roles
set :bundler_roles, %w(web app db) # excludes the no release role.
Read more about no_release flag: related Github issue.
Capistrano 2
I used to have a custom functions to close and reconnect ssh sessions.
Reference Paul Gross's Blog
Place the following methods in deploy.rb. Call with_user to switch ssh session. Slightly simplified version:
def with_user(new_user, &block)
old_user = user
set :user, new_user
close_sessions
yield
set :user, old_user
close_sessions
end
def close_sessions
sessions.values.each { |session| session.close }
sessions.clear
end
Usage:
task :update_nginx_config, :roles => :app do
with_user "root" do
sudo "nginx -s reload"
end
end

Answer of #activars didn't work for me. Because when I defined several roles for one environment - only one was deployed :(
So my solution was to create several enviroments, e.g. production.rb and productionroot.rb.
productionroot has content with no_release=true flag - just as you've specified:
server '146.120.89.81', user: 'root', roles: %w{foo_role}, no_release: true
After that I've created sh script which runs
#/usr/bin/env bash
bundle exec cap production deploy
bundle exec cap productionroot deploy

Related

Mina Deploy uses wrong user

I got some issue with the gem 'mina'. If I do set :user, 'username', he tries to connect to the server via Username#xxx.... wich is not working if the user is not existing. My PC is name Username. So mina setup and mina deploy are not working.
Does someone know a solution?.
Thanks
Best regards
Matze
EDIT:
Gemfile:
gem 'mina'
After that I run bundle install and mina init
deploy.rb:
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv' # for rbenv support. (https://rbenv.org)
require 'mina/rvm' # for rvm support. (https://rvm.io)
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
set :user, "user"
set :application_name, 'appname'
set :domain, 'xx.xxx.xxx.xxx'
set :deploy_to, '/var/www/user/appname'
set :repository, 'user#xx.xxx.xxx.xxx:/home/user/git/appname.git'
set :branch, 'master'
# Optional settings:
# set :user, 'user' # Username in the server to SSH to.
# set :port, '30000' # SSH port number.
# set :forward_agent, true # SSH forward_agent.
# Shared dirs and files will be symlinked into the app-folder by the 'deploy:link_shared_paths' step.
# Some plugins already add folders to shared_dirs like `mina/rails` add `public/assets`, `vendor/bundle` and many more
# run `mina -d` to see all folders and files already included in `shared_dirs` and `shared_files`
# set :shared_dirs, fetch(:shared_dirs, []).push('public/assets')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')
# This task is the environment that is loaded for all remote run commands, such as
# `mina deploy` or `mina rake`.
task :remote_environment do
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .ruby-version or .rbenv-version to your repository.
# invoke :'rbenv:load'
# For those using RVM, use this to load an RVM version#gemset.
# invoke :'rvm:use', 'ruby-1.9.3-p125#default'
end
# Put any custom commands you need to run at setup
# All paths in `shared_dirs` and `shared_paths` will be created on their own.
task :setup do
# command %{rbenv install 2.3.0 --skip-existing}
end
desc "Deploys the current version to the server."
task :deploy do
# uncomment this line to make sure you pushed your local branch to the remote origin
# invoke :'git:ensure_pushed'
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
on :launch do
in_path(fetch(:current_path)) do
command %{mkdir -p tmp/}
command %{touch tmp/restart.txt}
end
end
end
# you can use `run :local` to run tasks on local machine before of after the deploy scripts
# run(:local){ say 'done' }
end
# For help in making your deploy script, see the Mina documentation:
#
# - https://github.com/mina-deploy/mina/tree/master/docs
set :execution_mode, :exec if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
When I run now mina setup he print that:
$ mina setup
User#xx.xxx.xxx.xxx's password:
SSH Auth key working fine and git working fine he just puts User instead of user before the ip, what is not working because the user just exists with small letter. But my working machine is named User.

Capistrano deploy.rb can't access task defined in stage file

I'm trying to put together a capistrano recipe for my app that basically clones the git repo locally, does some build processing and then rsyncs to the remote server.
I have 2 environments - dev and prod:
deploy.rb
deploy/dev.rb
deploy/prod.rb
I'm getting this error:
$ cap dev deploy
(Backtrace restricted to imported tasks)
cap aborted!
Don't know how to build task 'stop_server'
Tasks: TOP => dev
(See full trace by running task with --trace)
Why does the deploy not know how to build task stop_server if it's in the same namespace (deploy) in dev.rb?
Capfile
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
#require 'capistrano/deploy' # COMMENTED OUT B/C I'M TRYING TO BUILD LOCALLY INSTEAD OF DOING A GIT CLONE ON THE REMOTE SERVER
# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
deploy.rb
# config valid only for current version of Capistrano
lock '3.3.5'
set :repo_url, 'mygiturl'
# Default value for :pty is false
set :pty, true
set :deploy_dir, Dir.pwd
set :tmp_dir, "#{fetch(:deploy_dir)}/tmp"
set :output_dir, "#{fetch(:deploy_dir)}/output"
namespace :deploy do
desc 'Kick off the deploy'
task :init do
invoke 'deploy:create_tmp_dir'
end
... other tasks...
after :create_tmp_dir, :fetch_code
after :fetch_code, :build
after :build, :move_output
after :move_output, :stop_server
end
desc 'Deploy a new release'
task :deploy do
invoke 'deploy:init'
end
dev.rb
role :app, fetch(:application)
role :web, fetch(:application)
role :db, fetch(:application)
set :application, 'myapp'
set :env, 'dev'
set :ip, '123.456.78.901'
set :user, 'myuser'
set :deploy_to, '/var/www/myapp'
set :ssh_options, {
user: fetch(:user),
keys: %w(~/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey),
port: 22
}
namespace :deploy do
desc "Stops the node forever server"
task :stop_server do
on roles(:app) do
puts '**** STOPPING THE NODE SERVER *****'
execute 'sudo /etc/init.d/myapp stop; true' # The "; true" ignores any error that may occur if there is no forever process running
end
end
desc "Restarts the forever server"
task :start_server do
on roles(:app) do
puts '**** STARTING THE NODE SERVER *****'
execute 'sudo /etc/init.d/myapp start'
end
end
end
The problem is that Capistrano initializes by loading deploy.rb first; then it loads dev.rb.
At the time that Capistrano parses this line:
after :move_output, :stop_server
It does not know what :stop_server refers to (since it hasn't loaded dev.rb yet). Hence the error message you are seeing:
Don't know how to build task 'stop_server'
One easy workaround is to declare an empty :stop_server task in deploy.rb.
namespace :deploy do
# "stub" the task, to be defined later in dev.rb
task :stop_server
after : move_output, :stop_server
end
Then when Capistrano later loads dev.rb, the real implementation of :stop_server will get slotted in.
Now when you run cap dev deploy you should get the desired result.

Argument error while deploying rails app via capistrano to bluehost

I have build a sample app using rails and trying to deploy it using capistrano to bluehost.
But I am failing to do so. I followed the instructions mentioned in this http://vasil-y.com/2012/08/21/rails-capistrano-git-bluehost/
This is the contents of my config/deploy.rb:
require 'bundler/capistrano'
set :application, "rails_scaffold"
# BlueHost SSH user
set :user, "username"
# App Domain
set :domain, "example.com"
# We don't need sudo on BlueHost
set :use_sudo, false
# git is our SCM
set :scm, :git
# master is our default git branch
set :branch, "master"
# Use local git repository
set :repository, "#{domain}:/home/#{user}/rails_apps/#{application}"
set :local_repository, "."
# Checkout, compress and send a local copy
set deploy_via, :copy
set deploy_to, "/home/#{user}/rails_apps/#{application}"
# We have all components of the app on the same server
server domain, :app, :web, :db, :primary => true
namespace :deploy do
task :start do ; end
task :stop do ; end
# Touch tmp/restart.txt to tell Phusion Passenger about new version
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path, 'tmp', 'restart.txt')}"
end
end
# Clean-up old releases
after "deploy:restart", "deploy:cleanup"
But when I run the cap deploy:setup command, I get the following error:
/home/swaroop/.rvm/gems/ruby-1.9.3-p362/gems/capistrano-2.14.2/lib/capistrano/configuration/variables.rb:22:in `set': invalid variable `/u/apps/rails_scaffold' (variables must begin with an underscore, or a lower-case letter) (ArgumentError)
It says the application name must begin with an underscore or lowercase alphabets. And my application name looks like it is valid.
What am I doin wrong here?
Thank You
A couple of your set calls have arguments which are not symbols:
set deploy_via, :copy
set deploy_to, "/home/#{user}/rails_apps/#{application}"
Those should be:
set :deploy_via, :copy
set :deploy_to, "/home/#{user}/rails_apps/#{application}"
(Note the colons before deploy_via and deploy_to)
You can see why it might look related to your :application variable if we inspect what is happening in the line containing :deploy_to: it first calls the deploy_to method (since you're missing the colon, it looks like a method call), and deploy_to defaults to "/u/apps/#{application}" in the Capistrano source code:
_cset(:deploy_to) { "/u/apps/#{application}" }
So really, your code is effectively trying trying to run this:
set "/u/apps/#{application}", "/home/#{user}/rails_apps/#{application}"
but "/u/apps/#{application}" is not a valid variable name in Capistrano. Adding colons to those lines should fix it.

Capistrano - can't deploy my database.yml

When I try to deploy my app with capistrano, I'll get this error:
failed: "sh -c 'cp
/var/www/my_app/releases/20120313115055/config/database.staging.yml
/var/www/my_app/releases/20120313115055/config/database.yml'" on
IP_ADDR
My database.yml ie empty, database.staging.yml:
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: my_db
pool: 15
username: my_user_name
password: my_pass
host: localhost
in the /confing/deploy are files "production" "staging"
What am I missing here/where should I look for a failure? The credentials to database on the server should be right.
EDIT - here is my deploy
set :application, "my_app"
set :repository, "https://IP_ADDR/svn/my_app"
set :scm, :subversion
set :scm_username, 'my_name'
set :scm_password, 'my_pass'
default_run_options[:pty] = true
set :user, "my_name"
set :domain, 'IP_ADDR'
set :deploy_to, "/var/www/my_app"
set :use_sudo, false
set :deploy_via, :remote_cache
#set :keep_releases, 1
set :rails_env, 'production'
role :web, domain
role :app, domain
role :db, domain, :primary => true # This is where Rails migrations will run
namespace :deploy do
task :build_gems, :roles => :app do
desc "Building gems"
run "cd #{release_path} && bundle install --deployment"
end
task :migrations do
desc "Migrating database"
run "cd #{release_path} && rake db:migrate RAILS_ENV=production"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with passenger"
task t, :roles => :app do ; end
end
desc "Restarting passenger with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{release_path}/tmp/restart.txt"
end
after "deploy:update_code", "deploy:build_gems", "db:copy_configuration", "config:copy", "deploy:migrations", "deploy:cleanup"
after "deploy:update", "bluepill:copy_config", "bluepill:restart"
end
namespace :db do
task :copy_configuration do
run "cp #{release_path}/config/database.staging.yml #{release_path}/config/database.yml"
end
end
namespace :config do
task :copy do
run "cp #{release_path}/config/config.staging.yml #{release_path}/config/config.yml"
end
end
namespace :bluepill do
desc "Restart bluepill process"
task :restart, :roles => [:app] do
run "#{release_path}/script/delayed_job stop"
sudo "/etc/init.d/bluepill.sh restart"
end
#desc "Load bluepill configuration and start it"
##task :start, :roles => [:app] do
# sudo "/etc/init.d/bluepill.sh start"
#end
desc "Prints bluepills monitored processes statuses"
task :status, :roles => [:app] do
sudo "bluepill status"
end
desc "Copy config"
task :copy_config, :roles => [:app] do
run "cp #{release_path}/config/bluepill/configuration.rb /srv/script/bluepill.rb"
end
end
The problem:
cp: cannot stat `/var/www/my_app/releases/20120313144907/config/database.staging.yml': No such file or directory
I'm not sure how to solve your problem. It looks like database.staging.yml is not being deployed, so there's nothing for it to copy over.
I think there's a better workflow, though. Things like settings and database configs do not typically change between deployments, so those things can go in the shared folder of all the capistrano releases. Typically, you don't want your database.yml to be in your repo either since it's sensitive information. You can satisfy both of these things by excluding config/database.yml in your .gitignore.
This requires you to do a one time set up on your servers. You need create a database.yml at your_app_path/shared/config. Shared is a sibling to current and releases.
Your deploy.rb should have a task that symlinks the newly deployed release's database.yml to the on in the shared directory. Like this:
before "deploy:assets:precompile" do
run ["ln -nfs #{shared_path}/config/settings.yml #{release_path}/config/settings.yml",
"ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml",
"ln -fs #{shared_path}/uploads #{release_path}/uploads"
].join(" && ")
end
This means that your repo will contain no database.yml files. Since they are probably already in your repo. You'll have to git rm them, commit. Add them to the .gitignore and commit that.
In Capistrano 3, linking files is built-in. John's answer is simply:
In the shared/ folder create config/database.yml
In config/deploy.rb use this line
set :linked_files, fetch(:linked_files, []).push('config/database.yml')
This does what John was saying.
If you don't need to "reference application objects or methods"(1) during precompile then you might be OK with setting config.assets.initialize_on_precompile to false in config/application.rb

Capistrano: How to Include common settings in multiple project deploy.rb files

this is probably a newbie ruby question. I have several libraries and apps that I need to deploy to several different hosts. All of the apps and libs will share some common settings for those hosts-- e.g. host name, database server/user/pass, etc.
My goal is to do something like:
cap host1 stage deploy
cap host2 stage deploy
cap host1 prod deploy
# ...
My question is how do you include these common settings in all of your deploy.rb files? More specifically, I want to create a an rb file that I can include that has some common settings and several host specific task definitions:
set :use_sudo, false
# set some other options
task :host1 do
role :app, "host1.example.com"
role :web, "host1.example.com"
role :db, "host1.example.com", :primary => true
set :rodb_host, "dbhost"
set :rodb_user, "user"
set :rodb_pass, "pass"
set :rodb_name, "db"
end
task :host2 do
#...
end
deploy.task :carsala do
transaction do
setup
update_code
symlink
end
end
And then "include" this file in all of my deploy.rb files where I define stage, prod, etc and overwrite any "common" configuration parameters as necessary. Any suggestions would be appreciated. I've tried a few different things, but I get errors from cap for all of them.
Edit: I've tried
require 'my_module'
But I get errors complaining about an undefined task object.
I just experimented with it a little more and what I discovered is that you have to:
load 'config/my_module'
I can put all of my common definitions here and just load it into my deploy.rb.
It appears from the docs that load loads and executes the file. Alternatively, require attempts to load the library specified. I'm not totally sure about real difference, but it appears that there is some separation between the current app symbol space and the library require'd (hence the errors about the undefined task object) that isn't a problem when you do a load.
require 'my_extension'
Save your extensions in my_extension.rb
Jon has it right, that's the simplest way to go, just save it in a separate file and use require 'filename'. You could also use something fancy like Webistrano for deployment which also supports this in the form of Capistrano 'Recipes'. I've been using it for a while on a few projects and have come to love it.
I'm not sure how complex your needs are, but this works well for me for deployment:
set :application, "app"
set :scm, :subversion
# ... set all your common variables
task :staging do
set :repository, "http://app/repository/trunk/"
# ... set other uncommon variables in task
end
task :production do
set :repository, "http://app/repository/production/"
# ...
end
Deployment is just
cap staging deploy
or
cap production deploy

Resources