From 3d3b9f2aa42dffc5c8594ee0b40fc83d89c43354 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Mon, 16 Jun 2014 20:21:21 +0200 Subject: [PATCH] squelette d'un playbook initial a passer lors de l'installation d'un serveur pour une premiere configuration fonctionnelle, + readme associe --- configuration-initiale.yml | 12 + configuration-initiale.yml.README | 56 + roles/common/defaults/main.yml | 4 + roles/common/files/root-.bashrc | 1885 +++++++++++++++++ roles/common/files/root-.inputrc | 8 + roles/common/files/root-.profile | 12 + .../common/files/root-.vim-colors-wombat.vim | 52 + roles/common/files/root-.vimrc | 25 + roles/common/files/root-README.root | 1 + roles/common/handlers/main.yml | 44 + roles/common/tasks/main.yml | 119 ++ roles/common/templates/etc-motd.j2 | 4 + roles/common/templates/etc-ntp.conf.j2 | 17 + 13 files changed, 2239 insertions(+) create mode 100644 configuration-initiale.yml create mode 100644 configuration-initiale.yml.README create mode 100644 roles/common/defaults/main.yml create mode 100644 roles/common/files/root-.bashrc create mode 100644 roles/common/files/root-.inputrc create mode 100644 roles/common/files/root-.profile create mode 100644 roles/common/files/root-.vim-colors-wombat.vim create mode 100644 roles/common/files/root-.vimrc create mode 100644 roles/common/files/root-README.root create mode 100644 roles/common/handlers/main.yml create mode 100644 roles/common/tasks/main.yml create mode 100644 roles/common/templates/etc-motd.j2 create mode 100644 roles/common/templates/etc-ntp.conf.j2 diff --git a/configuration-initiale.yml b/configuration-initiale.yml new file mode 100644 index 0000000..186f9e8 --- /dev/null +++ b/configuration-initiale.yml @@ -0,0 +1,12 @@ +--- +# Playbook permettant d'appliquer le role common sur un hote lors de l'install (configuration locales, ntp, premiers paquets, etc) + +- name: Deployer la configuration basique sur un hote + hosts: all + user: root + gather_facts: yes + + roles: + - common + +# vim: set textwidth=0 ft=yaml ts=2 sw=2 expandtab: diff --git a/configuration-initiale.yml.README b/configuration-initiale.yml.README new file mode 100644 index 0000000..b26c93a --- /dev/null +++ b/configuration-initiale.yml.README @@ -0,0 +1,56 @@ + ## README configuration-initiale.yml ## + +#### Description #### +Ce playbook permet de configurer basiquement une debian. +Il englobe la mise en place : + + - Des locales fr-UTF8 + - L'installation de paquets par défaut utiles + - La mise en place de clefs SSH pour permettre la connexion + - La mise en place de serveurs NTP personnalisés + - L'installation d'un bashrc basé sur liquid prompt + - L'installation de themes de couleurs pour vim + - La configuration de la timezone Europe/Paris + - La désactivation ou non de l'IPv6 selon les variables du serveur + - La mise en place d'un motd indiquant le fqdn du serveur et sa description + +Les configurations suivantes se font uniquement si la variable initialize=True est passee en parametre du playbook +(grace a l'option : -e initialize=True) + - La mise en place d'un fichier /root/README.root ou indiquer les modificatiosn faites sur le serveur + - La modification du fichier /root/.profile pour afficher les informations de /root/README.root + +#### Utilisation #### +Ce playbook demande un certain nombre de variables définies. +Il echouera si l'une de ces variables n'est pas définie. +L'ideal est que ces variables soient definies pour tous les hotes basiquement (via un groupe par exemple), avec eventuellement des personnalisation par serveur selon les besoins. +Après la description suivent des exemples de ces variables. + - admin_ssh_keys : la liste des clefs ssh a copier sur le serveur (pour root) + - default_packages_debian : la liste des packages debian a installer par defaut + - ansible_fqdn : le nom a donner au serveur + - description : La description du serveur (s'affichera a la connexion) + - ntp_server1 : le premier serveur NTP a utiliser + - ntp_server2 : le deuxième serveur ntp a installer + - disable_ipv6 : IPv6 doit il etre desactive ou non + +### Exemples de variables ### +fichier : /etc/ansible/group_vars/all : +admin_ssh_keys: + - ssh-rsa UwJmYYms/TRKBpKvux8JcukSdSA/cZRzlAnNn4ZDXhxpnHE6xcVCoHeHXg95479K8w33ZLsXTHviUripz2tP/a+BQ+wHFeROqLBjSvSZRX/oJtQRLDnghNXTq6XrMuqTenVWDP/YQFZd6i7lk/bt3Y8obrGa1OHNf+9RR783CZijjApiuctKobCN4SVd1n8xzjFtsp0uALwZy+WCJfH9DZanv4vOMLj0DPjvfQQ3h3/HWgA6+vDhJ0a5X8PAQ5mWdRsOqUUFZyFvoP/VslnP+/Fsf/FfO2Swp9uCqxVVm3vFi4tpyFj1XmjkFSCkFdTsBebA0nLL5hkzq0LxWSSNMSxW9S+D7cpy1mgwgrPBFzwS5c/mL1+sNakpxro4vrI0u+lRBw96DtqLpT1BGU5w2qbHMuSfrhu1SM/GyGTslgNorpNY5u5Rs5e6dMfAotmrM00f1oML/qORYV2K/QnQSVTHM5hcUWxyzXv7RXMRcuQWAKlWXPk0IxbnN0a12yIv50= foo@bar + +default_packages_debian: + - vim + - python-apt + - python-pycurl + - locales + - debconf-utils + +ansible_fqdn: serveur-debian.exemple.com + +description: Bienvenue sur ce serveur debian + +ntp_server1: 0.fr.pool.ntp.org +ntp_server2: 1.fr.pool.ntp.org + +disable_ipv6: yes + + diff --git a/roles/common/defaults/main.yml b/roles/common/defaults/main.yml new file mode 100644 index 0000000..ae4ed32 --- /dev/null +++ b/roles/common/defaults/main.yml @@ -0,0 +1,4 @@ +--- + +initialize: False +# vim: set textwidth=0 ft=yaml ts=2 sw=2 expandtab: diff --git a/roles/common/files/root-.bashrc b/roles/common/files/root-.bashrc new file mode 100644 index 0000000..b160cdf --- /dev/null +++ b/roles/common/files/root-.bashrc @@ -0,0 +1,1885 @@ +### THIS FILE IS DEPLOYED BY ANSIBLE + +export LS_OPTIONS='--color=auto' +eval "`dircolors`" +alias ls='ls $LS_OPTIONS' +alias ll='ls $LS_OPTIONS -l' +alias l='ls $LS_OPTIONS -lA' +export EDITOR=vim +# export PS1='\[\e[1;34m\]\u\[\e[1;30m\]@\[\e[1;35m\]\h\[\e[1;30m\]:\[\e[1;37m\]\w\[\e[1;30m\]\$\[\e[0m\] ' + +alias rm='rm -i' +alias cp='cp -i' +alias mv='mv -i' +alias grep='grep --color=auto' +export HISTTIMEFORMAT="%d/%m/%Y - %H:%M:%S " +man() { + env LESS_TERMCAP_mb=$'\E[01;31m' \ + LESS_TERMCAP_md=$'\E[01;38;5;74m' \ + LESS_TERMCAP_me=$'\E[0m' \ + LESS_TERMCAP_se=$'\E[0m' \ + LESS_TERMCAP_so=$'\E[38;5;246m' \ + LESS_TERMCAP_ue=$'\E[0m' \ + LESS_TERMCAP_us=$'\E[04;38;5;146m' \ + man "$@" +} + +#################################### +# LIQUID PROMPT CONFIGURATION FILE # +#################################### + +# If you want to use different themes and features, +# you can load the corresponding files here: +#source ~/code/liquidprompt/nojhan.theme +#LP_PS1_FILE="~/code/liquidprompt/nojhan.ps1" + +############# +# BEHAVIOUR # +############# + +# Maximal value under which the battery level is displayed +# Recommended value is 75 +LP_BATTERY_THRESHOLD=75 + +# Minimal value after which the load average is displayed +# Recommended value is 60 +LP_LOAD_THRESHOLD=60 + +# Minimal value after which the temperature is displayed (in celsius degrees) +# Recommended value is 60 +LP_TEMP_THRESHOLD=60 + +# The maximum percentage of the screen width used to display the path +# Recommended value is 35 +LP_PATH_LENGTH=35 + +# How many directories to keep at the beginning of a shortened path +# Recommended value is 2 +LP_PATH_KEEP=2 + +# Do you want to display the hostname, even if not connected through network? +# Defaults to 0 (do not display hostname when localy connected) +# set to 1 if you want to always see the hostname +LP_HOSTNAME_ALWAYS=1 + +# Do you want to display the user, even if he is the same than the logged one? +# Defaults to 1 (always display the user) +# set to 0 if you want to hide the logged user (it will always display different users) +LP_USER_ALWAYS=1 + +# Do you want to display the percentages of load/batteries along with their +# corresponding marks? Set to 0 to only print the colored marks. +# Defaults to 1 (display percentages) +LP_PERCENTS_ALWAYS=0 + +# Do you want to use the permissions feature ? +# Recommended value is 1 +LP_ENABLE_PERM=1 + +# Do you want to use the shorten path feature ? +# Recommended value is 1 +LP_ENABLE_SHORTEN_PATH=0 + +# Do you want to use the proxy detection feature ? +# Recommended value is 1 +LP_ENABLE_PROXY=1 + +# Do you want to use the jobs feature ? +# Recommended value is 1 +LP_ENABLE_JOBS=1 + +# Do you want to use the load feature ? +# Recommended value is 1 +LP_ENABLE_LOAD=1 + +# Do you want to use the batt feature ? +# Recommended value is 1 +LP_ENABLE_BATT=0 + +# Do you want to use vcs features with root account +# Recommended value is 0 +LP_ENABLE_VCS_ROOT=0 + +# Do you want to use the git special features ? +# Recommended value is 1 +LP_ENABLE_GIT=1 + +# Do you want to use the svn special features ? +# Recommended value is 1 +LP_ENABLE_SVN=1 + +# Do you want to use the mercurial special features ? +# Recommended value is 0 +LP_ENABLE_HG=0 + +# Do you want to use the fossil special features ? +# Recommended value is 0 +LP_ENABLE_FOSSIL=0 + +# Do you want to use the bzr special features ? +# Recommanded value is 0 +LP_ENABLE_BZR=0 + +# Show time of the last prompt display +# Recommended value is 0 +LP_ENABLE_TIME=0 + +# Show runtime of the last command if over LP_RUNTIME_THRESHOLD +# Recommended value is 0 +LP_ENABLE_RUNTIME=0 + +# Minimal runtime to be displayed +# Recommended value is 2 +LP_RUNTIME_THRESHOLD=2 + +# Display the virtualenv that is currently activated, if any +# Recommended value is 1 +LP_ENABLE_VIRTUALENV=1 + +# Show average system temperature +LP_ENABLE_TEMP=0 + +# When showing time, use an analog clock instead of numeric values. +# The analog clock is "accurate" to the nearest half hour. +# You must have a unicode-capable terminal and a font with the "CLOCK" +# characters. +# Recommended value is 0 +LP_TIME_ANALOG=0 + +# Use the liquid prompt as the title of the terminal window +# The content is not customizable, the implementation is very basic, +# and this may not work properly on exotic terminals, thus the +# recommended value is 0 +# See LP_TITLE_OPEN and LP_TITLE_CLOSE to change escape characters to adapt this +# feature to your specific terminal. +LP_ENABLE_TITLE=0 + +# Enable Title for screen and byobu +LP_ENABLE_SCREEN_TITLE=0 + +# Use differents colors for differents hosts you SSH in +LP_ENABLE_SSH_COLORS=1 + +# Specify a list of complete and colon (":") separated paths in which, all vcs +# will be disabled +LP_DISABLED_VCS_PATH="" + +# LiquidPrompt +########################################################## +# See the README.md file for a summary of features. + +# Issue #161: do not load if not an interactive shell +test -z "$TERM" -o "x$TERM" = dumb && return + +# Check for recent enough version of bash. +if test -n "$BASH_VERSION" -a -n "$PS1" ; then + bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.} + if [[ $bmajor -lt 3 ]] || [[ $bmajor -eq 3 && $bminor -lt 2 ]]; then + unset bash bmajor bminor + return + fi + unset bash bmajor bminor + + _LP_SHELL_bash=true + _LP_SHELL_zsh=false + _LP_OPEN_ESC="\[" + _LP_CLOSE_ESC="\]" + _LP_USER_SYMBOL="\u" + _LP_HOST_SYMBOL="\h" + _LP_TIME_SYMBOL="\t" + _LP_MARK_SYMBOL='\$' + _LP_FIRST_INDEX=0 + _LP_PWD_SYMBOL="\\w" +elif test -n "$ZSH_VERSION" ; then + _LP_SHELL_bash=false + _LP_SHELL_zsh=true + _LP_OPEN_ESC="%{" + _LP_CLOSE_ESC="%}" + _LP_USER_SYMBOL="%n" + _LP_HOST_SYMBOL="%m" + _LP_TIME_SYMBOL="%*" + _LP_MARK_SYMBOL='%(!.#.%%)' + _LP_FIRST_INDEX=1 + _LP_PWD_SYMBOL="%~" +else + #echo "liquidprompt: shell not supported" >&2 + return +fi + + +############### +# OS specific # +############### + +# LP_OS detection, default to Linux +case $(uname) in + FreeBSD) LP_OS=FreeBSD ;; + DragonFly) LP_OS=FreeBSD ;; + OpenBSD) LP_OS=OpenBSD ;; + Darwin) LP_OS=Darwin ;; + SunOS) LP_OS=SunOS ;; + *) LP_OS=Linux ;; +esac + +# Get cpu count +case "$LP_OS" in + Linux) _lp_CPUNUM=$( nproc 2>/dev/null || grep -c '^[Pp]rocessor' /proc/cpuinfo ) ;; + FreeBSD|Darwin|OpenBSD) _lp_CPUNUM=$( sysctl -n hw.ncpu ) ;; + SunOS) _lp_CPUNUM=$( kstat -m cpu_info | grep -c "module: cpu_info" ) ;; +esac + +# Extended regexp patterns for sed +# GNU/BSD sed +_LP_SED_EXTENDED=r +[[ "$LP_OS" = Darwin ]] && _LP_SED_EXTENDED=E + + +# get current load +case "$LP_OS" in + Linux) + _lp_cpu_load () { + local load eol + read load eol < /proc/loadavg + echo "$load" + } + ;; + FreeBSD|Darwin|OpenBSD) + _lp_cpu_load () { + local bol load eol + # If you have problems with syntax coloring due to the following + # line, do this: ln -s liquidprompt liquidprompt.bash + # and edit liquidprompt.bash + read bol load eol <<<$( LANG=C sysctl -n vm.loadavg ) + echo "$load" + } + ;; + SunOS) + _lp_cpu_load () { + LANG=C uptime | awk '{print substr($10,0,length($10))}' + } +esac + +# Reset so all PWD dependent variables are computed after loading +unset LP_OLD_PWD + +################# +# CONFIGURATION # +################# + +# The following code is run just once. But it is encapsulated in a function +# to benefit of 'local' variables. +# +# What we do here: +# 1. Setup variables that can be used by the user: the "API" of liquidprompt +# for config/theme. Those variables are local to the function. +# In practice, this is only color variables. +# 2. Setup default values +# 3. Load the configuration +_lp_source_config() +{ + + # TermInfo feature detection + local ti_sgr0="$( { tput sgr0 || tput me ; } 2>/dev/null )" + local ti_bold="$( { tput bold || tput md ; } 2>/dev/null )" + local ti_setaf + if tput setaf >/dev/null 2>&1 ; then + ti_setaf () { tput setaf "$1" ; } + elif tput AF >/dev/null 2>&1 ; then + # *BSD + ti_setaf () { tput AF "$1" ; } + elif tput AF 1 >/dev/null 2>&1 ; then + # OpenBSD + ti_setaf () { tput AF "$1" ; } + else + echo "liquidprompt: terminal $TERM not supported" >&2 + ti_setaf () { : ; } + fi + + # Colors: variables are local so they will have a value only + # during config loading and will not conflict with other values + # with the same names defined by the user outside the config. + local BOLD="${_LP_OPEN_ESC}${ti_bold}${_LP_CLOSE_ESC}" + + local BLACK="${_LP_OPEN_ESC}$(ti_setaf 0)${_LP_CLOSE_ESC}" + local BOLD_GRAY="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 0)${_LP_CLOSE_ESC}" + local WHITE="${_LP_OPEN_ESC}$(ti_setaf 7)${_LP_CLOSE_ESC}" + local BOLD_WHITE="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 7)${_LP_CLOSE_ESC}" + + local RED="${_LP_OPEN_ESC}$(ti_setaf 1)${_LP_CLOSE_ESC}" + local BOLD_RED="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 1)${_LP_CLOSE_ESC}" + local WARN_RED="${_LP_OPEN_ESC}$(ti_setaf 0 ; tput setab 1)${_LP_CLOSE_ESC}" + local CRIT_RED="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 7 ; tput setab 1)${_LP_CLOSE_ESC}" + local DANGER_RED="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 3 ; tput setab 1)${_LP_CLOSE_ESC}" + + local GREEN="${_LP_OPEN_ESC}$(ti_setaf 2)${_LP_CLOSE_ESC}" + local BOLD_GREEN="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 2)${_LP_CLOSE_ESC}" + + local YELLOW="${_LP_OPEN_ESC}$(ti_setaf 3)${_LP_CLOSE_ESC}" + local BOLD_YELLOW="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 3)${_LP_CLOSE_ESC}" + + local BLUE="${_LP_OPEN_ESC}$(ti_setaf 4)${_LP_CLOSE_ESC}" + local BOLD_BLUE="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 4)${_LP_CLOSE_ESC}" + + local PURPLE="${_LP_OPEN_ESC}$(ti_setaf 5)${_LP_CLOSE_ESC}" + local PINK="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 5)${_LP_CLOSE_ESC}" + + local CYAN="${_LP_OPEN_ESC}$(ti_setaf 6)${_LP_CLOSE_ESC}" + local BOLD_CYAN="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 6)${_LP_CLOSE_ESC}" + + # NO_COL is special: it will be used at runtime, not just during config loading + NO_COL="${_LP_OPEN_ESC}${ti_sgr0}${_LP_CLOSE_ESC}" + + unset ti_sgr0 ti_bold ti_setaf + + + # Default values (globals) + LP_BATTERY_THRESHOLD=${LP_BATTERY_THRESHOLD:-75} + LP_LOAD_THRESHOLD=${LP_LOAD_THRESHOLD:-60} + LP_TEMP_THRESHOLD=${LP_TEMP_THRESHOLD:-60} + LP_RUNTIME_THRESHOLD=${LP_RUNTIME_THRESHOLD:-2} + LP_PATH_LENGTH=${LP_PATH_LENGTH:-35} + LP_PATH_KEEP=${LP_PATH_KEEP:-2} + LP_PATH_DEFAULT="${LP_PATH_DEFAULT:-$_LP_PWD_SYMBOL}" + LP_HOSTNAME_ALWAYS=${LP_HOSTNAME_ALWAYS:-0} + LP_USER_ALWAYS=${LP_USER_ALWAYS:-1} + LP_PERCENTS_ALWAYS=${LP_PERCENTS_ALWAYS:-1} + LP_PS1=${LP_PS1:-""} + LP_PS1_PREFIX=${LP_PS1_PREFIX:-""} + LP_PS1_POSTFIX=${LP_PS1_POSTFIX:-""} + LP_TITLE_OPEN=${LP_TITLE_OPEN:-"\e]0;"} + LP_TITLE_CLOSE=${LP_TITLE_CLOSE:-"\a"} + LP_SCREEN_TITLE_OPEN=${LP_SCREEN_TITLE_OPEN:-"\033k"} + LP_SCREEN_TITLE_CLOSE=${LP_SCREEN_TITLE_CLOSE:-"\033\134"} + + LP_ENABLE_PERM=${LP_ENABLE_PERM:-1} + LP_ENABLE_SHORTEN_PATH=${LP_ENABLE_SHORTEN_PATH:-1} + LP_ENABLE_PROXY=${LP_ENABLE_PROXY:-1} + LP_ENABLE_TEMP=${LP_ENABLE_TEMP:-1} + LP_ENABLE_JOBS=${LP_ENABLE_JOBS:-1} + LP_ENABLE_LOAD=${LP_ENABLE_LOAD:-1} + LP_ENABLE_BATT=${LP_ENABLE_BATT:-1} + LP_ENABLE_GIT=${LP_ENABLE_GIT:-1} + LP_ENABLE_SVN=${LP_ENABLE_SVN:-1} + LP_ENABLE_FOSSIL=${LP_ENABLE_FOSSIL:-1} + LP_ENABLE_HG=${LP_ENABLE_HG:-1} + LP_ENABLE_BZR=${LP_ENABLE_BZR:-1} + LP_ENABLE_TIME=${LP_ENABLE_TIME:-0} + if $_LP_SHELL_bash; then + LP_ENABLE_RUNTIME=${LP_ENABLE_RUNTIME:-1} + else + LP_ENABLE_RUNTIME=${LP_ENABLE_RUNTIME:-0} + fi + LP_ENABLE_VIRTUALENV=${LP_ENABLE_VIRTUALENV:-1} + LP_ENABLE_VCS_ROOT=${LP_ENABLE_VCS_ROOT:-0} + LP_ENABLE_TITLE=${LP_ENABLE_TITLE:-0} + LP_ENABLE_SCREEN_TITLE=${LP_ENABLE_SCREEN_TITLE:-0} + LP_ENABLE_SSH_COLORS=${LP_ENABLE_SSH_COLORS:-0} + # LP_DISABLED_VCS_PATH="${LP_DISABLED_VCS_PATH}" + + # LP_MARK_DEFAULT="$LP_MARK_DEFAULT" + LP_MARK_BATTERY="${LP_MARK_BATTERY:-"⌁"}" + LP_MARK_ADAPTER="${LP_MARK_ADAPTER:-"⏚"}" + LP_MARK_LOAD="${LP_MARK_LOAD:-"⌂"}" + LP_MARK_TEMP="${LP_MARK_TEMP:-"θ"}" + LP_MARK_PROXY="${LP_MARK_PROXY:-"↥"}" + LP_MARK_HG="${LP_MARK_HG:-"☿"}" + LP_MARK_SVN="${LP_MARK_SVN:-"‡"}" + LP_MARK_GIT="${LP_MARK_GIT:-"±"}" + LP_MARK_FOSSIL="${LP_MARK_FOSSIL:-"⌘"}" + LP_MARK_BZR="${LP_MARK_BZR:-"⚯"}" + LP_MARK_DISABLED="${LP_MARK_DISABLED:-"⌀"}" + LP_MARK_UNTRACKED="${LP_MARK_UNTRACKED:-"*"}" + LP_MARK_STASH="${LP_MARK_STASH:-"+"}" + LP_MARK_BRACKET_OPEN="${LP_MARK_BRACKET_OPEN:-"["}" + LP_MARK_BRACKET_CLOSE="${LP_MARK_BRACKET_CLOSE:-"]"}" + LP_MARK_SHORTEN_PATH="${LP_MARK_SHORTEN_PATH:-" … "}" + # LP_MARK_PREFIX="$LP_MARK_PREFIX" + + LP_COLOR_PATH=${LP_COLOR_PATH:-$BOLD} + LP_COLOR_PATH_ROOT=${LP_COLOR_PATH_ROOT:-$BOLD_YELLOW} + LP_COLOR_PROXY=${LP_COLOR_PROXY:-$BOLD_BLUE} + LP_COLOR_JOB_D=${LP_COLOR_JOB_D:-$YELLOW} + LP_COLOR_JOB_R=${LP_COLOR_JOB_R:-$BOLD_YELLOW} + LP_COLOR_JOB_Z=${LP_COLOR_JOB_Z:-$BOLD_YELLOW} + LP_COLOR_ERR=${LP_COLOR_ERR:-$PURPLE} + LP_COLOR_MARK=${LP_COLOR_MARK:-$BOLD} + LP_COLOR_MARK_ROOT=${LP_COLOR_MARK_ROOT:-$BOLD_RED} + LP_COLOR_USER_LOGGED=${LP_COLOR_USER_LOGGED:-""} + LP_COLOR_USER_ALT=${LP_COLOR_USER_ALT:-$BOLD} + LP_COLOR_USER_ROOT=${_ROOT:-$BOLD_YELLOW} + LP_COLOR_HOST=${LP_COLOR_HOST:-""} + LP_COLOR_SSH=${LP_COLOR_SSH:-$BLUE} + LP_COLOR_SU=${LP_COLOR_SU:-$BOLD_YELLOW} + LP_COLOR_TELNET=${LP_COLOR_TELNET:-$WARN_RED} + LP_COLOR_X11_ON=${LP_COLOR_X11:-$GREEN} + LP_COLOR_X11_OFF=${LP_COLOR_X11:-$YELLOW} + LP_COLOR_WRITE=${LP_COLOR_WRITE:-$GREEN} + LP_COLOR_NOWRITE=${LP_COLOR_NOWRITE:-$RED} + LP_COLOR_UP=${LP_COLOR_UP:-$GREEN} + LP_COLOR_COMMITS=${LP_COLOR_COMMITS:-$YELLOW} + LP_COLOR_CHANGES=${LP_COLOR_CHANGES:-$RED} + LP_COLOR_DIFF=${LP_COLOR_DIFF:-$PURPLE} + LP_COLOR_CHARGING_ABOVE=${LP_COLOR_CHARGING_ABOVE:-$GREEN} + LP_COLOR_CHARGING_UNDER=${LP_COLOR_CHARGING_UNDER:-$YELLOW} + LP_COLOR_DISCHARGING_ABOVE=${LP_COLOR_DISCHARGING_ABOVE:-$YELLOW} + LP_COLOR_DISCHARGING_UNDER=${LP_COLOR_DISCHARGING_UNDER:-$RED} + LP_COLOR_TIME=${LP_COLOR_TIME:-$BLUE} + LP_COLOR_IN_MULTIPLEXER=${LP_COLOR_IN_MULTIPLEXER:-$BOLD_BLUE} + LP_COLOR_RUNTIME=${LP_COLOR_RUNTIME:-$YELLOW} + + LP_COLORMAP_0=${LP_COLORMAP_0:-""} + LP_COLORMAP_1=${LP_COLORMAP_1:-$GREEN} + LP_COLORMAP_2=${LP_COLORMAP_2:-$BOLD_GREEN} + LP_COLORMAP_3=${LP_COLORMAP_3:-$YELLOW} + LP_COLORMAP_4=${LP_COLORMAP_4:-$BOLD_YELLOW} + LP_COLORMAP_5=${LP_COLORMAP_5:-$RED} + LP_COLORMAP_6=${LP_COLORMAP_6:-$BOLD_RED} + LP_COLORMAP_7=${LP_COLORMAP_7:-$WARN_RED} + LP_COLORMAP_8=${LP_COLORMAP_8:-$CRIT_RED} + LP_COLORMAP_9=${LP_COLORMAP_9:-$DANGER_RED} + + # Debugging flags + LP_DEBUG_TIME=${LP_DEBUG_TIME:-0} + + # Default config file may be the XDG standard ~/.config/liquidpromptrc, + # but heirloom dotfile has priority. + + local configfile + if [[ -f "/etc/liquidpromptrc" ]] + then + source "/etc/liquidpromptrc" + fi + if [[ -f "$HOME/.liquidpromptrc" ]] + then + configfile="$HOME/.liquidpromptrc" + elif [[ -z "$XDG_HOME_DIR" ]] + then + configfile="$HOME/.config/liquidpromptrc" + else + configfile="$XDG_HOME_DIR/liquidpromptrc" + fi + if [[ -f "$configfile" ]] + then + source "$configfile" + fi +} +# do source config files +_lp_source_config +unset _lp_source_config + +# Disable features if the tool is not installed +[[ "$LP_ENABLE_GIT" = 1 ]] && { command -v git >/dev/null || LP_ENABLE_GIT=0 ; } +[[ "$LP_ENABLE_SVN" = 1 ]] && { command -v svn >/dev/null || LP_ENABLE_SVN=0 ; } +[[ "$LP_ENABLE_FOSSIL" = 1 ]] && { command -v fossil >/dev/null || LP_ENABLE_FOSSIL=0 ; } +[[ "$LP_ENABLE_HG" = 1 ]] && { command -v hg >/dev/null || LP_ENABLE_HG=0 ; } +[[ "$LP_ENABLE_BZR" = 1 ]] && { command -v bzr > /dev/null || LP_ENABLE_BZR=0 ; } +case "$LP_OS" in + Darwin) [[ "$LP_ENABLE_BATT" = 1 ]] && { command -v pmset >/dev/null || LP_ENABLE_BATT=0 ; };; + *) [[ "$LP_ENABLE_BATT" = 1 ]] && { command -v acpi >/dev/null || LP_ENABLE_BATT=0 ; };; +esac + +if [[ "$LP_ENABLE_RUNTIME" == 1 ]] && ! $_LP_SHELL_bash; then + echo Unfortunately, runtime printing for zsh is not yet supported. Turn LP_ENABLE_RUNTIME off in your config to hide this message. + LP_ENABLE_RUNTIME=0 +fi + +# If we are running in a terminal multiplexer, brackets are colored +if [[ "$TERM" == screen* ]]; then + LP_BRACKET_OPEN="${LP_COLOR_IN_MULTIPLEXER}${LP_MARK_BRACKET_OPEN}${NO_COL}" + LP_BRACKET_CLOSE="${LP_COLOR_IN_MULTIPLEXER}${LP_MARK_BRACKET_CLOSE}${NO_COL}" + (( LP_ENABLE_TITLE = LP_ENABLE_TITLE && LP_ENABLE_SCREEN_TITLE )) + LP_TITLE_OPEN="$LP_SCREEN_TITLE_OPEN" + LP_TITLE_CLOSE="$LP_SCREEN_TITLE_CLOSE" +else + LP_BRACKET_OPEN="${LP_MARK_BRACKET_OPEN}" + LP_BRACKET_CLOSE="${LP_MARK_BRACKET_CLOSE}" +fi + +[[ "_$TERM" == _linux* ]] && LP_ENABLE_TITLE=0 + +# update_terminal_cwd is a shell function available on MacOS X Lion that +# will update an icon of the directory displayed in the title of the terminal +# window. +# See http://hints.macworld.com/article.php?story=20110722211753852 +if [[ "$TERM_PROGRAM" == Apple_Terminal ]] && command -v update_terminal_cwd >/dev/null; then + _LP_TERM_UPDATE_DIR=update_terminal_cwd + # Remove "update_terminal_cwd; " that has been add by Apple in /et/bashrc. + # See issue #196 + PROMPT_COMMAND="${PROMPT_COMMAND//update_terminal_cwd; /}" +else + _LP_TERM_UPDATE_DIR=: +fi + +# Default value for LP_PERM when LP_ENABLE_PERM is 0 +LP_PERM=: # without color + + +# Escape the given strings +# Must be used for all strings that may comes from remote sources, +# like VCS branch names +_lp_escape() +{ + printf "%q" "$*" +} + + +############### +# Who are we? # +############### + +# Yellow for root, bold if the user is not the login one, else no color. +if [[ "$EUID" -ne "0" ]] ; then # if user is not root + # if user is not login user + if [[ ${USER} != "$(logname 2>/dev/null || echo $LOGNAME)" ]]; then + LP_USER="${LP_COLOR_USER_ALT}${_LP_USER_SYMBOL}${NO_COL}" + else + if [[ "${LP_USER_ALWAYS}" -ne "0" ]] ; then + LP_USER="${LP_COLOR_USER_LOGGED}${_LP_USER_SYMBOL}${NO_COL}" + else + LP_USER="" + fi + fi +else # root! + LP_USER="${LP_COLOR_USER_ROOT}${_LP_USER_SYMBOL}${NO_COL}" + LP_COLOR_MARK="${LP_COLOR_MARK_ROOT}" + LP_COLOR_PATH="${LP_COLOR_PATH_ROOT}" + # Disable VCS info for all paths + if [[ "$LP_ENABLE_VCS_ROOT" != 1 ]]; then + LP_DISABLED_VCS_PATH=/ + LP_MARK_DISABLED="$_LP_MARK_SYMBOL" + fi +fi + + +################# +# Where are we? # +################# + +_lp_connection() +{ + if [[ -n "$SSH_CLIENT$SSH2_CLIENT$SSH_TTY" ]] ; then + echo ssh + else + # TODO check on *BSD + local sess_src="$(who am i | sed -n 's/.*(\(.*\))/\1/p')" + local sess_parent="$(ps -o comm= -p $PPID 2> /dev/null)" + if [[ -z "$sess_src" || "$sess_src" = ":"* ]] ; then + echo lcl # Local + elif [[ "$sess_parent" = "su" || "$sess_parent" = "sudo" ]] ; then + echo su # Remote su/sudo + else + echo tel # Telnet + fi + fi +} + +# Put the hostname if not locally connected +# color it in cyan within SSH, and a warning red if within telnet +# else diplay the host without color +# The connection is not expected to change from inside the shell, so we +# build this just once +LP_HOST="" +_chroot() +{ + if [[ -r /etc/debian_chroot ]] ; then + local debchroot + debchroot="$(cat /etc/debian_chroot)" + echo "(${debchroot})" + fi +} +LP_HOST="$(_chroot)" +unset _chroot + +# If we are connected with a X11 support +if [[ -n "$DISPLAY" ]] ; then + LP_HOST="${LP_COLOR_X11_ON}${LP_HOST}@${NO_COL}" +else + LP_HOST="${LP_COLOR_X11_OFF}${LP_HOST}@${NO_COL}" +fi + +case "$(_lp_connection)" in +lcl) + if [[ "${LP_HOSTNAME_ALWAYS}" -eq "0" ]] ; then + # FIXME do we want to display the chroot if local? + LP_HOST="" # no hostname if local + else + LP_HOST="${LP_HOST}${LP_COLOR_HOST}${_LP_HOST_SYMBOL}${NO_COL}" + fi + ;; +ssh) + # If we want a different color for each host + if [[ "$LP_ENABLE_SSH_COLORS" -eq "1" ]]; then + # compute the hash of the hostname + # and get the corresponding number in [1-6] (red,green,yellow,blue,purple or cyan) + # FIXME check portability of cksum and add more formats (bold? 256 colors?) + hash=$(( 1 + $(hostname | cksum | cut -d " " -f 1) % 6 )) + color=${_LP_OPEN_ESC}$(ti_setaf $hash)${_LP_CLOSE_ESC} + LP_HOST="${LP_HOST}${color}${_LP_HOST_SYMBOL}${NO_COL}" + unset hash + unset color + else + # the same color for all hosts + LP_HOST="${LP_HOST}${LP_COLOR_SSH}${_LP_HOST_SYMBOL}${NO_COL}" + fi + ;; +su) + LP_HOST="${LP_HOST}${LP_COLOR_SU}${_LP_HOST_SYMBOL}${NO_COL}" + ;; +tel) + LP_HOST="${LP_HOST}${LP_COLOR_TELNET}${_LP_HOST_SYMBOL}${NO_COL}" + ;; +*) + LP_HOST="${LP_HOST}${_LP_HOST_SYMBOL}" # defaults to no color + ;; +esac + +# Useless now, so undefine +unset _lp_connection + + +# Shorten the path of the current working directory +# * Show only the current directory +# * Show as much of the cwd path as possible, if shortened display a +# leading mark, such as ellipses, to indicate that part is missing +# * show at least LP_PATH_KEEP leading dirs and current directory +_lp_shorten_path() +{ + + if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 ]] ; then + LP_PWD="$LP_PATH_DEFAULT" + if $_LP_SHELL_bash; then + [[ -n "$PROMPT_DIRTRIM" ]] && _lp_set_dirtrim + fi + return + fi + + local ret="" + + local p="${PWD/#$HOME/~}" + local mask="${LP_MARK_SHORTEN_PATH}" + local -i max_len=$(( ${COLUMNS:-80} * $LP_PATH_LENGTH / 100 )) + + if [[ ${LP_PATH_KEEP} == -1 ]]; then + # only show the current directory, excluding any parent dirs + ret="${p##*/}" # discard everything upto and including the last slash + [[ "${ret}" == "" ]] && ret="/" # if in root directory + elif (( ${#p} <= ${max_len} )); then + ret="${p}" + elif [[ ${LP_PATH_KEEP} == 0 ]]; then + # len is over max len, show as much of the tail as is allowed + ret="${p##*/}" # show at least complete current directory + p="${p:0:${#p} - ${#ret}}" + ret="${mask}${p:${#p} - (${max_len} - ${#ret} - ${#mask})}${ret}" + else + # len is over max len, show at least LP_PATH_KEEP leading dirs and + # current directory + local tmp=${p//\//} + local -i delims=$(( ${#p} - ${#tmp} )) + + for (( dir=0; dir < ${LP_PATH_KEEP}; dir++ )); do + (( ${dir} == ${delims} )) && break + + local left="${p#*/}" + local name="${p:0:${#p} - ${#left}}" + p="${left}" + ret="${ret}${name%/}/" + done + + if (( ${delims} <= ${LP_PATH_KEEP} )); then + # no dirs between LP_PATH_KEEP leading dirs and current dir + ret="${ret}${p##*/}" + else + local base="${p##*/}" + + p="${p:0:${#p} - ${#base}}" + + [[ ${ret} != "/" ]] && ret="${ret%/}" # strip trailing slash + + local -i len_left=$(( ${max_len} - ${#ret} - ${#base} - ${#mask} )) + + ret="${ret}${mask}${p:${#p} - ${len_left}}${base}" + fi + fi + # Escape special chars + if $_LP_SHELL_bash; then + LP_PWD="${ret//\\/\\\\}" + else # zsh + LP_PWD="${ret//\%/%%}" + fi +} + +# In bash shell, PROMPT_DIRTRIM is the number of directory to keep at the end +# of the displayed path (if "\w" is present in the PS1 var). +# liquidprompt can calculate this number under two condition, path shortening +# must be disabled and PROMPT_DIRTRIM must be already set. +_lp_set_dirtrim() { + local p="${PWD/$HOME/~}" + local -i max_len=${COLUMNS:-80}*$LP_PATH_LENGTH/100 + local -i dt=0 + + if (( ${#p} > max_len )); then + local q="/${p##*/}" + local show="$q" + # +3 because of the ellipsis: "..." + while (( ${#show}+3 < max_len )) + do + (( dt++ )) + p="${p%$q}" + q="/${p##*/}" + show="$q$show" + done + (( dt == 0 )) && dt=1 + fi + PROMPT_DIRTRIM=$dt + # For debugging + # echo PROMPT_DIRTRIM=$PROMPT_DIRTRIM >&2 +} + + + +################ +# Related jobs # +################ + +# Display the count of each if non-zero: +# - detached screens sessions and/or tmux sessions running on the host +# - attached running jobs (started with $ myjob &) +# - attached stopped jobs (suspended with Ctrl-Z) +_lp_jobcount_color() +{ + [[ "$LP_ENABLE_JOBS" != 1 ]] && return + + local running=$(( $(jobs -r | wc -l) )) + local stopped=$(( $(jobs -s | wc -l) )) + local n_screen=$(screen -ls 2> /dev/null | grep -c '([Dd]etach[^)]*)$') + local n_tmux=$(tmux list-sessions 2> /dev/null | grep -cv attached) + local detached=$(( $n_screen + $n_tmux )) + local m_detached="d" + local m_stop="z" + local m_run="&" + local ret="" + + if [[ $detached != "0" ]] ; then + ret="${ret}${LP_COLOR_JOB_D}${detached}${m_detached}${NO_COL}" + fi + + if [[ $running != "0" ]] ; then + if [[ $ret != "" ]] ; then ret="${ret}/"; fi + ret="${ret}${LP_COLOR_JOB_R}${running}${m_run}${NO_COL}" + fi + + if [[ $stopped != "0" ]] ; then + if [[ $ret != "" ]] ; then ret="${ret}/"; fi + ret="${ret}${LP_COLOR_JOB_Z}${stopped}${m_stop}${NO_COL}" + fi + + echo -ne "$ret" +} + + + +###################### +# VCS branch display # +###################### + +_lp_are_vcs_enabled() +{ + [[ -z "$LP_DISABLED_VCS_PATH" ]] && return 0 + local path + local IFS=: + for path in $LP_DISABLED_VCS_PATH; do + [[ "$PWD" == *"$path"* ]] && return 1 + done + return 0 +} + +# GIT # + +# Get the branch name of the current directory +_lp_git_branch() +{ + [[ "$LP_ENABLE_GIT" != 1 ]] && return + local gitdir + gitdir="$([ $(\git ls-files . 2>/dev/null | wc -l) -gt 0 ] && \git rev-parse --git-dir 2>/dev/null)" + [[ $? -ne 0 || ! $gitdir =~ (.*\/)?\.git.* ]] && return + local branch + # Recent versions of Git support the --short option for symbolic-ref, but + # not 1.7.9 (Ubuntu 12.04) + if branch="$(\git symbolic-ref -q HEAD)"; then + _lp_escape "${branch#refs/heads/}" + else + # In detached head state, use commit instead + # No escape needed + \git rev-parse --short -q HEAD + fi +} + +# Set a color depending on the branch state: +# - green if the repository is up to date +# - yellow if there is some commits not pushed +# - red if there is changes to commit +# +# Add the number of pending commits and the impacted lines. +_lp_git_branch_color() +{ + [[ "$LP_ENABLE_GIT" != 1 ]] && return + + local branch + branch="$(_lp_git_branch)" + if [[ -n "$branch" ]] ; then + + local end + end="$NO_COL" + if LC_ALL=C \git status 2>/dev/null | grep -q '\(# Untracked\)'; then + end="$LP_COLOR_CHANGES$LP_MARK_UNTRACKED$end" + fi + + if [[ -n "$(\git stash list 2>/dev/null)" ]]; then + end="$LP_COLOR_COMMITS$LP_MARK_STASH$end" + fi + + local remote + remote="$(\git config --get branch.${branch}.remote 2>/dev/null)" + + local has_commit + has_commit=0 + if [[ -n "$remote" ]] ; then + local remote_branch + remote_branch="$(\git config --get branch.${branch}.merge)" + if [[ -n "$remote_branch" ]] ; then + has_commit="$(\git rev-list --no-merges --count ${remote_branch/refs\/heads/refs\/remotes\/$remote}..HEAD 2>/dev/null)" + if [[ -z "$has_commit" ]] ; then + has_commit=0 + fi + fi + fi + + local ret + local shortstat # only to check for uncommitted changes + shortstat="$(LC_ALL=C \git diff --shortstat HEAD 2>/dev/null)" + + if [[ -n "$shortstat" ]] ; then + local u_stat # shorstat of *unstaged* changes + u_stat="$(LC_ALL=C \git diff --shortstat 2>/dev/null)" + u_stat=${u_stat/*changed, /} # removing "n file(s) changed" + + local i_lines # inserted lines + if [[ "$u_stat" = *insertion* ]] ; then + i_lines=${u_stat/ inser*} + else + i_lines=0 + fi + + local d_lines # deleted lines + if [[ "$u_stat" = *deletion* ]] ; then + d_lines=${u_stat/*\(+\), } + d_lines=${d_lines/ del*/} + else + d_lines=0 + fi + + local has_lines + has_lines="+$i_lines/-$d_lines" + + if [[ "$has_commit" -gt "0" ]] ; then + # Changes to commit and commits to push + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL},${LP_COLOR_COMMITS}$has_commit${NO_COL})" + else + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL})" # changes to commit + fi + else + if [[ "$has_commit" -gt "0" ]] ; then + # some commit(s) to push + ret="${LP_COLOR_COMMITS}${branch}${NO_COL}(${LP_COLOR_COMMITS}$has_commit${NO_COL})" + else + ret="${LP_COLOR_UP}${branch}" # nothing to commit or push + fi + fi + echo -ne "$ret$end" + fi +} + +# Search upwards through a directory structure looking for a file/folder with +# the given name. Used to avoid invoking 'hg' and 'bzr'. +_lp_upwards_find() +{ + local dir + dir="$PWD" + while [[ -n "$dir" ]] + do + [[ -d "$dir/$1" ]] && return 0 + dir="${dir%/*}" + done + return 1 +} + +# MERCURIAL # + +# Get the branch name of the current directory +_lp_hg_branch() +{ + [[ "$LP_ENABLE_HG" != 1 ]] && return + + # First do a simple search to avoid having to invoke hg -- at least on my + # machine, the python startup causes a noticeable hitch when changing + # directories. + _lp_upwards_find .hg || return + + # We found an .hg folder, so we need to invoke hg and see if we're actually + # in a repository. + + local branch + branch="$(hg branch 2>/dev/null)" + + [[ $? -eq 0 ]] && _lp_escape "$branch" +} + +# Set a color depending on the branch state: +# - green if the repository is up to date +# - red if there is changes to commit +# - TODO: yellow if there is some commits not pushed +_lp_hg_branch_color() +{ + [[ "$LP_ENABLE_HG" != 1 ]] && return + + local branch + local ret + branch="$(_lp_hg_branch)" + if [[ -n "$branch" ]] ; then + + local has_untracked + has_untracked=$(hg status 2>/dev/null | grep '\(^\?\)' | wc -l) + if [[ -z "$has_untracked" ]] ; then + has_untracked="" + else + has_untracked="$LP_COLOR_CHANGES$LP_MARK_UNTRACKED" + fi + + local has_commit + has_commit=$(hg outgoing --no-merges ${branch} 2>/dev/null | grep '\(^changeset\:\)' | wc -l) + if [[ -z "$has_commit" ]] ; then + has_commit=0 + fi + + if [[ $(( $(hg status --quiet -n | wc -l) )) = 0 ]] ; then + if [[ "$has_commit" -gt "0" ]] ; then + # some commit(s) to push + ret="${LP_COLOR_COMMITS}${branch}${NO_COL}(${LP_COLOR_COMMITS}$has_commit${NO_COL})${has_untracked}${NO_COL}" + else + ret="${LP_COLOR_UP}${branch}${has_untracked}${NO_COL}" # nothing to commit or push + fi + else + local has_lines + has_lines=$(hg diff --stat 2>/dev/null | tail -n 1 | awk 'FS=" " {printf("+%s/-%s\n", $4, $6)}') + if [[ "$has_commit" -gt "0" ]] ; then + # Changes to commit and commits to push + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL},${LP_COLOR_COMMITS}$has_commit${NO_COL})${has_untracked}${NO_COL}" + else + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL})${has_untracked}${NO_COL}" # changes to commit + fi + fi + echo -ne "$ret" + fi +} + +# SUBVERSION # + +# Get the branch name of the current directory +# For the first level of the repository, gives the repository name +_lp_svn_branch() +{ + [[ "$LP_ENABLE_SVN" != 1 ]] && return + local root + local url + eval $(LANG=C LC_ALL=C svn info 2>/dev/null | sed -n 's/^URL: \(.*\)/url="\1"/p;s/^Repository Root: \(.*\)/root="\1"/p' ) + [[ -z "$root" ]] && return + + # Make url relative to root + url="${url:${#root}}" + if [[ "$url" == */trunk* ]] ; then + echo -n trunk + else + _lp_escape "$(expr "$url" : '.*/branches/\([^/]*\)' || expr "$url" : '/\([^/]*\)' || basename "$root")" + fi +} + +# Set a color depending on the branch state: +# - green if the repository is clean +# (use $LP_SVN_STATUS_OPTS to define what that means with +# the --depth option of 'svn status') +# - red if there is changes to commit +# Note that, due to subversion way of managing changes, +# informations are only displayed for the CURRENT directory. +_lp_svn_branch_color() +{ + [[ "$LP_ENABLE_SVN" != 1 ]] && return + + local branch + branch="$(_lp_svn_branch)" + if [[ -n "$branch" ]] ; then + local commits + changes=$(( $(svn status $LP_SVN_STATUS_OPTIONS | grep -c -v "?") )) + if [[ $changes -eq 0 ]] ; then + echo "${LP_COLOR_UP}${branch}${NO_COL}" + else + echo "${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_DIFF}$changes${NO_COL})" # changes to commit + fi + fi +} + + +# FOSSIL # + +# Get the tag name of the current directory +_lp_fossil_branch() +{ + [[ "$LP_ENABLE_FOSSIL" != 1 ]] && return + local branch + branch="$(fossil status 2>/dev/null | grep tags: | cut -c17-)" + if [[ -n "$branch" ]] ; then + _lp_escape "$branch" + else + if fossil info &>/dev/null ; then + echo "no-tag" + fi + fi +} + +# Set a color depending on the branch state: +# - green if the repository is clean +# - red if there is changes to commit +# - yellow if the branch has no tag name +# +# Add the number of impacted files with a +# + when files are ADDED or EDITED +# - when files are DELETED +_lp_fossil_branch_color() +{ + [[ "$LP_ENABLE_FOSSIL" != 1 ]] && return + + local branch + branch="$(_lp_fossil_branch)" + + if [[ -n "$branch" ]] ; then + local C2E # Modified files (added or edited) + local C2D # Deleted files + local C2A # Extras files + local C2AA # Added files + local PLUSLINE # Added lines + local MINUSLINE # Deleted lines + local ret + C2E=$(fossil changes | wc -l) + C2A=$(fossil extras | wc -l) + PLUSLINE=$(fossil diff | egrep '^\+[^+].+$' | wc -l) + MINUSLINE=$(fossil diff | egrep '^-[^-].+$' | wc -l) + ret="" + + C2AA=`fossil changes | grep ADDED | wc -l` + if [[ $C2AA -gt 0 ]] ; then + # We count the line "à la" git + local ADDFILE + local FILE + ADDFILE=`fossil changes | grep ADDED | sed -e 's/\s\{2,\}/ /g' | cut -d" " -f2` + for FILE in $ADDFILE ; do + PLULI=$(wc -l $FILE | cut -d" " -f1) + let PLUSLINE=$PLUSLINE+$PLULI + done + fi + + if [[ $PLUSLINE -gt 0 ]] ; then + ret+="+$PLUSLINE" + fi + + C2D=`fossil changes | grep DELETED | wc -l` + if [[ $C2D -gt 0 ]] ; then + # We count the line "à la" git + local DELFILE + local FILE + DELFILE=`fossil changes | grep DELETED | sed -e 's/\s\{2,\}/ /g' | cut -d" " -f2` + for FILE in $DELFILE ; do + MINLI=$(wc -l $FILE | cut -d" " -f1) + let MINUSLINE=$MINUSLINE+$MINLI + done + fi + + if [[ $MINUSLINE -gt 0 ]] ; then + if [[ "$ret" = "" ]] ; then + ret+="-$MINUSLINE" + else + ret+="/-$MINUSLINE" + fi + fi + + if [[ "$C2E" -gt 0 ]] ; then + ret+=" in $C2E" + fi + + if [[ "$C2A" -gt 0 ]] ; then + C2A="$LP_MARK_UNTRACKED" + else + C2A="" + fi + + if [[ "$ret" != "" ]] ; then + ret="(${LP_COLOR_DIFF}$ret${NO_COL})" + fi + + + if [[ "$branch" = "no-tag" ]] ; then + # Warning, your branch has no tag name ! + branch="${LP_COLOR_COMMITS}$branch${NO_COL}$ret${LP_COLOR_COMMITS}$C2A${NO_COL}" + else + if [[ "$C2E" -eq 0 && "$C2D" -eq 0 ]] ; then + # All is up-to-date + branch="${LP_COLOR_UP}$branch$C2A${NO_COL}" + else + # There're some changes to commit + branch="${LP_COLOR_CHANGES}$branch${NO_COL}$ret${LP_COLOR_CHANGES}$C2A${NO_COL}" + fi + fi + _lp_escape "$branch" + fi +} + +# Bazaar # + +# Get the branch name of the current directory +_lp_bzr_branch() +{ + [[ "$LP_ENABLE_BZR" != 1 ]] && return + + # First do a simple search to avoid having to invoke bzr -- at least on my + # machine, the python startup causes a noticeable hitch when changing + # directories. + _lp_upwards_find .bzr || return + + # We found an .bzr folder, so we need to invoke bzr and see if we're + # actually in a repository. + + local branch + branch="$(bzr nick 2> /dev/null)" + [[ $? -ne 0 ]] && return + _lp_escape "$branch" +} + + +# Set a color depending on the branch state: +# - green if the repository is up to date +# - red if there is changes to commit +# - TODO: yellow if there is some commits not pushed +# +# Add the number of pending commits and the impacted lines. +_lp_bzr_branch_color() +{ + [[ "$LP_ENABLE_BZR" != 1 ]] && return + + # First do a simple search to avoid having to invoke bzr -- at least on my + # machine, the python startup causes a noticeable hitch when changing + # directories. + _lp_upwards_find .bzr || return + + # We found an .bzr folder, so we need to invoke bzr and see if we're + # actually in a repository. + + local output + output="$(bzr version-info --check-clean --custom --template='{branch_nick} {revno} {clean}' 2> /dev/null)" + [[ $? -ne 0 ]] && return + local tuple=($output) + local branch=${tuple[_LP_FIRST_INDEX+0]} + local revno=${tuple[_LP_FIRST_INDEX+1]} + local clean=${tuple[_LP_FIRST_INDEX+2]} + + if [[ -n "$branch" ]] ; then + if [[ "$clean" -eq 0 ]] ; then + ret="${LP_COLOR_CHANGES}${branch}${NO_COL}(${LP_COLOR_COMMITS}$revno${NO_COL})" + else + ret="${LP_COLOR_UP}${branch}${NO_COL}(${LP_COLOR_COMMITS}$revno${NO_COL})" + fi + + fi + echo -ne "$ret" +} + + +#################### +# Wifi link status # +#################### +_lp_wifi() +{ + # Linux + sed -n '3s/^ *[^ ]* *[^ ]* *\([0-9]*\).*/\1/p' /proc/net/wireless +} + +################## +# Battery status # +################## + +# Get the battery status in percent +# returns 0 (and battery level) if battery is discharging and under threshold +# returns 1 (and battery level) if battery is discharging and above threshold +# returns 2 (and battery level) if battery is charging but under threshold +# returns 3 (and battery level) if battery is charging and above threshold +# returns 4 if no battery support +case "$LP_OS" in + Linux) + _lp_battery() + { + [[ "$LP_ENABLE_BATT" != 1 ]] && return + local acpi + acpi="$(acpi --battery 2>/dev/null)" + # Extract the battery load value in percent + # First, remove the beginning of the line... + local bat="${acpi#Battery *, }" + bat="${bat%%%*}" # remove everything starting at '%' + + if [[ -z "${bat}" ]] ; then + # not battery level found + return 4 + + # discharging + elif [[ "$acpi" == *"Discharging"* ]] ; then + if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then + # under threshold + echo -n "${bat}" + return 0 + else + # above threshold + echo -n "${bat}" + return 1 + fi + + # charging + else + if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then + # under threshold + echo -n "${bat}" + return 2 + else + # above threshold + echo -n "${bat}" + return 3 + fi + fi + } + ;; + Darwin) + _lp_battery() + { + [[ "$LP_ENABLE_BATT" != 1 ]] && return + local pmset="$(pmset -g batt | tail -n1)" + local bat="$(cut -f2 <<<"$pmset")" + bat="${bat%%%*}" + case "$pmset" in + *charged*) + return 4 + ;; + *discharging*) + if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then + # under threshold + echo -n "${bat}" + return 0 + else + # above threshold + echo -n "${bat}" + return 1 + fi + ;; + *) + if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then + # under threshold + echo -n "${bat}" + return 2 + else + # above threshold + echo -n "${bat}" + return 3 + fi + ;; + esac + } + ;; +esac + +# Compute a gradient of background/foreground colors depending on the battery status +# Display: +# a green ⏚ if the battery is charging and above threshold +# a yellow ⏚ if the battery is charging and under threshold +# a yellow ⌁ if the battery is discharging but above threshold +# a red ⌁ if the battery is discharging and above threshold +_lp_battery_color() +{ + [[ "$LP_ENABLE_BATT" != 1 ]] && return + + local mark=$LP_MARK_BATTERY + local chargingmark=$LP_MARK_ADAPTER + local bat + local ret + bat="$(_lp_battery)" + ret=$? + + if [[ $ret == 4 || $bat == 100 ]] ; then + # no battery support or battery full: nothing displayed + return + elif [[ $ret == 3 && $bat != 100 ]] ; then + # charging and above threshold and not 100% + # green ⏚ + echo -ne "${LP_COLOR_CHARGING_ABOVE}$chargingmark${NO_COL}" + return + elif [[ $ret == 2 ]] ; then + # charging but under threshold + # yellow ⏚ + echo -ne "${LP_COLOR_CHARGING_UNDER}$chargingmark${NO_COL}" + return + elif [[ $ret == 1 ]] ; then + # discharging but above threshold + # yellow ⌁ + echo -ne "${LP_COLOR_DISCHARGING_ABOVE}$mark${NO_COL}" + return + + # discharging and under threshold + elif [[ "$bat" != "" ]] ; then + ret="${LP_COLOR_DISCHARGING_UNDER}${mark}${NO_COL}" + + if [[ "$LP_PERCENTS_ALWAYS" -eq "1" ]]; then + if [[ ${bat} -le 100 ]] && [[ ${bat} -gt 80 ]] ; then # -20 + ret="${ret}${LP_COLORMAP_1}" + elif [[ ${bat} -le 80 ]] && [[ ${bat} -gt 65 ]] ; then # -15 + ret="${ret}${LP_COLORMAP_2}" + elif [[ ${bat} -le 65 ]] && [[ ${bat} -gt 50 ]] ; then # -15 + ret="${ret}${LP_COLORMAP_3}" + elif [[ ${bat} -le 50 ]] && [[ ${bat} -gt 40 ]] ; then # -10 + ret="${ret}${LP_COLORMAP_4}" + elif [[ ${bat} -le 40 ]] && [[ ${bat} -gt 30 ]] ; then # … + ret="${ret}${LP_COLORMAP_5}" + elif [[ ${bat} -le 30 ]] && [[ ${bat} -gt 20 ]] ; then + ret="${ret}${LP_COLORMAP_6}" + elif [[ ${bat} -le 20 ]] && [[ ${bat} -gt 10 ]] ; then + ret="${ret}${LP_COLORMAP_7}" + elif [[ ${bat} -le 10 ]] && [[ ${bat} -gt 5 ]] ; then + ret="${ret}${LP_COLORMAP_8}" + elif [[ ${bat} -le 5 ]] && [[ ${bat} -gt 0 ]] ; then + ret="${ret}${LP_COLORMAP_9}" + else + # for debugging purpose + ret="${ret}${LP_COLORMAP_0}" + fi + + if $_LP_SHELL_bash; then + ret="${ret}${bat}%" + else # zsh + ret="${ret}${bat}%%" + fi + fi # LP_PERCENTS_ALWAYS + echo -ne "${ret}${NO_COL}" + fi # ret +} + +_lp_color_map() { + # Default scale: 0..100 + # Custom scale: 0..$2 + local -i scale value + scale=${2:-100} + # Transform the value to a 0..100 scale + value=100*$1/scale + if (( value < 50 )); then + if (( value < 30 )); then + if (( value < 10 )); then + echo -ne "${LP_COLORMAP_0}" + elif (( value < 20 )); then + echo -ne "${LP_COLORMAP_1}" + else # 40..59 + echo -ne "${LP_COLORMAP_2}" + fi + elif (( value < 40 )); then + echo -ne "${LP_COLORMAP_3}" + else # 80..99 + echo -ne "${LP_COLORMAP_4}" + fi + elif (( value < 80 )); then + if (( value < 60 )); then + echo -ne "${LP_COLORMAP_5}" + elif (( value < 70 )); then + echo -ne "${LP_COLORMAP_6}" + else + echo -ne "${LP_COLORMAP_7}" + fi + elif (( value < 90 )) ; then + echo -ne "${LP_COLORMAP_8}" + else # (( value >= 90 )) + echo -ne "${LP_COLORMAP_9}" + fi +} + +########################### +# runtime of last command # +########################### + +_LP_RUNTIME_LAST_SECONDS=$SECONDS + +_lp_runtime() +{ + [[ "$LP_ENABLE_RUNTIME" != 1 ]] && return + if [[ $_LP_RUNTIME_SECONDS -ge $LP_RUNTIME_THRESHOLD ]] + then + echo -ne "${LP_COLOR_RUNTIME}" + # display runtime seconds as days, hours, minutes, and seconds + [[ "$_LP_RUNTIME_SECONDS" -ge 86400 ]] && echo -ne $((_LP_RUNTIME_SECONDS / 86400))d + [[ "$_LP_RUNTIME_SECONDS" -ge 3600 ]] && echo -ne $((_LP_RUNTIME_SECONDS % 86400 / 3600))h + [[ "$_LP_RUNTIME_SECONDS" -ge 60 ]] && echo -ne $((_LP_RUNTIME_SECONDS % 3600 / 60))m + echo -ne $((_LP_RUNTIME_SECONDS % 60))s + echo -ne "${NO_COL}" + fi +} + +_lp_reset_runtime() +{ + # Compute number of seconds since program was started + _LP_RUNTIME_SECONDS=$((SECONDS - _LP_RUNTIME_LAST_SECONDS)) + + # If no proper command was executed (i.e., someone pressed enter without entering a command), + # reset the runtime counter + [ "$_LP_RUNTIME_COMMAND_EXECUTED" != 1 ] && _LP_RUNTIME_LAST_SECONDS=$SECONDS && _LP_RUNTIME_SECONDS=0 + + # A proper command has been executed if the last command was not related to liquidprompt + [ "$BASH_COMMAND" = _lp_set_prompt ] && _LP_RUNTIME_COMMAND_EXECUTED=0 && return + _LP_RUNTIME_COMMAND_EXECUTED=1 +} + +if [ "$LP_ENABLE_RUNTIME" = 1 ] +then + # _lp_reset_runtime gets called whenever bash executes a command + trap '_lp_reset_runtime' DEBUG +fi + +############### +# System load # +############### + +# Compute a gradient of background/forground colors depending on the battery status +_lp_load_color() +{ + # Colour progression is important ... + # bold gray -> bold green -> bold yellow -> bold red -> + # black on red -> bold white on red + # + # Then we have to choose the values at which the colours switch, with + # anything past yellow being pretty important. + + [[ "$LP_ENABLE_LOAD" != 1 ]] && return + + local tmp="$(_lp_cpu_load)" + tmp=${tmp/./} # Remove '.' + tmp=${tmp#0} # Remove leading '0' + tmp=${tmp#0} # Remove leading '0', again (ex: 0.09) + local -i load=${tmp:-0}/$_lp_CPUNUM + + if (( load > $LP_LOAD_THRESHOLD )); then + local ret="$(_lp_color_map $load 200)${LP_MARK_LOAD}" + + if [[ "$LP_PERCENTS_ALWAYS" == 1 ]]; then + if $_LP_SHELL_bash; then + ret="${ret}$load%" + else # zsh + ret="${ret}$load%%" + fi + fi + echo -nE "${ret}${NO_COL}" + fi +} + +###################### +# System temperature # +###################### + +_lp_temp_sensors() +{ + # Return the hottest system temperature we get through the sensors command + local i + for i in $(sensors | + sed -n -r "s/^(CPU|SYS|MB|Core|temp).*: *\+([0-9]*)\..°.*/\2/p"); do + [[ $i -gt $temperature ]] && temperature=$i + done +} + +_lp_temp_acpi() +{ + local i + for i in $(acpi -t | sed 's/.* \([0-9]*\)\.[0-9]* degrees C$/\1/p') + do + [[ $i -gt $temperature ]] && temperature=$i + done +} + +# Will set _LP_TEMP_FUNCTION so the temperature monitoring feature use an +# available command. _LP_TEMP_FUNCTION should return only a numeric value +if [[ "$LP_ENABLE_TEMP" = 1 ]]; then + if command -v acpi >/dev/null; then + _LP_TEMP_FUNCTION=_lp_temp_acpi + elif command -v sensors >/dev/null; then + _LP_TEMP_FUNCTION=_lp_temp_sensors + # elif command -v the_command_you_want_to_use; then + # _LP_TEMP_FUNCTION=your_function + else + LP_ENABLE_TEMP=0 + fi +fi + +_lp_temperature() { + # Will display the numeric value as we got it through the _LP_TEMP_FUNCTION + # and colorize it through _lp_color_map. + [[ "$LP_ENABLE_TEMP" != 1 ]] && return + + local temperature + temperature=0 + $_LP_TEMP_FUNCTION + if [[ $temperature -ge $LP_TEMP_THRESHOLD ]]; then + echo -ne "${LP_MARK_TEMP}$(_lp_color_map $temperature 120)$temperature°${NO_COL}" + fi +} + +########## +# DESIGN # +########## + + +# Sed expression using extended regexp to remove shell codes around terminal +# escape sequences +_LP_CLEAN_ESC="$(printf "s,%q|%q,,g" "$_LP_OPEN_ESC" "$_LP_CLOSE_ESC")" + +# Remove all colors and escape characters of the given string and return a pure text +_lp_as_text() +{ + # Remove colors from the computed prompt + echo -n "$1" | sed -$_LP_SED_EXTENDED "s/\x1B\[[0-9;]*[mK]//g;$_LP_CLEAN_ESC" +} + +_lp_title() +{ + [[ "$LP_ENABLE_TITLE" != "1" ]] && return + + # Get the current computed prompt as pure text + echo -n "${_LP_OPEN_ESC}${LP_TITLE_OPEN}$(_lp_as_text "$1")${LP_TITLE_CLOSE}${_LP_CLOSE_ESC}" +} + +# Set the prompt mark to ± if git, to ☿ if mercurial, to ‡ if subversion +# to # if root and else $ +_lp_smart_mark() +{ + local mark + case "$LP_VCS_TYPE" in + git) mark="$LP_MARK_GIT" ;; + git-svn) mark="$LP_MARK_GIT$LP_MARK_SVN" ;; + hg) mark="$LP_MARK_HG" ;; + svn) mark="$LP_MARK_SVN" ;; + fossil) mark="$LP_MARK_FOSSIL" ;; + bzr) mark="$LP_MARK_BZR" ;; + disabled) mark="$LP_MARK_DISABLED" ;; + *) + if [[ -n "$LP_MARK_DEFAULT" ]]; then + mark=$LP_MARK_DEFAULT + else + mark="$_LP_MARK_SYMBOL" + fi + ;; + esac + echo -ne "${LP_COLOR_MARK}${mark}${NO_COL}" +} + +# insert a space on the right +_lp_sr() +{ + [[ -n "$1" ]] && echo -n "$1 " +} + +# insert a space on the left +_lp_sl() +{ + [[ -n "$1" ]] && echo -n " $1" +} + +# insert two space, before and after +_lp_sb() +{ + [[ -n "$1" ]] && echo -n " $1 " +} + +################### +# CURRENT TIME # +################### +_lp_time_analog() +{ + # get the date as "hours(12) minutes" in a single call + # make a bash array with it + local -a d + d=( $(date "+%I %M") ) + # separate hours and minutes + local -i hour=${d[_LP_FIRST_INDEX+0]#0} # no leading 0 + local -i min=${d[_LP_FIRST_INDEX+1]#0} + + # The targeted unicode characters are the "CLOCK FACE" ones + # They are located in the codepages between: + # U+1F550 (ONE OCLOCK) and U+1F55B (TWELVE OCLOCK), for the plain hours + # U+1F55C (ONE-THIRTY) and U+1F567 (TWELVE-THIRTY), for the thirties + # + + local -a plain + plain=(🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚 🕛 ) + local -a half + half=(🕜 🕝 🕞 🕟 🕠 🕡 🕢 🕣 🕤 🕥 🕦 🕧 ) + + # array index starts at 0 + local -i hi=hour-1 + + # add a space for correct alignment + if (( min < 15 )) ; then + echo -n "${plain[hi]} " + elif (( min < 45 )) ; then + echo -n "${half[hi]} " + else + echo -n "${plain[hi+1]} " + fi +} + +_lp_time() +{ + [[ "$LP_ENABLE_TIME" != 1 ]] && return + if [[ "$LP_TIME_ANALOG" != 1 ]]; then + echo -n "${LP_COLOR_TIME}${_LP_TIME_SYMBOL}${NO_COL}" + else + echo -n "${LP_COLOR_TIME}" + _lp_time_analog + echo -n "${NO_COL}" + fi +} + +######################## +# Construct the prompt # +######################## + + +_lp_set_prompt() +{ + # Display the return value of the last command, if different from zero + # As this get the last returned code, it should be called first + local -i err=$? + if (( err != 0 )); then + LP_ERR=" $LP_COLOR_ERR$err$NO_COL" + else + LP_ERR= # Hidden + fi + + # Reset IFS to its default value to avoid strange behaviors + # (in case the user is playing with the value at the prompt) + local IFS="$(echo -e ' \t') +" # space, tab, LF + + # execute the old prompt + eval "$LP_OLD_PROMPT_COMMAND" + + # left of main prompt: space at right + LP_JOBS="$(_lp_sr "$(_lp_jobcount_color)")" + LP_TEMP="$(_lp_sr "$(_lp_temperature)")" + LP_LOAD="$(_lp_sr "$(_lp_load_color)")" + LP_BATT="$(_lp_sr "$(_lp_battery_color)")" + LP_TIME="$(_lp_sr "$(_lp_time)")" + + # in main prompt: no space + if [[ "$LP_ENABLE_PROXY,$http_proxy" = 1,?* ]] ; then + LP_PROXY="$LP_COLOR_PROXY$LP_MARK_PROXY$NO_COL" + else + LP_PROXY= + fi + + # Display the current Python virtual environnement, if available + if [[ "$LP_ENABLE_VIRTUALENV,$VIRTUAL_ENV" = 1,?* ]] ; then + LP_VENV=" [${LP_COLOR_VIRTUALENV}${VIRTUAL_ENV##*/}${NO_COL}]" + else + LP_VENV= + fi + + LP_RUNTIME=$(_lp_sl "$(_lp_runtime)") + + # if change of working directory + if [[ "$LP_OLD_PWD" != "LP:$PWD" ]]; then + # Update directory icon for MacOS X + $_LP_TERM_UPDATE_DIR + + LP_VCS="" + LP_VCS_TYPE="" + # LP_HOST is a global set at load time + + # LP_PERM: shows a ":" + # - colored in green if user has write permission on the current dir + # - colored in red if not + if [[ "$LP_ENABLE_PERM" = 1 ]]; then + if [[ -w "${PWD}" ]]; then + LP_PERM="${LP_COLOR_WRITE}:${NO_COL}" + else + LP_PERM="${LP_COLOR_NOWRITE}:${NO_COL}" + fi + fi + + _lp_shorten_path # set LP_PWD + + if _lp_are_vcs_enabled; then + LP_VCS="$(_lp_git_branch_color)" + LP_VCS_TYPE="git" + if [[ -n "$LP_VCS" ]]; then + # If this is a git-svn repository + if [[ -d "$(\git rev-parse --git-dir 2>/dev/null)/svn" ]]; then + LP_VCS_TYPE="git-svn" + fi # git-svn + else + LP_VCS="$(_lp_hg_branch_color)" + LP_VCS_TYPE="hg" + if [[ -z "$LP_VCS" ]]; then + LP_VCS="$(_lp_svn_branch_color)" + LP_VCS_TYPE="svn" + if [[ -z "$LP_VCS" ]]; then + LP_VCS="$(_lp_fossil_branch_color)" + LP_VCS_TYPE="fossil" + if [[ -z "$LP_VCS" ]]; then + LP_VCS="$(_lp_bzr_branch_color)" + LP_VCS_TYPE="bzr" + if [[ -z "$LP_VCS" ]]; then + LP_VCS="" + LP_VCS_TYPE="" + fi # nothing + fi # bzr + fi # fossil + fi # svn + fi # hg + + else # if this vcs rep is disabled + LP_VCS="" # not necessary, but more readable + LP_VCS_TYPE="disabled" + fi + + if [[ -z "$LP_VCS_TYPE" ]] ; then + LP_VCS="" + else + LP_VCS="$(_lp_sl "${LP_VCS}")" + fi + + # end of the prompt line: double spaces + LP_MARK="$(_lp_sb "$(_lp_smart_mark $LP_VCS_TYPE)")" + + # The color is different if user is root + LP_PWD="${LP_COLOR_PATH}${LP_PWD}${NO_COL}" + + LP_OLD_PWD="LP:$PWD" + + # if do not change of working directory but... + elif [[ -n "$LP_VCS_TYPE" ]]; then # we are still in a VCS dir + case "$LP_VCS_TYPE" in + # git, git-svn + git*) LP_VCS="$(_lp_sl "$(_lp_git_branch_color)")";; + hg) LP_VCS="$(_lp_sl "$(_lp_hg_branch_color)")";; + svn) LP_VCS="$(_lp_sl "$(_lp_svn_branch_color)")";; + fossil) LP_VCS="$(_lp_sl "$(_lp_fossil_branch_color)")";; + bzr) LP_VCS="$(_lp_sl "$(_lp_bzr_branch_color)")";; + disabled)LP_VCS="";; + esac + fi + + if [[ -f "$LP_PS1_FILE" ]]; then + source "$LP_PS1_FILE" + fi + + if [[ -z "$LP_PS1" ]] ; then + # add title escape time, jobs, load and battery + PS1="${LP_PS1_PREFIX}${LP_TIME}${LP_BATT}${LP_LOAD}${LP_TEMP}${LP_JOBS}" + # add user, host and permissions colon + PS1="${PS1}${LP_BRACKET_OPEN}${LP_USER}${LP_HOST}${LP_PERM}" + + PS1="${PS1}${LP_PWD}${LP_BRACKET_CLOSE}${LP_VENV}${LP_PROXY}" + + # Add VCS infos + # If root, the info has not been collected unless LP_ENABLE_VCS_ROOT + # is set. + PS1="${PS1}${LP_VCS}" + + # add return code and prompt mark + PS1="${PS1}${LP_RUNTIME}${LP_ERR}${LP_MARK_PREFIX}${LP_MARK}${LP_PS1_POSTFIX}" + + # "invisible" parts + # Get the current prompt on the fly and make it a title + LP_TITLE="$(_lp_title "$PS1")" + + # Insert it in the prompt + PS1="${LP_TITLE}${PS1}" + + # Glue the bash prompt always go to the first column. + # Avoid glitches after interrupting a command with Ctrl-C + # Does not seem to be necessary anymore? + #PS1="\[\033[G\]${PS1}${NO_COL}" + else + PS1=$LP_PS1 + fi +} + +prompt_tag() +{ + export LP_PS1_PREFIX="$(_lp_sr "$1")" +} + +# Activate the liquid prompt +prompt_on() +{ + # if liquidprompt has not been already set + if [[ -z "$LP_LIQUIDPROMPT" ]] ; then + LP_OLD_PS1="$PS1" + if $_LP_SHELL_bash; then + LP_OLD_PROMPT_COMMAND="$PROMPT_COMMAND" + else # zsh + LP_OLD_PROMPT_COMMAND="$precmd" + fi + fi + if $_LP_SHELL_bash; then + PROMPT_COMMAND=_lp_set_prompt + [[ "$LP_DEBUG_TIME" == 1 ]] && PROMPT_COMMAND="time $PROMPT_COMMAND" + else # zsh + # That doesn't seem to work: no time output + #if [[ "$LP_DEBUG_TIME" == 1 ]]; then + # function precmd { + # local TIMEFMT='LiquidPrompt build time: %*E' + # time _lp_set_prompt + # } + #else + function precmd { + _lp_set_prompt + } + #fi + fi + + # Keep in mind that LP has been sourced + # (to avoid recursive prompt command). + LP_LIQUIDPROMPT=1 +} + +# Come back to the old prompt +prompt_off() +{ + PS1=$LP_OLD_PS1 + if $_LP_SHELL_bash; then + PROMPT_COMMAND="$LP_OLD_PROMPT_COMMAND" + else # zsh + precmd="$LP_OLD_PROMPT_COMMAND" + fi +} + +# Use an empty prompt: just the \$ mark +prompt_OFF() +{ + PS1="\$ " + if $_LP_SHELL_bash; then + PROMPT_COMMAND="$LP_OLD_PROMPT_COMMAND" + else # zsh + precmd="$LP_OLD_PROMPT_COMMAND" + fi +} + +# By default, sourcing liquidprompt will activate the liquid prompt +prompt_on + +# vim: set et sts=4 sw=4 tw=120 ft=sh: diff --git a/roles/common/files/root-.inputrc b/roles/common/files/root-.inputrc new file mode 100644 index 0000000..21751b6 --- /dev/null +++ b/roles/common/files/root-.inputrc @@ -0,0 +1,8 @@ +### THIS FILE IS DEPLOYED BY ANSIBLE + +# alternate mappings for "page up" and "page down" to search the history +"\e[5~": history-search-backward +"\e[6~": history-search-forward +"\e[1;5C": forward-word # ctrl + right +"\e[1;5D": backward-word # ctrl + left + diff --git a/roles/common/files/root-.profile b/roles/common/files/root-.profile new file mode 100644 index 0000000..94e4976 --- /dev/null +++ b/roles/common/files/root-.profile @@ -0,0 +1,12 @@ +if [ "$BASH" ]; then + if [ -f ~/.bashrc ]; then + . ~/.bashrc + fi +fi + +mesg n + +echo +echo "--- dernières modifications:" +echo +cat /root/README.localserver diff --git a/roles/common/files/root-.vim-colors-wombat.vim b/roles/common/files/root-.vim-colors-wombat.vim new file mode 100644 index 0000000..34ca22d --- /dev/null +++ b/roles/common/files/root-.vim-colors-wombat.vim @@ -0,0 +1,52 @@ +" Maintainer: Lars H. Nielsen (dengmao@gmail.com) +" Cterm addition: Paul deGrandis +" Last Change: January 22 2007 + +set background=dark + +hi clear + +if exists("syntax_on") + syntax reset +endif + +let colors_name = "wombat" + + +" Vim >= 7.0 specific colors +if version >= 700 + hi CursorLine guibg=#2d2d2d ctermbg=236 + hi CursorColumn guibg=#2d2d2d ctermbg=236 + hi MatchParen guifg=#f6f3e8 guibg=#857b6f gui=bold ctermfg=230 ctermbg=101 cterm=bold + hi Pmenu guifg=#f6f3e8 guibg=#444444 ctermfg=230 ctermbg=238 + hi PmenuSel guifg=#000000 guibg=#cae682 ctermfg=0 ctermbg=186 +endif + +" General colors +hi Cursor guifg=NONE guibg=#656565 gui=none ctermfg=NONE ctermbg=241 cterm=none +hi Normal guifg=#f6f3e8 guibg=#242424 gui=none ctermfg=230 ctermbg=235 cterm=none +hi NonText guifg=#808080 guibg=#303030 gui=none ctermfg=244 ctermbg=236 cterm=none +hi LineNr guifg=#857b6f guibg=#000000 gui=none ctermfg=101 ctermbg=0 cterm=none +hi StatusLine guifg=#f6f3e8 guibg=#444444 gui=italic ctermfg=230 ctermbg=238 cterm=italic +hi StatusLineNC guifg=#857b6f guibg=#444444 gui=none ctermfg=101 ctermbg=238 cterm=none +hi VertSplit guifg=#444444 guibg=#444444 gui=none ctermfg=238 ctermbg=238 cterm=none +hi Folded guibg=#384048 guifg=#a0a8b0 gui=none ctermbg=237 ctermfg=248 cterm=none +hi Title guifg=#f6f3e8 guibg=NONE gui=bold ctermfg=230 ctermbg=NONE cterm=bold +hi Visual guifg=#f6f3e8 guibg=#444444 gui=none ctermfg=230 ctermbg=238 cterm=none +hi SpecialKey guifg=#808080 guibg=#343434 gui=none ctermfg=244 ctermbg=236 cterm=none + +" Syntax highlighting +hi Comment guifg=#99968b gui=italic ctermfg=246 cterm=none +hi Todo guifg=#8f8f8f gui=italic ctermfg=245 cterm=none +hi Constant guifg=#e5786d gui=none ctermfg=185 cterm=none +hi String guifg=#95e454 gui=italic ctermfg=154 cterm=none +hi Identifier guifg=#cae682 gui=none ctermfg=186 cterm=none +hi Function guifg=#cae682 gui=none ctermfg=187 cterm=bold +hi Type guifg=#cae682 gui=none ctermfg=186 cterm=none +hi Statement guifg=#8ac6f2 gui=none ctermfg=lightblue cterm=bold +hi Keyword guifg=#8ac6f2 gui=none ctermfg=105 cterm=none +hi PreProc guifg=#e5786d gui=none ctermfg=173 cterm=none +hi Number guifg=#e5786d gui=none ctermfg=185 cterm=none +hi Special guifg=#e7f6da gui=none ctermfg=7 cterm=none + + diff --git a/roles/common/files/root-.vimrc b/roles/common/files/root-.vimrc new file mode 100644 index 0000000..b28610a --- /dev/null +++ b/roles/common/files/root-.vimrc @@ -0,0 +1,25 @@ +syntax on +set nocp +color wombat +set t_Co=256 +let html_use_css=1 +let html_no_pre=1 +let html_use_xhtml=1 +set ts=2 +set ruler +set backspace=2 +set wildmode=list:full +set shiftwidth=4 +set tabstop=4 +set shiftwidth=4 +set expandtab +filetype on +filetype plugin on +autocmd FileType python set omnifunc=pythoncomplete#Complete +autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS +autocmd FileType html set omnifunc=htmlcomplete#CompleteTags +autocmd FileType css set omnifunc=csscomplete#CompleteCSS +autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags +autocmd FileType php set omnifunc=phpcomplete#CompletePHP +autocmd FileType c set omnifunc=ccomplete#Complete +autocmd FileType cpp set omnifunc=ccomplete#Complete diff --git a/roles/common/files/root-README.root b/roles/common/files/root-README.root new file mode 100644 index 0000000..945f1c9 --- /dev/null +++ b/roles/common/files/root-README.root @@ -0,0 +1 @@ +---- Deploiement du serveur via ansible ----- diff --git a/roles/common/handlers/main.yml b/roles/common/handlers/main.yml new file mode 100644 index 0000000..30bb051 --- /dev/null +++ b/roles/common/handlers/main.yml @@ -0,0 +1,44 @@ +--- +# handlers du role common + +- name: update aliases + command: newaliases + +- name: restart munin-node + service: name=munin-node state=restarted + +- name: restart hobbit-client + service: name=hobbit-client state=restarted + +- name: restart xymon-client + service: name=xymon-client state=restarted + +- name: restart gwm + service: name=gwm state=restarted + +- name: restart xend + service: name=xend state=restarted + +- name: update-grub + command: update-grub + +- name: restart collectd + service: name=collectd state=restarted + +- name: restart ntp + service: name=ntp state=restarted + +- name: restart xymon + service: name=xymon state=restarted + +- name: update mysql_relay_domains map + shell: postmap /etc/postfix/mysql_relay_domains.cf + +- name: restart postfix + service: name=postfix state=restarted + +- name: apt-update + command: apt-get update + +# vim: set textwidth=0 ft=yaml ts=2 sw=2 expandtab: + diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml new file mode 100644 index 0000000..220de5c --- /dev/null +++ b/roles/common/tasks/main.yml @@ -0,0 +1,119 @@ +--- +# Role commun à toutes les machines +# vars: admin_ssh_keys, repositories, default_packages, ntp_server1, ntp_server2, description + +# Bootstrap if necessary (install python-apt) + +- name: Bootstrap if necessary + tags: bootstrap + raw: python -c "import apt" || DEBIAN_FRONTEND=noninteractive apt-get --force-yes -y install python-apt + when: ansible_distribution == 'Debian' + +# Check mandatory variables + +- name: Check vars + fail: msg="Missing variable admin_ssh_keys" + when: admin_ssh_keys is not defined + +- name: Check vars + fail: msg="Missing variable default_packages_debian" + when: default_packages_debian is not defined + +- name: Check vars + fail: msg="Missing variable description" + when: description is not defined + +- name: Check vars + fail: msg="Missing variable ntp_server1" + when: ntp_server1 is not defined + +- name: Check vars + fail: msg="Missing variable ntp_server2" + when: ntp_server2 is not defined + +- name: Check vars + fail: msg="Missing variable disable_ipv6" + when: disable_ipv6 is not defined + +# SSH + +- name: Deploy SSH keys + tags: ssh_keys + authorized_key: user=root key="{{item}}" + with_items: "{{admin_ssh_keys}}" + +# Packages + +- name: Install default packages Debian. + apt: pkg={{item}} state=installed + with_items: "{{ default_packages_debian }}" + when: ansible_distribution == 'Debian' + +# Configure NTP + +- name: Configure NTP + tags: ntp + template: src=etc-ntp.conf.j2 dest=/etc/ntp.conf + + +# Basic Shell & vim configuration + +- name: Custom .bashrc + tags: custom + copy: src=root-.bashrc dest=/root/.bashrc +- name: Custom .vimrc + tags: custom + copy: src=root-.vimrc dest=/root/.vimrc +- name: Custom .inputrc + tags: custom + copy: src=root-.inputrc dest=/root/.inputrc +- name: Create .vim/colors + tags: custom + file: path=/root/.vim/colors state=directory +- name: Wombat vim colors theme is awesome + tags: custom + copy: src=root-.vim-colors-wombat.vim dest=/root/.vim/colors/wombat.vim + +# Set motd and README.root + +- name: Set the motd + tags: custom + template: src=etc-motd.j2 dest=/etc/motd + +- name: Modify /root/.profile + tags: custom + copy: src=root-.profile dest=/root/.profile + when: initialize == True +- name: Add basic README.root + tags: custom + copy: src=root-README.root dest=/root/README.root + when: initialize == True + +# Env setup + +- name: Set the locale + tags: environ + debconf: name=locales question='default_environment_locale' value='fr_FR.UTF-8' vtype='multiselect' + debconf: name=locales question='locales_to_be_generated' value='en_US.UTF-8 UTF-8, fr_FR.UTF-8 UTF-8' vtype='multiselect' + debconf: name=locales question='locales/locales_to_be_generated' value='fr_FR.UTF-8, UTF-8' vtype='multiselect' + when: ansible_distribution == 'Debian' + +- name: Set the timezone + tags: environ + debconf: name=tzdata question='tzdata/Zones/Etc' value='UTC' vtype='select' + debconf: name=tzdata question='tzdata/Areas' value='Europe' vtype='select' + debconf: name=tzdata question='tzdata/Zones/Europe' value='Paris' vtype='select' + when: ansible_distribution == 'Debian' + +- name: Disable IPv6 (need reboot) + tags: environ + lineinfile: dest=/etc/modprobe.d/blacklist line="blacklist ipv6" state="present" create="yes" + when: ansible_distribution == 'Debian' and disable_ipv6 == 'yes' + +- name: Enable IPv6 (need reboot) + tags: environ + lineinfile: dest=/etc/modprobe.d/blacklist line="blacklist ipv6" state="absent" create="yes" + when: ansible_distribution == 'Debian' and disable_ipv6 == 'no' + + +# vim: set textwidth=0 ft=yaml ts=2 sw=2 expandtab: diff --git a/roles/common/templates/etc-motd.j2 b/roles/common/templates/etc-motd.j2 new file mode 100644 index 0000000..3187aa6 --- /dev/null +++ b/roles/common/templates/etc-motd.j2 @@ -0,0 +1,4 @@ + + {{ ansible_fqdn }} - {{ description }} + + diff --git a/roles/common/templates/etc-ntp.conf.j2 b/roles/common/templates/etc-ntp.conf.j2 new file mode 100644 index 0000000..c4cc437 --- /dev/null +++ b/roles/common/templates/etc-ntp.conf.j2 @@ -0,0 +1,17 @@ +### THIS FILE IS DEPLOYED BY ANSIBLE + + +driftfile /var/lib/ntp/ntp.drift +statistics loopstats peerstats clockstats +filegen loopstats file loopstats type day enable +filegen peerstats file peerstats type day enable +filegen clockstats file clockstats type day enable +server {{ ntp_server1 }} +server {{ ntp_server2 }} +restrict default ignore +restrict -6 default ignore +restrict 127.0.0.1 +restrict ::1 +restrict {{ ntp_server1 }} nomodify nopeer +restrict {{ ntp_server2 }} nomodify nopeer +