Pass variables to the installer on OSX package Installer - macos

I´m trying to pass variables to my post-installation script but seems like isn´t available.
I tried this but doesn´t work:
sudo MY_VAR=VALUE installer -pkg my_package.pkg -target /
I need to access environments variables, without a config file, in post-installation script.
Thanks!

Use export var_name=value won't work in this case. To achieve this you must use launchctl and export your variables by running launchctl setenv var_name value. After that, you can get the value of your variables using launchctl getenv var_name. Here you can see an example:
sh-3.2# launchctl setenv var1 hello
sh-3.2# launchctl setenv var2 world
sh-3.2# launchctl getenv var1
hello
sh-3.2# launchctl getenv var2
world
You can also set multiple variables at once:
sh-3.2# launchctl setenv var3 one var4 line
sh-3.2# launchctl getenv var3
one
sh-3.2# launchctl getenv var4
line
To use this with your package, you can run something like this:
sh-3.2# launchctl setenv var_name value && installer -pkg your_package.pkg -target /

Related

GITHUB_ACTIONS environment variable check in a shell executed with root privileges results in wrong behaviour [duplicate]

When I use any command with sudo the environment variables are not there. For example after setting HTTP_PROXY the command wget works fine without sudo. However if I type sudo wget it says it can't bypass the proxy setting.
First you need to export HTTP_PROXY. Second, you need to read man sudo, and look at the -E flag. This works:
$ export HTTP_PROXY=foof
$ sudo -E bash -c 'echo $HTTP_PROXY'
Here is the quote from the man page:
-E, --preserve-env
Indicates to the security policy that the user wishes to preserve their
existing environment variables. The security policy may return an error
if the user does not have permission to preserve the environment.
The trick is to add environment variables to sudoers file via sudo visudo command and add these lines:
Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"
taken from ArchLinux wiki.
For Ubuntu 14, you need to specify in separate lines as it returns the errors for multi-variable lines:
Defaults env_keep += "http_proxy"
Defaults env_keep += "https_proxy"
Defaults env_keep += "HTTP_PROXY"
Defaults env_keep += "HTTPS_PROXY"
For individual variables you want to make available on a one off basis you can make it part of the command.
sudo http_proxy=$http_proxy wget "http://stackoverflow.com"
You can also combine the two env_keep statements in Ahmed Aswani's answer into a single statement like this:
Defaults env_keep += "http_proxy https_proxy"
You should also consider specifying env_keep for only a single command like this:
Defaults!/bin/[your_command] env_keep += "http_proxy https_proxy"
A simple wrapper function (or in-line for loop)
I came up with a unique solution because:
sudo -E "$#" was leaking variables that was causing problems for my command
sudo VAR1="$VAR1" ... VAR42="$VAR42" "$#" was long and ugly in my case
demo.sh
#!/bin/bash
function sudo_exports(){
eval sudo $(for x in $_EXPORTS; do printf '%q=%q ' "$x" "${!x}"; done;) "$#"
}
# create a test script to call as sudo
echo 'echo Forty-Two is $VAR42' > sudo_test.sh
chmod +x sudo_test.sh
export VAR42="The Answer to the Ultimate Question of Life, The Universe, and Everything."
export _EXPORTS="_EXPORTS VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7 VAR8 VAR9 VAR10 VAR11 VAR12 VAR13 VAR14 VAR15 VAR16 VAR17 VAR18 VAR19 VAR20 VAR21 VAR22 VAR23 VAR24 VAR25 VAR26 VAR27 VAR28 VAR29 VAR30 VAR31 VAR32 VAR33 VAR34 VAR35 VAR36 VAR37 VAR38 VAR39 VAR40 VAR41 VAR42"
# clean function style
sudo_exports ./sudo_test.sh
# or just use the content of the function
eval sudo $(for x in $_EXPORTS; do printf '%q=%q ' "$x" "${!x}"; done;) ./sudo_test.sh
Result
$ ./demo.sh
Forty-Two is The Answer to the Ultimate Question of Life, The Universe, and Everything.
Forty-Two is The Answer to the Ultimate Question of Life, The Universe, and Everything.
How?
This is made possible by a feature of the bash builtin printf. The %q produces a shell quoted string. Unlike the parameter expansion in bash 4.4, this works in bash versions < 4.0
Add code snippets to /etc/sudoers.d
Don't know if this is available in all distros, but in Debian-based distros, there is a line at or near the tail of the /etc/sudoers file that includes the folder /etc/sudoers.d. Herein, one may add code "snippets" that modify sudo's configuration. Specifically, they allow control over all environment variables used in sudo.
As with /etc/sudoers, these "code snippets" should be edited using visudo. You can start by reading the README file, which is also a handy place for keeping any notes you care to make:
$ sudo visudo -f /etc/sudoers.d/README
# files for your snippets may be created/edited like so:
$ sudo visudo -f /etc/sudoers.d/20_mysnippets
Read the "Command Environment" section of 'man 5 sudoers'
Perhaps the most informative documentation on environment configuration in sudo is found in the Command environment section of man 5 sudoers. Here, we learn that a sudoers environment variables that are blocked by default may be "whitelisted" using the env_check or env_keep options; e.g.
Defaults env_keep += "http_proxy HTTP_PROXY"
Defaults env_keep += "https_proxy HTTPS_PROXY"
Defaults env_keep += "ftp_proxy FTP_PROXY"
And so, in the OP's case, we may "pass" the sudoer's environment variables as follows:
$ sudo visudo -f /etc/sudoers.d/10_myenvwlist
# opens the default editor for entry of the following lines:
Defaults env_keep += "http_proxy HTTP_PROXY"
Defaults env_keep += "https_proxy HTTPS_PROXY"
# and any others deemed useful/necessary
# Save the file, close the editor, and you are done!
Get your bearings from '# sudo -V'
The OP presumably discovered the missing environment variable in sudo by trial-and-error. However, it is possible to be proactive: A listing of all environment variables, and their allowed or denied status is available (and unique to each host) from the root prompt as follows:
# sudo -V
...
Environment variables to check for safety:
...
Environment variables to remove:
...
Environment variables to preserve:
...
Note that once an environment variable is "whitelisted" as above, it will appear in subsequent listings of sudo -V under the "preserve" listing.
If you have the need to keep the environment variables in a script you can put your command in a here document like this. Especially if you have lots of variables to set things look tidy this way.
# prepare a script e.g. for running maven
runmaven=/tmp/runmaven$$
# create the script with a here document
cat << EOF > $runmaven
#!/bin/bash
# run the maven clean with environment variables set
export ANT_HOME=/usr/share/ant
export MAKEFLAGS=-j4
mvn clean install
EOF
# make the script executable
chmod +x $runmaven
# run it
sudo $runmaven
# remove it or comment out to keep
rm $runmaven

Command not found with sudo, but works without sudo

I've installed a binary dep in my GOPATH at /home/me/go/bin to be used.
Running dep successfully executes the binary, however running sudo dep results in sudo: dep: command not found:
$ dep
Dep is a tool for managing dependencies for Go projects
Usage: "dep [command]"
...
Use "dep help [command]" for more information about a command.
$ sudo dep
sudo: dep: command not found
The paths are not the issue here:
$ echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin
$ sudo echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin
The paths are identical as me and as superuser both referencing the key directory /home/me/go/bin.
Why does running dep without sudo succeed but with sudo results in command not found?
By default, sudo does NOT pass the user's original PATH into the superuser process, and it gets some default PATH defined on the system. That's easy to see if you run "sudo env" to see the entire environment of the sudo'ed process:
$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin
The command you tried, "sudo echo $PATH" doesn't check anything, because the shell first translates the $PATH to whatever value this variable has - and only then calls the command (sudo), so it just prints your outer environment's value :-)
To get your PATH to pass inside sudo, you can do something like this:
$ sudo PATH=$PATH sh -c env | grep PATH
PATH=/usr/share/Modules/bin:/usr/lib64/ccache:/home/nyh/gaps:/home/nyh/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/sbin:/sbin:/usr/games:/usr/local/android-sdk-linux/tools:/usr/local/android-sdk-linux/platform-tools:/home/nyh/google-cloud-sdk/bin
Basically the command I passed for sudo to run starts by setting PATH to $PATH (remember that $PATH is expanded by the outer shell, before sudo ever runs, so is the real path I want!) and running a shell (which will use this new PAT) to "env". As you can see, env did get the right path. You can replace "env" by whatever program you wanted to run.

Error open: command not found during command script in mac

I built a new script that set env and open intellij:
#! /bin/bash
launchctl setenv USERNAM ttt, PASSWORD
1234
#! /bin/bash

open -a "IntelliJ IDEA"
when i ran it on terminal separately it works but when i run on my script im getting this error "open: command not found"
i'm using mac osx
thanks
solution:
it was actually an extra space or end of line I made by mistake...
I'm not sure what launchctl setenv ... does, and how it affects your current shell, but I think your script can be more simply written:
#! /bin/bash
export USERNAM="ttt"
export PASSWORD="1234"
open -a "IntelliJ IDEA"
or
#! /bin/bash
env USERNAM="ttt" PASSWORD="1234" open -a "IntelliJ IDEA"

Script to take a backup of all git repo's and set a cronjob using the same script

I Want to set a script and use a cronjob to take a backup of all the repositories.
FYI...
RVM Version: 1.20.13, Ruby Version: 1.9.3p429, Gem Version: 1.8.25, Bundler Version:1.3.5, Rake Version: 10.0.4, GitLab information Version: 5.3.0
I Tried the methods below, but they didn't work. Please help me to set the required cronjob. When I execute the commands manually they were working fine.
Method 1:
#!/bin/bash
cd /home/git/gitlab/
bundle exec rake gitlab:backup:create RAILS_ENV=production
Error:
bundle: command not found
Method 2:
#!/bin/sh
cd /home/git/gitlab/
bundle exec rake gitlab:backup:create RAILS_ENV=production
Error:
/bin/sh: bundle: command not found
Method 3:
10 10 * * * cd /home/git/gitlab && PATH=$PATH:$/home/git/.rvm/gems/ruby-1.9.3-p429/bin:/home/git/.rvm/gems/ruby-1.9.3-p429#global/bin:/home/git/.rvm/rubies/ruby-1.9.3-p429/bin:/home/git/.rvm/bin::/home/git/.rvm/rubies/ruby-1.9.3-p429/.irbrc bundle exec rake gitlab:backup:create RAILS_ENV=production CRON=1 >> /tmp/git_bck.log 2>&1
Error:
/home/git/.rvm/gems/ruby-1.9.3-p429#global/bin/ruby_noexec_wrapper:7:in `require': no such file to load -- rubygems (LoadError) from /home/git/.rvm/gems/ruby-1.9.3-p429#global/bin/ruby_noexec_wrapper:7
you need to do 2 things:
1) recreate the same environment you have in the command line. For this run env in the command line and after that run env in a cron and output to a shell, compare the 2 of them
2) source rvm in (look in your .bashrc or whatever rc your shell has).
These 2 things should allow you to run the script from the cron. You can place them in the beging of the script.
Problem solved :
In .bashrc , added
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
export GIT_HOME=/home/git/git
export JAVA_HOME=/home/git/jdk
export PATH=$PATH:$GIT_HOME/bin:$JAVA_HOME/bin
source /home/git/.rvm/environments/default
In .bash_profile, added
PATH=$PATH:$HOME/bin
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
FYI..
cat /home/git/.rvm/environments/default
export PATH ; PATH="/home/git/.rvm/gems/ruby-1.9.3-p429/bin:/home/git/.rvm/gems/ruby-1.9.3-p429#global/bin:/home/git/.rvm/rubies/ruby-1.9.3-p429/bin:/home/git/.rvm/bin:$PATH"
export rvm_env_string ; rvm_env_string='ruby-1.9.3-p429'
export rvm_path ; rvm_path='/home/git/.rvm'
export rvm_ruby_string ; rvm_ruby_string='ruby-1.9.3-p429'
unset rvm_gemset_name
export RUBY_VERSION ; RUBY_VERSION='ruby-1.9.3-p429'
export GEM_HOME ; GEM_HOME='/home/git/.rvm/gems/ruby-1.9.3-p429'
export GEM_PATH ; GEM_PATH='/home/git/.rvm/gems/ruby-1.9.3-p429:/home/git/.rvm/gems/ruby-1.9.3-p429#global'
export MY_RUBY_HOME ; MY_RUBY_HOME='/home/git/.rvm/rubies/ruby-1.9.3-p429'
export IRBRC ; IRBRC='/home/git/.rvm/rubies/ruby-1.9.3-p429/.irbrc'
unset MAGLEV_HOME
unset RBXOPT
In crontab script :
cd /home/git/gitlab/
source $HOME/.bash_profile
source $HOME/.bashrc
bundle exec rake gitlab:backup:create RAILS_ENV=production

Understanding rubygems 'require' in sudo vs rvmsudo

I'm using Ubuntu 12.04.4 TLS
I wrote a simple shell script in /home/abdo/sample_serv.sh that executes a Ruby file:
#!/bin/bash
/home/abdo/.rvm/rubies/ruby-2.1.0/bin/ruby /home/abdo/sample_serv.rb
and /home/abdo/sample_serv.rb containing the code below:
puts $:
require 'sinatra'
set :port, 8084
get '/' do
%{ <html><body>Hello from Abdo</body></html> }
end
Executing rvmsudo ./home/abdo/sample_serv.sh works just fine but I would like to get sudo ./home/abdo/sample_serv.sh to work by passing the necessary environment variables because I am having issues with upstart.
The issue arises in my /etc/init/foo.conf
description "webserver test"
start on runlevel [23]
stop on shutdown
pre-start script
exec >> /var/log/unicorn_test.log 2>&1
echo starting
end script
script
exec >> /var/log/unicorn_test.log 2>&1
/bin/bash /home/abdo/.rvm/bin/rvmsudo /home/abdo/sample_serv.sh
echo started
end script
When the line /bin/bash /home/abdo/.rvm/bin/rvmsudo /home/abdo/sample_serv.sh is reached, I get
/home/abdo/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
`require': cannot load such file -- sinatra (LoadError)
I was able to get the same ruby -v as that of my user but it looks like the GEM_PATH (even if I set it inside the upstart config file), is not helping.
Basically, if I can understand how (and where -- gem env is not helping since gem is not a command when I do sudo) rubygems looks for a file being required, I should be able to move forward.
When you run under sudo you run in a different environment, where gem is not installed, the $PATH is different, so things may not run smoothly.
You may want to try this:
Changing sudo's strict defaults
There are 3 things needed to mitigate this situation if you encounter
it:
the user that is invoking sudo must have export rvmsudo_secure_path=0 set on his shell environment (think .bashrc,
.bash_profile or .zshrc)
comment out Defaults secure_path=... on /etc/sudoers
add Defaults env_keep +="rvm_bin_path GEM_HOME IRBRC MY_RUBY_HOME rvm_path rvm_prefix rvm_version GEM_PATH rvmsudo_secure_path
RUBY_VERSION rvm_ruby_string rvm_delete_flag" to /etc/sudoers in
rare cases it is required to add more variables - they should be
reported by first run of rvmsudo.
After these changes, you should be able to use rvmsudo preserving
the same password/no-password directives as "normal" sudo calls.
Edit
If you don't want to change the defaults, you can try to synchronize the values of the environment values stated above (rvm_bin_path GEM_HOME IRBRC MY_RUBY_HOME rvm_path rvm_prefix rvm_version GEM_PATH rvmsudo_secure_path RUBY_VERSION rvm_ruby_string rvm_delete_flag) to your root user.
If that doesn't work, you can try installing rvm as root, and using that environment (instead of /home/abdo/.rvm/bin/rvm) to run your code.

Resources