From acdc15c5fda9fbd45371d60fb2770ff126a4d5f0 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Mon, 3 Jun 2019 16:26:42 +0100 Subject: [PATCH] Implement bash state in saltstack --- bootstrap.sh | 31 ++++ minion | 8 + states/bash/files/bash_aliases | 52 ++++++ states/bash/files/bash_profile | 10 ++ states/bash/files/bashrc | 118 +++++++++++++ states/bash/files/dir_colors | 298 +++++++++++++++++++++++++++++++++ states/bash/files/inputrc | 4 + states/bash/files/profile | 44 +++++ states/bash/init.sls | 27 +++ states/dotfiles.sls | 2 + states/setup-gentoo.sls | 0 states/setup.sls | 2 + states/top.sls | 4 + states/user/init.sls | 0 states/user/max.sls | 11 ++ 15 files changed, 611 insertions(+) create mode 100644 bootstrap.sh create mode 100644 minion create mode 100644 states/bash/files/bash_aliases create mode 100644 states/bash/files/bash_profile create mode 100644 states/bash/files/bashrc create mode 100644 states/bash/files/dir_colors create mode 100644 states/bash/files/inputrc create mode 100644 states/bash/files/profile create mode 100644 states/bash/init.sls create mode 100644 states/dotfiles.sls create mode 100644 states/setup-gentoo.sls create mode 100644 states/setup.sls create mode 100644 states/top.sls create mode 100644 states/user/init.sls create mode 100644 states/user/max.sls diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100644 index 0000000..9cad37c --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +SALT_CMD="salt-call" + +install_salt() { + booststrap=${mktemp} + curl -L https://bootstrap.saltstack.com -o "${bootstrap}" + sudo sh "${bootstrap}" -P +} + +if ! which $SALT_CMD; then + install_salt +fi + +if [[ ! $1 ]]; then + sudo $SALT_CMD \ + --local \ + --config=./ \ + --state_verbose=True \ + --state-output=mixed \ + --log-level=quiet \ + --retcode-passthrough state.highstate +else + sudo $SALT_CMD \ + --local \ + --config=./ \ + --state_verbose=True \ + --state-output=mixed \ + --log-level=quiet \ + --retcode-passthrough state.sls $1 +fi diff --git a/minion b/minion new file mode 100644 index 0000000..af282e7 --- /dev/null +++ b/minion @@ -0,0 +1,8 @@ +file_client: local +file_roots: + base: + - ./states +grains: + user: max + homedir: /home/max + stateroot: ./states diff --git a/states/bash/files/bash_aliases b/states/bash/files/bash_aliases new file mode 100644 index 0000000..d5cc9d5 --- /dev/null +++ b/states/bash/files/bash_aliases @@ -0,0 +1,52 @@ +#!/bin/bash + +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 + +function la { + ls_out=$(\ls -lah --color=always "$@") + let "allowed_lines = $(tput lines) - 3" + if [ $(echo "$ls_out" | wc -l) -le $allowed_lines ]; then + echo "$ls_out" + else + echo "$ls_out" | less -r + fi +} +alias tmux='TERM=xterm-256color tmux' +if [ -x /usr/bin/gvim ]; then + alias vim='gvim -v' +fi + + +function ssh_in() { + control_file="$TMPDIR/${1}_ssh_in" + if [[ -e $control_file ]]; then + echo "Session is already open" + exit + fi + + cat ~/.tmux.conf | ssh \ + -M \ + -S "$control_file" \ + -o ControlPersist=yes \ + "${1}" \ + 'cat - > ~/.tmux_ssh.conf' + + + i3-sensible-terminal -e \ + sh -c \ + "ssh \ + -o RemoteCommand=\"/bin/sh -c 'exec tmux -f ~/.tmux_ssh.conf new -As remote'\" \ + -S \"$control_file\" \ + -t \ + \"$@\" + ssh -O exit -S \"$control_file\" \"$1\"" +} diff --git a/states/bash/files/bash_profile b/states/bash/files/bash_profile new file mode 100644 index 0000000..da0b8fb --- /dev/null +++ b/states/bash/files/bash_profile @@ -0,0 +1,10 @@ +if [ -r ~/.profile ]; then + . ~/.profile; +fi + +case "$-" in *i*) + if [ -r ~/.bashrc ]; then + . ~/.bashrc + fi + ;; +esac diff --git a/states/bash/files/bashrc b/states/bash/files/bashrc new file mode 100644 index 0000000..52753fa --- /dev/null +++ b/states/bash/files/bashrc @@ -0,0 +1,118 @@ +# ~/.bashrc: executed by bash(1) for non-login shells. +# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) +# for examples + +# 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=20000 + +# check the window size after each command and, if necessary, +# update the values of LINES and COLUMNS. +shopt -s checkwinsize + +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 + +BLACK=$(tput setaf 0) +RED=$(tput setaf 1) +GREEN=$(tput setaf 2) +YELLOW=$(tput setaf 3) +BLUE=$(tput setaf 4) +MAGENTA=$(tput setaf 5) +CYAN=$(tput setaf 6) +GRAY=$(tput setaf 7) +BOLD=$(tput bold) +RESET=$(tput sgr0) + +function __virtualenv_prompt() { + if [ -n "$VIRTUAL_ENV" ]; then + echo "(${VIRTUAL_ENV##*/}) " + fi +} + +# set up command prompt +function __prompt_command() +{ + # capture the exit status of the last command + EXIT="$?" + PS1="" + + # check if inside git repo + local git_status="`git status -unormal 2>&1`" + if git rev-parse --git-dir > /dev/null 2>&1; then + git_status=$(git status --porcelain) + # parse the porcelain output of git status + if [[ ! $git_status = *[![:space:]]* ]]; then + local Color_On=$GREEN + elif [[ $git_status =~ ^\?\? ]]; then + local Color_On=$CYAN + else + local Color_On=$RED + fi + + branch=$(git symbolic-ref -q HEAD) + branch=${branch##refs/heads/} + branch=${branch:-HEAD} + + # add the result to prompt + PS1+="\n\[$Color_On\][$branch]\[$RESET\]\n" + fi + + + PS1+="$(__virtualenv_prompt)" + + if [ $EXIT -eq 0 ]; then PS1+="\[$GREEN\][\!]\[$RESET\] "; else PS1+="\[$RED\][\!]\[$RESET\] "; fi + + # if logged in via ssh shows the ip of the client + if [ -n "$SSH_CLIENT" ]; then + IP=${SSH_CLIENT%% *} + PS1+="\[$YELLOW\]("$IP")\[$RESET\]"; + fi + + # debian chroot stuff (take it or leave it) + PS1+="${debian_chroot:+($debian_chroot)}" + + # basic information (user@host:path) + PS1+="\[$RED$BOLD\]\u\[$RESET\]@\[$RED$BOLD\]\h\[$RESET\]:\[$BLUE\]\w\[$RESET\]\n" + + + # prompt $ or # for root + PS1+="\$ " +} +PROMPT_COMMAND=__prompt_command + +if ! pgrep -x -u "${USER}" gpg-agent >/dev/null 2>&1; then + gpg-connect-agent /bye >/dev/null 2>&1 +fi + +unset SSH_AGENT_PID +if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then + export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket) +fi + +export GPG_TTY=($tty) +gpg-connect-agent updatestartuptty /bye > /dev/null + +export EDITOR="vim" +export TMPDIR="/tmp/${USER}" +eval `dircolors ~/.dir_colors` diff --git a/states/bash/files/dir_colors b/states/bash/files/dir_colors new file mode 100644 index 0000000..49056a7 --- /dev/null +++ b/states/bash/files/dir_colors @@ -0,0 +1,298 @@ + +# Dark 256 color solarized theme for the color GNU ls utility. +# Used and tested with dircolors (GNU coreutils) 8.5 +# +# @author {@link http://sebastian.tramp.name Sebastian Tramp} +# @license http://sam.zoy.org/wtfpl/ Do What The Fuck You Want To Public License (WTFPL) +# +# More Information at +# https://github.com/seebi/dircolors-solarized + +# Term Section +TERM Eterm +TERM ansi +TERM color-xterm +TERM con132x25 +TERM con132x30 +TERM con132x43 +TERM con132x60 +TERM con80x25 +TERM con80x28 +TERM con80x30 +TERM con80x43 +TERM con80x50 +TERM con80x60 +TERM cons25 +TERM console +TERM cygwin +TERM dtterm +TERM dvtm +TERM dvtm-256color +TERM eterm-color +TERM fbterm +TERM gnome +TERM gnome-256color +TERM jfbterm +TERM konsole +TERM konsole-256color +TERM kterm +TERM linux +TERM linux-c +TERM mach-color +TERM mlterm +TERM putty +TERM putty-256color +TERM rxvt +TERM rxvt-256color +TERM rxvt-cygwin +TERM rxvt-cygwin-native +TERM rxvt-unicode +TERM rxvt-unicode256 +TERM rxvt-unicode-256color +TERM screen +TERM screen-16color +TERM screen-16color-bce +TERM screen-16color-s +TERM screen-16color-bce-s +TERM screen-256color +TERM screen-256color-bce +TERM screen-256color-s +TERM screen-256color-bce-s +TERM screen-256color-italic +TERM screen-bce +TERM screen-w +TERM screen.linux +TERM screen.xterm-256color +TERM st +TERM st-meta +TERM st-256color +TERM st-meta-256color +TERM tmux +TERM tmux-256color +TERM vt100 +TERM xterm +TERM xterm-16color +TERM xterm-256color +TERM xterm-256color-italic +TERM xterm-88color +TERM xterm-color +TERM xterm-debian +TERM xterm-termite + +## Documentation +# +# standard colors +# +# Below are the color init strings for the basic file types. A color init +# string consists of one or more of the following numeric codes: +# Attribute codes: +# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed +# Text color codes: +# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white +# Background color codes: +# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white +# +# +# 256 color support +# see here: http://www.mail-archive.com/bug-coreutils@gnu.org/msg11030.html) +# +# Text 256 color coding: +# 38;5;COLOR_NUMBER +# Background 256 color coding: +# 48;5;COLOR_NUMBER + +## Special files + +NORMAL 00;38;5;244 # no color code at all +#FILE 00 # regular file: use no color at all +RESET 0 # reset to "normal" color +DIR 00;38;5;33 # directory 01;34 +LINK 00;38;5;37 # symbolic link. (If you set this to 'target' instead of a + # numerical value, the color is as for the file pointed to.) +MULTIHARDLINK 00 # regular file with more than one link +FIFO 48;5;230;38;5;136;01 # pipe +SOCK 48;5;230;38;5;136;01 # socket +DOOR 48;5;230;38;5;136;01 # door +BLK 48;5;230;38;5;244;01 # block device driver +CHR 48;5;230;38;5;244;01 # character device driver +ORPHAN 48;5;235;38;5;160 # symlink to nonexistent file, or non-stat'able file +SETUID 48;5;160;38;5;230 # file that is setuid (u+s) +SETGID 48;5;136;38;5;230 # file that is setgid (g+s) +CAPABILITY 30;41 # file with capability +STICKY_OTHER_WRITABLE 48;5;64;38;5;230 # dir that is sticky and other-writable (+t,o+w) +OTHER_WRITABLE 48;5;235;38;5;33 # dir that is other-writable (o+w) and not sticky +STICKY 48;5;33;38;5;230 # dir with the sticky bit set (+t) and not other-writable +# This is for files with execute permission: +EXEC 00;38;5;64 + +## Archives or compressed (violet + bold for compression) +.tar 00;38;5;61 +.tgz 00;38;5;61 +.arj 00;38;5;61 +.taz 00;38;5;61 +.lzh 00;38;5;61 +.lzma 00;38;5;61 +.tlz 00;38;5;61 +.txz 00;38;5;61 +.zip 00;38;5;61 +.z 00;38;5;61 +.Z 00;38;5;61 +.dz 00;38;5;61 +.gz 00;38;5;61 +.lz 00;38;5;61 +.xz 00;38;5;61 +.bz2 00;38;5;61 +.bz 00;38;5;61 +.tbz 00;38;5;61 +.tbz2 00;38;5;61 +.tz 00;38;5;61 +.deb 00;38;5;61 +.rpm 00;38;5;61 +.jar 00;38;5;61 +.rar 00;38;5;61 +.ace 00;38;5;61 +.zoo 00;38;5;61 +.cpio 00;38;5;61 +.7z 00;38;5;61 +.rz 00;38;5;61 +.apk 00;38;5;61 +.gem 00;38;5;61 + +# Image formats (yellow) +.jpg 00;38;5;136 +.JPG 00;38;5;136 #stupid but needed +.jpeg 00;38;5;136 +.gif 00;38;5;136 +.bmp 00;38;5;136 +.pbm 00;38;5;136 +.pgm 00;38;5;136 +.ppm 00;38;5;136 +.tga 00;38;5;136 +.xbm 00;38;5;136 +.xpm 00;38;5;136 +.tif 00;38;5;136 +.tiff 00;38;5;136 +.png 00;38;5;136 +.PNG 00;38;5;136 +.svg 00;38;5;136 +.svgz 00;38;5;136 +.mng 00;38;5;136 +.pcx 00;38;5;136 +.dl 00;38;5;136 +.xcf 00;38;5;136 +.xwd 00;38;5;136 +.yuv 00;38;5;136 +.cgm 00;38;5;136 +.emf 00;38;5;136 +.eps 00;38;5;136 +.CR2 00;38;5;136 +.ico 00;38;5;136 + +# Files of special interest (base1) +.tex 00;38;5;245 +.rdf 00;38;5;245 +.owl 00;38;5;245 +.n3 00;38;5;245 +.ttl 00;38;5;245 +.nt 00;38;5;245 +.torrent 00;38;5;245 +.xml 00;38;5;245 +*Makefile 00;38;5;245 +*Rakefile 00;38;5;245 +*Dockerfile 00;38;5;245 +*build.xml 00;38;5;245 +*rc 00;38;5;245 +*1 00;38;5;245 +.nfo 00;38;5;245 +*README 00;38;5;245 +*README.txt 00;38;5;245 +*readme.txt 00;38;5;245 +.md 00;38;5;245 +*README.markdown 00;38;5;245 +.ini 00;38;5;245 +.yml 00;38;5;245 +.cfg 00;38;5;245 +.conf 00;38;5;245 +.c 00;38;5;245 +.cpp 00;38;5;245 +.cc 00;38;5;245 +.sqlite 00;38;5;245 +.go 00;38;5;245 +.sql 00;38;5;245 +.csv 00;38;5;245 + +# "unimportant" files as logs and backups (base01) +.log 00;38;5;240 +.bak 00;38;5;240 +.aux 00;38;5;240 +.lof 00;38;5;240 +.lol 00;38;5;240 +.lot 00;38;5;240 +.out 00;38;5;240 +.toc 00;38;5;240 +.bbl 00;38;5;240 +.blg 00;38;5;240 +*~ 00;38;5;240 +*# 00;38;5;240 +.part 00;38;5;240 +.incomplete 00;38;5;240 +.swp 00;38;5;240 +.tmp 00;38;5;240 +.temp 00;38;5;240 +.o 00;38;5;240 +.pyc 00;38;5;240 +.class 00;38;5;240 +.cache 00;38;5;240 + +# Audio formats (orange) +.aac 00;38;5;166 +.au 00;38;5;166 +.flac 00;38;5;166 +.mid 00;38;5;166 +.midi 00;38;5;166 +.mka 00;38;5;166 +.mp3 00;38;5;166 +.mpc 00;38;5;166 +.ogg 00;38;5;166 +.ra 00;38;5;166 +.wav 00;38;5;166 +.m4a 00;38;5;166 +# http://wiki.xiph.org/index.php/MIME_Types_and_File_Extensions +.axa 00;38;5;166 +.oga 00;38;5;166 +.spx 00;38;5;166 +.xspf 00;38;5;166 + +# Video formats (as audio + bold) +.mov 00;38;5;166 +.MOV 00;38;5;166 +.mpg 00;38;5;166 +.mpeg 00;38;5;166 +.m2v 00;38;5;166 +.mkv 00;38;5;166 +.ogm 00;38;5;166 +.mp4 00;38;5;166 +.m4v 00;38;5;166 +.mp4v 00;38;5;166 +.vob 00;38;5;166 +.qt 00;38;5;166 +.nuv 00;38;5;166 +.wmv 00;38;5;166 +.asf 00;38;5;166 +.rm 00;38;5;166 +.rmvb 00;38;5;166 +.flc 00;38;5;166 +.avi 00;38;5;166 +.fli 00;38;5;166 +.flv 00;38;5;166 +.gl 00;38;5;166 +.m2ts 00;38;5;166 +.divx 00;38;5;166 +.webm 00;38;5;166 +# http://wiki.xiph.org/index.php/MIME_Types_and_File_Extensions +.axv 00;38;5;166 +.anx 00;38;5;166 +.ogv 00;38;5;166 +.ogx 00;38;5;166 + + diff --git a/states/bash/files/inputrc b/states/bash/files/inputrc new file mode 100644 index 0000000..9d4ca57 --- /dev/null +++ b/states/bash/files/inputrc @@ -0,0 +1,4 @@ +set editing-mode vi +set keymap vi-command + +set horizontal-scroll-mode Off diff --git a/states/bash/files/profile b/states/bash/files/profile new file mode 100644 index 0000000..43fbef4 --- /dev/null +++ b/states/bash/files/profile @@ -0,0 +1,44 @@ +export PATH="${HOME}/.local/bin:${HOME}/.cargo/bin:${PATH}" + +# Make XDG_RUNTIME_DIR, required for some sessions +if test -z "${XDG_RUNTIME_DIR}"; then + export XDG_RUNTIME_DIR=/tmp/${UID}-runtime-dir + if ! test -d "${XDG_RUNTIME_DIR}"; then + mkdir "${XDG_RUNTIME_DIR}" + chmod 0700 "${XDG_RUNTIME_DIR}" + fi +fi + +# Make TMPDIR +mkdir -p "/tmp/${USER}" +chmod 770 "/tmp/${USER}" + +export QT_QPA_PLATFORM=wayland-egl + +if [ -f ~/.config/sway/config.base ]; then + cat ~/.config/sway/config.base ~/.config/sway/config.${HOSTNAME} > ~/.config/sway/config +fi + +if [ -f ~/.Xresources ]; then + cat ~/.Xresources > ~/.Xdefaults + if [ -f ~/.Xresources.$HOSTNAME ]; then + cat ~/.Xresources.$HOSTNAME >> ~/.Xdefaults + fi +fi + +if [ -f ~/.config/termite/config.base ]; then + cat ~/.config/termite/config.base ~/.config/termite/config.${HOSTNAME} > \ + ~/.config/termite/config +fi + + +# make less more friendly for non-text input files, see lesspipe(1) +if [ -x /usr/bin/lesspipe ]; then + export LESSOPEN="|lesspipe %s" +elif [ -x /usr/bin/lesspipe.sh ]; then + export LESSOPEN="|lesspipe %s" +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' + diff --git a/states/bash/init.sls b/states/bash/init.sls new file mode 100644 index 0000000..d5a31aa --- /dev/null +++ b/states/bash/init.sls @@ -0,0 +1,27 @@ +bash installed: + pkg.installed: + - name: app-shells/bash + +include: + - user.max + +{% + set files = [ + 'bash_aliases', + 'bash_profile', + 'bashrc', + 'dir_colors', + 'inputrc', + 'profile' + ] +%} +{% for file in files %} +manage {{ file }}: + file.managed: + - name: {{ grains['homedir'] }}/.{{ file }} + - source: {{ grains['stateroot'] }}/bash/files/{{ file }} + - user: {{ grains['user'] }} + - group: {{ grains['user'] }} + - mode: 660 + - template: jinja +{% endfor %} diff --git a/states/dotfiles.sls b/states/dotfiles.sls new file mode 100644 index 0000000..6ad7e29 --- /dev/null +++ b/states/dotfiles.sls @@ -0,0 +1,2 @@ +include: + - bash diff --git a/states/setup-gentoo.sls b/states/setup-gentoo.sls new file mode 100644 index 0000000..e69de29 diff --git a/states/setup.sls b/states/setup.sls new file mode 100644 index 0000000..662362b --- /dev/null +++ b/states/setup.sls @@ -0,0 +1,2 @@ +include: + - setup-{{ grains['os_family'] | lower }} diff --git a/states/top.sls b/states/top.sls new file mode 100644 index 0000000..f1d7684 --- /dev/null +++ b/states/top.sls @@ -0,0 +1,4 @@ +base: + '*': + - setup + - dotfiles diff --git a/states/user/init.sls b/states/user/init.sls new file mode 100644 index 0000000..e69de29 diff --git a/states/user/max.sls b/states/user/max.sls new file mode 100644 index 0000000..ed4976a --- /dev/null +++ b/states/user/max.sls @@ -0,0 +1,11 @@ +ensure user max: + user.present: + - name: max + - home: /home/max + - shell: /bin/bash + - fullname: Maximilian Friedersdorff + +ensure max allows public key: + ssh_auth.present: + - name: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCemcR8m4Xr3LcZTvZcmKJOy0nPefKArbmdBjKroqokJlknHvcCuSFWzEcCH7Pn+4ZKP9e1RR5WG/SM4pbEdxvItHJz0PwKNe/xwK0vIUVYo+nRh2Gre6Nu0betGktkfflIMR18iK6NicP4lSHfC/uk+hs/3N0QLv6pVi3iT8qh8NBzXQLi6cwovgeAv4TbdaOONVTixUScdmFd2bDpdzK0eux0R600jOATQ8Z6XmCTAiVgEjGBdoxObLrSuvkeQbY0lwNT7k0wF5UvQesydJqDNKTyuKBbKSjrYvK0Me4RyYWTV3y6b5Gn1mqJuArQrY6DnXyVMv/0J0wF04YMP33H cardno:000604875749 + - user: max -- 2.45.2