Narayan Periwal
Narayan Periwal

Reputation: 99

Accessing environment variable inside the postinst script of the debian package

I have made a debian package for automating the oozie installation. The postinst script, which is basically a shell script, runs after the package is installed. I want to access the environment variable inside this script. Where should I set the environment variables?

Upvotes: 6

Views: 7586

Answers (3)

tripleee
tripleee

Reputation: 189789

Depending on what you are actually trying to accomplish, the proper way to pass in information to the package script is with a Debconf variable.

Briefly, you add a debian/templates file something like this:

Template: oozie/secret
Type: string
Default: xyzzy
Description: Secret word for teleportation?
 Configure the secret word which allows the player to teleport.

and change your postinst script to something like

#!/bin/sh -e

# Source debconf library.
. /usr/share/debconf/confmodule

db_input medium oozie/secret || true
db_go

# Check their answer.
db_get oozie/secret
instead_of_env=$RET
: do something with the variable

You can preseed the Debconf database with a value for oozie/secret before running the packaging script; then it will not prompt for the value. Simply do something like

debconf-set-selections <<<'oozie oozie/secret string plugh'

to preconfigure it with the value plugh.

See also http://www.fifi.org/doc/debconf-doc/tutorial.html

There is no way to guarantee that the installer runs in a particular environment or that dpkg is invoked by a particular user, or from an environment which can be at all manipulated by the user. Correct packaging requires robustness and predictability in these scenarios; also think about usability.

Upvotes: 4

Narayan Periwal
Narayan Periwal

Reputation: 99

Replying after a long time.

Actually I was deploying the oozie custom debian through dpkg as sudo user. So, to enable access of these environment variable, I had to actually do some changes in the /etc/sudoers file. The change that I made was adding each environment variable name in the file as

Defaults        env_keep += "ENV)VAR_NAME"

and after this I was able to access these variables in the postinst script.

Upvotes: 1

Walter Waldo
Walter Waldo

Reputation: 147

Add this to your postinst script:

#!/bin/sh -e
# ...
pid=$$
while [ -z "$YOUR_EVAR" -a $pid != 1 ]; do
    ppid=`ps -oppid -p$pid|tail -1|awk '{print $1}'`
    env=`strings /proc/$ppid/environ`
    YOUR_EVAR=`echo "$env"|awk -F= '$1 == "YOUR_EVAR" { print $2; }'`
    pid=$ppid
done
# ... Do something with YOUR_EVAR if it was set.

Only export YOUR_EVAR=... before dpkg -i is run.

Not the recommended way but it is compact, simple and is exactly what the PO is asking for.

Upvotes: 2

Related Questions