1

I am using gem net-ssh.

The connection is established. I am able to run some simple scripts(like "pwd", "ls", etc.) successfully, but when I run the "restart" script I get an error.

Here is a snippet:

output = ssh.exec!("cd /home/csrhub/git/csrhub-api; ./bin/restart")
puts "#{output}"

And the output:

./bin/restart:7:in exec': No such file or directory - bundle (Errno::ENOENT) from ./bin/restart:7:in' Environment determined as "development" based on the .environment file. Loading application.yml

And here is the script where the error occurs.

#!/usr/bin/env ruby

require 'yaml'
require_relative '../app/models/environment.rb'
config = Environment.config('application')

exec "bundle exec pumactl --control-url tcp://127.0.0.1:#{config['control_port']} --control-token #{config['control_token']} phased-restart"

Connecting via Putty, when I run the same command it gets executed without any problems.

EDIT

The PATH variable is different when I Putty:

/home/csrhub/.rbenv/shims:/home/csrhub/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

And when I run with net-ssh PATH is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Hairi
  • 3,318
  • 2
  • 29
  • 68
  • Don't `cd` -- Just run `/home/csrhub/git/csrhub-api/bin/restart` – Zak Jul 23 '19 at 16:23
  • @Zak Same, still doesn't work. I get the same error :( – Hairi Jul 23 '19 at 16:25
  • Check what is in the `.bashrc` on your server, it might have something ruby-specific. Replicate that to `.bash_profile`. There's a difference between running interactively and non-interactively. – phil pirozhkov Jul 23 '19 at 17:10
  • @philpirozhkov https://docs.google.com/document/d/1NVhu3Wju6m4LpB_0EUwH9Gp7wnIbkXABq_DonR4oBvM/edit?usp=sharing https://docs.google.com/document/d/1I0rZw38WQBERqBIOGHbIK-za5bQB19h64MEjmH2vr1A/edit?usp=sharing – Hairi Jul 23 '19 at 17:50
  • Seems like a `PATH` issue. The `PATH` for your `ssh-net` shell session is not the same as for your Putty session. This may be the reason the `bundle` command can not be found. – Casper Jul 23 '19 at 17:54
  • @Casper Yes, you are right. But where does this difference come from and how to fix it? – Hairi Jul 23 '19 at 17:58
  • This should get you running again: https://stackoverflow.com/questions/6854055/net-ssh-and-remote-environment – Casper Jul 23 '19 at 17:59
  • @Casper Still don't know how to load the missing paths to the PATH var. And how putty somehow loads it. – Hairi Jul 23 '19 at 18:23

1 Answers1

2

The problem is in your ssh.exec! call. Net::SSH does not use a login shell to run your commands. A login shell is required for Bash to execute .bash_profile.

Since your rbenv setup is in .bash_profile, your exec! call will be missing your full Ruby environment, and therefore running bundle later in you restart script will fail.

The solution should be to manually execute .bash_profile, so that rbenv gets loaded:

output = ssh.exec!("source ~/.bash_profile; ...rest of commands...")

More information and other proposals here:
net-ssh and remote environment

Casper
  • 33,403
  • 4
  • 84
  • 79