diff options
author | makefu <github@syntax-fehler.de> | 2013-11-05 23:40:11 +0100 |
---|---|---|
committer | makefu <github@syntax-fehler.de> | 2013-11-05 23:40:11 +0100 |
commit | 940d07624c6b82d5e7ec790c2ef49b4694a48794 (patch) | |
tree | 3f4bb46333429d8e9a4f566579f82cacbd3096c9 /ship | |
parent | fe1017207310d0cfd6448750205d08b2455f20a2 (diff) | |
parent | 85c99011060b4b37a760fa24d0a0e23e83413bef (diff) |
Merge branch 'master' of github.com:krebscode/painload
Diffstat (limited to 'ship')
-rw-r--r-- | ship/README | 1 | ||||
-rwxr-xr-x | ship/bin/punani | 4 | ||||
-rwxr-xr-x | ship/build | 93 | ||||
-rwxr-xr-x | ship/deploy | 10 | ||||
-rw-r--r-- | ship/develop | 5 | ||||
-rw-r--r-- | ship/lib/core | 34 | ||||
-rw-r--r-- | ship/lib/network | 49 | ||||
-rw-r--r-- | ship/lib/punani | 104 | ||||
-rw-r--r-- | ship/out/.placeholder | 0 |
9 files changed, 300 insertions, 0 deletions
diff --git a/ship/README b/ship/README new file mode 100644 index 00000000..b824b503 --- /dev/null +++ b/ship/README @@ -0,0 +1 @@ +# ship - shellscript installation processor diff --git a/ship/bin/punani b/ship/bin/punani new file mode 100755 index 00000000..28bf7d1a --- /dev/null +++ b/ship/bin/punani @@ -0,0 +1,4 @@ +#!/bin/sh +#@info +#@include punani +punani "$@" diff --git a/ship/build b/ship/build new file mode 100755 index 00000000..916953cc --- /dev/null +++ b/ship/build @@ -0,0 +1,93 @@ +#! /bin/sh +set -euf + +## SYNOPSIS +# build compile SRCFILE DSTFILE +# build deps SRCFILE +build() { + case "$1" in + compile) build_compile "$2" "$3";; + deps) build_deps "$2";; + *) echo "build: $1: bad command" >&2; return 23;; + esac +} + +## build directives +build_info_directive='#@info' +build_include_directive='#@include \([0-9A-Za-z]\+\)' + +input_parser="\ +s:^ *\([0-9]\+\) "$build_info_directive"$:build_info \1: +s:^ *\([0-9]\+\) "$build_include_directive"$:build_include \1 \2: +t +s:^ *\([0-9]\+\) .*:echo \1p:" + +## usage: build_include LINENO LIBNAME +build_include() { cat<<EOF +$1a\\ +# begin $2 +$1r$(build_resolve $2) +$1a\\ +# end $2 +EOF +} + +## usage: build_info LINENO +build_info() { cat<<EOF +$1a\\ +# this file was generated by //ship/build\\ +# date: $(date -u --rfc-3339=s)\\ +# version: $(git rev-parse HEAD) +EOF +} + +## usage: build_compile SRCFILE DSTFILE +build_compile() { + srcfile="$(cat "$1")" + + while needs_compilation "$srcfile"; do + script="$(make_sedscript_maker_shellscript "$srcfile")" + srcfile="$(echo "$srcfile" | sed -n "$script")" + done + + echo "$srcfile" > "$2" + chmod +x "$2" +} + +## usage: needs_compilation SHELLSCRIPT +# Returns true if SRCFILE contains compilation directives. +needs_compilation() { + echo "$1" | + grep -q "^\\($build_include_directive\\|$build_info_directive\\)$" +} + +## usage: make_sedscript_maker_shellscript SRCFILE +# Print a shellscript that creates a sedscript that resolves all the build +# directives in SRCFILE. +make_sedscript_maker_shellscript() { + sedscript_generator="$(echo "$1" | nl -b a -s ' ' | sed "$input_parser")" + sedscript="$(eval "$sedscript_generator")" + echo "$sedscript" +} + +## usage: build_deps SRCFILE +build_deps() { + for libname in $(sed -n 's:^'"$build_include_directive"'$:\1:p' "$1"); do + build_resolve "$libname" + done +} + +## usage: build_resolve LIBNAME +build_resolve() { + echo "$BUILD_PATH" | tr : \\n | + xargs -I: printf '%s/%s\n' : "$1" | + xargs -I: ls -d : 2>/dev/null | + head -n 1 | + grep . || + { + echo "build resolve: $1: library not found" >&2 + return 23 + } +} + +build "$@" diff --git a/ship/deploy b/ship/deploy new file mode 100755 index 00000000..0f3e5219 --- /dev/null +++ b/ship/deploy @@ -0,0 +1,10 @@ +#!/bin/sh +set -x +cd $(dirname $0) +bindir=$PWD/bin/ +libdir=$PWD/lib/ +outdir=$PWD/out/ +for file in `ls -1 $bindir`;do + BUILD_PATH=$libdir ./build compile bin/$file out/$file + chmod 755 $outdir/$file +done diff --git a/ship/develop b/ship/develop new file mode 100644 index 00000000..a961f9c2 --- /dev/null +++ b/ship/develop @@ -0,0 +1,5 @@ +#!/bin/sh +source_all(){ + LIBDIR=${1:-.} + for i in $LIBDIR/*; do . "$i"; done +} diff --git a/ship/lib/core b/ship/lib/core new file mode 100644 index 00000000..6d126142 --- /dev/null +++ b/ship/lib/core @@ -0,0 +1,34 @@ +#!/bin/sh + +# logging +msg() { printf "$@\n" >&2 ;} +info() { msg "** $@" ;} +error() { msg "!! $@" ;} +exists(){ type "$1" >/dev/null 2>/dev/null; } +is_root(){ + test $(id -u) -eq 0 +} +get_hostname(){ + # finds the current hostname + # if ENV HOSTN is set echo $HOSTN + # We try the following: + # $HOSTN + # $HOSTNAME + # hostname + # uci system.hostname + # /etc/hostname + # if everything fails, it returns 1 and prints 'unknown' + + if [ -n "${HOSTN:-}" ] ; then printf "${HOSTN:-}" + elif [ -n "${HOSTNAME:-}" ] ;then printf "$HOSTNAME" + elif exists hostname ; then printf "$(hostname)" + elif exists uci ; then printf "$(uci get system.@system[0].hostname)" + elif [ -e /etc/hostname ] ;then printf "$(cat /etc/hostname)" + else printf "unknown"; return 1 + fi + return 0 +} + +line_to_dot(){ + while read line; do printf .; done; +} diff --git a/ship/lib/network b/ship/lib/network new file mode 100644 index 00000000..9d7ea197 --- /dev/null +++ b/ship/lib/network @@ -0,0 +1,49 @@ +#!/bin/sh +#include core + +anytelnet(){ + # find Telnet or similar and executes it at the end + # requires exist + # if env TELNET is set, will be trying to run this + # Tries the following things: + # telnet + # nc + # netcat + # busybox telnet + if [ -e "${TELNET:-does_not_exist}" ]; then + info"Will be using $TELNET as Telnet Client" + elif exists telnet ;then + TELNET="$(command -v telnet)" + elif exists nc ;then + TELNET="$(command -v nc)" + elif exists netcat;then + TELNET="$(command -v netcat)" + elif exists busybox;then + TELNET="$(command -v busybox) telnet" + else + error "Cannot find telnet binary, please install either telnet-client or busybox or netcat or provided TELNET environment.\nbailing out!" + return 1 + fi + $TELNET $@ +} + +send_irc(){ + ## reads from stdin, writes to IRC + ## + ## requires func: exists() anytelnet() + if [ -z "${HOSTN:-}" ]; then + HOSTN="$(get_hostname)" + info "no HOSTN given, using $HOSTN instead" + fi + IRCCHANNEL=${IRCCHANNEL:-"#krebs_incoming"} + IRCSERVER=${IRCSERVER:-"irc.freenode.net"} + IRCPORT=${IRCPORT:-6667} + NICK="${HOSTN}_$(head /dev/urandom | tr -dc "0123456789" | head -c3)" + info "starting irc connect as $NICK" + ( echo "NICK $NICK"; + echo "USER $NICK $IRCSERVER bla : $NICK"; + echo "JOIN $IRCCHANNEL"; + sleep 23; + while read line; do echo "PRIVMSG $IRCCHANNEL :$line";sleep 1;done + sleep 5; ) | anytelnet $IRCSERVER $IRCPORT 2>/dev/null | line_to_dot +} diff --git a/ship/lib/punani b/ship/lib/punani new file mode 100644 index 00000000..e4a5a5e9 --- /dev/null +++ b/ship/lib/punani @@ -0,0 +1,104 @@ +#!/bin/sh +#@include core + +## begin punani DB +_punanidb_pacman_= +_punanidb_yum_= +_punanidb_aptget_= + +_punanidb_pacman_git=git +_punanidb_yum_git=git +_punanidb_aptget_git=git-core + +_punanidb_pacman_python2=python2 +_punanidb_yum_python2=python +_punanidb_aptget_python2=python + +_punanidb_pacman_python3=python +_punanidb_aptget_python3=python3 + +_punanidb_pacman_hostname=inetutils +_punanidb_aptget_hostname=hostname + +_punanidb_pacman_hostname=inetutils +_punanidb_aptget_hostname=hostname + +_punanidb_pacman_make=make +_punanidb_yum_make=make +_punanidb_aptget_make=make + +_punanidb_pacman_tinc=tinc +_punanidb_yum_tinc=tinc +_punanidb_aptget_tinc=tinc + +_punanidb_pacman_tor=tor +_punanidb_yum_tor=tor +_punanidb_aptget_tor=tor + +_punanidb_pacman_nano=nano +_punanidb_yum_nano=nano +_punanidb_aptget_nano=nano + +_punanidb_pacman_vim=vim +_punanidb_yum_vim=vim-enhanced +_punanidb_aptget_vim=vim + +## end punani DB + +_punani_resolve_package(){ + : ${PACKER?PACKER is not set,bailing out} + pkg=${1?please provide package name to resolve} + eval printf "%s" \"\${_punanidb_${PACKER}_${pkg}-}\" | grep . +} +_punani_aptget_install(){ apt-get -y install "$@" ;} +_punani_aptget_remove(){ apt-get -y remove "$@" ;} +_punani_aptget_has() { dpkg -s "$1" | grep -q "Status: install";} +_punani_yum_install(){ yum -y install "$@" ;} +_punani_yum_remove(){ yum -y remove "$@" ;} +_punani_yum_has() { rpm -qa --qf "%{NAME}\n"| egrep "^${1}\$" >/dev/null ;} +_punani_pacman_install(){ pacman --noconfirm -S --needed "$@" ;} +_punani_pacman_remove(){ pacman -Rcs "$@" ;} +_punani_pacman_has(){ pacman -Q "$1" >/dev/null;} +_punani_brew_install(){ brew install "$@"; } +_punani_brew_remove(){ brew remove "$@";} +_punani_brew_has(){ error "not implemented"; return 1 ;} + +punani(){ + ACTION="$1"; shift + PKGS="$*" + for p in apt-get pacman yum brew;do + exists "$p" && PACKER=`printf "%s" "$p" | sed 's/-//g'` && break + done + + [ -z "${PACKER:-}" ] && error "Error 2: no known package manager found; no punani for you!" && return 1 + info "using $PACKER for install" + [ -z "$PKGS" ] && error "no PACKAGE specified." && ACTION="usage" + + + for PKG in $PKGS; do + RES="`_punani_resolve_package $PKG`" + test -z "$RES" && error "could not resolve '$PKG'; no punani for you!"&& return 23 + case "$ACTION" in + install) + eval _punani_${PACKER}_has $RES && info "$RES already installed, skipping" && continue + ! is_root && error "punani requires super-user rights for installing" && return 1 + eval _punani_${PACKER}_install $RES || error "cannot install $RES with $PACKER" + ;; + remove) + ! eval _punani_${PACKER}_has $RES && info "$RES not installed, skipping" && continue + ! is_root && error "punani requires super-user rights for removing" && return 1 + eval _punani_${PACKER}_remove $RES || error "cannot install $RES with $PACKER" + ;; + has) + if eval _punani_${PACKER}_has $RES ;then + info "$RES is installed" + else + info "$RES is not installed" + fi + ;; + *) + error "usage: punani (install|remove|has) PACKAGE..." + return 23 + esac + done +} diff --git a/ship/out/.placeholder b/ship/out/.placeholder new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/ship/out/.placeholder |