How to execute Ruby Script from LAMP Web Application - ruby

I have installed rvm (mutiuser option) and ruby using "rvm install" on my test server running Ubuntu.
If I log in and execute "ruby test.rb", the script runs correctly.
But If I execute it using "exec" from my php code, I get an error.
If I "su www-data" and try to execute the script then I get the same error, so I guess the question is how to get "www-data" to have the correct environment to execute Ruby scripts. I've already tried adding "www-data" to "rvm" group and doesn't work. If I give the full path to Ruby bin, then the script executes, but fails when requiring gems.
I've also tried executing a .sh where the fist line is "rvm use ruby-xxx" and the second line is "ruby test.rb" and doesn't work either.
Should I install Ruby using "apt-get install"?
Thanks a lot.

Hm, I don't know about multi-user version, but if you were running a single user version, you could run scripts as that user, like this:
/bin/su - postgres -c "/path/to/backup_db.sh /tmp/test"
See this question and answers.

Related

'--enable-local-plugin-development' error- What is the fix for production

I have a ruby script that requires 3rd party gems, I'm using in the filter section of logstash built out with docker. I'm using the logstash base image and installing the ruby gems in the docker file .
eg
FROM .../logstash:7.16.2
RUN /usr/share/logstash/bin/ruby -S gem install *gem*
RUN echo "gem '*gem*'" >> /usr/share/logstash/Gemfil
This is the only solution I could find in order to utilize the gems in my ruby script thats being called in the filter of logstash.
From here if I run my image, I will get an error -
[Fatal] Logstash-Logstash was unable to start due to an unexpected Gemfile change.
If you are a user this is a bug.
If you are a logstash developer, please try restarting logstash with the '--enable-local-plugin-development' flag set.
If I append the noted flag to my run command I am able to run locally.
eg
docker run my-image-build --enable-local-plugin-development
But this does not work when I deploy to dev/prod.
I will receive the same error ([FATAL]) in the pod logs.
The noted flag is meant for development purposes
https://www.elastic.co/guide/en/logstash/current/running-logstash-command-line.html
and I don't know of a way to append the flag to the docker run command in dev/prod if I could use it.
What is the fix for this so I can use the script in dev/prod?
Have I attempted to add the gems in a way that doesn't work?
This was an additional question to get clarity with an issue I was facing -- this issue is resolved by this answer.
How to use a gem in logstash pipeline ruby filter
Specifically the docker file is where I'm installing ruby gems for use in logstash filter ruby script.
I have to add a final line at the end of the docker file after installing gems and adding them to the gemfile..
The docker file looks like this.
FROM .../logstash:7.16.2
RUN /usr/share/logstash/bin/ruby -S gem install *gem*
RUN echo "gem '*gem*'" >> /usr/share/logstash/Gemfile
RUN /usr/share/logstash/bin/logstash-plugin update logstash-filter-grok

Optimal way to install ruby on Docker where base-OS access is required?

I'm trying to install Ruby on Docker (no Rails), but I'm having some issues. I initially tried with RVM, but I had issues with it; after I'd installed it in the usual way, commands such as ruby or gem install aren't recognised, and I understand that RVM is not best practice for a docker environment. I tried building from binary, but that seemed to be missing so many essential things, it seemed to be an exercise in futility.
I've now tried using the official docker ruby:2.5.1 image, however when I attach to this, I get an irb prompt, and am unable to use operating system commands, such as apt-get due to this.
It's essential that I have operating system access - this script will be using a browser through headless Watir webdriver, attaching to Geckodriver, so there are a number of dependencies required that won't be included in the base ruby install.
What's the best way to handle this with Docker?
This will get you on the command-line of the Ruby box:
docker run -it ruby:2.5.1 bash
You'll now be able to run ruby tools as normal, e.g. ruby, irb, gem. As well as regular Debian commands including apt-get.
Suggestion:
If you want to roam around inside a separated environment, you should choose something like Vagrant.
If you are intended to use docker, give a try this approach.
You can place any code in your ruby file whichever you would like to.
$ docker run -it -v $(pwd)/:/data ruby:2.5 ruby -- /data/hello.rb
hello world!

pod install: command not found when called from bash script

I'm improving the continuos integration of a project. And we decided to take an extra step and start using cocoapods. All the rvm installation is legacy and indeed I have a lot of troubles installing ruby 2.2.0. The thing is that, when I test my build script using terminal it works fine, but when I try to run them without opening a terminal window (called from applescript, jenkins or another ruby script). The command is not found.
Already tried adding the path to .rvm/scripts to the PATH variable in both .bashrc and .bash_profile
Have you try to reconnect the server after you installed the cocoapods? sometimes it doesn't see the new vars till it disconnected and reconnected.
Also make suer that the vars that you see through the terminal are available for jenkins user. you can check that through the slave "Script Console"
If it still don't work, try to set the path in the "execute shell", just before you run the pod install.
This is how it works for me:
echo "Running pod install"
cd ${WORKSPACE}
export LANG=en_US.UTF-8
pod install

Rake task has wrong environment within cron job on Openshift

I'm trying to setup a cron job on Openshift due to import emails in a Redmine application. Therefore, I prepared a minutely script like this:
#!/bin/bash
rake RAILS_ENV=production -f ${OPENSHIFT_REPO_DIR}/Rakefile redmine:email:receive_imap host=imap.googlemail.com port=993 ssl=1 username=xxx#artistii.com password=yyy ...
It runs without problems when launched by hand on a ssh connection. When run by cron, instead, rake could not be found.
Making some debugging, I found that the path is not the same as the login shell; and even if I used a full path for rake, ruby that is found is version 1.8 (not 1.9 as per the cartridge), and whenever I set the very same path as the shell, then libruby-1.9 is not found.
Following some other advice I tried to add the following line in place of setting a custom PATH:
source /usr/bin/rhcsh
but nevertheless rake is still not found. I also tries to use bundle exec.
What is the right way to set an environment for cron on Openshift so that it can run like a login shell?
You may need to cd to the directory where your bundle is installed first (where your Gemfile is) something like this maybe?
cd $OPENSHIFT_REPO_DIR && bundle exec rake .....
This is a bug in the cron cartridge. You can refer to this question in SO. It is actually a question with the Python cartridge and the cron cartridge. But it is the cron cartridge which will affect all. There is also a OpenShift Bug Report mentioned within.
The bug is as you have observed, the cron cartridge uses Ruby 1.8 instead of Ruby 1.9. Thus, the gems installed with Ruby 1.9 are not available to the cron cartridge using Ruby 1.8.
There is already a bugfix for this bug, you can refer to the OpenShift Bug Report. But not too sure if it is pushed out already.
Meanwhile, there is a temporary workaround, by exporting the PATH and LD_LIBRARY_PATH in the cron script. You can refer to the OpenShift Bug Report.
Hope this helps.
If you are using rvm, openshift may getting some problem to shift to default rvm.You can also try something like this so it will set rvm to default before running bundle and can also generate your cron log as well to get the exact status of your cron job:
https://rvm.io/rvm/install
use bundle exec to get rid from more than one version of rake
cd $OPENSHIFT_REPO_DIR && rvm gemset use "yourgemsetname" && RAILS_ENV=production bundle exec rake cron_job:cron_job --silent >> log/cron_log

sudoers file for rvmsudo passenger-install-apache2-module

BACKGROUND:
I have an application that is freshly deployed every day -- a VM is created, and then root for that system creates a "systemuser", which then installs the application.
It's running:
CentOS 6
RVM
Ruby 1.8.7 on Rails
passenger
The way it's set up, I have to have Ruby on Rails install itself, and install passenger. To this end the user running the service has sudo powers, but the intention is to only allow the commands needed during installation. For security and maintainability reasons we cannot put the sudo password in the script.
my /etc/sudoers includes:
systemuser ALL = NOPASSWD: /usr/bin/env, /bin/bash, /usr/local/rvm/rubies/ruby-1.8.7-p358/bin/gem, /bin/cp, /bin/ln, /bin/mv, /bin/rm, /etc/init.d/httpd *
THE PROBLEM:
Until recently a variant of this worked great. However recently something changed somewhere (?) and the rvmsudo command no longer executes without a password prompt.
$ rvmsudo passenger-install-apache2-module
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for systemuser:
THE QUESTION:
What is the minimal set of commands that should be in /etc/sudoers to install rvm, ruby, bundler/gem, and then passenger2 on apache?
Yes, I've read the "documentation" at https://rvm.io/integration/passenger/
Creative solutions welcomed! I realize one may say this looks like a pretty hacky system, and I'd agree with you. But at the moment it's hard for me to improve it without understanding what commands are being run by this passenger install script.
Removing "env" and "bash" from the front and adding "rvm" gets a little farther:
systemuser ALL = NOPASSWD: /usr/local/rvm/bin/rvmsudo, /usr/local/rvm/gems/ruby-1.8.7-p358/bin/passenger-install-apache2-module, /bin/cp, /bin/ln, /bin/mv, /bin/rm, /etc/init.d/httpd *
running this:
$ rvmsudo passenger-install-apache2-module
[sudo] password for systemuser:
Thanks for your attention!
/usr/bin/env is a router allowing to run any command, the same for /bin/bash, you would have to add which passenger-install-apache2-module instead of them.

Resources