| 1 | #!/bin/bash |
| 2 | # Written by Steven Shiau <[email protected]> to use in DRBL for RedHat |
| 3 | # License: GPL |
| 4 | |
| 5 | # Load DRBL setting and functions |
| 6 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 7 | |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | |
| 10 | USAGE() { |
| 11 | echo "Usage: $0 Options SERVICE_NAME {on|off|reset|add|del|list}" |
| 12 | echo " Options:" |
| 13 | echo " -h, --host IP_ADDRESS: set only for the host with IP_ADDRESS instead of all DRBL clients" |
| 14 | echo " -g, --no-gen-ssi Do NOT generate DRBL SSI template tarball." |
| 15 | echo " Example: use the following to turn on the DRBL clients' kudzu" |
| 16 | echo " $0 kudzu on" |
| 17 | } |
| 18 | |
| 19 | # main |
| 20 | check_if_root |
| 21 | |
| 22 | # |
| 23 | unalias ls 2>/dev/null |
| 24 | |
| 25 | while [ $# -gt 0 ]; do |
| 26 | case "$1" in |
| 27 | -h|--host) |
| 28 | shift; specified_host="$1" |
| 29 | shift |
| 30 | ;; |
| 31 | -g|--no-gen-ssi) |
| 32 | gen_ssi="no" |
| 33 | shift;; |
| 34 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 35 | USAGE >& 2 |
| 36 | exit 2 ;; |
| 37 | *) break ;; |
| 38 | esac |
| 39 | done |
| 40 | |
| 41 | service=$1 |
| 42 | switch=$2 |
| 43 | |
| 44 | [ -z "${service}" ] && USAGE && exit 1 |
| 45 | [ -z "$switch" ] && USAGE && exit 1 |
| 46 | |
| 47 | # |
| 48 | if [ -n "$specified_host" ]; then |
| 49 | [ ! -d "$drblroot/$specified_host" ] && echo "Can NOT find DRBL client $specified_host (i.e. no $drblroot/$specified_host)! Program terminated!" && exit 1 |
| 50 | [ -n "$verbose" ] && echo "specified_host: $specified_host" |
| 51 | fi |
| 52 | |
| 53 | # set the host to be processed |
| 54 | # host_list is the IP address of client, like 192.168.1.1... |
| 55 | host_list="" |
| 56 | if [ -n "$specified_host" ]; then |
| 57 | # set the host path |
| 58 | host_list=$drblroot/$specified_host |
| 59 | else |
| 60 | # withoud specified_host, it must be all clients, append each one to $host_list |
| 61 | for ihost in $drblroot/*; do |
| 62 | host_list="$host_list $ihost" |
| 63 | done |
| 64 | fi |
| 65 | |
| 66 | # |
| 67 | for ihost in $host_list; do |
| 68 | case "$switch" in |
| 69 | on) |
| 70 | echo "Turning on the service \"${service}\" for DRBL client `basename $ihost`..." |
| 71 | if is_systemd_init; then |
| 72 | if [ -e "$drbl_common_root/lib/systemd/system/${service}.service" ]; then |
| 73 | # systemctl can't work in chroot environment :`chroot $drbl_common_root/ /sbin/chkconfig --add $i` |
| 74 | # Therefore manually link it: |
| 75 | _systemctl_target=$(grep -e "^WantedBy=" $drbl_common_root/lib/systemd/system/${service}.service | sed -e "s/WantedBy=//") |
| 76 | if [ ! -z "$_systemctl_target" ]; then |
| 77 | echo "to target: '$_systemctl_target'"; |
| 78 | for ti in $_systemctl_target; do |
| 79 | ln -s -v /lib/systemd/system/${service}.service $ihost/etc/systemd/system/$ti.wants/${service}.service |
| 80 | done |
| 81 | else |
| 82 | echo "Skip ..."; |
| 83 | fi |
| 84 | fi |
| 85 | elif [ -e /etc/debian_version ]; then |
| 86 | # Debian-like |
| 87 | get_debian_ubuntu_init_serv_control_prog |
| 88 | # prepare the update-rc.d env, perl is necessary for update-rc.d |
| 89 | prepare_update_rc_d_env $ihost |
| 90 | if [ "$dbn_ubn_serv_control_prog" = "use-insserv" ]; then |
| 91 | # Use insserv |
| 92 | chroot $ihost/ insserv ${service} &> /dev/null |
| 93 | else |
| 94 | # Use update-rc.d |
| 95 | chroot $ihost/ /usr/sbin/update-rc.d ${service} defaults &> /dev/null |
| 96 | fi |
| 97 | #clean_update_rc_d_env $ihost |
| 98 | elif [ -e /etc/SuSE-release ]; then |
| 99 | # SuSE |
| 100 | create_insserv_env $ihost |
| 101 | chroot $ihost/ /sbin/insserv -f ${service} |
| 102 | else |
| 103 | # RH-like |
| 104 | create_chkconfig_env $ihost |
| 105 | chroot $ihost/ /sbin/chkconfig ${service} on |
| 106 | #clean_chkconfig_env $ihost |
| 107 | echo "done!" |
| 108 | fi |
| 109 | ;; |
| 110 | off) |
| 111 | echo "Turning off the service \"${service}\" for DRBL client `basename $ihost`..." |
| 112 | if is_systemd_init; then |
| 113 | if [ -e "$drbl_common_root/lib/systemd/system/${service}.service" ]; then |
| 114 | # systemctl can't work in chroot environment :`chroot $drbl_common_root/ /sbin/chkconfig --add $i` |
| 115 | # Therefore manually link it: |
| 116 | _systemctl_target=$(grep -e "^WantedBy=" $drbl_common_root/lib/systemd/system/${service}.service | sed -e "s/WantedBy=//") |
| 117 | if [ ! -z "$_systemctl_target" ]; then |
| 118 | echo "to target: '$_systemctl_target'"; |
| 119 | for ti in $_systemctl_target; do |
| 120 | rm -fv $ihost/etc/systemd/system/$ti.wants/${service}.service |
| 121 | done |
| 122 | else |
| 123 | echo "Skip ..."; |
| 124 | fi |
| 125 | fi |
| 126 | elif [ -e /etc/debian_version ]; then |
| 127 | # Debian-like |
| 128 | get_debian_ubuntu_init_serv_control_prog |
| 129 | # prepare the update-rc.d env, perl is necessary for update-rc.d |
| 130 | prepare_update_rc_d_env $ihost |
| 131 | if [ "$dbn_ubn_serv_control_prog" = "use-insserv" ]; then |
| 132 | # Use insserv |
| 133 | chroot $ihost/ insserv -r ${service} &> /dev/null |
| 134 | else |
| 135 | # Use update-rc.d |
| 136 | chroot $ihost/ /usr/sbin/update-rc.d -f ${service} remove &> /dev/null |
| 137 | fi |
| 138 | #clean_update_rc_d_env $ihost |
| 139 | elif [ -e /etc/SuSE-release ]; then |
| 140 | # SuSE |
| 141 | create_insserv_env $ihost |
| 142 | chroot $ihost/ /sbin/insserv -f -r ${service} |
| 143 | else |
| 144 | create_chkconfig_env $ihost |
| 145 | chroot $ihost/ /sbin/chkconfig ${service} off |
| 146 | #clean_chkconfig_env $ihost |
| 147 | echo "done!" |
| 148 | fi |
| 149 | ;; |
| 150 | reset) |
| 151 | echo "Resetting the service \"${service}\" for DRBL client `basename $ihost`..." |
| 152 | if [ -e /etc/debian_version ]; then |
| 153 | echo "Reset service in DRBL clients is not supported! Sorry!" |
| 154 | elif [ -e /etc/SuSE-release ]; then |
| 155 | # SuSE |
| 156 | echo "Reset service in DRBL clients is not supported! Sorry!" |
| 157 | else |
| 158 | create_chkconfig_env $ihost |
| 159 | chroot $ihost/ /sbin/chkconfig ${service} reset |
| 160 | #clean_chkconfig_env $ihost |
| 161 | echo "done!" |
| 162 | fi |
| 163 | ;; |
| 164 | add) |
| 165 | echo "Adding the service \"${service}\" for DRBL client `basename $ihost`..." |
| 166 | if [ -e /etc/debian_version ]; then |
| 167 | echo "Add service for DRBL clients in not supported! Sorry!" |
| 168 | elif [ -e /etc/SuSE-release ]; then |
| 169 | # SuSE |
| 170 | echo "Add service for DRBL clients in not supported! Sorry!" |
| 171 | else |
| 172 | create_chkconfig_env $ihost |
| 173 | chroot $ihost/ /sbin/chkconfig --add ${service} |
| 174 | #clean_chkconfig_env $ihost |
| 175 | echo "done!" |
| 176 | fi |
| 177 | ;; |
| 178 | del) |
| 179 | echo "Deleting the service \"${service}\" for DRBL client `basename $ihost`..." |
| 180 | if [ -e /etc/debian_version ]; then |
| 181 | echo "Reset service in DRBL clients is not supported! Sorry!" |
| 182 | elif [ -e /etc/SuSE-release ]; then |
| 183 | # SuSE |
| 184 | echo "Reset service in DRBL clients is not supported! Sorry!" |
| 185 | else |
| 186 | create_chkconfig_env $ihost |
| 187 | chroot $ihost/ /sbin/chkconfig --del ${service} |
| 188 | #clean_chkconfig_env $ihost |
| 189 | echo "done!" |
| 190 | fi |
| 191 | ;; |
| 192 | list) |
| 193 | echo "Listing the service \"${service}\" for DRBL client `basename $ihost`..." |
| 194 | if [ -e /etc/debian_version ]; then |
| 195 | echo "Reset service in DRBL clients is not supported! Sorry!" |
| 196 | elif [ -e /etc/SuSE-release ]; then |
| 197 | # SuSE |
| 198 | echo "Reset service in DRBL clients is not supported! Sorry!" |
| 199 | else |
| 200 | create_chkconfig_env $ihost |
| 201 | chroot $ihost/ /sbin/chkconfig --list ${service} |
| 202 | #clean_chkconfig_env $ihost |
| 203 | echo "done!" |
| 204 | fi |
| 205 | ;; |
| 206 | *) |
| 207 | USAGE |
| 208 | exit 1 |
| 209 | esac |
| 210 | done |
| 211 | |
| 212 | # |
| 213 | if [ "$gen_ssi" != "no" ]; then |
| 214 | echo "-------------------------------------------------------" |
| 215 | echo "Since some config files are modified in template client, creating template tarball for DRBL SSI..." |
| 216 | drbl-gen-ssi-files |
| 217 | fi |