add ability to copy files from server to local machine

This commit is contained in:
Aaron Bieber 2020-05-17 07:35:09 -06:00
parent ee376ffaf7
commit 95de3d6372
3 changed files with 109 additions and 2 deletions

View File

@ -25,12 +25,16 @@ DRY=0
MAINTENANCE=0 MAINTENANCE=0
SNAPSHOT="" SNAPSHOT=""
FORCE=0 FORCE=0
COPY=0
SSH_CTL_PATH="/tmp/boxctl-%r@%h:%p" SSH_CTL_PATH="/tmp/boxctl-%r@%h:%p"
SSH_OPTS="-o ControlMaster=auto -o ControlPersist=60s -o ControlPath=${SSH_CTL_PATH}" SSH_OPTS="-o ControlMaster=auto -o ControlPersist=60s -o ControlPath=${SSH_CTL_PATH}"
RSYNC_OPTS="--rsync-path=/usr/bin/openrsync -Dlrt" RSYNC_OPTS="--rsync-path=/usr/bin/openrsync -Dlrt"
while getopts "fh:mnu:sv" arg; do while getopts "cfh:mnu:sv" arg; do
case $arg in case $arg in
c)
COPY=1
;;
f) f)
FORCE=1 FORCE=1
;; ;;
@ -216,6 +220,23 @@ fnc() {
echo "${1} ${_count}" echo "${1} ${_count}"
} }
if [ "${COPY}" == "1" ]; then
if [ -f ./files ]; then
msg 0 "syncing $(fnc files) from ${SERVER}"
for file in $(cat files | grep -v ^#); do
local _src _dest _mode _owner _group _dir
read _src _owner _group _mode _dest <<EOF
$(echo $file | sed 's/:/ /g')
EOF
_dir=$(dirname $_dest)
msg 1 "\t${_dest} -> ${_src}"
_rsync "${RUN_USER}@${SERVER}:$_dest" $_src
done
fi
exit 0
fi
if [ -f ./packages ]; then if [ -f ./packages ]; then
msg 0 "installing $(fnc packages)" msg 0 "installing $(fnc packages)"
cmd=$(printf "${PKG_DIFF_INSTALL}" $V $V) cmd=$(printf "${PKG_DIFF_INSTALL}" $V $V)

View File

@ -24,7 +24,7 @@
machines machines
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm boxctl .Nm boxctl
.Op Fl fmnsv .Op Fl cfmnsv
.Op Fl h Ar host .Op Fl h Ar host
.Op Fl u Ar user .Op Fl u Ar user
.Sh DESCRIPTION .Sh DESCRIPTION
@ -40,6 +40,8 @@ base.
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl c
Copy managed files from server into local directory.
.It Fl h Ar host .It Fl h Ar host
Remote host to be managed. Remote host to be managed.
.It Fl u Ar user .It Fl u Ar user

84
smart_users.diff Normal file
View File

@ -0,0 +1,84 @@
diff --git a/boxctl.sh b/boxctl.sh
index da15b17..5037112 100755
--- a/boxctl.sh
+++ b/boxctl.sh
@@ -208,6 +208,35 @@ _ssh() {
fi
}
+if_optval() {
+ local _a=$1 _b=$2 _c
+
+ if [ ! -z $3 ]; then
+ _c=$3
+ else
+ _c=""
+ fi
+
+ _a=${_a%%\"}
+ _a=${_a##\"}
+
+ _b=${_b%%\"}
+ _b=${_b##\"}
+
+ _c=${_c%%\"}
+ _c=${_c##\"}
+
+ if [ "${_b}" != "" ]; then
+ echo "${_a} ${_b}"
+ else
+ if [ "$_c" != "" ]; then
+ echo "${_a} ${_c}"
+ else
+ echo ""
+ fi
+ fi
+}
+
V=$(expand_v)
fnc() {
@@ -239,20 +268,34 @@ fi
if [ -f ./users ]; then
msg 0 "adding $(fnc users)"
for user in $(cat users | grep -v ^#); do
- local _u _uid _gid _c _home _shell _pass
- read _u _uid _gid _c _home _shell _pass <<EOF
- $(echo $user | sed 's/:/ /g')
+ local _u _uid _gid _groups _c _home _shell _pass
+ read _u _uid _gid _groups _c _home _shell _pass <<EOF
+ $(echo "${user}" | awk -F: \
+ '{for(i = 1; i <= NF; i++) { printf "\"%s\" ", $i; }}')
EOF
msg 1 "\t${_u} (${_c})"
_ssh ${RUN_USER}@${SERVER} "grep -q ^${_u} /etc/passwd || \
/usr/sbin/useradd \
- -s ${_shell} \
- -c '${_c}' \
- -d '${_home}' \
+ $V \
+ -m \
+ $(if_optval "-s" "${_shell}" "/bin/ksh") \
+ $(if_optval "-c" "${_c}" "") \
+ $(if_optval "-d" "${_home}" "") \
+ $(if_optval "-g" "${_gid}" "") \
+ $(if_optval "-G" "${_groups}" "") \
+ $(if_optval "-u" "${_uid}" "") \
+ $(if_optval "-p" "${_pass}" "") \
+ ${_u} && \
+ /usr/sbin/usermod \
+ $V \
-m \
- -g ${_gid} \
- -u ${_uid} \
- -p ${_pass} \
+ $(if_optval "-s" "${_shell}" "/bin/ksh") \
+ $(if_optval "-c" "${_c}" "") \
+ $(if_optval "-d" "${_home}" "") \
+ $(if_optval "-g" "${_gid}" "") \
+ $(if_optval "-G" "${_groups}" "") \
+ $(if_optval "-u" "${_uid}" "") \
+ $(if_optval "-p" "${_pass}" "") \
${_u}"
done
fi