diff options
-rw-r--r-- | Readme.md | 2 | ||||
-rw-r--r-- | libs/pkgmanager | 29 | ||||
-rwxr-xr-x | ltask | 12 | ||||
-rw-r--r-- | tasks/example | 7 | ||||
-rw-r--r-- | tasks/initial-user-setup | 37 | ||||
-rw-r--r-- | tasks/update | 23 | ||||
-rwxr-xr-x | tools/rexec.sh | 26 |
7 files changed, 93 insertions, 43 deletions
@@ -32,6 +32,8 @@ Reference for environment variables # To be set by task SSH_USER username to connect as SSH_IDENTITY path to ssh-key used for authentification of specified user + FILES Array of Files to copy over before script invocation + INCLUDES Array of libraries to include # Available in setup ASSET_DIR paht to folder where eg keys are stored diff --git a/libs/pkgmanager b/libs/pkgmanager new file mode 100644 index 0000000..7c1efd3 --- /dev/null +++ b/libs/pkgmanager @@ -0,0 +1,29 @@ +# vi: ft=sh + +PKGMAN="/bin/false" + +[ -f "/etc/os-release" ] && source /etc/os-release +case $ID in + arch) + PKGMAN="pacman" + INSTALL_CMD="--noconfirm -S" + REMOVE_CMD="--noconfirm -Rs" + UPDATE_CMD="--noconfirm -Sy" + UPGRADE_CMD="--noconfirm -Syu" + FULLUPGRADE_CMD="--noconfirm -Syu" + ;; + debian) + PKGMAN="apt-get" + INSTALL_CMD="install -y" + REMOVE_CMD="purge -y" + UPDATE_CMD="update" + UPGRADE_CMD="upgrade -y" + FULLUPGRADE_CMD="dist-upgrade" + ;; +esac + +alias pkg_install="sudo $PKGMAN $INSTALL_CMD" +alias pkg_remove="sudo $PKGMAN $REMOVE_CMD" +alias pkg_update="sudo $PKGMAN $UPDATE_CMD" +alias pkg_upgrade="sudo $PKGMAN $UPGRADE_CMD" +alias pkg_fullupgrade="sudo $PKGMAN $FULLUPGRADE_CMD" @@ -31,12 +31,14 @@ export readonly TASK_DIR=$BASEDIR/tasks export readonly ASSET_DIR=$BASEDIR/assets export readonly TARGET_DIR=$BASEDIR/targets export readonly TOOL_DIR=$BASEDIR/tools +export readonly INCLUDE_DIR=$BASEDIR/libs TASK= TARGET= HOSTS=() FILES=() +INCLUDES=() #block run when sourcing task export readonly HOSTMODE="yes" @@ -62,10 +64,7 @@ function parse_args() { echo $0 -h for help exit 1;; esac - done - #[ -z $TASK ] && echo No task specified && exit 1 - #[ -z $TARGET ] && echo No target specified && exit 1 } function print_help() { @@ -110,15 +109,14 @@ for hoststring in "${HOSTS_UNIQUE[@]}"; do [ ! -z $port ] && rexecargs+="-p $port " rexecargs+="-i ${SSH_IDENTITY_FILE:=~/.ssh/id_rsa} " + [ ${#FILES[@]} -gt 0 ] && rexecargs+="-f \"${FIELS[@]}\" " [ ! -z $SSH_USER ] && rexecargs+="$SSH_USER@" - rexecargs+="$hostname " - rexecargs+="$TASK_DIR/$TASK" + rexecargs+="$hostname ${INCLUDES[@]} $TASK_DIR/$TASK" echo echo "<=== $hostname ===>" echo - - [ ${#FILES[@]} -gt 0 ] && scp -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no -S "$SSH" -i ${SSH_IDENTITY_FILE:=~/.ssh/id_rsa} -P ${port:=22} ${FILES[@]} ${SSH_USER:=$USER}@$hostname: + echo $rexecargs $TOOL_DIR/rexec.sh $rexecargs done diff --git a/tasks/example b/tasks/example index f5c8a9a..bc78361 100644 --- a/tasks/example +++ b/tasks/example @@ -10,10 +10,15 @@ function task_setup() { # Available environment variables # $ASSET_DIR # $TOOL_DIR - + # $INCLUDE_DIR + + INCLUDES+=() + SSH_USER=server SSH_IDENTITY_FILE=$ASSET_DIR/id_rsa + FILES+=() + TASK_ISSET="yes" } diff --git a/tasks/initial-user-setup b/tasks/initial-user-setup index fb402bb..de3aee9 100644 --- a/tasks/initial-user-setup +++ b/tasks/initial-user-setup @@ -1,18 +1,18 @@ #!/bin/bash +function task_setup() { + SSH_USER=root + SSH_IDENTITY_FILE=$ASSET_DIR/id_jenkins + + FILES+=("$ASSET_DIR/id_user.pub" "$ASSET_DIR/id_jenkins.pub") + INCLUDES+=("pkgmanager") + + TASK_ISSET="yes" +} + function task_exec() { - source /etc/os-release - case $ID in - arch) - pacman -Sy - INSTALL_CMD="pacman --noconfirm -S";; - debian) - apt update - INSTALL_CMD="apt-get install -y" - esac - - PACKAGES="sudo vim git" - [ ! -z "$INSTALL_CMD" ] && $INSTALL_CMD $PACKAGES + pkg_update + pkg_install sudo groupadd sudo useradd -s /bin/bash -d /home/server -m -U -G sudo server @@ -33,19 +33,6 @@ EOF echo > /etc/motd } -function task_setup() { - # Available environment variables - # $ASSET_DIR - # $TOOL_DIR - - SSH_USER=root - #SSH_IDENTITY_FILE=$ASSET_DIR/id_jenkins - - FILES+=("$ASSET_DIR/id_user.pub" "$ASSET_DIR/id_jenkins.pub") - - TASK_ISSET="yes" -} - if [ -z $HOSTMODE ]; then task_exec else diff --git a/tasks/update b/tasks/update new file mode 100644 index 0000000..e392b76 --- /dev/null +++ b/tasks/update @@ -0,0 +1,23 @@ +#!/bin/bash + +function task_exec() { + pkg_update + pkg_upgrade +} + +function task_setup() { + INCLUDES+=("$INCLUDE_DIR/pkgmanager") + + SSH_USER=server + #SSH_IDENTITY_FILE=$ASSET_DIR/id_rsa + + FILES+=() + + TASK_ISSET="yes" +} + +if [ -z $HOSTMODE ]; then + task_exec +else + task_setup +fi diff --git a/tools/rexec.sh b/tools/rexec.sh index b45410c..98652c4 100755 --- a/tools/rexec.sh +++ b/tools/rexec.sh @@ -27,17 +27,18 @@ SSH_HOST= SSH_OPTIONS="-o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no" SSH="ssh $SSH_OPTIONS" -SCRIPT_FILE= +SCRIPT_FILES=() SCRIPT_INTERPRETER=/bin/sh +FILES="" + function parse_args() { for (( i=0; i < $ARGC;i++ )); do local ARGREGEX="^-.*" if [[ ! ${ARGV[$i]} =~ $ARGREGEX ]]; then [ -z $SSH_HOST ] && SSH_HOST=${ARGV[$i]} && continue - [ -z $SCRIPT_FILE ] && SCRIPT_FILE=${ARGV[$i]} && continue - - print_help 1 + SCRIPT_FILES+=(${ARGV[$i]}) + continue fi case ${ARGV[$i]} in @@ -50,6 +51,9 @@ function parse_args() { -i) i=$((i+1)) SSH_IDENTITY=${ARGV[$i]};; + -f) + i=$((i+1)) + FILES="${ARGV[$i]}";; -h) print_help 0;; *) @@ -61,7 +65,7 @@ function parse_args() { done [ -z $SSH_HOST ] && echo No host specified && exit 1 - [ -z $SCRIPT_FILE ] && echo No script specified && exit 1 + [ ${#SCRIPT_FILES[@]} -eq 0 ] && echo No script specified && exit 1 } function print_help() { @@ -73,6 +77,7 @@ $0 [OPTIONS] [USER@]HOST SCRIPT -p <PORT> SSH port (default: 22) -s <SSH> Custom SSH program -i <IDENTITY> SSH Identity file (default: ~/.ssh/id_rsa.pub) + -f FILE Copy FILE to target before execution -h Print this help text EOF exit $1 @@ -80,10 +85,11 @@ EOF parse_args -[ ! -f $SCRIPT_FILE ] && echo \"$SCRIPT_FILE\" not found && exit 1 +#[ ! -f $SCRIPT_FILE ] && echo \"$SCRIPT_FILE\" not found && exit 1 -INTERPRETER_STRING=$(head -n 1 $SCRIPT_FILE) -INTERPRETER_REGEX="^#!.*" -[[ $INTERPRETER_STRING =~ $INTERPRETER_REGEX ]] && SCRIPT_INTERPRETER=$(tail -c +3 <<< $INTERPRETER_STRING) +#INTERPRETER_STRING=$(head -n 1 $SCRIPT_FILE) +#INTERPRETER_REGEX="^#!.*" +#[[ $INTERPRETER_STRING =~ $INTERPRETER_REGEX ]] && SCRIPT_INTERPRETER=$(tail -c +3 <<< $INTERPRETER_STRING) -cat $SCRIPT_FILE | $SSH -p $SSH_PORT -i $SSH_IDENTITY $SSH_HOST $SCRIPT_INTERPRETER +[ ! -z "$FILES" ] && scp -S "$SSH" -i $SSH_IDENTITY -P $SSH_PORT $FILES $SSH_HOST: +cat ${SCRIPT_FILES[@]} | $SSH -p $SSH_PORT -i $SSH_IDENTITY $SSH_HOST "/bin/bash" |