Initialize rbenv and run ruby script from shell script
I want svnserve to run pre-commit hook, written on ruby. As the svnserve is run as root user, it knows nothing about user rbenv installation.
I set a soft link /usr/bin/ruby -> /home/admin/.rbenv/shims/ruby .
As result, when i try
#!/usr/bin/ruby
puts "Pre-commit hook!"
It shows error:
Transmitting file data .svn: Commit failed (details follow):
svn: Commit blocked by pre-commit hook (exit code 255) with no output.
When i run manually on Server:
admin $ sudo ./pre-commit
/usr/bin/ruby: line 4: exec: rbenv: not found
So, i suppose, that rbenv initialization is needed, but how?
In hooks path:
pre-commit:
#!/bin/bash
export HOME=/home/user
if [ -d $HOME/.rbenv ]; then
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
fi
$0.rb $*
pre-commit.rb:
#!/usr/bin/env ruby
ARGV.each_with_index { |arg, index| puts "Index #{index}: Argument #{arg}" }
you should initialize rbenv before using it.
/path/to/user/home/.rbenv/bin/rbenv init
then set ruby version globally:
rbenv global DESIRED-RUBY-VERSION
or localy:
rbenv local DESIRED-RUBY-VERSION
or per shell:
rbenv shell DESIRED-RUBY-VERSION
though not sure shell setting will work without a tty.
so you could create a shell script, pre-commit.sh and register it as a svn hook.
inside it initialize rbenv and call your pre-commit.rb file
Related
I'm having trouble setting up the rbenv paths
I follow the instructions as specified here: rbenv installation page
I run the command on a zsh terminal:
rbenv init
The terminal gives me the instruction to run
eval "$(rbenv init - zsh)"
I then close the restart the terminal and check to see if all is configured correctly by running:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
and I get the following failure:
Checking for rbenv shims in PATH: not found
If I try open the fils .zshrc I find only the following path written to it
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
but no shims. Can I add the shims manually? I don't know what it should look like? Or is there any reason the command eval "$(rbenv init - zsh)" is not working properly?
Got it working: I need to run echo 'eval "$(rbenv init -)"' >> ~/.zshrc
Using RBENV on 16.04 sudo environment.
How to use RBENV shell $rubyversion or similar solution to run a ruby app from a .desktop file for the menu with predefined ruby version?
EDIT:
Heres how i did it with the solution posted by Ḱathryin:
I added interactive shell options to bash, not sure how exactly it works but it helped.
desktop file:
[Desktop Entry]
Name=app
Encoding=UTF-8
Exec=bash -ic "/path/app.sh;${SHELL:-bash}"
Type=Application
The sh exec which runs the app
echo '#!/bin/bash -i
cd /...../appdir
source ~/.bashrc
eval "$(rbenv init -)"
rbenv shell 2.4.1
./app $*
' > app.sh
sudo ln -s /path/app.sh /usr/local/bin/app
running apps with sudo also appears to work!
For the details of setting up a .desktop file, please see: Creating a .desktop file for a new application.
Since rbenv (when installed properly) is a function and not an executable, it won't be as simple as pointing to the right file. One solution would be to create a shell script that sets up the environment:
#!/usr/bin/env bash # Execute as a bash script
eval "$(rbenv init -)" # Initialize rbenv
ruby $* # Run the ruby script specified the .desktop file
Then make sure the script is executable and put it in your .desktop Exec key:
Exec=/path/to/script/ruby_stub.sh script_you_want_to_run.rb ARGS
I have written a shell script where i wrote one rvm-shell command. The issue is any line after this command will not get executed. The script is as follows:
#!/bin/bash
set x
rvm-shell ruby-1.9.3-p448#global //after this line not anything get executed
echo $? //not get executed
clear //not get executed
rails s //not get executed which i want to run
rvm-shell ruby-1.9.3-p448#global
without any parameters is equivalent to running
rvm ruby-1.9.3-p448#global do bash
which will enter shell session and wait for your input.
what you need is:
rvm use ruby-1.9.3-p448#global
or:
source "$( rvm ruby-1.9.3-p448#global do rvm env --path )"
debugging source:
env_file="$( rvm ruby-1.9.3-p448#global do rvm env --path )"
echo "env_file:$env_file:"
source "$env_file"
or use this script:
#!/usr/bin/env rvm-shell ruby-1.9.3-p448#global
rails s
I installed ruby 2.0 into ~/.rbenv/versions last and now nothing but that is available
$ rbenv versions
system
*ruby-1.9.3-p392 (set by /apps/test_app/.ruby-version)
ruby-2.0.0-p0
$ ruby -v
ruby 2.0.0.p0
$ env | grep PATH
PATH=/home/cbron/.rbenv/shims:/home/cbron/.rbenv/bin
$cat ~/.bash_profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
$rbenv global
ruby-1.9.3-p392
$rbenv local
ruby-1.9.3-p392
$rbenv shell
rbenv: no shell-specific version configured
edit: now set the shell, still nothing.
$rbenv shell
ruby-1.9.3-p392
ruby -v still getting
ruby 2.0.0p0
I already sourced my bash_profile, even restarted the computer.
I had the same issue using zsh and this fixed it:
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv
$ echo 'eval "$(rbenv init -)"' >> ~/.zshenv
$ echo 'source $HOME/.zshenv' >> ~/.zshrc
$ exec $SHELL
So basically moving the lines from profile to env!
Extracted from rbenv readme:
rbenv shell
Sets a shell-specific Ruby version by setting the RBENV_VERSION
environment variable in your shell. This version overrides
application-specific versions and the global version.
$ rbenv shell jruby-1.7.1
When run without a version number, rbenv shell reports the current
value of RBENV_VERSION. You can also unset the shell version:
$ rbenv shell --unset
Note that you'll need rbenv's shell integration enabled (step 3 of the
installation instructions) in order to use this command. If you prefer
not to use shell integration, you may simply set the RBENV_VERSION
variable yourself:
$ export RBENV_VERSION=jruby-1.7.1
So in order to use it you need to specify the ruby version as rbenv shell argument (f.e. rbenv shell 2.0.0.p0, or set RBENV_VERSION (f.e. export RBENV_VERSION=2.0.0.p0)
TL;DR: just reinstall rbenv, unless you are as stubborn as me...
I had messed up permissions in the .rbenv folder, so the shims weren't loading because they didn't have execute permissions
chmod u+x ~/.rbenv/shims/*
Disclaimer
I had deeper permissions problems, rbenv wasn't even working, so I did other chmod u+x ... previously
chmod u+x ~/.rbenv/libexec/*
chmod u+x ~/.rbenv/**/bin/*
Probably easier and safer to just reinstall rbenv. But this was fun!
I'm using ruby 1.8 and have gem https://github.com/delano/rye installed that need to remote ssh to amazon.
require 'rubygems'
require 'rye'
instance = Rye::Box.new('amazone_ip', :user => "ubuntu", :safe => false, :keys => "amazone_key")
I got errors by running those commands:
instance.execute 'rvm list' #bash: rvm: command not found
instance.execute 'ruby -v' #bash: ruby: command not found
It's ok if I run the command instance.execute ls -la.
I would think there is problem with $PATH load, that I have checked it
instance.execute 'echo $PATH'
#[/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games, , 0, ]
#But it's in server:
$echo $PATH
#/home/ubuntu/.rvm/gems/ruby-1.9.2-p290/bin:/home/ubuntu/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/ubuntu/.rvm/rubies/ruby-1.9.2-p290/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
I'll wager you don't have rvm loading because its either not loading your local .bashrc or you don't have the RVM loading line (sourcing line) in your .bashrc, its in your .bash_profile. Just move the RVM line to your .bashrc and then source the .bashrc at the end of your .bash_profile.
If its calling bash in sh-compatible mode, then you can set BASH_ENV in your .profile and point it at your .bashrc which will then load RVM that way.
Try
instance.execute('source ~/.bash_profile && rvm list')
In case someone else finds it useful as this is an old question -
For a non-login shell the user's paths and environment variables are not loaded.
I needed to add this in my .bashrc file right at the very top.
[[ -s "/home/douser/.rvm/scripts/rvm" ]] && source "/home/douser/.rvm/scripts/rvm"