| 1 | #!/bin/bash |
| 2 | # Author: Blake, Kuo-Lien Huang |
| 3 | # License: GPL |
| 4 | # Description: put/get the file to/from all DRBL clients, |
| 5 | # file utility for host(fuh) |
| 6 | # |
| 7 | # Modified by Steven Shiau <[email protected]> to be used in DRBL for RedHat |
| 8 | |
| 9 | # Load DRBL setting and functions |
| 10 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 11 | |
| 12 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 13 | |
| 14 | # |
| 15 | run_cmd="`basename $0`" |
| 16 | |
| 17 | # to backword compatible |
| 18 | case "$run_cmd" in |
| 19 | "drbl-cp-host"|"drbl-host-cp") |
| 20 | run_cmd="drbl-fuh-put" |
| 21 | ;; |
| 22 | "drbl-rm-host"|"drbl-host-rm") |
| 23 | run_cmd="drbl-fuh-rm" |
| 24 | ;; |
| 25 | "drbl-get-host"|"drbl-host-get") |
| 26 | run_cmd="drbl-fuh-get" |
| 27 | ;; |
| 28 | esac |
| 29 | |
| 30 | usage() { |
| 31 | echo "NAME Put(default)/get/remove files to/from DRBL clients." |
| 32 | echo |
| 33 | echo "DESCRIPTION" |
| 34 | echo "drbl-cp-host (or drbl-host-cp) for putting files, drbl-get-host (or drbl-host-get) for collecting files, drbl-rm-host (or drbl-host-rm) for removing files." |
| 35 | echo |
| 36 | echo "Usage: `basename $0` [-d|--directory DIR ] [-v|--verbose] source target" |
| 37 | echo "-d, --directory=DIR: process the DIR relative to client root directory" |
| 38 | echo "-v, --verbose prints out verbose information" |
| 39 | echo "\"target\" can be a file or a directory, it it's file, it's new filename for source to be copied. If it's a directory, the source will be copied into that directory." |
| 40 | echo |
| 41 | echo "Example:" |
| 42 | echo "drbl-cp-host ./XF86_DEFAULT /etc" |
| 43 | echo "will copy XF86_DEFAULT into client's /etc directory" |
| 44 | echo |
| 45 | echo "drbl-cp-host -d local source_files new_file.txt" |
| 46 | echo "will copy source_files into client's /local directory as file name new_file.txt" |
| 47 | echo |
| 48 | echo "drbl-rm-host -d local target.txt" |
| 49 | echo "will remove target.txt from client's /local directory" |
| 50 | echo |
| 51 | echo "drbl-get-host -d local target.txt" |
| 52 | echo "will get target.txt from client's /local directory" |
| 53 | echo |
| 54 | echo "drbl-get-host -d local \"*.txt\"" |
| 55 | echo "will get *.txt from client's /local directory" |
| 56 | echo "NOTE!!! You must put \" \" before and after the star wildcard * filename!" |
| 57 | } |
| 58 | |
| 59 | # |
| 60 | if [ "$run_cmd" = "drbl-fuh" ]; then |
| 61 | echo "use drbl-cp-host (or drbl-host-cp) for putting files, drbl-get-host (or drbl-host-get) for collecting files, drbl-rm-host (or drbl-host-rm) for removing files." |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | while [ $# -gt 0 ]; do |
| 66 | case "$1" in |
| 67 | -d|--directory) |
| 68 | shift; sub_dir="$1" |
| 69 | shift;; |
| 70 | -v|--verbose) |
| 71 | VERBOSE="on" |
| 72 | shift;; |
| 73 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 74 | usage >& 2 |
| 75 | exit 2 ;; |
| 76 | *) break ;; |
| 77 | esac |
| 78 | done |
| 79 | |
| 80 | # get the source and target |
| 81 | source_file=$1 |
| 82 | target_file=$2 |
| 83 | |
| 84 | # check if source_file is dir or file, if dir, append / |
| 85 | [ -d "$source_file" ] && source_file=$source_file"/" |
| 86 | |
| 87 | # check parameter |
| 88 | if [ $# -le 0 ]; then |
| 89 | usage |
| 90 | exit 1 |
| 91 | fi |
| 92 | case "$run_cmd" in |
| 93 | "drbl-fuh-put") |
| 94 | mode="put" |
| 95 | ;; |
| 96 | "drbl-fuh-get") |
| 97 | mode="get" |
| 98 | ;; |
| 99 | "drbl-fuh-rm") |
| 100 | mode="rm" |
| 101 | ;; |
| 102 | *) |
| 103 | echo "Unknown mode! Did you change the filename ?" |
| 104 | echo "Program terminated!" |
| 105 | exit 1 |
| 106 | ;; |
| 107 | esac |
| 108 | |
| 109 | # set the default values if empty |
| 110 | [ -z "$mode" ] && mode="put" |
| 111 | [ -z "$source_file" ] && echo "No source! Program terminated!" && exit 1 |
| 112 | [ -z "$target_file" ] && target_file="$source_file" |
| 113 | |
| 114 | # |
| 115 | if [ -n "$VERBOSE" ]; then |
| 116 | echo "mode=$mode" |
| 117 | echo "source_file=$source_file" |
| 118 | echo "target_file=$target_file" |
| 119 | fi |
| 120 | |
| 121 | # main |
| 122 | unalias ls 2>/dev/null |
| 123 | nodes_list=$(ls $drblroot) |
| 124 | |
| 125 | # |
| 126 | if [ "$mode" = "rm" ]; then |
| 127 | echo "Warning! This will delete $source_file in each DRBL client!!! Are you sure ?" |
| 128 | echo "[y/N]" |
| 129 | read rm_confirm |
| 130 | case "$rm_confirm" in |
| 131 | y|[yY][eE][sS]) |
| 132 | if [ -n "$sub_dir" ]; then |
| 133 | echo "Do you want to remove the directory \"$sub_dir\" for each host in $drblroot ?" |
| 134 | echo "[y/N]" |
| 135 | read rm_dir_confirm |
| 136 | case "$rm_dir_confirm" in |
| 137 | y|[yY][eE][sS]) |
| 138 | rm_dir_confirm="yes" |
| 139 | ;; |
| 140 | *) |
| 141 | rm_dir_confirm="no" |
| 142 | ;; |
| 143 | esac |
| 144 | fi |
| 145 | ;; |
| 146 | esac |
| 147 | fi |
| 148 | |
| 149 | # store the LC_ALL |
| 150 | LC_ALL_org=$LC_ALL |
| 151 | export LC_ALL=C |
| 152 | |
| 153 | # main |
| 154 | for inode in $nodes_list; do |
| 155 | CLIENT_DIR="$drblroot/$inode" |
| 156 | case "$mode" in |
| 157 | "rm") |
| 158 | case "$rm_confirm" in |
| 159 | y|[yY][eE][sS]) |
| 160 | # just in case, we separate the rm -rf $sub_dir into two steps |
| 161 | rm -rfv $CLIENT_DIR/$sub_dir/$source_file |
| 162 | if [ "$rm_dir_confirm" = "yes" ]; then |
| 163 | rmdir -v $CLIENT_DIR/$sub_dir/ |
| 164 | fi |
| 165 | ;; |
| 166 | *) |
| 167 | echo "abort!" |
| 168 | exit |
| 169 | esac |
| 170 | ;; |
| 171 | "get") |
| 172 | for ifile in $CLIENT_DIR/$sub_dir/$source_file; do |
| 173 | [ -f "$ifile" ] && cp -av $ifile ${inode}_"$(basename ${ifile})" |
| 174 | done |
| 175 | ;; |
| 176 | "put") |
| 177 | [ ! -e "$source_file" ] && echo "$source_file does not exist!" && exit 1 |
| 178 | if [ -z "$(echo "$target_file" | grep -Ew "^/etc")" -a \ |
| 179 | -z "$(echo "$target_file" | grep -Ew "^/root")" -a \ |
| 180 | -z "$(echo "$target_file" | grep -Ew "^/var")" ]; then |
| 181 | echo "You should use drblpush to deploy the files, not this program. This program `basename $0` will only copy files to DRBL client's /etc, /root, or /var!". |
| 182 | else |
| 183 | [ -n "$sub_dir" ] && mkdir -p $CLIENT_DIR/$sub_dir |
| 184 | cp -av $source_file $CLIENT_DIR/$sub_dir/$target_file |
| 185 | fi |
| 186 | ;; |
| 187 | *) |
| 188 | usage |
| 189 | ;; |
| 190 | esac |
| 191 | done |
| 192 | |
| 193 | # |
| 194 | echo "-------------------------------------------------------" |
| 195 | echo "Since some config files are modified in template client, creating template tarball for DRBL SSI..." |
| 196 | drbl-gen-ssi-files |
| 197 | |
| 198 | # restore the LC_ALL |
| 199 | export LC_ALL=$LC_ALL_org |