database.yml deployment best practice - ruby-on-rails

I don't check my database.yml file into source control and I was wondering what others do/best practice for copying this file over to the server when deploying.
I use Capistrano for deployment.

Currently, I keep a shared folder called shared that lives outside of my deply_to dirs. I keep my database.yml and other config files there and have a hook in cap to cp those over during deployment. Here is my simple cap task for doing the copy:
after "deploy:update_code","deploy:config_symlink"
namespace :deploy do
task :config_symlink do
run "cp #{shared_path}/../../shared/database.yml #{release_path}/config/database.yml"
end
end

My deployment script was breaking using the after "deploy:update_code" hook because that step seemed to be trying to access the DB already. So I do:
before "deploy:assets:precompile", 'deploy:symlink_shared'
namespace :deploy do
task :symlink_shared do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
end

I've been beating my head on this one for quite some time, and have figured out an easier solution for Capistrano 3. Hope this helps those out there.
I couldn't for the life of me get the "run" or "execute" commands to work in my deploy.rb file. As it turns out, in Capistrano 3, you should put your database.yml underneath the deployment_directory/shared folder. Then, in your deployment.rb file, include the file in the linked_files variable and it automatically gets symlinked to the same path during deployment.
Here's an example:
For starters, I don't have database.yml checked into source control. My database.yml is located here on the production server:
var/www/myapp/shared/config/database.yml
In my deployment.rb, I've added this line
set :linked_files, %w{config/database.yml}
During the deployment, capistrano automatically symlinks the file to:
var/www/myapp/current/config/database.yml
Hope this helps others out there. I've really been beating my head on the wall on this today.

Related

syncing folder after deploy

Everytime I deploy my redmine repository with capistrano my files are gone. I can see that capistrano symlinks the public directory, but not the {ROOT}/files directory.
I've read some articles on the internet but can't seem to get it work.
So I want is to keep my files in the {APPROOT/files} directory after a deploy
Many thanks for reading this.
You have two solutions:
1: Put your files directory in public/shared, which persists over the deployments.
2: Add a task in your capistrano recipe to create the symlink after each deploy:
task :create_files_symlink, :roles => :app do
run "ln -nfs #{shared_path}/files #{release_path}/files"
end
after 'deploy:update_code', 'create_files_symlink'

How to have one public/.htaccess per stage using capistrano-ext

I'm using Capistrano multistage (capistrano-ext) to deploy to staging or production.
The problem is I'm using Passenger in my shared hosting and need to specify the PassengerAppRoot in the public/.htaccess file. Obviously this is different between stages.
How can I keep different "stage-versions" of this file?
I haven't used capistrano-ext, but I'm assuming somewhere in your Capfile you'll have the stage as a variable. Let's assume it's the variable 'stage'. Let's also assume you have the two different versions checked in somewhere in your code (public/.htaccess-{production|staging})
You could set up a task to symlink (or copy) the right file after a deploy:
desc 'Set up a stage-appropriate .htaccess file'
task 'update_htaccess' do
run "ln -s #{release_path}/public/.htaccess-#{stage} #{release_path}/public/.htaccess"
end
after "deploy:update_code", "update_htaccess"

Apache2, Git, Capistrano & Rails - creating symlinks

I'm sort of stuck with adding symlinks to my app on the server. I currently have the following in .gitignore:
/non-public/system/uploads/*
I basically don't want Git to store the contents of the upload directory. So far so good.
On my server, inside my deploy.rb, I have the following:
namespace :customs do
task :symlink, :roles => :app do
run <<-CMD
ln -nfs #{shared_path}/system/uploads #{release_path}/non-public/system/uploads
CMD
end
end
after "deploy:symlink","customs:symlink"
after "deploy", "deploy:cleanup"
I want to create a symlink after each deployment for the uploads directory, but I keep getting a failed error message because the non-public/system/uploads directory doesn't exist in the git repository in the first place.
I've verified this by taking a look at the repository, and the structure /non-public/system/uploads doesn't exist because I have that set in .gitignore to ignore it.
I've looked at the Git wiki and it doesn't track directories, so I must be missing something. How do other developers symlink the uploads directory with their server?
What I'll usually do on my cap deploys is to create the directories, by doing a basic
set :deploy_to, "/this/dir"
run "mkdir -p #{deploy_to}/then/more/dirs"
after "deploy:symlink"
namespace :deploy....
....
....
then provide the run code to do some symlinks either on an after or whatever. This probably not optimal for all situations, but for the simple stuff it usually gets the job done.

Deploy from Git using Capistrano without a hard reset?

I've an issue at the moment where we are running a CMS within a site
(browsercms) that lets the user upload files. However, every time I
do a deploy Capistrano runs a hard reset thus nuking any uploaded
files.
Does anyone have any suggestions as to how to prevent the hard reset,
and just do a pull, or a way of moving the uploaded files elsewhere,
without having to change the application code?
This might not be the right approach.
You should include your 'images' folder in your .gitignore and symlink the $current_release/images folder to $shared/images.
This may be done automatically on every deployment if you put in your deploy.rb:
task :link_imgs do
run "ln -s #{shared_path}/photos #{release_path}/photos"
end
after "deploy:update_code", :link_imgs
I've done the same with my CMS and it works like a charm
This doesn't quite meet your criteria of "without having to change the application code".
However after running into a similar issue I shifted my uploaded image from /public/images to /public/system/images the /public/system directory is not 'versioned' by each capistrano deployment so the images survive.
Could it be the capistrano 'versioning' causing the problem (instead of a git reset)?
cap deploy calls deploy:update and deploy:restart
deploy:update makes the versioning, copying stuff
deploy:restart does the true restart, overload it at your convenince, usually in your config/deploy.rb file
namespace :deploy do
desc "Softly restart the server"
task :restart, :roles => :app, :except => { :no_release => true } do
my_own.restart_recipe
end
end

Where do you put your app-config-files when deploying rails with capistrano and svn

I have two config-files
/app/config/database.yml
and
/app/config/userconfig.yml
i don't want to put the database credentials and userconfig in the svn-repository, so i have database.yml.dist and userconfig.yml.dist checked in.
What is the best way to get copys of the dist-files in the shared-directory when deploying the app for the first time?
For later deploys i'll link to them from /app/current/config
You should place your config files in
/path/to/deployed_app/shared
Then in a capistrano task, sym link to those files:
namespace :deploy do
task :symlink_shared do
run "ln -s #{shared_path}/database.yml #{release_path}/config/"
end
end
before "deploy:restart", "deploy:symlink_shared"
In Capistrano v3, you can use a task called deploy:symlink:shared.
Provide a list of files you placed in the shared directory, so Capistrano knows which files to symlink when the task is run. This is typically done in deploy.rb:
set :linked_files, %w{
app/config/database.yml
app/config/userconfig.yml
}
Related: Capistrano - How to put files in the shared folder?

Resources