Carrierwave files with Capistrano - ruby-on-rails

I'm using rails 3.2 with asset and carrierwave for upload some images, they store in /public/uploads/photo/.....
but when I do a cap:deploy (with capistrano) my current directory application doesn't contain the files I uploaded, because capistrano make a new version ....
=== Update ===
After all I use this :
inside :deploy namespace
task :symlink_uploads do
run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
end
and after:
after 'deploy:update_code', 'deploy:symlink_uploads'
=== Re Update ===
The solution of #tristanm is the best way to solve this.

How about this:
# config/deploy.rb
set :shared_children, shared_children + %w{public/uploads}
:shared_children defaults to %w(public/system log tmp/pids) so we're just expanding this list.
EDIT:
Don't forget to run cap deploy:setup after changing :shared_children so that the new targets are created under shared.
EDIT Capistrano 3:
Capistrano 3 uses the linked_dirs setting and doesn't specify public/system as a default anymore.
set :linked_dirs, fetch(:linked_dirs) + %w{public/system public/uploads}

With Capistrano 3 and without needing to redeploy.
Like #tristanm mentioned add this to your config/deploy.rb
# config/deploy.rb
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}
To have capistrano create shared/public/uploads
cap deploy:check:linked_dirs
Now cap can create the symlink
cap deploy:symlink:shared
Finally, if you have backups of the uploads you can put them in shared/public/uploads/ and they should work without needing to redeploy.

Capistrano creates new directory for every deploy.
There are some exceptions to it. For example, the log files are shared between the deployment directories because they are just symlinks. You have to create a symlink for public/uploads as well.
Here is the command:
run <<-CMD
rm -rf #{latest_release}/public/uploads &&
ln -s #{shared_path}/uploads #{latest_release}/public/uploads
CMD

Go to your app server shared folder and create an uploads directory.
mkdir uploads
In your deploy.rb file insert these codes under deploy namespace
task :symlink_uploads do
run "rm -rf #{latest_release}/public/uploads && ln -nfs #{shared_path}/uploads #{latest_release}/public/uploads"
end
after 'deploy:update_code', 'deploy:symlink_uploads'
Now delete the old files present already as they won't work. Upload a new file and cap deploy your app again. It should work now.

Using Capistrano 3, I just added this line to my config/deploy.rb
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
Then, run:
$ cap production deploy

Related

Capistrano + whenever + db database.yml not found

I'm trying to run Capistrano 2.15.5 with Whenever 0.8.4 and Ruby 2.0.0
I've added
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
to the end of my deploy.rb file and it always run:
/releases/20150910075216 && bundle exec whenever --update-crontab
app_name --set environment=production --roles db
and fails wth
No such file or directory -
/releases/20150910075216/config/database.yml
because database.yml is in folder /shared/ anyways I don't need the db -
how to get rid of --roles db in the capistrano command?
I have
server("my_server", :app, :web, :db, :primary => true)
because I need the :db for other scripts.
Thanks for help!
David
You should set linked_files Symlink linked files database.yml as following in deploy.rb for capistrano 3
set :linked_files, fetch(:linked_files, []).push('config/database.yml')
Or you can do Symlink manually for older version.
desc "Make symlink for database yml"
task :symlink do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end

preserve public folder after deployment with capistrano

I have a folder public/recipes with recipes inside of this folder.
However after deployment via remote_cache this folder and recipes are removed/deleted.
I do not want that capistrano remove/delete this folder and these recipes.
How I can get it?
If you are using Capistrano 3 then just specify public/recipes directory in linked_dirs variable:
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/uploads public/recipes}
It will set link between your directory and shared/public/recipes.
For Capistrano 2 you should set link "manually" in deploy.rb use ln command i.e. use something like this:
task :configure_symlinks, :roles => :web do
run "ln -nfs #{shared_path}/public/recipes #{current_release}/public/recipes"
end
after "deploy:update_code", "configure_symlinks"
If you don't want a symlink, you will probably need something like https://github.com/capistrano/copy-files.
However, I highly recommend that you use linked dirs as ethyl.bradtke suggests. That is the best way to handle this.

dotenv and linked_files with capistrano 3

Having a really hard time getting this to work. I just added dotenv gem to accommodate for the Rails 4.1 secrets.yml file. I also have in the .env file the database.yml's password.
To add to my deploy:
set :linked_files, %w{config/database.yml .env}
When I run cap production deploy I get:
/shared/config/database.yml does not exist on 107.170.....
How can I get the database.yml to be added?
I looked at the capistrano touch gem with no luck because after I create the empty files, ActiveRecord throws an error of No 'production' database
Create task for upload your .env and database.yml.Look example below:
desc "Database config"
task :setup_config, roles: :app do
# upload you database.yml from config dir to shared dir on server
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
# make symlink
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
# upload you database.yml from config dir to shared dir on server
put File.read(".env"), "#{shared_path}/config/.env"
# make symlink
run "ln -nfs #{shared_path}/config/.env #{current_path}/.env"
end
And add before and after hooks.
Or use dotenv-deployment that contain the same tasks.

Capistrano 3 recipe for dynamic_sitemaps

I'm trying to "translate" the Capistrano recipe to deploy dynamic_sitemaps to work with Capistrano 3.
The snippet suggested in the readme looks like this:
after "deploy:update_code", "sitemaps:create_symlink"
namespace :sitemaps do
task :create_symlink, roles: :app do
run "mkdir -p #{shared_path}/sitemaps"
run "rm -rf #{release_path}/public/sitemaps"
run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end
end
But this doesn't work with Capistrano 3. I pasted this code into config/deploy.rb and the first error I got was: Don't know how to build task 'sitemaps:create_symlink'`.
I read somewhere that in Capistrano 3 the namespaces have to be defined before the calls so I reversed the order of the blocks, defining the namespace first and having the after call last. I got NoMethodError: undefined method `map' for :roles:Symbol`.
So I rewrote the namespace block to:
namespace :sitemaps do
task :create_symlink do
on roles(:web) do
run "mkdir -p #{shared_path}/sitemaps"
run "rm -rf #{release_path}/public/sitemaps"
run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end
end
end
And now I'm getting Don't know how to build task 'deploy:update_code' and I'm at loss.
While I couldn't solve precisely the issue I posted above, the solution is actually very simple. If using Capistrano 3 just add public/sitemaps to your :linked_dirs setting as such:
set :linked_dirs, %w{bin log tmp vendor/bundle public/system public/sitemaps}
This will create a symbolic link between #{release_path}/public/sitemaps and #{shared_path}/public/sitemap creating the latter if needed.

how to copy a file to capistrano shared folder

I have file config/secrets.yml which has to be in my remote server in the shared folder. And it is not in my git repo, so it's not updated as other regular files.
I don't know how generate file the first time.
What I have done till the moment is to create a symlink each time I deploy with
run "ln -nfs #{shared_path}/config/secrets.yml #{release_path}/config/secrets.yml"
which will "update" file in each deploy, but the first time? how to generate secret.yml the first time?
You have to add to your .gitignore file:
/config/secrets.yml
the cap task:
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/config/secrets.yml #{release_path}/config/secrets.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
The .yml file in the shared folder I usually create it myself.
If you're using capistrano v3.x, you can easily use the :linked_files configuration variable to achieve this.
You can safely add config/secrets.yml to .gitignore and use capistrano's :linked_files configuration variable.
capistrano generally adds a template line with config/database.yml and the config/secrets.yml to the array when you cap install. You just have to uncomment that line that looks like :
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
Regarding how to generate the secrets.yml file for the first time (it is a one-time task of course), you do have to run rake secret and put the secret key generated out of it, in the file. It looks like this :
development:
secret_key_base: <some alphanumeric hash>
some_key: 338a3312d82
some_secret: f5d9c3214e7b
other_environment: development
other_password: password
production:
secret_key_base: <the key generated with `rake secret`>
some_key: 338a3312d82
some_secret: f5d9c3214e7b
other_environment: development
other_password: password

Resources