install_arbi.sh 12.8 KB
Newer Older
H
Hui Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/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
H
Hui Li 已提交
16
install_main_dir="/usr/local/tarbitrator"
H
Hui Li 已提交
17 18

# old bin dir
H
Hui Li 已提交
19
bin_dir="/usr/local/tarbitrator/bin"
H
Hui Li 已提交
20 21 22 23 24 25 26 27 28 29 30 31

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 "
H
Hui Li 已提交
33 34 35 36 37 38 39 40
fi

update_flag=0

initd_mod=0
service_mod=2
if pidof systemd &> /dev/null; then
    service_mod=0
41
elif $(which service &> /dev/null); then
H
Hui Li 已提交
42
    service_mod=1
43
    service_config_dir="/etc/init.d"
H
Hui Li 已提交
44
    if $(which chkconfig &> /dev/null); then
45
         initd_mod=1
H
Hui Li 已提交
46 47 48 49 50 51 52
    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
53
else
H
Hui Li 已提交
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
    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}"
85
  echo " This is an officially unverified linux system,"
H
Hui Li 已提交
86 87 88 89 90 91 92 93
  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   || :
H
Hui Li 已提交
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
H
Hui Li 已提交
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/*
H
Hui Li 已提交
112 113

    #Make link
114 115
    [ -x ${install_main_dir}/bin/remove_arbi.sh ] && ${csudo}ln -s ${install_main_dir}/bin/remove_arbi.sh ${bin_link_dir}/rmtarbitrator  || :
    [ -x ${install_main_dir}/bin/tarbitrator ] && ${csudo}ln -s ${install_main_dir}/bin/tarbitrator ${bin_link_dir}/tarbitrator || :
H
Hui Li 已提交
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
H
Hui Li 已提交
124 125
}

126 127 128 129
function install_jemalloc() {
    jemalloc_dir=${script_dir}/jemalloc

    if [ -d ${jemalloc_dir} ]; then
130
        ${csudo}/usr/bin/install -c -d /usr/local/bin
131 132

        if [ -f ${jemalloc_dir}/bin/jemalloc-config ]; then
133
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/bin/jemalloc-config /usr/local/bin
134 135
        fi
        if [ -f ${jemalloc_dir}/bin/jemalloc.sh ]; then
136
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/bin/jemalloc.sh /usr/local/bin
137 138
        fi
        if [ -f ${jemalloc_dir}/bin/jeprof ]; then
139
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/bin/jeprof /usr/local/bin
140 141
        fi
        if [ -f ${jemalloc_dir}/include/jemalloc/jemalloc.h ]; then
142 143
            ${csudo}/usr/bin/install -c -d /usr/local/include/jemalloc
            ${csudo}/usr/bin/install -c -m 644 ${jemalloc_dir}/include/jemalloc/jemalloc.h /usr/local/include/jemalloc
144 145
        fi
        if [ -f ${jemalloc_dir}/lib/libjemalloc.so.2 ]; then
146 147 148 149
            ${csudo}/usr/bin/install -c -d /usr/local/lib
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/lib/libjemalloc.so.2 /usr/local/lib
            ${csudo}ln -sf libjemalloc.so.2 /usr/local/lib/libjemalloc.so
            ${csudo}/usr/bin/install -c -d /usr/local/lib
150
            if [ -f ${jemalloc_dir}/lib/libjemalloc.a ]; then
151
                ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/lib/libjemalloc.a /usr/local/lib
152 153
            fi
            if [ -f ${jemalloc_dir}/lib/libjemalloc_pic.a ]; then
154
                ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/lib/libjemalloc_pic.a /usr/local/lib
155 156
            fi
            if [ -f ${jemalloc_dir}/lib/libjemalloc_pic.a ]; then
157 158
                ${csudo}/usr/bin/install -c -d /usr/local/lib/pkgconfig
                ${csudo}/usr/bin/install -c -m 644 ${jemalloc_dir}/lib/pkgconfig/jemalloc.pc /usr/local/lib/pkgconfig
159 160 161
            fi
        fi
        if [ -f ${jemalloc_dir}/share/doc/jemalloc/jemalloc.html ]; then
162 163
            ${csudo}/usr/bin/install -c -d /usr/local/share/doc/jemalloc
            ${csudo}/usr/bin/install -c -m 644 ${jemalloc_dir}/share/doc/jemalloc/jemalloc.html /usr/local/share/doc/jemalloc
164 165
        fi
        if [ -f ${jemalloc_dir}/share/man/man3/jemalloc.3 ]; then
166 167
            ${csudo}/usr/bin/install -c -d /usr/local/share/man/man3
            ${csudo}/usr/bin/install -c -m 644 ${jemalloc_dir}/share/man/man3/jemalloc.3 /usr/local/share/man/man3
168
        fi
169 170

        if [ -d /etc/ld.so.conf.d ]; then
171 172
            echo "/usr/local/lib" | ${csudo}tee /etc/ld.so.conf.d/jemalloc.conf > /dev/null || echo -e "failed to write /etc/ld.so.conf.d/jemalloc.conf"
            ${csudo}ldconfig
173 174 175
        else
            echo "/etc/ld.so.conf.d not found!"
        fi
176 177 178
    fi
}

H
Hui Li 已提交
179 180
function clean_service_on_sysvinit() {
    if pidof tarbitrator &> /dev/null; then
181
        ${csudo}service tarbitratord stop || :
H
Hui Li 已提交
182 183 184
    fi

    if ((${initd_mod}==1)); then
185
      if [ -e ${service_config_dir}/tarbitratord ]; then
186
        ${csudo}chkconfig --del tarbitratord || :
H
Hui Li 已提交
187 188 189
      fi
    elif ((${initd_mod}==2)); then
      if [ -e ${service_config_dir}/tarbitratord ]; then
190
        ${csudo}insserv -r tarbitratord || :
H
Hui Li 已提交
191 192 193
      fi
    elif ((${initd_mod}==3)); then
      if [ -e ${service_config_dir}/tarbitratord ]; then
194
        ${csudo}update-rc.d -f tarbitratord remove || :
H
Hui Li 已提交
195
      fi
196
    fi
H
Hui Li 已提交
197

198
    ${csudo}rm -f ${service_config_dir}/tarbitratord || :
199

H
Hui Li 已提交
200
    if $(which init &> /dev/null); then
201
        ${csudo}init q || :
H
Hui Li 已提交
202 203 204 205 206 207 208 209 210
    fi
}

function install_service_on_sysvinit() {
    clean_service_on_sysvinit
    sleep 1

    # Install taosd service
    if ((${os_type}==1)); then
211 212
        ${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
H
Hui Li 已提交
213
    elif ((${os_type}==2)); then
214 215
        ${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
H
Hui Li 已提交
216
    fi
217

H
Hui Li 已提交
218
    if ((${initd_mod}==1)); then
219 220
        ${csudo}chkconfig --add tarbitratord || :
        ${csudo}chkconfig --level 2345 tarbitratord on || :
H
Hui Li 已提交
221
    elif ((${initd_mod}==2)); then
222 223
        ${csudo}insserv tarbitratord || :
        ${csudo}insserv -d tarbitratord || :
H
Hui Li 已提交
224
    elif ((${initd_mod}==3)); then
225
        ${csudo}update-rc.d tarbitratord defaults || :
H
Hui Li 已提交
226 227 228 229 230 231 232
    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..."
233
      ${csudo}systemctl stop tarbitratord &> /dev/null || echo &> /dev/null
H
Hui Li 已提交
234
  fi
235
  ${csudo}systemctl disable tarbitratord &> /dev/null || echo &> /dev/null
H
Hui Li 已提交
236

237
  ${csudo}rm -f ${tarbitratord_service_config}
H
Hui Li 已提交
238 239 240 241 242 243 244
}

function install_service_on_systemd() {
    clean_service_on_systemd

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

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    ${csudo}bash -c "echo '[Unit]'                                  >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'Description=TDengine 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
H
Hui Li 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

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_TDengine() {
    # Start to update
    echo -e "${GREEN}Start to update TDengine's arbitrator ...${NC}"
    # Stop the service if running
    if pidof tarbitrator &> /dev/null; then
        if ((${service_mod}==0)); then
284
            ${csudo}systemctl stop tarbitratord || :
H
Hui Li 已提交
285
        elif ((${service_mod}==1)); then
286
            ${csudo}service tarbitratord stop || :
H
Hui Li 已提交
287 288 289 290 291
        else
            kill_tarbitrator
        fi
        sleep 1
    fi
292

H
Hui Li 已提交
293 294 295 296
    install_main_path
    #install_header
    install_bin
    install_service
297 298
    install_jemalloc

H
Hui Li 已提交
299 300
    echo
    if ((${service_mod}==0)); then
301
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}systemctl start tarbitratord${NC}"
H
Hui Li 已提交
302
    elif ((${service_mod}==1)); then
303
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}service tarbitratord start${NC}"
H
Hui Li 已提交
304 305
    else
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ./tarbitrator${NC}"
306
    fi
H
Hui Li 已提交
307 308 309 310 311 312 313
    echo
    echo -e "\033[44;32;1mTDengine's arbitrator is updated successfully!${NC}"
}

function install_TDengine() {
    # Start to install
    echo -e "${GREEN}Start to install TDengine's arbitrator ...${NC}"
314 315

    install_main_path
H
Hui Li 已提交
316 317 318
    #install_header
    install_bin
    install_service
319 320
    install_jemalloc

H
Hui Li 已提交
321 322
    echo
    if ((${service_mod}==0)); then
323
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}systemctl start tarbitratord${NC}"
H
Hui Li 已提交
324
    elif ((${service_mod}==1)); then
325
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: ${csudo}service tarbitratord start${NC}"
H
Hui Li 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    else
        echo -e "${GREEN_DARK}To start arbitrator     ${NC}: tarbitrator${NC}"
    fi

    echo -e "\033[44;32;1mTDengine'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_TDengine
else
    install_TDengine
fi