From 28641c108d3c66eb789b2dbcfabfa9c8f9e6864b Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 9 Dec 2016 00:33:43 +0000 Subject: [PATCH] cleaning, splitting, ordering --- README.md | 4 +- etc/profile | 3 +- etc/profile.d/aliases | 65 +------------------- etc/profile.d/docker.sh | 15 +++-- etc/profile.d/envs | 11 +++- etc/profile.d/functions | 87 +++++++++++++++------------ etc/profile.d/mac.sh | 11 +++- etc/profile.d/nb.sh | 54 ----------------- etc/profile.d/nico.sh | 127 ++++++++++++++++++++++++++++++++++++++++ etc/profile.d/zsh.sh | 1 - 10 files changed, 205 insertions(+), 173 deletions(-) create mode 100644 etc/profile.d/nico.sh delete mode 100644 etc/profile.d/zsh.sh diff --git a/README.md b/README.md index 54d3fc28..5156ec48 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -NodeBoot +NB ============= ## Synopsis -NodeBoot main library +NB main library Copyright (c) 2015 - Nicolas Boisselier diff --git a/etc/profile b/etc/profile index c9281033..2e488a62 100755 --- a/etc/profile +++ b/etc/profile @@ -77,8 +77,7 @@ else ${NB_ROOT}/etc/profile.d/aliases \ $(nb_repo "etc/profile" "etc/profile.d/*.sh" | grep -vFx "${NB_ROOT}/etc/profile") \ ;do -#unset i tmp NB_CURRENT_DIR NB_DEBUG NB_LOOP; return - [ -n "$NB_DEBUG" ] && $NB_DEBUG "OK: . $i" + [ -n "$NB_DEBUG" ] && $NB_DEBUG ". $i" [ -r "$i" -a -f "$i" ] && . "$i" done diff --git a/etc/profile.d/aliases b/etc/profile.d/aliases index 15ab0c93..a522e17a 100644 --- a/etc/profile.d/aliases +++ b/etc/profile.d/aliases @@ -8,8 +8,8 @@ #-------------------------------------------------------------------------------- # Others # -which blkid &>/dev/null alias uuid-hds='blkid -o full -s UUID' -alias dpkg-sort-size="dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1nr" +which blkid &>/dev/null && alias uuid-hds='blkid -o full -s UUID' +which dpkg-query &>/dev/null && alias dpkg-sort-size="dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1nr" which apg &>/dev/null && alias password-gener='apg -m 12 -x 12 -a 0 -M L|head -1;' which mysql &>/dev/null && alias mysqlshow-view="mysql -e \"SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW'\"" which nmap &>/dev/null && alias nmap-ping="nmap -sP -PE -PS443 -PA21,22,23,80,3389" @@ -17,76 +17,17 @@ which arp &>/dev/null && alias arp-list="arp -an" which gs &>/dev/null && alias pdf2jpegs='gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=5 -sOutputFile=pdf2jpegs%d.jpg -dJPEGQ=100 -r500 -c quit' which maildirmake &>/dev/null && alias nb-maildir-make="maildirmake -S Maildir; for d in Spam Trash Sent;do maildirmake -f $d Maildir; done" -#[ "$UID" = "0" ] && grep -q '^_\?postgres:' /etc/passwd && alias psql='sudo -u postgres psql' - -#-------------------------------------------------------------------------------- -# Etherwake -# Cable: alias advent-on="etherwake -i eth0 00:22:68:7b:fb:99 -D -b" -# -#alias advent-on="etherwake -i eth0 48:5b:39:eb:db:8a -D -b" - #-------------------------------------------------------------------------------- # Beep # alias beep='printf "\a"' -#-------------------------------------------------------------------------------- -# Mac iTerm.app -# -test "$TERM_PROGRAM" = "iTerm.app" && export PROMPT_COMMAND='printf "\033]0;%s\007" "${USER}@${HOSTNAME%%.*}"' - -#-------------------------------------------------------------------------------- -# Get / Head -# -#alias Get='wget --no-check-certificate --quiet -O -' -# NB 10.09.16 function Get() { -# NB 10.09.16 exec wget --tries 1 --no-check-certificate --quiet -O - $@ -# NB 10.09.16 } -# NB 10.09.16 function Head() { -# NB 10.09.16 Get --max-redirect 0 --server-response --spider $@ 2>&1|sed 's/^ *//' -# NB 10.09.16 #--tries 1 -# NB 10.09.16 #exec wget --server-response --spider --no-check-certificate --quiet -O - $@ 2>&1|sed 's/^ *//' -# NB 10.09.16 } -#alias Get='lynx --source -dump' -#alias Head='lynx --head -dump' -alias Get='curl -sk' -alias Head='curl -sk --head' -alias myip='Get http://nicolas.boisselier.free.fr/myip/' - #-------------------------------------------------------------------------------- # bc # -alias bc="bc -q" - -#-------------------------------------------------------------------------------- -# iconv -# -alias latin1_utf8='iconv -f latin1 -t utf8' -alias utf8_latin1='iconv -f utf8 -t latin1' +which bc 1>/dev/null && alias bc="bc -q" #-------------------------------------------------------------------------------- # vim # which vim &>/dev/null && alias vi=vim - -#-------------------------------------------------------------------------------- -# xterm -# -[ -x /usr/bin/xterm ] && alias xterm="xterm -bg black -fg gray -fn 10x20" - -#-------------------------------------------------------------------------------- -# Torrent -# - -# btlaunchmanycurses.py -#alias torrent="btdownloadcurses.py --max_download_rate 150 --max_files_open 0" -#alias torrentdir="btlaunchmanycurses.py --max_download_rate 150 --max_files_open 0" - -# btdownloadcurses -which btdownloadcurses &>/dev/null && alias torrent="btdownloadcurses --max_upload_rate 1 --minport 6881 --maxport 6999" -which btlaunchmanycurses &>/dev/null && alias torrentdir="btlaunchmanycurses --max_upload_rate 1 --minport 6881 --maxport 6999" - -#-------------------------------------------------------------------------------- -# debian upgrade -# -which server-package.pl &>/dev/null && alias servers-upgrade='server-package.pl update,upgrade $((printf "big.cascais.loc\n";mysql -h mysql izi -NBe "select host from server where not hidden") | sort -u)' diff --git a/etc/profile.d/docker.sh b/etc/profile.d/docker.sh index b4040c26..e77fa5ab 100644 --- a/etc/profile.d/docker.sh +++ b/etc/profile.d/docker.sh @@ -4,8 +4,7 @@ which docker > /dev/null || return 0 [ -z "$DOCKER_MACHINE_NAME" ] && DOCKER_MACHINE_NAME="docker" -[ -d ~/ownCloud/Docker ] && export DOCKER_HOME=~/ownCloud/Docker -[ -d ~/ownCloud/Docker ] && export DOCKER_HOME=~/Dropbox/Docker +[ -d $CLOUD_DIR/Docker ] && export DOCKER_HOME=$CLOUD_DIR/Docker [ -z "$DOCKER_HUB" ] && DOCKER_HUB="" # Docker env only if `is_prompt` !!! @@ -19,12 +18,6 @@ if is_prompt && which docker-machine > /dev/null && [ -z "$MYVIMRC" ]; then fi -if which docker-compose > /dev/null; then -docker_compose_up_force() { - docker-compose $@ up --force-recreate --build --remove-orphans -d -} -fi - docker_build() { shell_help "Usage: $FUNCNAME [DIR_DOCKERFILE]" "$@" && return @@ -162,6 +155,12 @@ docker_clean() { [ -n "$containers" ] && docker rm -vf $containers | sed "s/^/CONTAINERS /" } +if which docker-compose > /dev/null; then +docker_compose_up_force() { + docker-compose $@ up --force-recreate --build --remove-orphans -d +} +fi + #[ -n "$containers" -a -n "$volumes" -a -n "$images" ] # NB 12.11.16 docker-image2dockerfile() { # NB 12.11.16 docker history --no-trunc "$1" | \ diff --git a/etc/profile.d/envs b/etc/profile.d/envs index 77bb4e29..e2dbffca 100644 --- a/etc/profile.d/envs +++ b/etc/profile.d/envs @@ -14,14 +14,17 @@ [ -z "$OSTYPE" ] && OSTYPE=`uname|tr [:upper:] [:lower:]` # NB 25.08.16 [ -z "PWD" ] && PWD=`pwd` -# We don't want to destroyed variables +[ -z "$CLOUD_DIR" ] && [ -e ~nico/ownCloud ] && CLOUD_DIR=~nico/ownCloud +[ -z "$CLOUD_DIR" ] && [ -e ~nico/Dropbox ] && CLOUD_DIR=~nico/Dropbox + +# > env_add_path +#We don't want to destroyed variables if function does not exists ! if true || env_add_path &>/dev/null; then # # PATH # [ -n "$NB_ROOT" ] && PATH=$(env_add_path "$PATH" "$NB_ROOT/bin") # to exec tools - #$($NB_ROOT/bin/nb_repo bin sbin) PATH=$(env_add_path "$PATH" \ /bin \ /sbin \ @@ -153,5 +156,7 @@ unset ls_opt ll_opt # # bye # -[ -n "$NB_DEBUG" ] && $NB_DEBUG "Color: $color_prompt, TERM=$TERM" +#[ -n "$NB_DEBUG" ] && $NB_DEBUG "Color: $color_prompt, TERM=$TERM" +[ -n "$NB_DEBUG" ] && [ -n "$color_prompt" ] && $NB_DEBUG "Color: $color_prompt" +[ -n "$NB_DEBUG" ] && [ -n "$TERM" ] && $NB_DEBUG "TERM=$TERM" unset color_prompt diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 0b2245be..c56674c6 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -1,10 +1,10 @@ ################################################################################# # -# FUNCTIONS - SH +# FUNCTIONS - CORE # ################################################################################# nb_repo() { -# NB 24.11.16: TODO +# NB 24.11.16: TODEL if false && which perl &> /dev/null; then nb-repo-dirs $@ else @@ -257,11 +257,6 @@ json2perl() { perl -MJSON -MData::Dumper -e 'print Dumper JSON::decode_json(join("",<>))' $@ } -################################################################################# -# -# FUNCTIONS - BASH -# -################################################################################# hl(){ #cat | grep --color=always -E "(^|($@))" perl -MTerm::ANSIColor -pe '$e=qq@'$@'@;$e=~s,/,\\/,g; s/($e)/color("red").$1.color("reset")/ge' @@ -314,38 +309,6 @@ getexp() { getexp_url() { curl -ks "$2" | perl -ne 'm,('$1'), and print $1 and exit'; } -radio_play() { - local prog=`which mplayer` - if [ -z "$prog" ]; then - case $OSTYPE in - darwin*) prog="open" ;; - esac - fi -perl -e ' -@_=grep{$_} split("\n"," -FranceInter | http://direct.franceinter.fr/live/franceinter-midfi.mp3 -Indie | http://107.155.126.42:17160/listen.pls -FranceInfo | http://direct.franceinfo.fr/live/franceinfo-midfi.mp3 -FranceCulture | http://direct.franceculture.fr/live/franceculture-midfi.mp3 -BBC 1 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p -BBC 2 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p -BBC 3 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio3_mf_p -BBC 4 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p -BBC 5 live | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5live_mf_p -BBC 6 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_6music_mf_p -"); -if (@ARGV) { $play=shift @ARGV; } else { - $i=1; - map{printf("%2d | %s\n",$i++,$_)} @_; - print "\nChoose one > "; - chomp($play=<>); -} -$play eq "" && exit; @_ = $_[$play-1] =~ /^(.*?)\s*\|\s*(.*?)$/ or exit; -print "[ $play - ".$_[0]." ]\n\n"; -exec "'$prog'",$_[1]; -' "$@" -} - ls_img_black() { local threshold; threshold=10 @@ -744,3 +707,49 @@ elsif (/^.{1,3} /dev/null +myip() { + local url=''; + for url in http://ipinfo.io/ip http://www.ipchicken.com http://icanhazip.com http://ident.me http://nicolas.boisselier.free.f/myip/; + do + Get "$url" | perl -ne 'print "$1\n" and exit 64 if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/'; + [ "$?" == "64" ] && return 0; + done; + return 1 +} + +Get() { + if which curl &>/dev/null; then + curl -sk $* + + elif which wget &>/dev/null; then + wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $* + + elif which lynx &>/dev/null; then + lynx --source -dump $* + + else + echo "Can't find web client" 1>&2 + fi +} + +Head() { + if which curl &>/dev/null; then + curl -sk --head $* + + elif which wget &>/dev/null; then + zGet --save-headers - $* | perl -ne '/^\s*$/ and last;print' + + elif which lynx &>/dev/null; then + lynx --head -dump $* + + else + echo "Can't find web client" 1>&2 + fi +} diff --git a/etc/profile.d/mac.sh b/etc/profile.d/mac.sh index 395f054e..278362dc 100644 --- a/etc/profile.d/mac.sh +++ b/etc/profile.d/mac.sh @@ -27,7 +27,6 @@ alias updatedb='sudo sh -c "cd / && exec /usr/libexec/locate.updatedb"' alias seq='jot -' -alias mac_vlc_playlists='open -a vlc --args /home/nico/Music/*/*.m3u' alias mac_top_proc="echo 'TOP PROCESSES:'; ps -Aro'%cpu, ucomm, user' | grep root | grep -v ' 0.0 '" alias mac_sleep='pmset sleepnow' alias mac_mount_nfs='sudo mount -o resvport -t nfs' @@ -40,13 +39,21 @@ alias mac_halt='halt=1 && bash -c "(sleep 3 && halt) &" && logout' #alias mac-wakeup-screen="pmset schedule wake '\$(date +\"%d/%m/%Y %T\")'" #alias mac-wakeup-screen2='osascript -e \'tell application "System Events" to key code 123\'' alias macps_top="echo 'TOP PROCESSES:'; ps -Aro'%cpu, ucomm, user' | grep $USER | grep -v ' 0.0 '" -alias mac_vlc_playlists="open -a vlc --args /home/nico/Music/*/*.m3u" alias mac_mount_nfs="sudo mount -o resvport -t nfs" if ! which mysql > /dev/null && which mysql5 > /dev/null; then +function mysql() { + if ! which mysql > /dev/null && which mysql5 > /dev/null; then + mysql5 $* + else + $(which mysql) $* + fi +} alias mysql=mysql5 fi +test "$TERM_PROGRAM" = "iTerm.app" && export PROMPT_COMMAND='printf "\033]0;%s\007" "${USER}@${HOSTNAME%%.*}"' + free() { vm_stat | perl -ne '/page size of (\d+)/ and $size=$1; /Pages\s+([^:]+)[^\d]+(\d+)/ and printf("%-23s: %10.2f Mo\n",$1, $2 * $size / 1048576);' } diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index 6bbc64f0..b4f2835e 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -16,10 +16,6 @@ nb_repos_commit_push() { cd "$pwd" } -nb_auth() { - curl -ku nico https://nico.nbdom.net/private/auth/ -} - nb_repos_status() { shell_help "List repos git status" "$@" && return local status @@ -71,53 +67,3 @@ nb_rb() { nb_perl() { ruby -r$NB_ROOT/lib/perl/NB -e "$@" } - -nb_alert() { - local cmd=". $NB_ROOT/etc/profile" - local host - case "$1" in - led) - host=pi.brighton.loc - cmd="$cmd && echo $FUNCNAME > /var/run/gpiod.sock" - ;; - say) - host=pi.brighton.loc - shift - cmd="$cmd && say <<< \"$@\"" - ;; - beep) - host=pi.brighton.loc - cmd="$cmd && play 2>/dev/null -v 5 /home/pi/python_games/beep1.ogg" - ;; - -h|-help|--help) - echo "Usage: $FUNCNAME [led|say|beep|-h] OR TEXT" - return - ;; - *) - host=macbook.brighton.loc - case $HOSTNAME in *[Mm][Aa][Cc][Bb][Oo][Oo][Kk]*) host="";; esac - local msg="$FUNCNAME from $HOSTNAME "$(echo "$*" | tr "\"\`'" "”") - cmd="$cmd && mac_xmessage \"$msg\" 1>/dev/null" - ;; - esac - #host="" - #cmd="" - - if [ -n "$host" ]; then - - local ip=`dig +short $host` - [ -z "$ip" ] && echo "Can't resolve $host" 1>&2 && return 1 - - ping -qc1 "$host" &> /dev/null || ( echo "Can't ping $host" 1>&2; return 1 ) - - ips | grep -qxFm1 "$ip" || cmd="ssh -o BatchMode=yes $host -- '$cmd'" - - fi - - if [ "$USER" == "nico" ]; then - eval "$cmd" - else - su - nico -c "$cmd" - #echo sudo -u nico -- "$cmd" - fi -} diff --git a/etc/profile.d/nico.sh b/etc/profile.d/nico.sh new file mode 100644 index 00000000..1fd8d47c --- /dev/null +++ b/etc/profile.d/nico.sh @@ -0,0 +1,127 @@ +#-------------------------------------------------------------------------------- +# +# ALIASES +# +#-------------------------------------------------------------------------------- + +#[ "$UID" = "0" ] && grep -q '^_\?postgres:' /etc/passwd && alias psql='sudo -u postgres psql' + +#-------------------------------------------------------------------------------- +# Etherwake +# Cable: alias advent-on="etherwake -i eth0 00:22:68:7b:fb:99 -D -b" +# +#alias advent-on="etherwake -i eth0 48:5b:39:eb:db:8a -D -b" + +#-------------------------------------------------------------------------------- +# Torrent +# + +# btlaunchmanycurses.py +#alias torrent="btdownloadcurses.py --max_download_rate 150 --max_files_open 0" +#alias torrentdir="btlaunchmanycurses.py --max_download_rate 150 --max_files_open 0" + +# btdownloadcurses +which btdownloadcurses &>/dev/null && alias torrent="btdownloadcurses --max_upload_rate 1 --minport 6881 --maxport 6999" +which btlaunchmanycurses &>/dev/null && alias torrentdir="btlaunchmanycurses --max_upload_rate 1 --minport 6881 --maxport 6999" + +#-------------------------------------------------------------------------------- +# xterm +# +[ -x /usr/bin/xterm ] && alias xterm="xterm -bg black -fg gray -fn 10x20" + +#-------------------------------------------------------------------------------- +# debian upgrade +# +which server-package.pl &>/dev/null && alias servers-upgrade='server-package.pl update,upgrade $((printf "big.cascais.loc\n";mysql -h mysql izi -NBe "select host from server where not hidden") | sort -u)' + +#-------------------------------------------------------------------------------- +# +# FUNCTIONS +# +#-------------------------------------------------------------------------------- +nb_alert() { + local cmd=". $NB_ROOT/etc/profile" + local host + case "$1" in + led) + host=pi.brighton.loc + cmd="$cmd && echo $FUNCNAME > /var/run/gpiod.sock" + ;; + say) + host=pi.brighton.loc + shift + cmd="$cmd && say <<< \"$@\"" + ;; + beep) + host=pi.brighton.loc + cmd="$cmd && play 2>/dev/null -v 5 /home/pi/python_games/beep1.ogg" + ;; + -h|-help|--help) + echo "Usage: $FUNCNAME [led|say|beep|-h] OR TEXT" + return + ;; + *) + host=macbook.brighton.loc + case $HOSTNAME in *[Mm][Aa][Cc][Bb][Oo][Oo][Kk]*) host="";; esac + local msg="$FUNCNAME from $HOSTNAME "$(echo "$*" | tr "\"\`'" "”") + cmd="$cmd && mac_xmessage \"$msg\" 1>/dev/null" + ;; + esac + #host="" + #cmd="" + + if [ -n "$host" ]; then + + local ip=`dig +short $host` + [ -z "$ip" ] && echo "Can't resolve $host" 1>&2 && return 1 + + ping -qc1 "$host" &> /dev/null || ( echo "Can't ping $host" 1>&2; return 1 ) + + ips | grep -qxFm1 "$ip" || cmd="ssh -o BatchMode=yes $host -- '$cmd'" + + fi + + if [ "$USER" == "nico" ]; then + eval "$cmd" + else + su - nico -c "$cmd" + #echo sudo -u nico -- "$cmd" + fi +} + +nb_auth() { + curl -ku "$USER" https://nico.nbdom.net/private/auth/ +} + +radio_play() { + local prog=`which mplayer` + if [ -z "$prog" ]; then + case $OSTYPE in + darwin*) prog="open" ;; + esac + fi +perl -e ' +@_=grep{$_} split("\n"," +FranceInter | http://direct.franceinter.fr/live/franceinter-midfi.mp3 +Indie | http://107.155.126.42:17160/listen.pls +FranceInfo | http://direct.franceinfo.fr/live/franceinfo-midfi.mp3 +FranceCulture | http://direct.franceculture.fr/live/franceculture-midfi.mp3 +BBC 1 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p +BBC 2 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p +BBC 3 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio3_mf_p +BBC 4 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p +BBC 5 live | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5live_mf_p +BBC 6 | http://bbcmedia.ic.llnwd.net/stream/bbcmedia_6music_mf_p +"); +if (@ARGV) { $play=shift @ARGV; } else { + $i=1; + map{printf("%2d | %s\n",$i++,$_)} @_; + print "\nChoose one > "; + chomp($play=<>); +} +$play eq "" && exit; @_ = $_[$play-1] =~ /^(.*?)\s*\|\s*(.*?)$/ or exit; +print "[ $play - ".$_[0]." ]\n\n"; +exec "'$prog'",$_[1]; +' "$@" +} + diff --git a/etc/profile.d/zsh.sh b/etc/profile.d/zsh.sh deleted file mode 100644 index 50a1417d..00000000 --- a/etc/profile.d/zsh.sh +++ /dev/null @@ -1 +0,0 @@ -#case "$SHELL" in */zsh) BASH_SOURCE=${(%):-%N} ;; esac -- 2.47.3