install_arbi_power.sh 13.1 KB
Newer Older
H
Hui Li 已提交
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 "
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_power.sh ] && ${csudo}ln -s ${install_main_dir}/bin/remove_arbi_power.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 119 120 121
function install_jemalloc() {
    jemalloc_dir=${script_dir}/jemalloc

    if [ -d ${jemalloc_dir} ]; then
122
        ${csudo}/usr/bin/install -c -d /usr/local/bin
123 124

        if [ -f ${jemalloc_dir}/bin/jemalloc-config ]; then
125
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/bin/jemalloc-config /usr/local/bin
126 127
        fi
        if [ -f ${jemalloc_dir}/bin/jemalloc.sh ]; then
128
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/bin/jemalloc.sh /usr/local/bin
129 130
        fi
        if [ -f ${jemalloc_dir}/bin/jeprof ]; then
131
            ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/bin/jeprof /usr/local/bin
132 133
        fi
        if [ -f ${jemalloc_dir}/include/jemalloc/jemalloc.h ]; then
134 135
            ${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
136 137
        fi
        if [ -f ${jemalloc_dir}/lib/libjemalloc.so.2 ]; then
138 139 140 141
            ${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
142
            if [ -f ${jemalloc_dir}/lib/libjemalloc.a ]; then
143
                ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/lib/libjemalloc.a /usr/local/lib
144 145
            fi
            if [ -f ${jemalloc_dir}/lib/libjemalloc_pic.a ]; then
146
                ${csudo}/usr/bin/install -c -m 755 ${jemalloc_dir}/lib/libjemalloc_pic.a /usr/local/lib
147 148
            fi
            if [ -f ${jemalloc_dir}/lib/libjemalloc_pic.a ]; then
149 150
                ${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
151 152 153
            fi
        fi
        if [ -f ${jemalloc_dir}/share/doc/jemalloc/jemalloc.html ]; then
154 155
            ${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
156 157
        fi
        if [ -f ${jemalloc_dir}/share/man/man3/jemalloc.3 ]; then
158 159
            ${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
160
        fi
161 162

        if [ -d /etc/ld.so.conf.d ]; then
163 164
            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
165 166 167
        else
            echo "/etc/ld.so.conf.d not found!"
        fi
168 169 170
    fi
}

H
Hui Li 已提交
171
function install_header() {
172 173 174 175 176
    ${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 已提交
177 178 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 211
    fi
}

function install_service_on_sysvinit() {
    clean_service_on_sysvinit
    sleep 1

    # Install powerd service

    if ((${os_type}==1)); then
212 213
        ${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 已提交
214
    elif ((${os_type}==2)); then
215 216
        ${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 已提交
217
    fi
218

H
Hui Li 已提交
219
    #restart_config_str="power:2345:respawn:${service_config_dir}/powerd start"
220
    #${csudo}grep -q -F "$restart_config_str" /etc/inittab || ${csudo}bash -c "echo '${restart_config_str}' >> /etc/inittab"
221

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

241
  ${csudo}rm -f ${tarbitratord_service_config}
H
Hui Li 已提交
242 243 244 245 246 247 248 249 250
}

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

function install_service_on_systemd() {
    clean_service_on_systemd

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

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    ${csudo}bash -c "echo '[Unit]'                                  >> ${tarbitratord_service_config}"
    ${csudo}bash -c "echo 'Description=PowerDB 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 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

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_PowerDB() {
    # Start to update
    echo -e "${GREEN}Start to update PowerDB's arbitrator ...${NC}"
    # Stop the service if running
    if pidof tarbitrator &> /dev/null; then
        if ((${service_mod}==0)); then
290
            ${csudo}systemctl stop tarbitratord || :
H
Hui Li 已提交
291
        elif ((${service_mod}==1)); then
292
            ${csudo}service tarbitratord stop || :
H
Hui Li 已提交
293 294 295 296 297
        else
            kill_tarbitrator
        fi
        sleep 1
    fi
298

H
Hui Li 已提交
299 300 301 302
    install_main_path
    #install_header
    install_bin
    install_service
303 304
    install_jemalloc

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

function install_PowerDB() {
    # Start to install
    echo -e "${GREEN}Start to install PowerDB's arbitrator ...${NC}"
320 321

    install_main_path
H
Hui Li 已提交
322 323 324
    #install_header
    install_bin
    install_service
325 326
    install_jemalloc

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

    echo -e "\033[44;32;1mPowerDB'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_PowerDB
else
    install_PowerDB
fi