diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index d70848278..4eaef2128 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "common-utils", - "version": "2.1.0", + "version": "2.1.1", "name": "Common Utilities", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils", "description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.", diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index d3f7ef267..1c87b0372 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -376,6 +376,15 @@ if [ "${USERNAME}" != "root" ] && [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" EXISTING_NON_ROOT_USER="${USERNAME}" fi +# ********************************* +# ** Ensure config directory ** +# ********************************* +user_config_dir="${user_home}/.config" +if [ ! -d "${user_config_dir}" ]; then + mkdir -p "${user_config_dir}" + chown ${USERNAME}:${group_name} "${user_config_dir}" +fi + # ********************************* # ** Shell customization section ** # ********************************* @@ -428,6 +437,8 @@ fi # Optionally configure zsh and Oh My Zsh! if [ "${INSTALL_ZSH}" = "true" ]; then + umask g-w,o-w + if [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then if [ "${ADJUSTED_ID}" = "rhel" ]; then global_rc_path="/etc/zshrc" @@ -450,57 +461,60 @@ if [ "${INSTALL_ZSH}" = "true" ]; then chsh --shell /bin/zsh ${USERNAME} fi - # Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme. - # See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script. if [ "${INSTALL_OH_MY_ZSH}" = "true" ]; then - user_rc_file="${user_home}/.zshrc" - oh_my_install_dir="${user_home}/.oh-my-zsh" - template_path="${oh_my_install_dir}/templates/zshrc.zsh-template" - if [ ! -d "${oh_my_install_dir}" ]; then - umask g-w,o-w - mkdir -p ${oh_my_install_dir} + # Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme. + # See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script. + omz_source_dirname=".oh-my-zsh" + user_omz_install_dir="${user_home}/${omz_source_dirname}" + omz_added_filesnames=("${omz_source_dirname}") + if [ ! -d "${user_omz_install_dir}" ]; then + mkdir -p ${user_omz_install_dir} git clone --depth=1 \ -c core.eol=lf \ -c core.autocrlf=false \ -c fsck.zeroPaddedFilemode=ignore \ -c fetch.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \ - "https://github.com/ohmyzsh/ohmyzsh" "${oh_my_install_dir}" 2>&1 - + "https://github.com/ohmyzsh/ohmyzsh" "${user_omz_install_dir}" 2>&1 # Shrink git while still enabling updates - cd "${oh_my_install_dir}" - git repack -a -d -f --depth=1 --window=1 + GIT_WORK_TREE="${user_omz_install_dir}" GIT_DIR="${user_omz_install_dir}/.git" git repack\ + -a -d -f --depth=1 --window=1 fi - # Add Dev Containers theme - mkdir -p ${oh_my_install_dir}/custom/themes - cp -f "${FEATURE_DIR}/scripts/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme" - ln -sf "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/codespaces.zsh-theme" + # Add dev containers theme + user_omz_theme_filepath="${user_omz_install_dir}/custom/themes" + user_devcontainer_theme_target="${user_omz_theme_filepath}/devcontainers.zsh-theme" + user_codespaces_theme_target="${user_omz_theme_filepath}/codespaces.zsh-theme" + theme_template_path="${FEATURE_DIR}/scripts/devcontainers.zsh-theme" + mkdir -p "${user_omz_theme_filepath}" + cp -f "${theme_template_path}" "${user_devcontainer_theme_target}" + cp -f "${theme_template_path}" "${user_codespaces_theme_target}" - # Add devcontainer .zshrc template if [ "$INSTALL_OH_MY_ZSH_CONFIG" = "true" ]; then - echo -e "$(cat "${template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file} + # Add devcontainer .zshrc template + omz_rc_filename=".zshrc" + user_rc_file="${user_home}/${omz_rc_filename}" + omz_zshrc_template_path="${user_omz_install_dir}/templates/zshrc.zsh-template" + echo -e "$(cat "${omz_zshrc_template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file} sed -i -e 's/ZSH_THEME=.*/ZSH_THEME="devcontainers"/g' ${user_rc_file} + omz_added_filesnames+=("${omz_rc_filename}") fi + # TODO remove installed files from previous step + + user_omz_filepaths=( "${omz_added_filesnames[@]/#/$user_home/}" ) - # Copy to non-root user if one is specified if [ "${USERNAME}" != "root" ]; then - copy_to_user_files=("${oh_my_install_dir}") - [ -f "$user_rc_file" ] && copy_to_user_files+=("$user_rc_file") - cp -rf "${copy_to_user_files[@]}" /root - chown -R ${USERNAME}:${group_name} "${oh_my_install_dir}" "${user_rc_file}" + # Copy files to alternate user if one is specified + cp -rf "${user_omz_filepaths[@]}" /root + # Set permissions for root user + chown -R root:root "${omz_added_filesnames[@]/#//root/}" fi + + # Set permissions for current user + chown -R "${USERNAME}:${group_name}" "${user_omz_filepaths[@]}" fi fi -# ********************************* -# ** Ensure config directory ** -# ********************************* -user_config_dir="${user_home}/.config" -if [ ! -d "${user_config_dir}" ]; then - mkdir -p "${user_config_dir}" - chown ${USERNAME}:${group_name} "${user_config_dir}" -fi # **************************** # ** Utilities and commands ** diff --git a/test/common-utils/configure_zsh_no_template_first_step.sh b/test/common-utils/configure_zsh_no_template_first_step.sh new file mode 100644 index 000000000..d26874d3a --- /dev/null +++ b/test/common-utils/configure_zsh_no_template_first_step.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Definition specific tests +check "default-zsh-with-no-zshrc" bash -c "[ ! -e ~/.zshrc ]" + +# Report result +reportResults diff --git a/test/common-utils/configure_zsh_no_template.sh b/test/common-utils/configure_zsh_no_template_second_step.sh similarity index 57% rename from test/common-utils/configure_zsh_no_template.sh rename to test/common-utils/configure_zsh_no_template_second_step.sh index c62cae967..734fa66e9 100644 --- a/test/common-utils/configure_zsh_no_template.sh +++ b/test/common-utils/configure_zsh_no_template_second_step.sh @@ -4,12 +4,11 @@ set -e # Optional: Import test library source dev-container-features-test-lib - # Definition specific tests function file_not_overridden() { - cat ~/.zshrc | grep 'alias fnomockalias=' | grep testingmock + cat $1 | grep 'alias fnomockalias=' | grep testingmock } -check "default-zsh-with-no-zshrc" file_not_overridden +check "default-zsh-with-no-zshrc" file_not_overridden /home/devcontainer/.zshrc # Report result reportResults diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index 904a47bac..d7b296d41 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -115,10 +115,20 @@ } } }, - "configure_zsh_no_template": { + "configure_zsh_no_template_second_step": { "image": "mcr.microsoft.com/devcontainers/base:ubuntu", - "postCreateCommand": "echo alias fnomockalias=testingmock >> /root/.zshrc", - "remoteUser": "root", + "postCreateCommand": "echo alias fnomockalias=testingmock >> /home/devcontainer/.zshrc", + "remoteUser": "devcontainer", + "features": { + "common-utils": { + "installZsh": true, + "installOhMyZshConfig": false + } + } + }, + "configure_zsh_no_template_first_step": { + "image": "debian:bullseye", + "remoteUser": "devcontainer", "features": { "common-utils": { "installZsh": true, diff --git a/test/common-utils/test.sh b/test/common-utils/test.sh index 5e16a33c7..743d6664f 100755 --- a/test/common-utils/test.sh +++ b/test/common-utils/test.sh @@ -12,7 +12,8 @@ check "git" git --version check "zsh" zsh --version check "ps" ps --version check "Oh My Zsh! theme" test -e $HOME/.oh-my-zsh/custom/themes/devcontainers.zsh-theme -check "zsh theme symlink" test -e $HOME/.oh-my-zsh/custom/themes/codespaces.zsh-theme +check "zsh theme filename" test -e $HOME/.oh-my-zsh/custom/themes/codespaces.zsh-theme +check "oh-my-zsh executes" zsh -c 'source $HOME/.zshrc && echo $0 | grep zsh' # Report result reportResults \ No newline at end of file