install_arbi_tq.sh 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/bin/bash
#
# This file is used to install database on linux systems. The operating system
# is required to use systemd to manage services at boot

set -e
#set -x

# -----------------------Variables definition---------------------
script_dir=$(dirname $(readlink -f "$0"))

bin_link_dir="/usr/bin"
#inc_link_dir="/usr/include"

#install main path
install_main_dir="/usr/local/tarbitrator"

# old bin dir
bin_dir="/usr/local/tarbitrator/bin"

service_config_dir="/etc/systemd/system"

# Color setting
RED='\033[0;31m'
GREEN='\033[1;32m'
GREEN_DARK='\033[0;32m'
GREEN_UNDERLINE='\033[4;32m'
NC='\033[0m'

csudo=""
if command -v sudo > /dev/null; then
32
    csudo="sudo "
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fi

update_flag=0

initd_mod=0
service_mod=2
if pidof systemd &> /dev/null; then
    service_mod=0
elif $(which service &> /dev/null); then    
    service_mod=1
    service_config_dir="/etc/init.d" 
    if $(which chkconfig &> /dev/null); then
         initd_mod=1 
    elif $(which insserv &> /dev/null); then
        initd_mod=2
    elif $(which update-rc.d &> /dev/null); then
        initd_mod=3
    else
        service_mod=2
    fi
else 
    service_mod=2
fi


# get the operating system type for using the corresponding init file
# ubuntu/debian(deb), centos/fedora(rpm), others: opensuse, redhat, ..., no verification
#osinfo=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
if [[ -e /etc/os-release ]]; then
  osinfo=$(cat /etc/os-release | grep "NAME" | cut -d '"' -f2)   ||:
else
  osinfo=""
fi
#echo "osinfo: ${osinfo}"
os_type=0
if echo $osinfo | grep -qwi "ubuntu" ; then
#  echo "This is ubuntu system"
  os_type=1
elif echo $osinfo | grep -qwi "debian" ; then
#  echo "This is debian system"
  os_type=1
elif echo $osinfo | grep -qwi "Kylin" ; then
#  echo "This is Kylin system"
  os_type=1
elif  echo $osinfo | grep -qwi "centos" ; then
#  echo "This is centos system"
  os_type=2
elif echo $osinfo | grep -qwi "fedora" ; then
#  echo "This is fedora system"
  os_type=2
else
  echo " osinfo: ${osinfo}"
  echo " This is an officially unverified linux system," 
  echo " if there are any problems with the installation and operation, "
  echo " please feel free to contact taosdata.com for support."
  os_type=1
fi

function kill_tarbitrator() {
  pid=$(ps -ef | grep "tarbitrator" | grep -v "grep" | awk '{print $2}')
  if [ -n "$pid" ]; then
94
    ${csudo}kill -9 $pid   || :
95 96 97 98 99
  fi
}

function install_main_path() {
    #create install main dir and all sub dir
100 101 102 103 104
    ${csudo}rm -rf ${install_main_dir}    || :
    ${csudo}mkdir -p ${install_main_dir}
    ${csudo}mkdir -p ${install_main_dir}/bin  
    #${csudo}mkdir -p ${install_main_dir}/include
    ${csudo}mkdir -p ${install_main_dir}/init.d
105 106 107 108
}

function install_bin() {
    # Remove links
109 110 111
    ${csudo}rm -f ${bin_link_dir}/rmtarbitrator   || :
    ${csudo}rm -f ${bin_link_dir}/tarbitrator     || :
    ${csudo}cp -r ${script_dir}/bin/* ${install_main_dir}/bin && ${csudo}chmod 0555 ${install_main_dir}/bin/*
112 113

    #Make link
114 115
    [ -x ${install_main_dir}/bin/remove_arbi_tq.sh ] && ${csudo}ln -s ${install_main_dir}/bin/remove_arbi_tq.sh ${bin_link_dir}/rmtarbitrator  || :
    [ -x ${install_main_dir}/bin/tarbitrator ] && ${csudo}ln -s ${install_main_dir}/bin/tarbitrator ${bin_link_dir}/tarbitrator || :
116 117 118
}

function install_header() {
119 120 121 122 123
    ${csudo}rm -f ${inc_link_dir}/taos.h ${inc_link_dir}/taosdef.h ${inc_link_dir}/taoserror.h    || :
    ${csudo}cp -f ${script_dir}/inc/* ${install_main_dir}/include && ${csudo}chmod 644 ${install_main_dir}/include/*
    ${csudo}ln -s ${install_main_dir}/include/taos.h ${inc_link_dir}/taos.h
    ${csudo}ln -s ${install_main_dir}/include/taosdef.h ${inc_link_dir}/taosdef.h
    ${csudo}ln -s ${install_main_dir}/include/taoserror.h ${inc_link_dir}/taoserror.h
124 125 126 127
}

function clean_service_on_sysvinit() {
    if pidof tarbitrator &> /dev/null; then
128
        ${csudo}service tarbitratord stop || :
129 130 131 132
    fi

    if ((${initd_mod}==1)); then
      if [ -e ${service_config_dir}/tarbitratord ]; then 
133
        ${csudo}chkconfig --del tarbitratord || :
134 135 136
      fi
    elif ((${initd_mod}==2)); then
      if [ -e ${service_config_dir}/tarbitratord ]; then
137
        ${csudo}insserv -r tarbitratord || :
138 139 140
      fi
    elif ((${initd_mod}==3)); then
      if [ -e ${service_config_dir}/tarbitratord ]; then
141
        ${csudo}update-rc.d -f tarbitratord remove || :
142 143 144
      fi
    fi    

145
    ${csudo}rm -f ${service_config_dir}/tarbitratord || :
146 147
    
    if $(which init &> /dev/null); then
148
        ${csudo}init q || :
149 150 151 152 153 154 155 156 157 158
    fi
}

function install_service_on_sysvinit() {
    clean_service_on_sysvinit
    sleep 1

    # Install tqd service

    if ((${os_type}==1)); then
159 160
        ${csudo}cp -f ${script_dir}/init.d/tarbitratord.deb ${install_main_dir}/init.d/tarbitratord
        ${csudo}cp    ${script_dir}/init.d/tarbitratord.deb ${service_config_dir}/tarbitratord && ${csudo}chmod a+x ${service_config_dir}/tarbitratord
161
    elif ((${os_type}==2)); then
162 163
        ${csudo}cp -f ${script_dir}/init.d/tarbitratord.rpm ${install_main_dir}/init.d/tarbitratord
        ${csudo}cp    ${script_dir}/init.d/tarbitratord.rpm ${service_config_dir}/tarbitratord && ${csudo}chmod a+x ${service_config_dir}/tarbitratord
164 165 166
    fi
    
    #restart_config_str="tq:2345:respawn:${service_config_dir}/tqd start"
167
    #${csudo}grep -q -F "$restart_config_str" /etc/inittab || ${csudo}bash -c "echo '${restart_config_str}' >> /etc/inittab"
168 169
    
    if ((${initd_mod}==1)); then
170 171
        ${csudo}chkconfig --add tarbitratord || :
        ${csudo}chkconfig --level 2345 tarbitratord on || :
172
    elif ((${initd_mod}==2)); then
173 174
        ${csudo}insserv tarbitratord || :
        ${csudo}insserv -d tarbitratord || :
175
    elif ((${initd_mod}==3)); then
176
        ${csudo}update-rc.d tarbitratord defaults || :
177 178 179 180 181 182 183
    fi
}

function clean_service_on_systemd() {
  tarbitratord_service_config="${service_config_dir}/tarbitratord.service"
  if systemctl is-active --quiet tarbitratord; then
      echo "tarbitrator is running, stopping it..."
184
      ${csudo}systemctl stop tarbitratord &> /dev/null || echo &> /dev/null
185
  fi
186
  ${csudo}systemctl disable tarbitratord &> /dev/null || echo &> /dev/null
187

188
  ${csudo}rm -f ${tarbitratord_service_config}
189 190 191 192 193 194 195 196 197
}

# tq:2345:respawn:/etc/init.d/tarbitratord start

function install_service_on_systemd() {
    clean_service_on_systemd

    tarbitratord_service_config="${service_config_dir}/tarbitratord.service"

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    ${csudo}bash -c "echo '[Unit]'                                  >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'Description=TQ arbitrator service' >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'After=network-online.target'             >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'Wants=network-online.target'             >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo                                           >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo '[Service]'                               >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'Type=simple'                             >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'ExecStart=/usr/bin/tarbitrator'          >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'TimeoutStopSec=1000000s'                 >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'LimitNOFILE=infinity'                    >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'LimitNPROC=infinity'                     >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'LimitCORE=infinity'                      >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'TimeoutStartSec=0'                       >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'StandardOutput=null'                     >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'Restart=always'                          >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'StartLimitBurst=3'                       >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'StartLimitInterval=60s'                  >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo                                           >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo '[Install]'                               >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'WantedBy=multi-user.target'              >> ${tarbitratord_service_config}"
    ${csudo}systemctl enable tarbitratord
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}

function install_service() {
    if ((${service_mod}==0)); then
        install_service_on_systemd
    elif ((${service_mod}==1)); then
        install_service_on_sysvinit
    else
        kill_tarbitrator
    fi
}

function update_tq() {
    # Start to update
    echo -e "${GREEN}Start to update TQ's arbitrator ...${NC}"
    # Stop the service if running
    if pidof tarbitrator &> /dev/null; then
        if ((${service_mod}==0)); then
237
            ${csudo}systemctl stop tarbitratord || :
238
        elif ((${service_mod}==1)); then
239
            ${csudo}service tarbitratord stop || :
240 241 242 243 244 245 246 247 248 249 250 251 252
        else
            kill_tarbitrator
        fi
        sleep 1
    fi
    
    install_main_path
    #install_header
    install_bin
    install_service
		
    echo
    if ((${service_mod}==0)); then
253
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}systemctl start tarbitratord${NC}"
254
    elif ((${service_mod}==1)); then
255
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}service tarbitratord start${NC}"
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    else
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ./tarbitrator${NC}"
    fi                
    echo
    echo -e "\033[44;32;1mTQ's arbitrator is updated successfully!${NC}"
}

function install_tq() {
    # Start to install
    echo -e "${GREEN}Start to install TQ's arbitrator ...${NC}"
    
    install_main_path	   
    #install_header
    install_bin
    install_service
    echo
    if ((${service_mod}==0)); then
273
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}systemctl start tarbitratord${NC}"
274
    elif ((${service_mod}==1)); then
275
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}service tarbitratord start${NC}"
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    else
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: tarbitrator${NC}"
    fi

    echo -e "\033[44;32;1mTQ's arbitrator is installed successfully!${NC}"
    echo
}


## ==============================Main program starts from here============================
# Install server and client
if [ -x ${bin_dir}/tarbitrator ]; then
    update_flag=1
    update_tq
else
    install_tq
fi