forked from tomeichlersmith/denv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_denv_entrypoint
executable file
·111 lines (100 loc) · 3.79 KB
/
_denv_entrypoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the denv project: https://github.com/tomeichlersmith/denv
#
# Copyright (C) 2021 denv contributors
#
# denv is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# denv is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with denv; if not, see <http://www.gnu.org/licenses/>.
# POSIX
set -o errexit
set -o nounset
# Assumptions on the configuration of the container running this script
# 1. The denv workspace is mounted to the home directory and HOME is defined
# 2. Some other denv environment variables are defined
# DENV_RUNNER, DENV_IMAGE, DENV_NAME
# 3. Optional environment variables should be defined for the container
# if they are defined for denv (e.g. DENV_DEBUG, DENV_INFO)
if [ -n "${DENV_DEBUG+x}" ]; then
set -o xtrace
fi
if [ -n "${DENV_INFO+x}" ]; then
_denv_info() {
printf "\033[32;1m INFO: \033[0m\033[32m%s\n" "$1"
shift
while [ "$#" -gt "0" ]; do
printf ' %s\n' "$1"
shift
done
printf "\033[0m"
}
else
_denv_info() {
:
}
fi
# print each argument on its own line with the first line
# prefixed with "ERROR: ".
_denv_error() {
printf >&2 "\033[1;31mERROR: \033[0m\033[31m%s\n" "$1"
shift
while [ "$#" -gt "0" ]; do
printf >&2 " %s\n" "$1"
shift
done
printf >&2 "\033[0m"
}
# we have some variables that necessarily are defined outside this script
# shellcheck disable=SC2154
if [ -z "${DENV_RUNNER+x}" ] || [ -z "${DENV_IMAGE+x}" ] || [ -z "${DENV_NAME+x}" ] || [ -z "${HOME+x}" ]; then
_denv_error "The necessary environment variables have not been passed into the runner." \
"_denv_entrypoint should only be run within a container launced by denv."
exit 1
fi
if [ -d "${HOME}/.denv" ] && [ ! -f "${HOME}/.denv/skel-init" ]; then
# If we do not have profile files in the home, we should copy the
# skeleton files, if present.
# Ensure we copy only if the dotfile is not already present.
if [ -d "/etc/skel" ]; then
skel_files="$(find /etc/skel/ -type f || :)"
for skel_file in ${skel_files}; do
base_file_name=$(basename "${skel_file}")
skel_file_path=$(dirname "${skel_file}")
file_path_for_home=${skel_file_path#/etc/skel}
if [ -n "${file_path_for_home}" ] &&
[ ! -d "${HOME}/${file_path_for_home:+"${file_path_for_home}"}" ]; then
mkdir -p "${HOME}/${file_path_for_home:+"${file_path_for_home}"/}"
fi
if [ ! -f "${HOME}/${file_path_for_home:+"${file_path_for_home}"/}${base_file_name}" ] &&
[ ! -L "${HOME}/${file_path_for_home:+"${file_path_for_home}"/}${base_file_name}" ]; then
cp "${skel_file}" "${HOME}/${file_path_for_home:+"${file_path_for_home}"/}${base_file_name}"
fi
done
fi
if [ -f "${HOME}/.bashrc" ]; then
if [ "${DENV_RUNNER}" = "singularity" ] || [ "${DENV_RUNNER}" = "apptainer" ]; then
# for apptainer/singularity runners
{
echo "# apptainer/singularity set the environment varible PROMPT_COMMAND"
echo "# to overwrite PS1 with a container-specific prompt, we undo that"
echo "# overwwriting here to allow the '.bashrc'-generated prompt to persist"
echo "# https://github.com/tomeichlersmith/denv/issues/3"
echo "unset PROMPT_COMMAND"
} >> "${HOME}/.bashrc"
fi
fi
touch "${HOME}/.denv/skel-init"
fi
# use a login shell to give users an opportunity to update env variables
# via the ~/.profile file if desired
sh -lc "$*"