How to change rvm gemset over ssh on os x server

2019-09-16 18:31发布

问题:

ok

I don't know how to change rvm version over ssh under os x server.

What I do:

  1. Login on server over ssh
  2. run script (below) and catch error
  3. Error: 'rvm is not a funciton, many-many-words'

What I have as script:

use File::Spec;
my $server_directory = File::Spec->catfile($ENV{HOME},'MyProject');

my $exec_file = File::Spec->catfile($server_directory,'run_script.rb');

my $run_ruby_script = qq'bundle exec ruby $exec_file'.' '.join(' ',@ARGV);

# reload bash profile                                                                                                                                                             
print qx(source $ENV{HOME}/.bash_profile);                                                                                                                                       

print qx(source $ENV{HOME}/.bashrc);                                                                                                                                             

# reload ruby                                                                                                                                                          
print qx(source $ENV{HOME}/.rvm/scripts/rvm);                                                                                                                                    

my $ruby_setup = qq([[ -s "$ENV{HOME}/.rvm/scripts/rvm" ]] && source "$ENV{HOME}/.rvm/scripts/rvm");

print $ruby_setup. "\n";

# change directory                                                                                                                                                                
chdir($server_directory);

# configure gemset name                                                                                                                                                           
my $version = qx(cat .ruby-version);
chomp($version);
my $gemset = qx(cat .ruby-gemset);
chomp($gemset);


my $change_rvm_gemset = qq(rvm use $version\@$gemset);
print qx($ruby_setup && $change_rvm_gemset);

print qx(rvm current);

回答1:

Ok, after all.

def exec_via_bash(line)
    puts %Q(#{line})
    exec = 'bash -c "#{line}"'
    puts `#{exec}`
end

def RubySetup
    # reload bash profile
    homedir = ENV['HOME']
    exec_via_bash %Q(source #{homedir}/.bash_profile);

    exec_via_bash %Q(source #{homedir}/.bashrc);

    # reload ruby
    exec_via_bash %Q(source #{homedir}/.rvm/scripts/rvm);

    ruby_setup = %Q([[ -s "#{homedir}/.rvm/scripts/rvm" ]] && source "#{homedir}/.rvm/scripts/rvm")

    puts ruby_setup
    ruby_setup
end

if ARGV.empty?
    puts "there is not enough arguments passed. maybe you forget ruby file to exec?"
    exit(1)
end

ruby_script_path = ARGV.shift;

exec_file_absolute_path = File.expand_path(ruby_script_path)

unless File.exists? exec_file_absolute_path
    puts "file #{exec_file_absolute_path} doesn't exists!"
    exit(1)
end

exec_file_directory = File.dirname(exec_file_absolute_path)
exec_bundle = %Q'bundle exec ruby #{exec_file_absolute_path}' + ' ' + ARGV.join(' ')



# change directory
Dir.chdir(exec_file_directory);
# print %x(ls);

# configure gemset name
version = %x(cat .ruby-version).strip;
gemset = %x(cat .ruby-gemset).strip;
change_rvm_gemset = %Q(rvm use #{version}\@#{gemset});

ruby_setup = RubySetup()

exec_bash_login_line = [ruby_setup, change_rvm_gemset, exec_bundle].join ' && ';
puts 'exec bash login line: ' + exec_bash_login_line
forced = %Q(bash --login -c '#{exec_bash_login_line}');
puts forced, "\n";
puts %x(#{forced});

ok, this script is not a kind of beauty, but it works well.

Example of usage?

ruby script.rb ~/bla/bla/bla/run_your_program.rb --first_argument --second_argument a,b,c --etc

As I said before:

I've already on the server via ssh.

So, I need to run scripts via launchd.

And I should do it with

# part of launchd worker
<string>bash</string>
<string>-c</string>
<string>ruby ~/PathToCharmScript.rb -r a</string>

P.S:

Please, help me with improvements of this script for others!

Here is a gist: wow_this_works



标签: ruby ssh rvm