简体   繁体   English

如何在不知道gem是否安装在该服务器上的情况下将简单的ruby脚本移动到另一台服务器?

[英]How to move simple ruby script onto another server without knowing if gem is installed on that server?

I have a simple Ruby script that require Mail gem. 我有一个需要Mail gem的简单Ruby脚本。 It works fine on my local laptop. 在我的本地笔记本电脑上工作正常。 If I want to deploy the script on a server. 如果要在服务器上部署脚本。 What to do to make sure the script can run successfully on the server? 如何确保脚本可以在服务器上成功运行? I'm thinking about "bundle exec ruby sample.rb" but it gives "Could not locate Gemfile or .bundle/ directory error. 我正在考虑“ bundle exec ruby​​ sample.rb”,但它提示“无法找到Gemfile或.bundle /目录错误。

New to programming. 编程新手。 No Rails just simple Rb script 没有Rails只是简单的Rb脚本

You need to install the mail gem on the server. 您需要在服务器上安装邮件gem。 Open your shell and type: gem install nameofgem 打开外壳并输入:gem install nameofgem

Your script should work then. 这样您的脚本应该可以工作了。

I've also seen that you can copy the entire ruby directory to other computers and that works too. 我还看到您可以将整个ruby目录复制到其他计算机,并且也可以。 I'm not sure about newer versions of Ruby, but I have done this in the past. 我不确定较新的Ruby版本,但是我过去已经这样做过。

This is a common problem with scripting languages, how to create a release binary that is self sufficient or at least has a minimal number of pre-installed dependencies. 这是脚本语言的一个常见问题,即如何创建自足的或至少具有最少数量的预安装依赖项的发行二进制文件。

Omnibus package 综合包

The chef guys created the very useful omnibus packager to solve this problem. 厨师们创造了非常有用的多功能包装机来解决这个问题。 This bundles an application with a specific version of ruby and any associated gems. 这会将应用程序与特定版本的红宝石和任何相关的宝石捆绑在一起。

Docker 码头工人

A simpler option might be to deploy your scripts a docker container. 一个更简单的选择可能是将脚本部署到Docker容器。

https://www.docker.com/ https://www.docker.com/

The following is an example building a container using a ruby script: 以下是使用ruby脚本构建容器的示例:

rvm rvm

A final option is to use a tool like rvm on your target machine (Although this is really a tool tor managing development machines). 最后一种选择是在目标计算机上使用rvm之类的工具(尽管这实际上是管理开发计算机的工具)。

$ rvm use ruby-2.1.5@mygemset --ruby-version --create
ruby-2.1.5 - #gemset created /home/mark/.rvm/gems/ruby-2.1.5@mygemset
ruby-2.1.5 - #generating mygemset wrappers..........
Using /home/mark/.rvm/gems/ruby-2.1.5 with gemset mygemset

$ ls -a
.ruby-version
.ruby-gemset  

Rvm enables you to switch between ruby versons and collections of gems called gemsets. 通过Rvm,您可以在Ruby verson和称为gemsets的宝石集合之间切换。

The simplest way without bundler would be to require the gem then rescue if it is not installed. 没有捆绑程序的最简单方法是要求gem,然后在未安装的情况下进行救援。 Note the tick orientation. 注意刻度方向。

begin
  require 'gemname'
rescue LoadError
  `gem install gemname`
end

require 'gemname'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM