diff --git a/roles/core/files/root-dotfiles/bash-preexec b/roles/core/files/root-dotfiles/bash-preexec new file mode 100644 index 0000000..d93cb9a --- /dev/null +++ b/roles/core/files/root-dotfiles/bash-preexec @@ -0,0 +1,292 @@ +#!/bin/bash +# +# bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions. +# https://github.com/rcaloras/bash-preexec +# +# +# 'preexec' functions are executed before each interactive command is +# executed, with the interactive command as its argument. The 'precmd' +# function is executed before each prompt is displayed. +# +# Author: Ryan Caloras (ryan@bashhub.com) +# Forked from Original Author: Glyph Lefkowitz +# +# V0.3.1 +# + +# General Usage: +# +# 1. Source this file at the end of your bash profile so as not to interfere +# with anything else that's using PROMPT_COMMAND. +# +# 2. Add any precmd or preexec functions by appending them to their arrays: +# e.g. +# precmd_functions+=(my_precmd_function) +# precmd_functions+=(some_other_precmd_function) +# +# preexec_functions+=(my_preexec_function) +# +# 3. If you have anything that's using the Debug Trap, change it to use +# preexec. (Optional) change anything using PROMPT_COMMAND to now use +# precmd instead. +# +# Note: This module requires two bash features which you must not otherwise be +# using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. prexec_and_precmd_install +# will override these and if you override one or the other this will most likely break. + +# Avoid duplicate inclusion +if [[ "$__bp_imported" == "defined" ]]; then + return 0 +fi +__bp_imported="defined" + +# Should be available to each precmd and preexec +# functions, should they want it. +__bp_last_ret_value="$?" + +# Command to set our preexec trap. It's invoked once via +# PROMPT_COMMAND and then removed. +__bp_trap_install_string="trap '__bp_preexec_invoke_exec' DEBUG;" + +# Remove ignorespace and or replace ignoreboth from HISTCONTROL +# so we can accurately invoke preexec with a command from our +# history even if it starts with a space. +__bp_adjust_histcontrol() { + local histcontrol + histcontrol="${HISTCONTROL//ignorespace}" + # Replace ignoreboth with ignoredups + if [[ "$histcontrol" == *"ignoreboth"* ]]; then + histcontrol="ignoredups:${histcontrol//ignoreboth}" + fi; + export HISTCONTROL="$histcontrol" +} + +# This variable describes whether we are currently in "interactive mode"; +# i.e. whether this shell has just executed a prompt and is waiting for user +# input. It documents whether the current command invoked by the trace hook is +# run interactively by the user; it's set immediately after the prompt hook, +# and unset as soon as the trace hook is run. +__bp_preexec_interactive_mode="" + +__bp_trim_whitespace() { + local var=$@ + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +# This function is installed as part of the PROMPT_COMMAND; +# It sets a variable to indicate that the prompt was just displayed, +# to allow the DEBUG trap to know that the next command is likely interactive. +__bp_interactive_mode() { + __bp_preexec_interactive_mode="on"; +} + + +# This function is installed as part of the PROMPT_COMMAND. +# It will invoke any functions defined in the precmd_functions array. +__bp_precmd_invoke_cmd() { + + # Save the returned value from our last command + __bp_last_ret_value="$?" + + # For every function defined in our function array. Invoke it. + local precmd_function + for precmd_function in "${precmd_functions[@]}"; do + + # Only execute this function if it actually exists. + # Test existence of functions with: declare -[Ff] + if type -t "$precmd_function" 1>/dev/null; then + __bp_set_ret_value $__bp_last_ret_value + $precmd_function + fi + done +} + +# Sets a return value in $?. We may want to get access to the $? variable in our +# precmd functions. This is available for instance in zsh. We can simulate it in bash +# by setting the value here. +__bp_set_ret_value() { + return $1 +} + +__bp_in_prompt_command() { + + local prompt_command_array + IFS=';' read -ra prompt_command_array <<< "$PROMPT_COMMAND" + + local trimmed_arg + trimmed_arg=$(__bp_trim_whitespace "$1") + + local command + for command in "${prompt_command_array[@]}"; do + local trimmed_command + trimmed_command=$(__bp_trim_whitespace "$command") + # Only execute each function if it actually exists. + if [[ "$trimmed_command" == "$trimmed_arg" ]]; then + return 0 + fi + done + + return 1 +} + +# This function is installed as the DEBUG trap. It is invoked before each +# interactive prompt display. Its purpose is to inspect the current +# environment to attempt to detect if the current command is being invoked +# interactively, and invoke 'preexec' if so. +__bp_preexec_invoke_exec() { + + # Checks if the file descriptor is not standard out (i.e. '1') + # __bp_delay_install checks if we're in test. Needed for bats to run. + # Prevents preexec from being invoked for functions in PS1 + if [[ ! -t 1 && -z "$__bp_delay_install" ]]; then + return + fi + + if [[ -n "$COMP_LINE" ]]; then + # We're in the middle of a completer. This obviously can't be + # an interactively issued command. + return + fi + if [[ -z "$__bp_preexec_interactive_mode" ]]; then + # We're doing something related to displaying the prompt. Let the + # prompt set the title instead of me. + return + else + # If we're in a subshell, then the prompt won't be re-displayed to put + # us back into interactive mode, so let's not set the variable back. + # In other words, if you have a subshell like + # (sleep 1; sleep 2) + # You want to see the 'sleep 2' as a set_command_title as well. + if [[ 0 -eq "$BASH_SUBSHELL" ]]; then + __bp_preexec_interactive_mode="" + fi + fi + + if __bp_in_prompt_command "$BASH_COMMAND"; then + # If we're executing something inside our prompt_command then we don't + # want to call preexec. Bash prior to 3.1 can't detect this at all :/ + __bp_preexec_interactive_mode="" + return + fi + + local this_command + this_command=$(HISTTIMEFORMAT= history 1 | { read -r _ this_command; echo "$this_command"; }) + + # Sanity check to make sure we have something to invoke our function with. + if [[ -z "$this_command" ]]; then + return + fi + + # If none of the previous checks have returned out of this function, then + # the command is in fact interactive and we should invoke the user's + # preexec functions. + + # For every function defined in our function array. Invoke it. + local preexec_function + for preexec_function in "${preexec_functions[@]}"; do + + # Only execute each function if it actually exists. + # Test existence of function with: declare -[fF] + if type -t "$preexec_function" 1>/dev/null; then + __bp_set_ret_value $__bp_last_ret_value + $preexec_function "$this_command" + fi + done +} + +# Returns PROMPT_COMMAND with a semicolon appended +# if it doesn't already have one. +__bp_prompt_command_with_semi_colon() { + + # Trim our existing PROMPT_COMMAND + local trimmed + trimmed=$(__bp_trim_whitespace "$PROMPT_COMMAND") + + # Take our existing prompt command and append a semicolon to it + # if it doesn't already have one. + local existing_prompt_command + if [[ -n "$trimmed" ]]; then + existing_prompt_command=${trimmed%${trimmed##*[![:space:]]}} + existing_prompt_command=${existing_prompt_command%;} + existing_prompt_command=${existing_prompt_command/%/;} + else + existing_prompt_command="" + fi + + echo -n "$existing_prompt_command" +} + +__bp_install() { + + # Remove setting our trap from PROMPT_COMMAND + PROMPT_COMMAND="${PROMPT_COMMAND//$__bp_trap_install_string}" + + # Remove this function from our PROMPT_COMMAND + PROMPT_COMMAND="${PROMPT_COMMAND//__bp_install;}" + + # Exit if we already have this installed. + if [[ "$PROMPT_COMMAND" == *"__bp_precmd_invoke_cmd"* ]]; then + return 1; + fi + + # Adjust our HISTCONTROL Variable if needed. + __bp_adjust_histcontrol + + + # Issue #25. Setting debug trap for subshells causes sessions to exit for + # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash. + # + # Disabling this by default. It can be enabled by setting this variable. + if [[ -n "$__bp_enable_subshells" ]]; then + + # Set so debug trap will work be invoked in subshells. + set -o functrace > /dev/null 2>&1 + shopt -s extdebug > /dev/null 2>&1 + fi; + + + local existing_prompt_command + existing_prompt_command=$(__bp_prompt_command_with_semi_colon) + + # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've + # actually entered something. + PROMPT_COMMAND="__bp_precmd_invoke_cmd; ${existing_prompt_command} __bp_interactive_mode;" + trap '__bp_preexec_invoke_exec' DEBUG; + + # Add two functions to our arrays for convenience + # of definition. + precmd_functions+=(precmd) + preexec_functions+=(preexec) + + # Since this is in PROMPT_COMMAND, invoke any precmd functions we have defined. + __bp_precmd_invoke_cmd + # Put us in interactive mode for our first command. + __bp_interactive_mode +} + +# Sets our trap and __bp_install as part of our PROMPT_COMMAND to install +# after our session has started. This allows bash-preexec to be inlucded +# at any point in our bash profile. Ideally we could set our trap inside +# __bp_install, but if a trap already exists it'll only set locally to +# the function. +__bp_install_after_session_init() { + + # Make sure this is bash that's running this and return otherwise. + if [[ -z "$BASH_VERSION" ]]; then + return 1; + fi + + local existing_prompt_command + existing_prompt_command=$(__bp_prompt_command_with_semi_colon) + + # Add our installation to be done last via our PROMPT_COMMAND. These are + # removed by __bp_install when it's invoked so it only runs once. + PROMPT_COMMAND="${existing_prompt_command} $__bp_trap_install_string __bp_install;" +} + +# Run our install so long as we're not delaying it. +if [[ -z "$__bp_delay_install" ]]; then + __bp_install_after_session_init +fi; diff --git a/roles/core/files/root-dotfiles/bashrc b/roles/core/files/root-dotfiles/bashrc new file mode 100644 index 0000000..c8c0fa7 --- /dev/null +++ b/roles/core/files/root-dotfiles/bashrc @@ -0,0 +1,135 @@ +# ~/.bashrc: executed by bash(1) for non-login shells. +# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) +# for examples + +# 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 + +# After each command, append to the history file and reread it +export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r" + +# 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 -hl' +alias la='ll -A' +alias l='ls -1A' +alias sl='ls' + +# 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 + +# log all commands +if [ -f ~/.bash-preexec ]; then + if [ -z "$ROOT_PREEXEC_CONFIGURED" ]; then + export ROOT_PREEXEC_CONFIFGURED=1 + source ~/.bash-preexec + + log() { + USER=$(whoami) + if [ ! -z "$SUDO_USER" ]; then + USER="$SUDO_USER" + fi + logger -p local6.debug -t rootshell "$USER [$$]: $1" + } + + preexec_functions+=(log) + fi +fi diff --git a/roles/core/files/root-dotfiles/vimrc b/roles/core/files/root-dotfiles/vimrc new file mode 100644 index 0000000..26bc836 --- /dev/null +++ b/roles/core/files/root-dotfiles/vimrc @@ -0,0 +1,122 @@ +set nocompatible + +if has('autocmd') + filetype plugin indent on +endif + +if has('syntax') && !exists('g:syntax_on') + syntax enable +endif + +" Use :help 'option' to see the documentation for the given option. +set autoindent +set backspace=indent,eol,start +set complete-=i +set showmatch +set showmode +set smarttab + +set nrformats-=octal +set shiftround + +set ttimeout +set ttimeoutlen=50 + +set incsearch +" Use to clear the highlighting of :set hlsearch. +if maparg('', 'n') ==# '' + nnoremap :nohlsearch +endif + +set laststatus=2 +set ruler +set showcmd +set wildmenu + +set autoread + +set encoding=utf-8 +set tabstop=2 shiftwidth=2 expandtab +"set listchars=tab:▒,trail: +"set list + +inoremap u + +set number +set hlsearch +set ignorecase +set smartcase + +" Don't use Ex mode, use Q for formatting +map Q gq + +" In many terminal emulators the mouse works just fine, thus enable it. +if has('mouse') + set mouse=a +endif + +" do not history when leavy buffer +set hidden + +" FIXME: (broken) ctrl s to save +noremap :update +vnoremap :update +inoremap :update + +set nobackup +set nowritebackup +set noswapfile +set fileformats=unix,dos,mac + +" exit insert mode +inoremap + +set completeopt=menuone,longest,preview + + +" +" Basic shortcuts definitions +" most in visual mode / selection (v or ⇧ v) +" + +" Find +map / +" indend / deindent after selecting the text with (⇧ v), (.) to repeat. +vnoremap > +vnoremap < +" comment / decomment & normal comment behavior +vmap gc +" Disable tComment to escape some entities +let g:tcomment#replacements_xml={} +" Text wrap simpler, then type the open tag or '," +vmap S +" Cut, Paste, Copy +vmap d +vmap p +vmap y +" Undo, Redo (broken) +nnoremap :undo +inoremap :undo +nnoremap :redo +inoremap :redo +" Tabs +nnoremap :tabprevious +inoremap :tabpreviousi +nnoremap :tabnext +inoremap :tabnexti +nnoremap :tabnew +inoremap :tabnewi +nnoremap :tabclose +inoremap :tabclosei + +" lazy ':' +map \ : + +let mapleader = ',' +nnoremap p :set paste +nnoremap o :set nopaste + +" this machine config +if filereadable(expand("~/.vimrc.local")) + source ~/.vimrc.local +endif diff --git a/roles/core/tasks/main.yml b/roles/core/tasks/main.yml index 88ee4f9..8d3ed38 100644 --- a/roles/core/tasks/main.yml +++ b/roles/core/tasks/main.yml @@ -2,6 +2,7 @@ - import_tasks: dns.yml - import_tasks: mirrors.yml - import_tasks: remote_access.yml +- import_tasks: root.yml - import_tasks: ntp.yml when: not(ansible_virtualization_role == 'guest' and ansible_virtualization_type == 'lxc') - import_tasks: logging.yml diff --git a/roles/core/tasks/root.yml b/roles/core/tasks/root.yml new file mode 100644 index 0000000..4c3264c --- /dev/null +++ b/roles/core/tasks/root.yml @@ -0,0 +1,13 @@ +--- + +- name: Copy root dotfiles + copy: + src: '{{ item.src }}' + dest: '{{ item.dest }}' + with_items: + - src: root-dotfiles/bashrc + dest: /root/.bashrc + - src: root-dotfiles/bash-preexec + dest: /root/.bash-preexec + - src: root-dotfiles/vimrc + dest: /root/.vimrc