I want to execute Shell command after Capistrano deployment (cp) : swift
# config valid only for current version of Capistrano
lock '3.5.0'
set :application, 'Dometech.fr'
set :repo_url, 'ssh://git#MYIP:MYPORT/var/www/depotsGit/myproject.git/'
set :deploy_to, '/var/www/dev/Myproject'
set :symfony_directory_structure, 2
set :controllers_to_clear, []
namespace :deploy do
after "deploy:updated" , "composer:install"
# Clear ACL only before switching version
before "deploy:publishing" , "symfony:fixes_acl"
end
namespace :swift do
desc 'Swift config'
task :swift do
on roles(:web) do
execute :cp, '/var/www/a.php /var/www/b.php'
end
end
end
But this simple command never executed ... can you help me ?
You defined the task, but didn't set it to run. If you add this:
after "deploy", "swift:swift"
it should be called after deploy.
Related
I am configuring a custom SCM because i don't need the default git in the development local environment, but i would like to trigger a custom logic, mainly based on creating a release starting from a source_directory.
As described in the documentation (http://capistranorb.com/documentation/advanced-features/custom-scm/) i wrote a module that extends the Capistrano::Plugin, and set required methods to handle the custom SCM implementation used by deploy Capistrano flow.
Besides this, when i put in my config/deploy/<environment>.rb the entry:
set :scm, :<custom plugin name>
Capistrano keeps to use the default git scm, even if is not declared.
in my Capfile there are loaded both as follow:
require_relative 'scm/local.rb'
install_plugin Capistrano::LocalPlugin
require 'capistrano/git
install_plugin Capistrano::SCM::Git
Here also the module of custom SCM :
require 'capistrano/scm/plugin'
module Capistrano
class Capistrano::SCM::LocalPlugin < ::Capistrano::Plugin
def set_defaults
set_if_empty :source_dir, 'non-exisisting-dir'
end
def define_tasks
namespace :local do
task :create_release do
run_locally do
execute :mkdir, '-p', :'tmp'
execute "cd #{fetch(:source_dir)} && tar -cz --exclude tests --exclude vendor --exclude .git --exclude node_modules --exclude tmp/#{fetch(:release_timestamp)}.tar.gz -f tmp/#{fetch(:release_timestamp)}.tar.gz ."
end
on release_roles :all do
execute :mkdir, '-p', release_path
upload! "tmp/#{fetch(:release_timestamp)}.tar.gz", "#{release_path}/#{fetch(:release_timestamp)}.tar.gz"
execute "tar -xvf #{release_path}/#{fetch(:release_timestamp)}.tar.gz --directory #{release_path}"
execute "rm #{release_path}/#{fetch(:release_timestamp)}.tar.gz"
end
run_locally do
execute "rm -rf tmp"
end
end
desc 'Determine the revision that will be deployed'
task :set_current_revision do
run_locally do
set :current_revision, capture(:git, " --git-dir #{fetch(:source_dir)}/.git rev-parse --short #{fetch(:branch)}")
end
end
end
end
def register_hooks
after 'deploy:new_release_path', 'local:create_release'
end
end
end
Does anyone know which is the black magic to use in order to say to Capistrano to use my scm instead of the default git one ?
set :scm, 'myscm' is deprecated. Until the next major version of Capistrano (4.0), there is a class which checks for an SCM having been installed via install_plugin, and if not, checks for the set :scm definition. If install_plugin has been called, then set :scm is ignored and deleted.
install_plugin only registers the plugin. It looks to me from the code that Capistrano will run both plugins if two are installed.
So, in a nutshell, Capistrano doesn't support selecting multiple SCMs based on environment. The closest thing to that you could try is using an environment variable to conditionally load the SCM in your Capfile. Something like:
if ENV['CAP_SCM'] == 'local'
require_relative 'scm/local.rb'
install_plugin Capistrano::LocalPlugin
else
require 'capistrano/git'
install_plugin Capistrano::SCM::Git
end
This is all documented here: https://github.com/capistrano/capistrano/blob/master/UPGRADING-3.7.md
I use Capistrano and Symfony plugin ( https://github.com/capistrano/symfony ) for my deployment (I have Symfony 2.7). But, my CSS is wrong. I think assetic is not generated.
I used default deploy.rb and added ACL commands for chmod.
# config valid only for current version of Capistrano
lock '3.5.0'
set :application, 'Dometech.fr'
set :repo_url, 'ssh://git#37.187.154.125:9325/var/www/depotsGit/dometech.git/'
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/www/dev/Dometech'
set :symfony_directory_structure, 2
namespace :deploy do
after "deploy:updated" , "composer:install"
# Clear ACL only before switching version
before "deploy:publishing" , "symfony:fixes_acl"
end
namespace :symfony do
desc "Add ACL on cache directory"
task :fixes_acl do
on roles :web do
execute :setfacl, "-R -m u:www-data:rwX #{fetch(:release_path)}/app/cache #{fetch(:release_path)}/app/logs"
end
end
end
Can you help me for assetic?
Apparently, the Symfony Capistrano plugin removed Assetic support, so you should add a task to your deploy.rb to take care of it. You can probably just take what was removed:
set :assetic_dump_flags, ''
namespace :assetic do
desc "Dump assets with Assetic"
task :dump do
on release_roles(:all) do
symfony_console "assetic:dump", fetch(:assetic_dump_flags)
end
end
end
and make sure it’s invoked with something like:
after 'deploy:updated', 'symfony:assetic:dump'
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.
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.
I am trying to deploy with capistrano. RVM is installed on the server and the ruby version is 1.93p385.
Here is the log of cap production deploy:
http://pastie.org/private/vs336nrgejpwdkuelufnma#
Why is capistrano failing to deploy?
Here is the deploy file:
require "rvm/capistrano"
require "bundler/capistrano"
set :rvm_ruby_string, "1.9.3-p385"
set :rvm_type, :user #Should the user by the username?
require "capistrano/ext/multistage"
set :http_server, :apache2
set :rake, "#{rake} --trace"
set :application, "app"
set :user, "myuser" # The server's user for deploys
set :ruby_version, "1.9.3-p385"
set :scm, "git"
set :repository, "my git repo here"
set :deploy_to, "/var/www/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, true
default_run_options[:pty] = true # Must be set for the password prompt from git to work
ssh_options[:forward_agent] = true
set :nodejs, true
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"
The server is an Ubuntu 10.04 LTS
** [out :: server] No such file or directory - /var/www/app/releases/20130216170229/config/database.yml
Does this path exist on the server? You might need to create the /var/www/app/releases portion by hand, which capistrano will then deploy into.
It is a common pattern to .gitignore database.yml for capistrano deployment. The database config then resides in <:deploy_to>/shared/config/. The you use this cap task to symlink the db config in your release directory:
namespace :deploy do
task :start do ; end
task :stop do ; end
desc "Symlink shared folders on each deployment"
task :symlink_shared do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
end
before "deploy:assets:precompile", "deploy:symlink_shared"