How do I set the environment variables that mongoid.yml wants? - nginx

My server is nginx using passenger
This is the environment variable I have been trying to set:
production:
database: <%= ENV['MONGOID_DATABASE'] %>
I've tried setting it in:
/root/.bashrc
/etc/profile
... and I see it when I run $ env, but Passenger I guess doesn't?
I get the following error:
db_name must be a string or symbol
If I set database: my_app, then it works fine.

Try to replace
PassengerRuby /usr/bin/ruby
In your nginx.conf with some .sh script like this
#!/bin/sh
export MONGOID_DATABASE=my_app
exec "/usr/bin/ruby" "$#"

Related

VirtualBox start Script from host

I have a VM started and want to start a script from the host, inside the vm.
My Test Script looks like:
sql /nolog <<!
exit
!
when i start from the host:
VBoxManage --nologo guestcontrol "vmname" run --exe "pathToScript" --username xyz --password xyz
i receive an error: sql command not found. BUT in the VM it works!
Why??
It was because my env variables werent loaded. So you have to load them in your script like:
. .profile #this file contains your env variables
sql /nolog <<!
exit
!

Unable to export env variable from script

I'm currently struggling with running a .sh script I'm trying to trigger from Jenkins.
Within the Jenkins "execute shell" section, I'm connecting to a remote server (The Jenkins agent does not have right OS to build what I need.), using:
cp -r . /to/shared/drive/to/have/access/on/remote
ssh -t -t username#servername << EOF
cd /to/shared/drive/to/have/access/on/remote
source build.sh dev
exit
EOF
Inside build.sh, I'm exporting R_LIBS to build a package for different R versions.
...
for path in "${!rVersionPaths[#]}"; do
export R_LIBS="${path}"
Rscript -e 'install.packages(c("someDependency", "someOtherDependency"), repos="http://cran.r-project.org");'
...
Setting R_LIBS should functions here like setting lib within install.packages(...). For some reason the R_LIBS export doesn't get picked up. Also setting other env variables like http_proxy are ignored. This causes any requests outside the network to fail.
Is there any particular way of achieving this?
Maybe pass those variables with env, like
env R_LIBS="${path}" Rscript -e 'install.packages(c("someDependency", .....
Well i'm not able to comment on the question, so posting it as answer.
I had similar problem when calling remote shell script from Jenkins, the problem was somehow bash_profile variables were not loaded when called the script from Jenkins but locally it worked. Loading the bash profile in ssh connection solved it for me.
Add source to bash_profile in build.sh
. ~/.bash_profile OR source ~/.bash_profile
Or
Reload bash_profile in ssh connection
`ssh -t -t username#servername << EOF
. ~/.bash_profile
your commands here
exit
EOF
You can set that variable in the same command line like this:
R_LIBS="${path}" Rscript -e \
'install.packages(c("someDependency", "someOtherDependency"), repos="http://cran.r-project.org");'
It's possible to append more variables in this way. Note that this will set those environment variables only for the command being called after them (and its children processes as well).
You said that "R_LIBS export doesn't get picked up". Question Is the value UNSET? Or is it set to some other value & you are trying to override it?
It is possible that SSH may be invoking "/bin/sh -c". Based on the second answer to: Why does 'cd' command not work via SSH?, you can simplify the SSH command and explicitly invoke the build.sh script in Bash:
cp -r . /to/shared/drive/to/have/access/on/remote
ssh -t -t username#servername "cd /to/shared/drive/to/have/access/on/remote && bash -f build.sh dev"
This makes the SSH invocation more similar to invoking the command within a remote interactive shell. (You can avoid sourcing scripts and exporting variables.)
You don't need to export R_LIBSor env R_LIBS when it is possible to prefix any command with local environment variable overrides (agrees with Luis' answer):
...
for path in "${!rVersionPaths[#]}"; do
R_LIBS="${path}" Rscript -e 'install.packages(c("someDependency", "someOtherDependency"), repos="http://cran.r-project.org");'
...
The Rscript may be doing a lot with env vars. You can verify that you are setting the R_LIBS env var by replacing Rscript with the env command and observe the output:
...
for path in "${!rVersionPaths[#]}"; do
R_LIBS="${path}" env
...
According to this manual "Initialization at Start of an R Session", Rscript looks in several places to load "site and user files":
$R_PROFILE
$R_HOME/etc/Renviron
$R_HOME/etc/Renviron.site
$R_ENVIRON_USER
$R_PROFILE_USER
./.Rprofile
$HOME/.Rprofile
./.RData
The "Examples" section of that manual shows this:
## Not run:
## Example ~/.Renviron on Unix
R_LIBS=~/R/library
PAGER=/usr/local/bin/less
If you add the --vanilla command-line option to ignore all of these files, then you may get different results and know something in the site/init/environ files is affecting your R_LIBS! I cannot run this system myself. Hopefully we have given you some areas to investigate.
You probably don't want to source build.sh, just invoke it directly (i.e. remove the source command).
By source-ing the file your script is executed in the SSH shell (likely sh) rather than by bash, which it sounds like is what you intended.

sudoers file NOPASSWD entries ignored?

My sudoers file is seemingly ignored. I appended these two lines via vim (sudo visudo):
theonlygusti ALL=(ALL) NOPASSWD: /usr/sbin/networksetup -setsocksfirewallproxy
theonlygusti ALL=(ALL) NOPASSWD: /usr/sbin/networksetup -setsocksfirewallproxystate
but when I run either command I am prompted to type a password:
$ sudo -n networksetup -setsocksfirewallproxy "Wi-Fi" localhost 3000
sudo: a password is required
The sudoers file only looks for exactly the command you give it. You are trying to run it with extra parameters that are not defined in the sudoers file; That is why you are getting prompted for a password.
Try something like this:
theonlygusti ALL = NOPASSWD : /usr/sbin/networksetup -setsocksfirewallproxy *, /usr/sbin/networksetup -setsocksfirewallproxystate *

PM2 + Meteor not working

I get an error when trying to run it:
~/projects/test-app
/usr/local/bin/meteor:3
# This is the script that we install somewhere in your $PATH (as "meteor")
Here is the command I run:
pm2 start meteor-pm2.json
And here is meteor-pm2.json:
{
"name" : "test-app",
"script" : "/usr/local/bin/meteor",
"MAIL_URL":"smtp://yourmail_configuration_here",
"MONGO_URL":"mongodb://localhost:27017/meteor",
"ROOT_URL":"https://www.mysite.com/",
"PORT":"3000",
"out_file":"/home/josh/logs/app.log",
"error_file":"/home/josh/logs/err.log"
}
I also try this:
cat start
#!/bin/bash
MONGO_URL="mongodb://localhost:27017/meteor"
PORT=3000
ROOT_URL="https://www.mysite.com/"
/usr/local/bin/meteor
and I run it with:
pm2 start ./start -x interpreter bash
and I get:
/usr/local/bin/meteor
^
ReferenceError: usr is not defined
when i modify the bash script by adding the export:
#!/bin/bash
export MONGO_URL="mongodb://localhost:27017/meteor"
export PORT=3000
export ROOT_URL="https://www.mysite.com/"
/usr/local/bin/meteor
I get:
export - SyntaxError: Unexpected reserved word
Any ideas what am I doing wrong?
Is pm2 trying to run the bash script in it's own special script interpreter that doesn't allow the use of export?
I believe this process.json syntax is more correct:
{
"apps": [
{
"name": "myAppName",
"script": "./bundle/main.js",
"log_date_format": "YYYY-MM-DD",
"exec_mode": "fork_mode",
"env": {
"PORT": 3000,
"MONGO_URL": "mongodb://127.0.0.1/meteor",
"ROOT_URL": "https://myapp.example.com/",
"BIND_IP": "127.0.0.1"
}
}
]
}
then I just start it using run.sh which contains:
#!/bin/sh
#
# This shell script starts the actual
# app in the production environtment.
#
pm2 start process.json -i max # Enable load-balancer and cluster features
Note: the BIND_IP env var is there to change it from the default (0.0.0.0). The 0.0.0.0 would make the app accessible around the ssl proxy layer (if you use SSL/TLS with nginx or some other web server and the BIND_IP is set to 0.0.0.0 then pretty much anyone could access it via http://myapp.example.com:3000 around the encrypted layer, unless you block that port in your web server's configuration).
This is how I got my meteor app (Telescope) working
ROOT_URL=http://localhost:3000 PORT=3000 MONGO_URL=mongodb://127.0.0.1:27017/Telescope pm2 start main.js
inside .meteor/local/build
Meteor isn't actually running from /usr/local/bin/meteor, that script is only used for bootstrapping etc, that when done redirects to ~/.meteor/meteor
From /usr/local/bin/meteor:
# All this script does is exec ~/.meteor/meteor. But what if you don't have it
# yet? In that case, it downloads a "bootstrap tarball", which contains the
# latest version of the Meteor tools, and plops it down at ~/.meteor. In fact,
# once you've run this once, you don't even really need this script: you can put
# ~/.meteor/ into your PATH, or a symlink to ~/.meteor/meteor into some other
# PATH directory. No special permissions needed!
So what you need to do is change your script pointer to use meteor in your "Warehouse dir" (~/meteor/meteor)
This means that pm2 expects some syntax and finds another in the start script. In order to direct it to the right syntax, add this to your config.json file:
"interpreter" : "bash"
P.S.: addition of this parameter to the command line didn't work

How to modify PATH for non-interactive SSH call in RHEL 5?

I am trying to modify the PATH variable of my SSH server such at a non-interactive shell command ssh myserver.com 'echo $PATH' returns the desired path. I tried modifying ~/.bashrc and ~/.profile files but they only modify PATH for when I log in to the server interactively, i.e. ssh myserver.com.
Can I change this behavior in RHEL5?
~/.bash_profile is where you want to place these variables. I don't believe that bash uses ~/.profile ( I believe it is used by csh and tcsh )

Resources