Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
setup-account.sh 7.17 KiB
Newer Older
#!/bin/bash
## Functions to setup BDAP user account for bash & git (only if pending).
#
# Call this script with the name of the action-function as arg[1].

mydir=$(realpath "${0%/*}")
cd "$mydir"
mycmd="${0##*/}"
conda_env_prefix="$mydir/env"
ipython_startup_ustore_vars="$HOME/.ipython/profile_default/startup/50-ustor-vars.py"
function upd20_inputrc {
    if _mute grep -v ':history-search-forward' ~/.inputrc; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.inputrc <<- "EOF"
$include /etc/inputrc

## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
"\C-x\C-r": re-read-init-file

EOF
        echo "UPDATED"
    fi
}

function upd20_bashrc_git_completions {
    if _mute grep "/usr/share/bash-completion/completions/git" ~/.bashrc ; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bashrc <<- "EOF"

## Enable git completions
source /usr/share/bash-completion/completions/git

EOF
        echo "UPDATED"
    fi
}

function upd20_bashrc_history {
    if _mute grep "PROMPT_COMMAND=\"history -a\"" ~/.bashrc ; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bashrc <<- "EOF"

####
## HISTORY CONTROL:
## (more options may lie above))
##
## Save all lines of a multiple-line command in the same history entry.
shopt -s cmdhist
## Save multi-line commands to the history with embedded newlines.
shopt -s lithist
## Save each and EVERY cmd to history
#  (not to loose it if terminal closed abruptly!)
PROMPT_COMMAND="history -a"
## Don't put duplicate lines or lines starting with space in the history.
## Ignore 1-char cmds
HISTIGNORE=?:?

EOF
        echo "UPDATED"
    fi
}

function upd20_bashrc_ustore_vars {
    if _mute grep "export USTORE=" ~/.bashrc; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bashrc <<- EOF

## BDAP Storage locations
export PSTORE="/eos/jeodpp/data/projects/LEGENT"
export USTORE="/eos/jeodpp/home/users/${USER}"
export UTRANS="/eos/jeodpp/home/users/${USER}/transfer"
export PTRANS="/eos/jeodpp/data/projects/LEGENT/transfer"

EOF
        echo "UPDATED"
    fi
}

function upd20_bash_aliases {
    if _mute grep 'alias lg=' ~/.bash_aliases; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        cat >> ~/.bash_aliases <<- "EOF"

## GIT ALIASES
alias lg='git lg'
alias lgg='git lgg'
alias status='git status'
alias show='git show'
alias log='git log'

EOF
        echo "UPDATED"
    fi
}

function upd30_git_user {
    if _mute  git config --global --get user.name; then
        echo "${FUNCNAME[0]}.name: already ok"
    else
        echo "${FUNCNAME[0]}.name: updating..."
        read -e -p "  +--Enter your git author-name:" inp
        git config --global user.name "inp"
        echo "UPDATED"
    fi

    if _mute  git config --global --get user.email; then
        echo "${FUNCNAME[0]}.email: already ok"
    else
        echo "${FUNCNAME[0]}.email: updating..."
        read -e -p "  +--Enter your git author-email:" inp
        git config --global user.email "inp"
        echo "UPDATED"
    fi
}

function upd20_git_lg {
    if _mute  git config --global --get alias.lg; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
        git config --global alias.lgg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"
        git config --global credential.helper store
        echo "UPDATED"
    fi
}

function upd40_bashrc_conda_init {
    if _mute  grep ">>> conda initialize >>>" ~/.bashrc; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        # Strict-priority for when using conda-forge
        # eg for `conda-bash-completion` package`,
        # see https://conda-forge.org/docs/user/tipsandtricks.html#how-to-fix-it.
        conda config --set channel_priority strict
        # Suggested by BDAP docs:
        # https://jeodpp.jrc.ec.europa.eu/apps/gitlab/for-everyone/documentation/-/wikis/Jeodpp_services/Jeodesk/JEO-Desk-20-user-manual#conda-use-example-creation-of-an-environment-with-python-35-and-numpy-accessible-via-jupyterlab
        conda config --set auto_activate_base false
        # To reduce bash-prompt width, from:
        # https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#id3
        conda config --set env_prompt '({name})'

        conda init bash  # Effective only if never used conda before.
        echo "UPDATED"
    fi
}

function upd20_ipython_ustore_vars {
    if [ -f "$ipython_startup_ustore_vars" ]; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo -n "${FUNCNAME[0]}: updating..."
        mkdir -p  $(dirname "$ipython_startup_ustore_vars")
        cat > "$ipython_startup_ustore_vars" <<- EOF
## BDAP Storage locations
PSTORE="/eos/jeodpp/data/projects/LEGENT"
USTORE="/eos/jeodpp/home/users/${USER}"
UTRANS="/eos/jeodpp/home/users/${USER}/transfer"
PTRANS="/eos/jeodpp/data/projects/LEGENT/transfer"

EOF
        echo "UPDATED"
    fi
}

## This function must be the 1st to run after all `upd_xxx` actions.
#  The last command of this action, activating conda-env,
#  will fail if not re-logged in after `conda init bash`.
function upd50_create_conda_env {
    if [ -d "$conda_env_prefix" ]; then
        echo "${FUNCNAME[0]}: already ok"
    else
        echo "${FUNCNAME[0]}: would take ~5' to create conda-env..."
        conda env create -p "$conda_env_prefix" -f environment.yml
        ## Setup any conda-env variables:
        #  - PYTHONPATH pointing to project-root, to run scripts from there.
        conda env config vars set -p $conda_env_prefix PYTHONPATH="$PYTHONPATH:$mydir/"
        echo "${FUNCNAME[0]}: echo ACTIVATE YOUR ENVIRONMENT with: conda activate $conda_env_prefix"
    declare -F|grep -oE ' [a-zA-Z]\w+$' | sort
}

function all {
    for cmd in $(_list_all_actions); do
        if [ "$cmd" != "${FUNCNAME[0]}" ]; then
            $cmd
        fi
    done
}
if [[ $# -eq 1 && ( "$1" = "-h" || "$1" = "--help" ) ]]; then
    echo -e "$mycmd - actions to setup a BDAP user account.\n"
    echo -e "SYNTAX:\n    $mycmd\n    $mycmd ACTION\nWHERE:\n    ACTION is one of:"
    echo -e '\nWithout any args, it  launches `all` actions, in order.'
    echo -e '\nNOTES:\n    * The `upd30_git_user` action awaits interactively input from the user.'
    echo -e '    * You may have to logout & re-login after `upd40_bashrc_conda_init`,
      for `upd50_create_conda_env` to succeed.'
    echo -e '    * You can repeatedly run this script, until all action succeed.'
    echo "Running all actions..."
    echo "Running '${@}' action..."