5

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?

sth
  • 222,467
  • 53
  • 283
  • 367
Narayan Periwal
  • 89
  • 1
  • 10
  • Environment variables are inherited (copied) from parent to child. So, in the parent, i.e. whatever runs the script. – cdarke Dec 16 '15 at 07:46
  • @cdarke, when I deploy the debian, still I am unable to access the env variable. I am setting them in the bashrc file. – Narayan Periwal Dec 17 '15 at 08:42
  • 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`. – cdarke Dec 17 '15 at 13:26
  • 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. – Narayan Periwal Dec 18 '15 at 06:27
  • You posted this in the `bash` tag, but `postinst` scripts will need to be able to run under `sh`. – tripleee Dec 18 '15 at 16:46
  • @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). – Charles Duffy Dec 21 '15 at 05:18

3 Answers3

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 manipulatel by the user. Correct packaging requires robustness and predictability in these scenarios; also think about usability.

tripleee
  • 175,061
  • 34
  • 275
  • 318
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.

Walter Waldo
  • 129
  • 2
  • 2
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.

Narayan Periwal
  • 89
  • 1
  • 10
  • 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. – tripleee Oct 13 '18 at 07:58