From e83998ef510e22cd6eac1979e395b765c9054dc1 Mon Sep 17 00:00:00 2001 From: Guillaume Raffy Date: Thu, 30 Nov 2023 18:33:55 +0100 Subject: [PATCH] changed file hierarchy to allow for a readme file, an also because this repos is no longer expected to be in cloned in the home directory (too dangerous and it messes up some tools that see git repos into git repos) - added a readme - added scripts and bash config files that I want to be under source control --- home/.bash_logout | 7 + home/.bashrc | 134 +++++++++++++++ home/.profile | 33 ++++ home/bin/ansible.vault.ipr.sh | 3 + home/bin/cssh | 163 +++++++++++++++++++ {bin => home/bin}/debops.init-machine.bash | 2 +- {bin => home/bin}/debops.update-debops.bash | 2 +- {bin => home/bin}/debops.update-machine.bash | 3 +- home/bin/diffBranches.bash | 47 ++++++ home/bin/make_color.py | 44 +++++ readme.md | 23 +++ 11 files changed, 458 insertions(+), 3 deletions(-) create mode 100644 home/.bash_logout create mode 100644 home/.bashrc create mode 100644 home/.profile create mode 100755 home/bin/ansible.vault.ipr.sh create mode 100755 home/bin/cssh rename {bin => home/bin}/debops.init-machine.bash (94%) rename {bin => home/bin}/debops.update-debops.bash (92%) rename {bin => home/bin}/debops.update-machine.bash (84%) create mode 100755 home/bin/diffBranches.bash create mode 100755 home/bin/make_color.py create mode 100644 readme.md diff --git a/home/.bash_logout b/home/.bash_logout new file mode 100644 index 0000000..de4f5f7 --- /dev/null +++ b/home/.bash_logout @@ -0,0 +1,7 @@ +# ~/.bash_logout: executed by bash(1) when login shell exits. + +# when leaving the console clear the screen to increase privacy + +if [ "$SHLVL" = 1 ]; then + [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q +fi diff --git a/home/.bashrc b/home/.bashrc new file mode 100644 index 0000000..4524e21 --- /dev/null +++ b/home/.bashrc @@ -0,0 +1,134 @@ +case "$0" in + -sh|sh|*/sh) modules_shell=sh ;; + -ksh|ksh|*/ksh) modules_shell=ksh ;; + -zsh|zsh|*/zsh) modules_shell=zsh ;; + -bash|bash|*/bash) modules_shell=bash ;; +esac +module() { eval `/usr/bin/tclsh8.6 /usr/lib/x86_64-linux-gnu/modulecmd.tcl $modules_shell $*`; } +# ~/.bashrc: executed by bash(1) for non-login shells. +# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) +# for examples + +export PATH=$PATH:~/bin + +# If not running interactively, don't do anything +case $- in + *i*) ;; + *) return;; +esac + +# don't put duplicate lines or lines starting with space in the history. +# See bash(1) for more options +HISTCONTROL=ignoreboth + +# append to the history file, don't overwrite it +shopt -s histappend + +# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) +HISTSIZE=1000 +HISTFILESIZE=2000 + +# check the window size after each command and, if necessary, +# update the values of LINES and COLUMNS. +shopt -s checkwinsize + +# If set, the pattern "**" used in a pathname expansion context will +# match all files and zero or more directories and subdirectories. +#shopt -s globstar + +# make less more friendly for non-text input files, see lesspipe(1) +[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" + +# set variable identifying the chroot you work in (used in the prompt below) +if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then + debian_chroot=$(cat /etc/debian_chroot) +fi + +# set a fancy prompt (non-color, unless we know we "want" color) +case "$TERM" in + xterm-color|*-256color) color_prompt=yes;; +esac + +# uncomment for a colored prompt, if the terminal has the capability; turned +# off by default to not distract the user: the focus in a terminal window +# should be on the output of commands, not on the prompt +#force_color_prompt=yes + +if [ -n "$force_color_prompt" ]; then + if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then + # We have color support; assume it's compliant with Ecma-48 + # (ISO/IEC-6429). (Lack of such support is extremely rare, and such + # a case would tend to support setf rather than setaf.) + color_prompt=yes + else + color_prompt= + fi +fi + +if [ "$color_prompt" = yes ]; then + PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' +else + PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' +fi +unset color_prompt force_color_prompt + +# If this is an xterm set the title to user@host:dir +case "$TERM" in +xterm*|rxvt*) + PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" + ;; +*) + ;; +esac + +# enable color support of ls and also add handy aliases +if [ -x /usr/bin/dircolors ]; then + test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" + alias ls='ls --color=auto' + #alias dir='dir --color=auto' + #alias vdir='vdir --color=auto' + + alias grep='grep --color=auto' + alias fgrep='fgrep --color=auto' + alias egrep='egrep --color=auto' +fi + +# colored GCC warnings and errors +#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' + +# some more ls aliases +alias ll='ls -alF' +alias la='ls -A' +alias l='ls -CF' + +# Add an "alert" alias for long running commands. Use like so: +# sleep 10; alert +alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' + +# Alias definitions. +# You may want to put all your additions into a separate file like +# ~/.bash_aliases, instead of adding them here directly. +# See /usr/share/doc/bash-doc/examples in the bash-doc package. + +if [ -f ~/.bash_aliases ]; then + . ~/.bash_aliases +fi + +# enable programmable completion features (you don't need to enable +# this, if it's already enabled in /etc/bash.bashrc and /etc/profile +# sources /etc/bash.bashrc). +if ! shopt -oq posix; then + if [ -f /usr/share/bash-completion/bash_completion ]; then + . /usr/share/bash-completion/bash_completion + elif [ -f /etc/bash_completion ]; then + . /etc/bash_completion + fi +fi + +# 14/01/2022 : graffy added virtualenvwrapper environment as suggested by https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment +export WORKON_HOME=$HOME/.virtualenvs +export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 +export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 ' +export PROJECT_HOME=$HOME/work/venvwrapperprojects +source /usr/local/bin/virtualenvwrapper.sh + diff --git a/home/.profile b/home/.profile new file mode 100644 index 0000000..5057203 --- /dev/null +++ b/home/.profile @@ -0,0 +1,33 @@ +if [ -f /skel/profile.modules ] +then + . /skel/profile.modules +# put your own module loads here + module load null +fi +# ~/.profile: executed by the command interpreter for login shells. +# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login +# exists. +# see /usr/share/doc/bash/examples/startup-files for examples. +# the files are located in the bash-doc package. + +# the default umask is set in /etc/profile; for setting the umask +# for ssh logins, install and configure the libpam-umask package. +#umask 022 + +# if running bash +if [ -n "$BASH_VERSION" ]; then + # include .bashrc if it exists + if [ -f "$HOME/.bashrc" ]; then + . "$HOME/.bashrc" + fi +fi + +# set PATH so it includes user's private bin if it exists +if [ -d "$HOME/bin" ] ; then + PATH="$HOME/bin:$PATH" +fi + +# set PATH so it includes user's private bin if it exists +if [ -d "$HOME/.local/bin" ] ; then + PATH="$HOME/.local/bin:$PATH" +fi diff --git a/home/bin/ansible.vault.ipr.sh b/home/bin/ansible.vault.ipr.sh new file mode 100755 index 0000000..b9106cb --- /dev/null +++ b/home/bin/ansible.vault.ipr.sh @@ -0,0 +1,3 @@ +#!/bin/sh +gpg --quiet --batch --use-agent --decrypt "${HOME}"/.password-store/work/ansible.vault.ipr.gpg + diff --git a/home/bin/cssh b/home/bin/cssh new file mode 100755 index 0000000..78590ec --- /dev/null +++ b/home/bin/cssh @@ -0,0 +1,163 @@ +#!/bin/bash + +HOSTNAME=`echo $@ | sed s/.*@//` + +AS_ROOT='false' +if [ "$(echo $@ | grep '^root@' > /dev/null)" ] +then + AS_ROOT='true' +fi + +function getHostRealFqdn() +{ + local strHostname="$1" + + local strHostFqdn="$(host $strHostname | tail -1 | awk '{print $1}')" + echo "$strHostFqdn" +} + +HOST_FQDN=$(getHostRealFqdn $HOSTNAME) + +SOLARIZED_BACKGROUND='{0, 8256, 10368}' #002b36 (solarized dark base03) +SAILOR_BLUE='{8832, 14208, 18816}' # 2e4a62 +PRUNE='{15360, 10944, 14592}' #50394c +TAUPE='{19968, 18816, 16512}' #686256 +DARK_ORANGE='{27456, 12864, 0}' #8f4300 +DARK_OLIVE='{8832, 11904, 5568}' #2e3e1d + +function get_current_terminal_window_id() +{ + local terminal_windows_id='' + terminal_windows_id=$(osascript -e "tell application \"Terminal\" to get id of window 1") + echo "$terminal_windows_id" +} + +function set_bg () +{ + local strOsxTermWindowId="$1" + local strColor="$2" # eg {45000, 0, 0, 50000} + + osascript -e "tell application \"Terminal\" to set background color of (every window whose id is $strOsxTermWindowId) to $strColor" +} + +function on_exit () +{ + set_bg "$THIS_OSX_TERM_WINDOW_ID" "$SOLARIZED_BACKGROUND" +} + +# if [ $(echo $HOST_FQDN | grep '^simpatix') ] +# then +# BG_COLOR="$SAILOR_BLUE" +# elif [ $(echo $HOST_FQDN | grep '^physix') ] +# then +# BG_COLOR="$TAUPE" +# else +# case $HOST_FQDN in +# 'pr079234.spm.univ-rennes1.fr') +# BG_COLOR="$SOLARIZED_BACKGROUND" +# ;; +# 'puppet3.ipr.univ-rennes1.fr') +# BG_COLOR="$PRUNE" +# ;; +# *) +# BG_COLOR="$DARK_OLIVE" +# ;; +# esac +# fi + +#if [ "$AS_ROOT" = 'true' ] +#then +# BG_COLOR="$DARK_ORANGE" +#fi + +DCONF_PROFILES_PATH='/org/gnome/terminal/legacy/profiles:' + +create_new_profile() +{ + # copied from https://askubuntu.com/questions/270469/how-can-i-create-a-new-profile-for-gnome-terminal-via- command-line + + local profile_ids=($(dconf list $DCONF_PROFILES_PATH/ | grep ^: |\ + sed 's/\///g' | sed 's/://g')) + local profile_name="$1" + local profile_ids_old="$(dconf read "$DCONF_PROFILES_PATH"/list | tr -d "]")" + local profile_id="$(uuidgen)" + + [ -z "$profile_ids_old" ] && local lb="[" # if there's no `list` key + [ ${#profile_ids[@]} -gt 0 ] && local delimiter=, # if the list is empty + dconf write $DCONF_PROFILES_PATH/list \ + "${profile_ids_old}${delimiter} '$profile_id']" + dconf write "$DCONF_PROFILES_PATH/:$profile_id"/visible-name "'$profile_name'" + echo $profile_id +} + + +function get_terminal_profile_uuid() +{ + # returns the id of the profile which has the given name. + # if the named profile doesn't exist, it creates it first + local profile_name="$1" # eg physix.ipr.univ-rennes1.fr + + local profile='' + + local this_profile='' + local profile_uuid='' + + for this_profile in $(dconf list "$DCONF_PROFILES_PATH/") + do + # eg value for profile : ':b1dcc9dd-5262-4d8d-a863-c897e6d979b9/' + local this_profile_name='' + this_profile_name=$(dconf read $DCONF_PROFILES_PATH/${this_profile}visible-name | tr -d "'" ) + # echo "this_profile_name=$this_profile_name" + if [ "$this_profile_name" = "$profile_name" ] + then + profile_uuid=$(echo "$this_profile" | sed 's/^://' | sed 's|/||') + echo "$profile_uuid" + return 0 + fi + done + + # the profile named $profile_name doesn't exist... create it then + profile_uuid=$(create_new_profile $profile_name) + echo "$profile_uuid" +} + +function set_terminal_profile_bg_color() +{ + local profile_name="$1" # eg physix.ipr.univ-rennes1.fr + local bg_color="$2" # eg rgb(17, 25, 24) + + local profile_uuid='' + profile_uuid=$(get_terminal_profile_uuid $profile_name) + echo "profile_name=$profile_name profile_uuid=$profile_uuid" + + dconf write $DCONF_PROFILES_PATH/:$profile_uuid/use-theme-colors 'false' + dconf write $DCONF_PROFILES_PATH/:$profile_uuid/background-color "'rgb$bg_color'" +} + +COLOR_SATURATION='0.3' +COLOR_VALUE='0.2' +OS_NAME=$(uname) +case "$OS_NAME" in + 'Darwin') + BG_COLOR=$(make_color.py $HOST_FQDN $COLOR_VALUE $COLOR_SATURATION osx) + THIS_OSX_TERM_WINDOW_ID=$(get_current_terminal_window_id) + set_bg "$THIS_OSX_TERM_WINDOW_ID" "$BG_COLOR" + trap on_exit EXIT + /usr/bin/ssh "$@" + ;; + 'Linux') + BG_COLOR=$(make_color.py $HOST_FQDN $COLOR_VALUE $COLOR_SATURATION linux) + if [ $? != 0 ] + then + echo "error : make_color.py failed" + exit 1 + fi + set_terminal_profile_bg_color "$HOST_FQDN" "$BG_COLOR" + + gnome-terminal --window-with-profile=$HOST_FQDN --title=$HOST_FQDN -- /usr/bin/ssh "$@" + ;; + *) + echo "error : unexpeced os name : $OS_NAME" + exit 1 +esac + diff --git a/bin/debops.init-machine.bash b/home/bin/debops.init-machine.bash similarity index 94% rename from bin/debops.init-machine.bash rename to home/bin/debops.init-machine.bash index d6da393..9911fdc 100755 --- a/bin/debops.init-machine.bash +++ b/home/bin/debops.init-machine.bash @@ -8,7 +8,7 @@ mkdir -p "${REPORTS_DIR}" REPORT_FILE_PATH=${REPORTS_DIR}/$(date --iso=seconds)-init-${TARGET_HOST_FQDN} echo "installing debops bootstrap on ${TARGET_HOST_FQDN} (report stored in ${REPORT_FILE_PATH})" pushd $HOME/work/debops/ansible.debops - source ../debops-venv/bin/activate + source ../debops.venv/bin/activate ANS_HOST=$(echo ${TARGET_HOST_FQDN} | sed -E 's/\.univ-rennes[1]?\.fr$//') echo "ANS_HOST=${ANS_HOST}" debops run bootstrap-ldap -l "${ANS_HOST:-/dev/null}" | tee --append ${REPORT_FILE_PATH} diff --git a/bin/debops.update-debops.bash b/home/bin/debops.update-debops.bash similarity index 92% rename from bin/debops.update-debops.bash rename to home/bin/debops.update-debops.bash index 0cf24ea..0bb2f16 100755 --- a/bin/debops.update-debops.bash +++ b/home/bin/debops.update-debops.bash @@ -6,7 +6,7 @@ mkdir -p "${REPORTS_DIR}" REPORT_FILE_PATH=${REPORTS_DIR}/$(date --iso=seconds)-update-debops echo "updating debops itself" pushd $HOME/work/debops/ansible.debops - source ../debops-venv/bin/activate + source ../debops.venv/bin/activate git pull | tee --append ${REPORT_FILE_PATH} ./bin/update-prod.sh | tee --append ${REPORT_FILE_PATH} ./bin/update-dev.sh | tee --append ${REPORT_FILE_PATH} diff --git a/bin/debops.update-machine.bash b/home/bin/debops.update-machine.bash similarity index 84% rename from bin/debops.update-machine.bash rename to home/bin/debops.update-machine.bash index d7f62a6..647f452 100755 --- a/bin/debops.update-machine.bash +++ b/home/bin/debops.update-machine.bash @@ -8,7 +8,8 @@ mkdir -p "${REPORTS_DIR}" REPORT_FILE_PATH=${REPORTS_DIR}/$(date --iso=seconds)-update-${TARGET_HOST_FQDN} echo "applying debops configuration on ${TARGET_HOST_FQDN} (report stored in ${REPORT_FILE_PATH})" pushd $HOME/work/debops/ansible.debops - source ../debops-venv/bin/activate + echo "ansible.debops version: $(git rev-parse HEAD)" >> ${REPORT_FILE_PATH} + source ../debops.venv/bin/activate ANS_HOST=$(echo ${TARGET_HOST_FQDN} | sed -E 's/\.univ-rennes[1]?\.fr$//') echo "ANS_HOST=${ANS_HOST}" debops run site --limit "${ANS_HOST:-/dev/null}" | tee --append ${REPORT_FILE_PATH} diff --git a/home/bin/diffBranches.bash b/home/bin/diffBranches.bash new file mode 100755 index 0000000..dfef6e5 --- /dev/null +++ b/home/bin/diffBranches.bash @@ -0,0 +1,47 @@ +#!/bin/bash +# script made by graffy on 12/01/2016 to understand the diff between ./data/ipr/svnworkspaces/main and ./owncloud/svnworkspaces/main +srcPath="$1" # eg ./data/ipr/svnworkspaces/main +dstPath="$2" # eg ./ownCloud/svnworkspaces/main +bDeleteFilesFromSourceWhenIdentical="$3" # 'true' or 'false' + +function deleteFile() +{ + local strFilePath="$1" + + local bDebug='false' + if [ "$bDebug" = 'true' ] + then + echo "fake delete of $strFilePath" + else + echo "deleting $strFilePath" + rm -f "$strFilePath" + fi +} + +SAVEIFS=$IFS +IFS=$(echo -en "\n\b") +for srcFile in $(find "$srcPath" -type f) +do + #echo $srcFile + if [ $(basename "$srcFile") != '.DS_Store' ] + then + dstFile=$(echo "$srcFile" | sed "s|$srcPath|$dstPath|g") + #echo $dstFile + if [ -f "$dstFile" ] + then + diff "$srcFile" "$dstFile" + if [ $? != 0 ] + then echo "$srcFile != $dstFile" + else + if [ "$bDeleteFilesFromSourceWhenIdentical" = 'true' ] + then + # as $srcFile and $dstFile are identical, delete srcFile + deleteFile "$srcFile" + fi + fi + else + echo "missing file : $dstFile" + fi + fi +done +IFS=$SAVEIFS diff --git a/home/bin/make_color.py b/home/bin/make_color.py new file mode 100755 index 0000000..e387d5b --- /dev/null +++ b/home/bin/make_color.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# this program generates a color value by hashing the input string + +import sys +from colorsys import hsv_to_rgb +from hashlib import md5 + + +def float_to_16b(component): + # print(hex_comp) + correction = 3. / 4. # empiric correction, otherwise colors are not what's expected + return int(component * 65536.0 * correction) + + +def float_to_8b(component): + # print(hex_comp) + return int(component * 256.0) + + +def hsv_to_osx(hue, saturation, value): + (r, g, b) = hsv_to_rgb(hue, saturation, value) + return "{%d, %d, %d}" % (float_to_16b(r), float_to_16b(g), float_to_16b(b)) + + +def hsv_to_linux(hue, saturation, value): + (r, g, b) = hsv_to_rgb(hue, saturation, value) + return "(%d, %d, %d)" % (float_to_8b(r), float_to_8b(g), float_to_8b(b)) + + +if __name__ == '__main__': + seed_string = sys.argv[1] + # print('seed_string = ', seed_string) + color_value = float(sys.argv[2]) + color_saturation = float(sys.argv[3]) + string_format = sys.argv[4] # eg 'osx' 'linux' + # color_hue = int(md5(seed_string).hexdigest()[:8], 16) # taken from http://www.guguncube.com/3237/python-string-to-number-hash + color_hue = float(int(md5(seed_string).hexdigest(), 16) % 100000) / 100000.0 + + # print(color_hue) + if string_format == 'osx': + print(hsv_to_osx(color_hue, color_saturation, color_value)) + + if string_format == 'linux': + print(hsv_to_linux(color_hue, color_saturation, color_value)) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5bd6ed0 --- /dev/null +++ b/readme.md @@ -0,0 +1,23 @@ + +This repository stores the work environment used by graffy at Institut de Physique de Rennes + +The work environment includes: +- a `bin` directory containing custom scripts +- bash configuration files +- etc. + +## how to use + +clone the repository to a chosen location `local_repos_path` + +```sh +graffy@graffy-ws2:~/work$ git clone git@vmgit.ipr.univ-rennes.fr:graffy/graffyworkenv.git graffyworkenv.git +``` + +then install the environment by creating symbolic links in your home dir that point to some files in the `local_repos_path` +```sh +graffy@graffy-ws2:~$ ln -s ./work/graffyworkenv.git/home/bin ./bin +graffy@graffy-ws2:~$ ln -s ./work/graffyworkenv.git/home/.profile ./.profile +graffy@graffy-ws2:~$ ln -s ./work/graffyworkenv.git/home/.bashrc ./.bashrc +graffy@graffy-ws2:~$ ln -s ./work/graffyworkenv.git/home/.bash_logout ./.bash_logout +```