1
0
mirror of /repos/baseimage-docker.git synced 2026-02-27 17:41:59 +01:00

Store environment variables in a file, and allow init scripts to change environment variables.

Closes GH-23.
This commit is contained in:
Hongli Lai (Phusion)
2014-02-25 12:47:02 +01:00
parent c0e872b3e3
commit 300adc0bf2
4 changed files with 138 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/python2 -u
import os, sys, stat, signal, errno, argparse, time
import os, os.path, sys, stat, signal, errno, argparse, time, json, re, posixfile
KILL_PROCESS_TIMEOUT = 5
KILL_ALL_PROCESSES_TIMEOUT = 5
@@ -54,6 +54,42 @@ def is_exe(path):
except OSError:
return False
def import_envvars():
new_env = {}
for envfile in listdir("/etc/container_environment"):
name = os.path.basename(envfile)
with open("/etc/container_environment/" + envfile, "r") as f:
value = f.read()
new_env[name] = value
os.environ.clear()
for name, value in new_env.items():
os.environ[name] = value
def export_envvars(to_dir = True):
shell_dump = ""
for name, value in os.environ.items():
if to_dir:
with open("/etc/container_environment/" + name, "w") as f:
f.write(value)
shell_dump += "export " + shquote(name) + "=" + shquote(value) + "\n"
with open("/etc/container_environment.sh", "w") as f:
f.write(shell_dump)
with open("/etc/container_environment.json", "w") as f:
f.write(json.dumps(dict(os.environ)))
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
def shquote(s):
"""Return a shell-escaped version of the string *s*."""
if not s:
return "''"
if _find_unsafe(s) is None:
return s
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"
def waitpid_reap_other_children(pid):
done = False
status = None
@@ -101,6 +137,11 @@ def run_command_killable(*argv):
error("%s failed with exit code %d\n" % (filename, status))
sys.exit(1)
def run_command_killable_and_import_envvars(*argv):
run_command_killable(*argv)
import_envvars()
export_envvars(False)
def kill_all_processes(time_limit):
info("Killing all processes...")
try:
@@ -134,12 +175,12 @@ def run_startup_files():
filename = "/etc/my_init.d/" + name
if is_exe(filename):
info("Running %s..." % filename)
run_command_killable(filename)
run_command_killable_and_import_envvars(filename)
# Run /etc/rc.local.
if is_exe("/etc/rc.local"):
info("Running /etc/rc.local...")
run_command_killable("/etc/rc.local")
run_command_killable_and_import_envvars("/etc/rc.local")
def start_runit():
info("Booting runit daemon...")
@@ -172,6 +213,9 @@ def install_insecure_key():
run_command_killable("/usr/sbin/enable_insecure_key")
def main(args):
import_envvars()
export_envvars()
if args.enable_insecure_key:
install_insecure_key()
@@ -212,6 +256,9 @@ def main(args):
parser = argparse.ArgumentParser(description = 'Initialize the system.')
parser.add_argument('main_command', metavar = 'MAIN_COMMAND', type = str, nargs = '*',
help = 'The main command to run. (default: runit)')
parser.add_argument('--enable-insecure-key', dest = 'enable_insecure_key',
action = 'store_const', const = True, default = False,
help = 'Install the insecure SSH key')
parser.add_argument('--skip-startup-files', dest = 'skip_startup_files',
action = 'store_const', const = True, default = False,
help = 'Skip running /etc/my_init.d/* and /etc/rc.local')
@@ -224,9 +271,6 @@ parser.add_argument('--no-kill-all-on-exit', dest = 'kill_all_on_exit',
parser.add_argument('--quiet', dest = 'log_level',
action = 'store_const', const = LOG_LEVEL_WARN, default = LOG_LEVEL_INFO,
help = 'Only print warnings and errors')
parser.add_argument('--enable-insecure-key', dest = 'enable_insecure_key',
action = 'store_const', const = True, default = False,
help = 'Install the insecure SSH key')
args = parser.parse_args()
log_level = args.log_level

View File

@@ -6,6 +6,11 @@ set -x
## Install init process.
cp /build/my_init /sbin/
mkdir -p /etc/my_init.d
mkdir -p /etc/container_environment
touch /etc/container_environment.sh
touch /etc/container_environment.json
chmod 700 /etc/container_environment
chmod 600 /etc/container_environment.sh /etc/container_environment.json
## Install runit.
$minimal_apt_get_install runit