2## @brief System functions
4(( $EC2PINIT_SYSTEM_INCLUDED )) &&
return
5EC2PINIT_SYSTEM_INCLUDED=1
11## System uses DNF package manager
14## System uses YUM package manager
17## System uses APT package manager
20## System is based on Red Hat
23## System is based on Debian
26## System is based on Ubuntu
29## System is based on Arch
35## @fn sys_check_admin()
36## @brief Determine if the current user is root
37## @retval true if root
38## @retval 1 if not root
40 if (( $EUID > 0 )); then
47## @brief Lazily "become" another user
48## @details This sidesteps sudo's environment limitations allowing
49## one to execute scripts on behalf of the named user. Anything modified
50## while ``sys_user_push()`` is active will need to have its ownership and/or
51## octal permissions normalized. If ``name`` does not exist it will be created.
52## @param name the user to become
55 local current=
"$(id -n -u)"
57 _sys_user_old=$current
59 if [ -z
"$HOME" ]; then
60 useradd -m -s /bin/bash
"$name"
68## @brief Restore caller environment after ``sys_user_push()``
70 HOME=
"$_sys_user_home_old"
71 export USER=
"$_sys_user_old"
72 export _sys_user_home_old=
''
73 export _sys_user_old=
''
78## @brief Get system platform (``Linux``, ``Darwin``, etc)
79## @retval platform string
81 local result=$(uname -s)
83 # placeholder - convert platform name to miniconda platform string
91## @brief Get system architecture (``i386``, ``x86_64``, etc)
92## @retval architecture string
94 local result=$(uname -m)
96 # placeholder - convert cpu architecture name to miniconda architecture string
102## @fn sys_user_home()
103## @brief Get account home directory
104## @details This function returns the home directory defined in /etc/passwd unless
105## ``name`` is the caller's account; in which case it will use the value of ``$HOME``.
106## @param user account to inspect
107## @retval home directory path
109 local user=
"${1:-$USER}"
111 if [ -z
"$user" ]; then
115 # short circuit - if the user is the one we're logged in as, return its home variable
116 if [[ $(
id -n -u) ==
"$user" ]]; then
120 getent passwd $user | awk -F:
'{ print $6 }'
123## @fn sys_reset_home_ownership()
124## @brief Resets ownership of a user (after ``sys_user_push()``/``sys_user_pop()``)
125## @param user account to modify
128 local user=
"${1:-$USER}"
130 if [ -z
"$user" ]; then
131 io_error "sys_reset_home_ownership: user name required"
135 home=
"$(getent passwd $user | awk -F: '{ print $6 }')"
136 if [ -z
"$home" ] || (( $(wc -l <<<
"$home") > 1 )) ; then
137 io_error "sys_reset_home_ownership: reset failed"
141 io_info "sys_reset_home_ownership: ${home} will be owned by ${user}"
142 chown -R
"${user}":
"${home}"
145## @fn sys_pkg_get_manager()
146## @brief Get the system package manager
147## @retval result the path to the package manager
156 for manager in
"${managers[@]}";
do
157 local tmp=$(type -p $manager)
158 if [ -x
"$tmp" ]; then
167# Configure package manager globals
169case "$sys_manager_cmd" in
173 sys_manager_cmd_install=
"dnf -y install"
174 sys_manager_cmd_update=
"dnf -y update"
175 sys_manager_cmd_clean=
"dnf clean all"
176 sys_manager_cmd_list=
"rpm -qa"
181 sys_manager_cmd_install=
"yum -y install"
182 sys_manager_cmd_update=
"yum -y update"
183 sys_manager_cmd_clean=
"yum clean all"
184 sys_manager_cmd_list=
"rpm -qa"
188 DEBIAN_FRONTEND=noninteractive
189 sys_manager_cmd_install=
"apt update && apt -y install"
190 sys_manager_cmd_update=
"apt update && apt -y upgrade"
191 sys_manager_cmd_clean=
"apt -y autoremove && apt -y clean"
192 sys_manager_cmd_list=
"apt -qq list"
201 if [ -f
"/etc/redhat-release" ]; then
205 if [ -L
"/etc/dpkg/origins/default" ]; then
206 if [
"$(basename $(readlink -f /etc/dpkg/origins/default))" ==
"debian" ]; then
208 elif [
"$(basename $(readlink -f /etc/dpkg/origins/default))" ==
"ubuntu" ]; then
217 io_info "system: Detected package manager: $sys_manager_cmd"
218 io_info "system: is based on Red Hat? $(( HAVE_REDHAT ))"
219 io_info "system: is based on Debian? $(( HAVE_DEBIAN ))"
220 io_info "system: is based on Ubuntu? $(( HAVE_UBUNTU ))"
223## @fn sys_pkg_install()
224## @brief Install a system package
225## @param ... a variable length list of packages to install
226## @retval 1 if not supported
227## @retval exit_code of system package manager
230## # Install vim and nano
231## sys_pkg_install nano
232## if (( $HAVE_REDHAT )); then
233## sys_pkg_install vim
234## elif (( $HAVE_DEBIAN )); then
235## sys_pkg_install vim-common
238## # Alternative method using an array to dynamically set dependencies
240## (( $HAVE_REDHAT )) && deps+=(vim)
241## (( $HAVE_DEBIAN )) && deps+=(vim-common)
242## sys_pkg_install "${deps[@]}"
246 io_error "sys_pkg_install: unsupported package manager"
249 if ((
"$#" < 1 )); then
250 io_error "sys_pkg_install: at least one package name is required"
253 io_info "sys_pkg_install: Installing $*"
254 bash -c
"$sys_manager_cmd_install $*"
257## @fn sys_pkg_update_all()
258## @brief Update all system packages
259## @retval 1 if not supported
260## @retval exit_code of system package manager
263 io_error "sys_pkg_update_all: unsupported package manager"
266 io_info "sys_pkg_update_all: Updating system packages"
267 bash -c
"$sys_manager_cmd_update"
270## @fn sys_pkg_installed()
271## @brief Test if a system package is installed
272## @param name of a system package
273## @retval 1 if not supported
274## @retval 1 if package is NOT installed
275## @retval 0 if package is installed
280 io_error "sys_pkg_installed: unsupported package manager"
284 if ((
"$#" < 1 )); then
285 io_error "sys_pkg_installed: package name is required"
289 output=
"$($sys_manager_cmd_list $name | tail -n 1)"
290 if (( $HAVE_YUM )) || (( $HAVE_DNF )); then
291 if grep -E
''^$1\..*
'' <<<
"$output" &>/dev/null; then
294 elif (( $HAVE_APT )); then
295 if grep -E
''^$1/.*\\[installed\\]$
'' <<<
"$output" &>/dev/null; then
303## @fn sys_pkg_clean()
304## @brief Clean the system package manager's cache(s)
307 io_error "sys_pkg_clean: unsupported package manager"
310 io_info "sys_pkg_clean: Clearing caches"
311 bash -c
"$sys_manager_cmd_clean"
314## @fn sys_initialize()
315## @brief Install dependencies required by ``ec2_post_init``
316## @retval exit_code value of ``sys_pkg_install()``
319## if ! sys_pkg_initialize; then
320## io_error "Initialization failed!"
322## io_info "Initialization succeeded!"
336 # Handle barren Debian images
337 ((
HAVE_DEBIAN )) && want+=(procps build-essential)
339 # NOTE: Aside from the sys_* functions most operations in ec2_post_init
340 # don
't strictly require root access.
341 io_info "sys_initialize: Administrator check..."
342 if ! sys_check_admin; then
343 io_warn "sys_initialize: SYSTEM FUNCTIONS REQUIRE ROOT ACCESS!"
344 io_warn "sys_initialize: Beyond this point errors thrown by sys_* functions are NOT bugs!"
347 io_info "sys_initialize: User is root!"
350 io_info "sys_initialize: Checking system requirements..."
351 for x in "${want[@]}"; do
352 if ! sys_pkg_installed "$x"; then
353 io_info "sys_initialize: $x marked for installation"
357 if (( ! ${#need[@]} )); then
358 io_info "sys_initialize: No additional packages required"
362 io_info "sys_initialize: Installing packages required by ec2_post_init..."
363 sys_pkg_install ${need[@]}
io_info(...)
Print a message.
io_error(...)
Print an error message.
sys_user_pop()
Restore caller environment after sys_user_push()
Exported String HAVE_UBUNTU
System is based on Ubuntu.
sys_pkg_install(...)
Install a system package.
sys_user_push(name)
Lazily "become" another user.
sys_reset_home_ownership(user)
Resets ownership of a user (after sys_user_push()/sys_user_pop())
sys_check_admin()
Determine if the current user is root.
sys_initialize()
Install dependencies required by ec2_post_init
Exported String HAVE_DNF
System uses DNF package manager.
Exported String HAVE_SUPPORT
System is supported.
sys_pkg_get_manager()
Get the system package manager.
Exported String HAVE_YUM
System uses YUM package manager.
Exported String HAVE_DEBIAN
System is based on Debian.
sys_arch()
Get system architecture (i386, x86_64, etc)
sys_user_home(user)
Get account home directory.
Exported String HAVE_APT
System uses APT package manager.
sys_pkg_installed(name)
Test if a system package is installed.
sys_pkg_clean()
Clean the system package manager's cache(s)
Exported String HAVE_ARCH
System is based on Arch.
sys_pkg_update_all()
Update all system packages.
sys_platform()
Get system platform (Linux, Darwin, etc)
Exported String HAVE_REDHAT
System is based on Red Hat.