Accessing environment variable inside the postinst script of the debian package
Asked Answered
K

3

6

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?

Kelleekelleher answered 16/12, 2015 at 0:59 Comment(6)
Environment variables are inherited (copied) from parent to child. So, in the parent, i.e. whatever runs the script.Jackboot
@cdarke, when I deploy the debian, still I am unable to access the env variable. I am setting them in the bashrc file.Kelleekelleher
Are you sure the .bashrc file is being executed? It isn't normally executed for scripts, and not executed if bash is invoked as sh.Jackboot
yes the .bashrc file is executed. When I run the postinst like a normal shell script I am able to access the env variables in the .bashrc but through debian I am not able to access the environment variables.Kelleekelleher
You posted this in the bash tag, but postinst scripts will need to be able to run under sh.Supersede
@cdarke, ...if the package manager doesn't enforce a preset environment at install time -- which would be a very usual thing to do. (Part of the point of packages being consistent behavior, after all).Jeanett
S
4

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.

Supersede answered 21/12, 2015 at 5:13 Comment(0)
D
2

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.

Depict answered 8/2, 2017 at 3:1 Comment(0)
K
1

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.

Kelleekelleher answered 8/8, 2018 at 16:4 Comment(1)
A properly built package should run correctly regardless of how it's invoked. For example, the Debian installer should be able to install it when a system is first set up.Supersede

© 2022 - 2024 — McMap. All rights reserved.