Capistrano: cap aborted! SSHKit::Runner::ExecuteError: Exception while executing on host - capistrano3

I'm trying to deploy code with Capistrano 3 to Ubuntu server from GIT repository, but I'm getting this error.
==========================================================================
Here is my Gemfile.
gem 'capistrano', '~> 3.1.0'
#//Use unicorn as the app server
gem 'unicorn'
#// Use Capistrano for deployment
group :development do
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-rbenv', "~> 2.0"
end
source 'https://rubygems.org'
==========================================================================
#deploy.rb
lock '3.1.0'
#// Define the name of the application
set :application, 'my_app'
#// Define where can Capistrano access the source repository
#// set :repo_url, 'https://github.com/[user name]/[application name].git'
set :scm, :git
set :repo_url, 'git#github.com:jaipratik/rw.git'
set :use_sudo, true
set :log_level, :debug
#// Define where to put your application code
set :deploy_to, "/var/www/my_app"
set :pty, true
set :format, :pretty
==========================================================================
#// production.rb
role :app, %w{ubuntu#{IP/Host}}
server '{IP/Host}', user: 'ubuntu', roles: %w{web app}
set :ssh_options, {
keys: %w(/Users/jay/.ssh/id_rsa),
forward_agent: false,
user: 'user'
# auth_methods: %w(password)
}
==========================================================================
result for $ bundle exec cap production deploy --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke deploy (first_time)
** Execute deploy
** Invoke deploy:starting (first_time)
** Execute deploy:starting
** Invoke deploy:check (first_time)
** Execute deploy:check
** Invoke git:check (first_time)
** Invoke git:wrapper (first_time)
** Execute git:wrapper
INFO[f8299d4f] Running /usr/bin/env mkdir -p /tmp/my_app/ on {IP/Host}
DEBUG[f8299d4f] Command: /usr/bin/env mkdir -p /tmp/my_app/
INFO[f8299d4f] Finished in 0.723 seconds with exit status 0 (successful).
DEBUGUploading /tmp/my_app/git-ssh.sh 0.0%
INFOUploading /tmp/my_app/git-ssh.sh 100.0%
INFO[b509dfb7] Running /usr/bin/env chmod +x /tmp/my_app/git-ssh.sh on {IP/Host}
DEBUG[b509dfb7] Command: /usr/bin/env chmod +x /tmp/my_app/git-ssh.sh
INFO[b509dfb7] Finished in 0.084 seconds with exit status 0 (successful).
** Execute git:check
DEBUG[9646aea0] Running /usr/bin/env git ls-remote git#github.com:jaipratik/rw.git on {IP/Host}
DEBUG[9646aea0] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/my_app/git-ssh.sh /usr/bin/env git ls-remote git#github.com:jaipratik/rw.git )
DEBUG[9646aea0] c452c845bb80f72d3023557d2ea8f776950c659f
DEBUG[9646aea0]
DEBUG[9646aea0] HEAD
DEBUG[9646aea0]
DEBUG[9646aea0] c452c845bb80f72d3023557d2ea8f776950c659f
DEBUG[9646aea0]
DEBUG[9646aea0] refs/heads/master
DEBUG[9646aea0]
DEBUG[9646aea0] Finished in 0.940 seconds with exit status 0 (successful).
** Invoke deploy:check:directories (first_time)
** Execute deploy:check:directories
INFO[fa4b1f56] Running /usr/bin/env mkdir -pv /var/www/my_app/shared /var/www/my_app/releases on {IP/Host}
DEBUG[fa4b1f56] Command: /usr/bin/env mkdir -pv /var/www/my_app/shared /var/www/my_app/releases
INFO[fa4b1f56] Finished in 0.086 seconds with exit status 0 (successful).
** Invoke deploy:check:linked_dirs (first_time)
** Execute deploy:check:linked_dirs
** Invoke deploy:check:make_linked_dirs (first_time)
** Execute deploy:check:make_linked_dirs
** Invoke deploy:check:linked_files (first_time)
** Execute deploy:check:linked_files
** Invoke deploy:started (first_time)
** Execute deploy:started
** Invoke deploy:updating (first_time)
** Invoke deploy:new_release_path (first_time)
** Execute deploy:new_release_path
** Execute deploy:updating
** Invoke git:create_release (first_time)
** Invoke git:update (first_time)
** Invoke git:clone (first_time)
** Invoke git:wrapper
** Execute git:clone
DEBUG[fa77f295] Running /usr/bin/env [ -f /var/www/my_app/repo/HEAD ] on {IP/Host}
DEBUG[fa77f295] Command: [ -f /var/www/my_app/repo/HEAD ]
DEBUG[fa77f295] Finished in 0.081 seconds with exit status 1 (failed).
DEBUG[0cc407cc] Running /usr/bin/env if test ! -d /var/www/my_app; then echo "Directory does not exist '/var/www/my_app'" 1>&2; false; fi on {IP/Host}
DEBUG[0cc407cc] Command: if test ! -d /var/www/my_app; then echo "Directory does not exist '/var/www/my_app'" 1>&2; false; fi
DEBUG[0cc407cc] Finished in 0.075 seconds with exit status 0 (successful).
INFO[de52ca0e] Running /usr/bin/env git clone --mirror git#github.com:jaipratik/rw.git /var/www/my_app/repo on {IP/Host}`enter code here`
DEBUG[de52ca0e] Command: cd /var/www/my_app && ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/my_app/git-ssh.sh /usr/bin/env git clone --mirror git#github.com:jaipratik/rw.git /var/www/my_app/repo )
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host {IP/Host}: git exit status: 1
git stdout: Nothing written
git stderr: Nothing written
/Users/jay/.rvm/gems/ruby-2.1.0/gems/sshkit-1.5.1/

Just write the command in console:
ssh-add
If that don't work, write below command in console:
ssh-add ~/.ssh/id_rsa
and then re-run the command (i.e. cap production deploy), It will work for sure.

Capistrano wasn't able to create the folder on ec2. Once I created the folder it worked like a charm.
So if you also have similar issues try creating the folder on ec2 and then execute the cap production deploy.

If all the above solutions did not work for you, try this. It works for me. Cheer.
ssh-add ~/.ssh/your_private_id_rsa
and run
eval `ssh-agent`
Ref: http://mjacobus.github.io/2015/08/20/solving-weird-capistrano-problems-with-ssh-authentication.html

So what are the permissions for /var/www/my_app in your remote machine ? Make sure they are owned by the same user that you specified in this config option:
set :user, "mydeployuser"
If not I believe Capistrano defaults to the user you are using to run the 'cap' command on the client machine. Make sure that it has permissions to modify/create /var/www/my_app
Since you have:
set :use_sudo, true
it's possible that sudo may not be setup as passwordless sudo like suggested in the Cap 3 docs

I had this issue when trying to deploy a Rails application to a server using Capistrano.
When I run the command cap deploy, I get the error:
SSHKit::runner::ExecuteError: Exception while executing as deploy
Here' how I fixed it:
The issue was that the server to which I was trying to deploy to did not have my SSH key authorized. I had to do the following:
Generate a new SSH key on my local machine:
ssh-keygen
Display the contents of the public file. In my case mine was id_rsa.pub:
cat ~/.ssh/your-public-file
Login to the server which you want to deploy the app to:
ssh your-username#server-ip-address
Paste the contents of the public file into the ~/.ssh/authorized_keys file on a new line.
That's all.
I hope this helps

I had the same problem. Turns out I forgot to add my Gitlab instance SSH key (the 'runner') to my server's ~/.ssh/authorized_keys file.

If you are setting up a new Ubuntu server, public key authentication may be switched off by default which you will have to switch on. See this answer

Related

Dockerfile - RUN and setting ENV - intermediate container

EDIT
I want to pass the last git short hash into my React app build with the following command:
git log -1 --pretty=%h
Thus in my Dockerfile I want something such as:
ARG REACT_APP_GIT_SHORTHASH
RUN git clone https://token#github.com/paulywill/repo.git && cd repo && export REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h`
ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH
In my Github Actions build I'm getting the following:
Step 6/14 : ARG REACT_APP_GIT_SHORTHASH
---> Running in f45f530d5c76
Removing intermediate container f45f530d5c76
---> 87a91c010aaf
Step 7/14 : RUN git clone https://***#github.com/paulywill/repo.git && cd repo && export REACT_APP_GIT_SHORTHASH=$(git log -1 --pretty=%h)
---> Running in b8a8fa3cd703
Cloning into 'repo'...
Removing intermediate container b8a8fa3cd703
---> 5bbf3a76b928
Step 8/14 : ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH
---> Running in f624f2e59dc6
Removing intermediate container f624f2e59dc6
---> d15c3c276062
Are these command even visible or able to pass values if they're different intermediate containers?
ROM ubuntu:20.04
ARG REACT_APP_GIT_SHORTHASH
RUN apt-get update -y
RUN apt-get install git -y
RUN git clone https://github.com/pooya-mohammadi/deep_utils.git
WORKDIR deep_utils
RUN REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h`
ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH
Change deep_utils with your repo name. I found the cd <directory> to be problematic.
As per a previous answer and thanks in part for #david-maze for pointing me in the right direction, I can easily grab the git short hash before the docker build.
.github/deploy.yml
...
- name: Set outputs
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
- name: Check outputs
run: echo ${{ steps.vars.outputs.sha_short }}
...
docker build --build-arg REACT_APP_GIT_SHORTHASH=${{ steps.vars.outputs.sha_short }} -t $ECR_REPOSITORY_CLIENT .
Dockerfile
FROM node:16-alpine
ARG VERSION
ENV VERSION $VERSION
ARG REACT_APP_GIT_SHORTHASH
ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH
...

RUN Script after sql service up in dockerfile

I have a database for my app and I want to create it in run time docker
I have a file CreateDB.sh and it creates all the tables and stored procedure that I want.
I tried this :
FROM mcr.microsoft.com/mssql/server
ENV ACCEPT_EULA=Y \
SA_PASSWORD=qwe123QWE
USER root
RUN mkdir /home/db
COPY ./db /home/db
RUN chmod +x /home/db/DbScriptLinux.sh
WORKDIR /home/db/
CMD ["/bin/bash", "/home/db/DbScriptLinux.sh"]
but it returns an error :
LoginTimeout
is there any way to run my script after all services (sql-server) start?
You can use an if statement, for example, RUN if [[ -z "$arg" ]] ; then echo Argument not provided ; else echo Argument is $arg ; fi
Another way would be to use command1 && command2 so if the command 1 is successfull, then command 2 would run afterwards.
Your last line CMD ["/bin/bash", "/home/db/DbScriptLinux.sh"] if this is to start the database every time you start the container, as your default command to run, then should be alright, otherwise it would be better to use the RUN command.

Nomad task getting killed

I have two tasks in task group
1) a db task to bring up a db and
2) the app that needs the db to be up.
Both start in parallel and the db tasks takes a lil bit time but by then the app recognizes that db is not up and kills the db task. Any solutions? Please advise.
It's somewhat common to have an entrypoint script that checks if the db is healthy. Here's a script i've used before:
#!/bin/sh
set -e
cmd="$*"
postgres_ready() {
if test -z "${NO_DB}"
then
PGPASSWORD="${RDS_PASSWORD}" psql -h "${RDS_HOSTNAME}" -U "${RDS_USERNAME}" -d "${RDS_DB_NAME}" -c '\l'
return $?
else
echo "NO_DB Postgres will pretend to be up"
return 0
fi
}
until postgres_ready
do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - continuing..."
exec "${cmd}"
You could save it as entrypoint.sh and run it with your application start script as the argument. eg: entrypoint.sh python main.py

Warning: Return code of 127 for check of service was out of bounds. Make sure the plugin you're trying to run actually exists

I'm trying to use the phanton-js plugin for Nagios:
https://github.com/hggh/phantomjs-nagios
but despite me double checking the set-up, I'm getting
"Return code 127 is out of bounds : Plugin may be missing"
The plugin file exists in the plugins folder:
# stat /usr/lib64/nagios/plugins/check_http_load_time.rb
File: `/usr/lib64/nagios/plugins/check_http_load_time.rb'
Size: 9108 Blocks: 24 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 275201 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
I can execute it when su'd to the nagios user locally:
-bash-4.1$ whoami
nagios
-bash-4.1$ /usr/lib64/nagios/plugins/phantomjs-nagios/check_http_load_time.rb -u https://google.com -w 2 -c 3
OK: https://google.com load time: 0.43
This is how it is defined in the checkcommands.cfg file:
define command {
command_name check_web_page_load_time
command_line $USER1$/check_http_load_time.rb -u $ARG1$ -w $ARG2$ -c $ARG3$
}
and this is the service definition
define service {
use generic-service
host_name test_host
service_description https://google.com web load time
check_command check_web_page_load_time!https://google.com!2!3
contact_groups support-emails
}
I've restarted nagios and confirmed that the config check passes.
Am I missing something obvious?
edit:
plugin file is located in 2 folders:
/usr/lib64/nagios/plugins/check_http_load_time.rb
and
/usr/lib64/nagios/plugins/phantomjs-nagios/check_http_load_time.rb
both files are identical, both can be executed by the nagios user successfully and neither location works with command definition. I tried calling it with full path and with the $USR1$ variable, which points to the standard location where all the other plugins are located
/usr/lib64/nagios/plugins/
Manged to eventually solve it. The error message was not very helpful, as problem was with the environment variables of the nagios user.
The original script had the following shebang:
#!/usr/bin/env ruby
After changing it to the actual location of ruby binary the script works:
#!/usr/local/rvm/rubies/ruby-2.5.1/bin/ruby
Still surprised that it worked when su'd to the Nagios user.

How do i access the env of Capistrano 3?

I am executing the following command:
cap staging assets:precompile
How do I access the variable env in my Capistrano deploy.rb?
where env is equal to 'staging' in this case?
You can use fetch(:stage) or #{stage} in a string for example like this:
task :sunspot_stop do
run "cd #{current_path} && bundle exec rake sunspot:solr:stop RAILS_ENV=#{stage}"
end

Resources