I am trying to deploy my rails app with Capistrano. While deploying it asks for the passphrase for key '/home/gokul/.ssh/id_rsa' but, I can't able to type the passphrase the characters are visible and not authenticating.
deploy.rb
# Server
server 'xxx.xxx.xxx.xxx', roles: [:web, :app, :db], primary: true
# Repository
set :repo_url, 'git#bitbucket.org:gokul/testapp.git'
set :scm_passphrase, ''
set :application, 'testapp'
set :user, 'gokul'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/APP/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), auth_methods: ['publickey'], keys: %w(~/.ssh/privatekey.pem) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, %w(tmp/pids)
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
Deployment Log:
It is asking for the passphrase but while entering the passphrase it is visible and not authenticating.
gokul$ cap production deploy
rvm 1.29.1 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
ruby-2.3.1
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
00:00 git:wrapper
01 mkdir -p /tmp
✔ 01 gokul#xxx.xxx.xxx.xxx 0.708s
Uploading /tmp/git-ssh-testapp-gokul.sh 100.0%
02 chmod 700 /tmp/git-ssh-testapp-gokul.sh
✔ 02 gokul#xxx.xxx.xxx.xxx 0.706s
00:03 git:check
01 git ls-remote git#bitbucket.org:gokul/testapp.git HEAD
01 Enter passphrase for key '/home/gokul/.ssh/id_rsa':
password
a
sde
we
ere
re
e
e
e
^C(Backtrace restricted to imported tasks)
cap aborted!
Interrupt:
Tasks: TOP => deploy:check => git:check
(See full trace by running task with --trace)
The deploy has failed with an error:
I tried setting pty as false as answered in https://stackoverflow.com/a/23227003/4172728. But this doesn't work for me.
Can anyone help me please.
Thank you.
I solved this issue by adding my local machine public key from ~/.ssh/id_rsa.pub to the list of access keys in the bitbucket repository settings.
And then, added the id_rsa to the ssh-agent as below:
gokul$ ssh-add ~/.ssh/id_rsa
Ref: https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html
Related
I have a rails application that is not rendering assets. It was previously working fine. When I added linked files it not only uploaded the files I specified but also precompiled assets. Even after clobbering all assets and recompiling it fails to render them even though that exist on the box.
error
Caddyfile
mydomain.com {
gzip
log stdout
root /home/deploy/apps/rails-app/current/public
proxy / unix:///home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock {
except /assets # this is /public/assets directory
except /solr
transparent
websocket
policy round_robin
}
errors stdout
header / {
Strict-Transport-Security "max-age=31536000"
}
proxy /solr localhost:8983 {
transparent
}
}
deploy.rb
# frozen_string_literal: true
# config valid only for current version of Capistrano
# lock '3.13.0'
# Change these
server '...', port: 2221, roles: %i[web app db], primary: true
set :repo_url, '...'
set :application, '...'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w[~/.ssh/id_rsa], auth_methods: %w(publickey) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :bundle_flags, '--no-cache'
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.6.6'
set :rails_env, 'production'
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)
## Defaults:
set :branch, 'master'
set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# set :linked_dirs, %w{data_files}
set :linked_dirs, fetch(:linked_dirs, []).push('public/system')
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
# before :start, :make_dirs
end
namespace :deploy do
desc 'Make sure local git is in sync with remote.'
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts 'WARNING: HEAD is not the same as origin/master'
puts 'Run `git push` to sync changes.'
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke! 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke! 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
I was able to get this working but using the fileserver directive.
https://example.com {
encode zstd gzip
root * /home/deploy/apps/rails-app/current/public
file_server
tls user#email.com
#notStatic {
not file
}
reverse_proxy #notStatic unix//home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock
header / {
Strict-Transport-Security "max-age=31536000"
}
}
I have been following this tutorial: https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma
But I get this error:
root#demo:~/app # cap demo deploy:initial
[Deprecation Notice] Future versions of Capistrano will not load the Git SCM
plugin by default. To silence this deprecation warning, add the following to
your Capfile after require "capistrano/deploy":
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host x.x.x.x: Connection refused - connect(2) for "x.x.x.x." port 5000
Errno::ECONNREFUSED: Connection refused - connect(2) for "x.x.x.x" port 5000
I have been researching for a few hours and have no idea how to fix this as I cannot find a formulated answer.
Heres the deploy.rb
server 'x.x.x.x', port: 5000, roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com:company/app.git'
set :application, 'app'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, true
set :stage, :demo
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/app.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
set :branch, :develop
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/develop`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
I am trying to deploy my Ruby on Rails app to the production server hosted on Digital Ocean. There is no problem occurs when I set my repository to "Public", but when I set my repository on "Private" I got the "fatal: Authentication failed" error:
SSHKit::Runner::ExecuteError: Exception while executing on host 178.62.16.69: git exit status: 128
git stdout: remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/ryzalyusoff/xxxxxx.git/'
git stderr: Nothing written
SSHKit::Command::Failed: git exit status: 128
git stdout: remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/ryzalyusoff/xxxxxx.git/'
git stderr: Nothing written
Tasks: TOP => git:check
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing on host 178.62.16.69: git exit status: 128
git stdout: remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/ryzalyusoff/xxxxxx.git/'
git stderr: Nothing written
So, what should I do so that I will be able to deploy to my production server without needed to set my repo on "Public" ?
Thanks!
------------------------- UPDATE 1 ----------------------------
I already generated the public key and put it on my github account. I have confirmed this by tested it by using the command below:
ssh -T git#github.com
------------------------- UPDATE 2 ----------------------------
I have changed my set :repo_url to ssh, but still the error persist. Here is my deploy.rb file:
# Change these
server '178.62.16.69', port: 22, roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com:ryzalyusoff/xxxxxx.git'
set :application, 'xxxxxx'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, false
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
set :whenever_identifier, ->{ "myapp_#{fetch(:stage)}" }
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
THE ERROR
Capistrano assumes you will SSH key forwarding to authenticate to Git. This means the key that you use on your local machine will be the one that your server uses as well when you perform a cap deploy.
Of course, this will only work if you are using SSH to connect to Git. Based on your error messages, it looks like you are not. You seem to be using the https: URL instead of the SSH one.
Change your :repo_url to the SSH style and you should be good.
E.g.
set :repo_url, "git#github.com:ryzalyusoff/xxxxxx.git"
You need to create an ssh key and provide github with your public key.
$ssh-keygen -t rsa
Copy the text *.pub to your clipboard and add it to your github account.
I am using figaro gem to store my environment variables in both deployment and production environments.
However on each deploy the production application.yml file gets deleted (which I create manually, because my development application.yml is listed in gitignore). Am I missing some basic git/capistrano stuff?
I'm not using Heroku, hence this is my approach:
Other Hosts
If you're not deploying to Heroku, you have two options:
Generate a remote configuration file
My deploy.rb:
# Change these
server 'xx.xxx.xxx.xx', port: 4012, roles: [:web, :app, :db], primary: true
set :repo_url, 'git-repo'
set :application, 'mosflash'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, fetch(:linked_dirs) + %w{public/system public/uploads}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
This line is included in my .gitignore:
# Ignore application configuration
/config/application.yml
Right now I'm recreating application.yml after each deploy, but this is quite tiresome.
I keep getting an error when trying to deploy with capistrano to my remote server. It is telling me that my local files aren't synced up to the remote repo, but when I git push I get the Everything is up-to-date message. I'm not sure what the problem is. I've also checked the rev-parse for both origin and HEAD and they return the same values.
Error:
WARNING: HEAD is not the same as origin/master
Run `git push` to sync changes.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host [IP address]
exit
SystemExit: exit
Tasks: TOP => deploy:starting => deploy:check_revision
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing on host [IP address]: exit
deploy.rb
# Change these
server '[ipaddress]', port: 22, roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com:name/rails_site.git'
set :application, 'rails_site'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
set :scm, :git
set :branch, :master
set :format, :pretty
set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless 'git rev-parse HEAD' === 'git rev-parse origin/master'
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
EDIT:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
I don't know why you wrote this:
unless 'git rev-parse HEAD' === 'git rev-parse origin/master'
instead of this:
unless `git rev-parse HEAD` === `git rev-parse origin/master`
If it is a typo, I'll remove this answer. If not, you are comparing string instead of result of commands.
also you can use:
unless %x{git rev-parse HEAD} === %x{git rev-parse origin/master}
see more at method ` definition. Also I prefer use == instead of === (but for strings is the same).
In your deploy.rb, comment temporary the before :starting, :check_revision and deploy without committing this change.
Alright, I have a solution for you. I have tested it on my machine and it works. Please be advised that this feels like a hack and might not be the most efficient answer. Nevertheless, it works.
Follow these steps to remove your existing project, clone a new one, and merge what you have locally with what you have in git:
# back out of your project
cd ..
# delete your project
sudo rm -r yourproject
# clone fresh instance of your project
git clone https://github.com/yourusername/yourproject.git
# navigate to your project
cd yourproject
# remove .git folder
sudo rm -r .git
# initialize a new .git
git init
# add all files for upcoming commit/merge
git add .
# set commit
git commit -m "refreshed sync"
# set remote origin to merge with
git remote add origin https://github.com/yourusername/yourproject.git
# make a pull to merge the two
# you will enter a nano screen to enter comment
# press CTRL + X to continue
git pull https://github.com/yourusername/yourproject.git
# push to remote location and set upstream
git push --set-upstream origin master
# Now you're all set!
Use git branch to check if you are in different branch. If your server use master branch, then git checkout master git push solved my issue.