Add bash configuration files

This commit is contained in:
xavier
2013-04-24 16:58:31 +02:00
parent 1849cd96a2
commit 9fad99b12f
30 changed files with 570 additions and 0 deletions

5
libs/basedir.bash Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
function basedir() {
echo $( cd $(dirname $0) && pwd )
}

16
libs/cgroups.bash Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
# Use cgroups
if [ "$PS1" ] ; then
if [ -d /sys/fs/cgroup ] ; then
cdir=/sys/fs/cgroup
elif [ -d /dev/cgroup ]; then
cdir=/dev/cgroup
fi
if [ -z "$cdir" ]; then
mkdir -p -m 0700 $cdir/user/$$ > /dev/null 2>&1
/bin/echo $$ > $cdir/user/$$/tasks
/bin/echo '1' > $cdir/user/$$/notify_on_release
unset -v cdir
fi
fi

94
libs/colors.bash Executable file
View File

@ -0,0 +1,94 @@
#!/bin/bash
#ANSI CODES
#Code Effect
#0 All attributes off
#1 Bold
#2 Faint
#3 Italic
#4 Underline
#5 Blink
#6 Rapid blink
#7 Reverse video
#8 Concealed
#30 Black foreground
#31 Red foreground
#32 Green foreground
#33 Yellow foreground
#34 Blue foreground
#35 Magenta foreground
#36 Cyan foreground
#37 White foreground
#40 Black background
#41 Red background
#42 Green background
#43 Yellow background
#44 Blue background
#45 Magenta background
#46 Cyan background
#47 White background
#48 Subscript
#49 Superscript
#T_ROWS=`tput lines` # Define current terminal dimension
#T_COLS=`tput cols` #+ in rows and columns.
#_UNDERLINE_ON=`tput smul` # turn on underline
#_UNDERLINE_OFF=`tput rmul` # turn off underline
#tput setf 4 # tput setf {fg color number}
#tput setb 2 # tput setb {bg color number}
#tput bold - Bold effect
#tput tsmi - Italic
#tput rev - Display inverse colors
#tput sgr0 - Reset everything
default='\E[39;49;00m'
bold='\E[1m'
italic='\E[3m'
underline='\E[4m'
blink='\E[5m'
blackonwhite='\E[30;47m'
#reset=`escape 0m`
#black='\E[30;47m'
#red='\E[31;47m'
#green='\E[32;47m'
#yellow='\E[33;47m'
#blue='\E[34;47m'
#magenta='\E[35;47m'
#cyan='\E[36;47m'
#white='\E[37;47m'
#bold_on=`escape 1m`
#bold_off=`escape 22m`
#blink_on=`escape 5m`
#blink_off=`escape 25m`
function echo_reverse
{
echo -ne $blackonwhite
echo -ne "$*"
echo -e $default
}
function echo_italic
{
echo -ne $italic
echo -ne "$*"
echo -e $default
}
function echo_bold
{
echo -ne $bold
echo -ne "$*"
echo -e $default
}
function echo_underline
{
echo -ne $underline
echo -ne "$*"
echo -e $default
}

1
libs/grepp.bash Executable file
View File

@ -0,0 +1 @@
grepp() { x=$1; shift; perl -00ne ' print if /'"$x"'/i ' "$*" ; }

31
libs/ipplan.bash Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
function ipplan_search_by_ip()
{
echo "select inet_ntoa(ipaddr) as ip, location, hname, descrip, userinf from ipaddr where ipaddr = inet_aton('$1');" | ssh virt-cha-nettools mysql --database ipplan --default-character-set=latin1 --table
}
function ipplan_search_by_location()
{
echo "select inet_ntoa(ipaddr) as ip, location, hname, descrip, userinf from ipaddr where location like '%$1%';" | ssh virt-cha-nettools mysql --database ipplan --default-character-set=latin1 --table
}
function ipplan_search_by_name()
{
echo "select inet_ntoa(ipaddr) as ip, location, hname, descrip, userinf from ipaddr where hname like '%$1%';" | ssh virt-cha-nettools mysql --database ipplan --default-character-set=latin1 --table
}
function ipplan_search_by_desc()
{
echo "select inet_ntoa(ipaddr) as ip, location, hname, descrip, userinf from ipaddr where descrip like '%$1%';" | ssh virt-cha-nettools mysql --database ipplan --default-character-set=latin1 --table
}
function ipplan_search_by_user()
{
echo "select inet_ntoa(ipaddr) as ip, location, hname, descrip, userinf from ipaddr where userinf like '%$1%';" | ssh virt-cha-nettools mysql --database ipplan --default-character-set=latin1 --table
}
function ipplan_list_ingesys()
{
echo "select inet_ntoa(ipaddr) as ip, location, hname, descrip, userinf from ipaddr where userinf like '%ing%sys%'; " | ssh virt-cha-nettools mysql --database ipplan --default-character-set=latin1 --table
}

17
libs/password.bash Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
encpasswd() {
local password_clear=$1
local password_md5hash=$(openssl passwd -1 $password_clear)
echo "Password : $password_clear"
echo "MD5HASH : $password_md5hash"
}
genpasswd() {
local l=$1
[ "$l" == "" ] && l=12
local password_clear=$(tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs)
local password_md5hash=$(openssl passwd -1 $password_clear)
echo "Password : $password_clear"
echo "MD5HASH : $password_md5hash"
}

26
libs/screen.bash Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
# GNU Screen stuff
if ( which screen &> /dev/null )
then
if [[ $TERM != screen* ]]
then
if [ "$(screen -ls | sed -ne 's/[[:space:]]//' -ne 's/(Attached)// p')" ]
then
echo ''
echo_reverse 'Il existe des sessions screen attachées sur cette machine pour cet utilisateur !'
echo ''
screen -ls | grep "Attached" | sed -e 's/^/ /'
echo ''
fi
if [ "$(screen -ls | sed -ne 's/[[:space:]]//' -ne 's/(Detached)// p')" ]
then
echo ''
echo_reverse "Il existe des sessions screen détachées sur cette machine pour cet utilisateur !"
echo ''
screen -ls | grep "Detached" | sed -e 's/^/ /'
echo ''
fi
fi
fi

17
libs/term.bash Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
function term_change_title
{
case $TERM in
# Change the window title of X terminals
xterm*|rxvt*|urxvt*|Eterm)
PROMPT_COMMAND='echo -ne "\033]0;${1}\007"'
;;
# Change the window title of screen terminals
screen*)
PROMPT_COMMAND='echo -ne "\033k${1}\033\\"'
;;
esac
}

11
libs/urxvt.bash Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
function urxvt_set_font
{
printf '\33]50;%s\007' "xft:$1"
}
function urxvt_set_title
{
# set window title
printf '\33]2;%s\007' "$1"
}

152
libs/virtualization.bash Executable file
View File

@ -0,0 +1,152 @@
#!/bin/bash
hypervisor_connect() {
if [ -e "${1}" ]; then echo "Usage : $0 hypervisor_name_or_ip"; return 1; fi
hypervisor=${1}
echo
echo "Trying to start interactive virsh session on ${hypervisor}"
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system
}
hypervisor_command() {
if [ -e "${1}" ]; then echo "Usage : $0 hypervisor_name_or_ip"; return 1; fi
hypervisor=${1}
shift
echo
echo "Trying to exec virsh command ($*) on ${hypervisor}"
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system "$*"
}
vm_create() {
if [ $# -ne 5 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name vm_ram_size vm_disk_size vm_mac"; return 1; fi
hypervisor=${1}
name=${2}
ramsize=${3}
disksize=${4}
mac=${5}
#mac=$( grep ${name} ~/repositories/fai/config-dhcp/dhcpd-hosts.conf | sed -e 's/^.*hardware.*ethernet\s*\([0-9a-zA-Z:]*\);.*$/\1/' )
echo "Creating VM with virt-install"
virt-install --connect=qemu+ssh://${USER}@${hypervisor}/system \
--name=${name} \
--description="${name}" \
--cpu=host \
--ram=${ramsize} \
--disk=/dev/lvm_guests_system/${name},size=${disksize},bus=virtio,cache=writethrough,io=native \
--network=bridge=br192,mac=${mac},model=virtio \
--os-type=linux \
--os-variant=debiansqueeze \
--boot=network,hd,menu=on \
--pxe \
--graphics=vnc \
--noautoconsole
echo "Done"
}
vm_start() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi
hypervisor=${1}
name=${2}
echo
echo "Trying to start ${name} on ${hypervisor} with virsh"
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system start ${name}
}
vm_stop() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi
hypervisor=${1}
name=${2}
echo
echo "Trying to stop ${name} on ${hypervisor} with virsh"
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system shutdown ${name}
}
vm_destroy() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi
hypervisor=${1}
name=${2}
echo
echo "Trying to destroy ${name} on ${hypervisor} with virsh"
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system destroy ${name}
}
vm_connect() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi
hypervisor=${1}
name=${2}
echo
echo "Trying to connect to ${name} on ${hypervisor} with virsh"
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system ttyconsole ${name}
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system console ${name}
}
#### A tester
vm_change_bridge() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name vm_mac vm_new_bridge"; return 1; fi
hypervisor=${1}
name=${2}
mac=${3}
bridge=${4}
#domiflist ${name}
#detach-interface --config --domain ${name} --type bridge --mac ${mac}
#attach-interface --config --domain ${name} --type bridge --mac ${mac} --source ${bridge}
}
#### A tester
vm_add_data_disk() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name size"; return 1; fi
hypervisor=${1}
name=${2}
size=${3}
#pool-list
#vol-create-as --pool guests_data --name virt-cha-mirror --capacity 200G --format raw
#attach-disk virt-cha-mirror /dev/lvm_guests_data/virt-cha-mirror vdb --cache writethrough
#domblklist virt-cha-mirror
}
#### bash completion
_hypervisors() {
if [ -e ~/.ssh/config ]; then
configured_hypervisors=$(cat ~/.ssh/config | egrep -i "^\s*host\s+[a-zA-Z]" | sed -e "s/^host\s*//i" | grep -i hypervisor)
fi
if [ -e ~/.ssh/known_hosts ]; then
known_hypervisors=$(cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | grep -v "\[" | grep -i hypervisor | uniq)
fi
echo $configured_hypervisors $known_hypervisors
}
#complete -W "$(cat ~/.ssh/config | egrep -i "^\s*host\s+[a-zA-Z]" | sed -e "s/^host\s*//i" | grep -i hypervisor)" hypervisor_connect
complete -W "$(_hypervisors)" hypervisor_connect
complete -W "$(_hypervisors)" hypervisor_command
complete -W "$(_hypervisors)" vm_create
complete -W "$(_hypervisors)" vm_start
complete -W "$(_hypervisors)" vm_stop
complete -W "$(_hypervisors)" vm_connect