install_client_jh.sh 6.8 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/bin/bash
#
# This file is used to install jh_taos client on linux systems. The operating system
# is required to use systemd to manage services at boot

set -e
#set -x

# -----------------------Variables definition---------------------

osType=Linux
pagMode=full

if [ "$osType" != "Darwin" ]; then
    script_dir=$(dirname $(readlink -f "$0"))
    # Dynamic directory
    data_dir="/var/lib/jh_taos"
    log_dir="/var/log/jh_taos"
else
    script_dir=`dirname $0`
    cd ${script_dir}
    script_dir="$(pwd)"
    data_dir="/var/lib/jh_taos"
    log_dir="~/jh_taos/log"
fi

log_link_dir="/usr/local/jh_taos/log"

cfg_install_dir="/etc/jh_taos"

if [ "$osType" != "Darwin" ]; then
    bin_link_dir="/usr/bin"
    lib_link_dir="/usr/lib"
    lib64_link_dir="/usr/lib64"
    inc_link_dir="/usr/include"
else
    bin_link_dir="/usr/local/bin"
    lib_link_dir="/usr/local/lib"
    inc_link_dir="/usr/local/include"
fi

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

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

# 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
57
    csudo="sudo "
58 59 60 61 62 63 64
fi

update_flag=0

function kill_client() {
  pid=$(ps -ef | grep "jh_taos" | grep -v "grep" | awk '{print $2}')
  if [ -n "$pid" ]; then
65
    ${csudo}kill -9 $pid   || :
66 67 68 69 70
  fi
}

function install_main_path() {
    #create install main dir and all sub dir
71 72 73 74 75 76 77 78
    ${csudo}rm -rf ${install_main_dir}    || :
    ${csudo}mkdir -p ${install_main_dir}
    ${csudo}mkdir -p ${install_main_dir}/cfg
    ${csudo}mkdir -p ${install_main_dir}/bin
    ${csudo}mkdir -p ${install_main_dir}/connector
    ${csudo}mkdir -p ${install_main_dir}/driver
    ${csudo}mkdir -p ${install_main_dir}/examples
    ${csudo}mkdir -p ${install_main_dir}/include
79 80 81 82
}

function install_bin() {
  # Remove links
83
  ${csudo}rm -f ${bin_link_dir}/jh_taos         || :
84
  if [ "$osType" != "Darwin" ]; then
85 86
      ${csudo}rm -f ${bin_link_dir}/jhdemo  || :
      ${csudo}rm -f ${bin_link_dir}/jh_taosdump  || :
87
  fi
88 89
  ${csudo}rm -f ${bin_link_dir}/rmjh       || :
  ${csudo}rm -f ${bin_link_dir}/set_core      || :
90

91
  ${csudo}cp -r ${script_dir}/bin/* ${install_main_dir}/bin && ${csudo}chmod 0555 ${install_main_dir}/bin/*
92 93

  #Make link
94
  [ -x ${install_main_dir}/bin/jh_taos ] && ${csudo}ln -s ${install_main_dir}/bin/jh_taos ${bin_link_dir}/jh_taos    || :
95
  if [ "$osType" != "Darwin" ]; then
96 97
      [ -x ${install_main_dir}/bin/jhdemo ] && ${csudo}ln -s ${install_main_dir}/bin/jhdemo ${bin_link_dir}/jhdemo || :
      [ -x ${install_main_dir}/bin/jh_taosdump ] && ${csudo}ln -s ${install_main_dir}/bin/jh_taosdump ${bin_link_dir}/jh_taosdump || :
98
  fi
99 100
  [ -x ${install_main_dir}/bin/remove_client_jh.sh ] && ${csudo}ln -s ${install_main_dir}/bin/remove_client_jh.sh ${bin_link_dir}/rmjh || :
  [ -x ${install_main_dir}/bin/set_core.sh ] && ${csudo}ln -s ${install_main_dir}/bin/set_core.sh ${bin_link_dir}/set_core || :
101 102 103 104 105 106 107 108 109
}

function clean_lib() {
    sudo rm -f /usr/lib/libtaos.* || :
    sudo rm -rf ${lib_dir} || :
}

function install_lib() {
    # Remove links
110 111
    ${csudo}rm -f ${lib_link_dir}/libtaos.*         || :
    ${csudo}rm -f ${lib64_link_dir}/libtaos.*       || :
112

113
    ${csudo}cp -rf ${script_dir}/driver/* ${install_main_dir}/driver && ${csudo}chmod 777 ${install_main_dir}/driver/*
114 115

    if [ "$osType" != "Darwin" ]; then
116 117
        ${csudo}ln -s ${install_main_dir}/driver/libtaos.* ${lib_link_dir}/libtaos.so.1
        ${csudo}ln -s ${lib_link_dir}/libtaos.so.1 ${lib_link_dir}/libtaos.so
118 119
        
        if [ -d "${lib64_link_dir}" ]; then
120 121
	        ${csudo}ln -s ${install_main_dir}/driver/libtaos.* ${lib64_link_dir}/libtaos.so.1       || :
	        ${csudo}ln -s ${lib64_link_dir}/libtaos.so.1 ${lib64_link_dir}/libtaos.so               || :
122 123
	      fi
    else
124 125
        ${csudo}ln -s ${install_main_dir}/driver/libtaos.* ${lib_link_dir}/libtaos.1.dylib
        ${csudo}ln -s ${lib_link_dir}/libtaos.1.dylib ${lib_link_dir}/libtaos.dylib
126 127
    fi
    
128
    ${csudo}ldconfig
129 130 131
}

function install_header() {
132 133 134 135
    ${csudo}rm -f ${inc_link_dir}/taos.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/taoserror.h ${inc_link_dir}/taoserror.h
136 137 138 139
}

function install_config() {
    if [ ! -f ${cfg_install_dir}/taos.cfg ]; then
140 141 142
        ${csudo}mkdir -p ${cfg_install_dir}
        [ -f ${script_dir}/cfg/taos.cfg ] && ${csudo}cp ${script_dir}/cfg/taos.cfg ${cfg_install_dir}
        ${csudo}chmod 644 ${cfg_install_dir}/*
143 144
    fi

145 146
    ${csudo}cp -f ${script_dir}/cfg/taos.cfg ${install_main_dir}/cfg/taos.cfg.org
    ${csudo}ln -s ${cfg_install_dir}/taos.cfg ${install_main_dir}/cfg
147 148 149 150
}


function install_log() {
151
    ${csudo}rm -rf ${log_dir}  || :
152 153

    if [ "$osType" != "Darwin" ]; then
154
        ${csudo}mkdir -p ${log_dir} && ${csudo}chmod 777 ${log_dir}
155
    else
156
        mkdir -p ${log_dir} && ${csudo}chmod 777 ${log_dir}
157
    fi
158
    ${csudo}ln -s ${log_dir} ${install_main_dir}/log
159 160 161
}

function install_connector() {
162
    ${csudo}cp -rf ${script_dir}/connector/* ${install_main_dir}/connector
163 164 165 166
}

function install_examples() {
    if [ -d ${script_dir}/examples ]; then
167
        ${csudo}cp -rf ${script_dir}/examples/* ${install_main_dir}/examples
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    fi
}

function update() {
    # Start to update
    if [ ! -e jh_taos.tar.gz ]; then
        echo "File jh_taos.tar.gz does not exist"
        exit 1
    fi
    tar -zxf jh_taos.tar.gz

    echo -e "${GREEN}Start to update jh_iot client...${NC}"
    # Stop the client shell if running
    if pidof jh_taos &> /dev/null; then
        kill_client
        sleep 1
    fi

    install_main_path

    install_log
    install_header
    install_lib
    install_examples
    install_bin
    install_config

    echo
    echo -e "\033[44;32;1mjh_iot client is updated successfully!${NC}"

    rm -rf $(tar -tf jh_taos.tar.gz)
}

function install() {
    # Start to install
    if [ ! -e jh_taos.tar.gz ]; then
        echo "File jh_taos.tar.gz does not exist"
        exit 1
    fi
    tar -zxf jh_taos.tar.gz

    echo -e "${GREEN}Start to install jh_taos client...${NC}"

	install_main_path
    install_log
    install_header
    install_lib
    install_examples
    install_bin
    install_config

    echo
    echo -e "\033[44;32;1mjh_iot client is installed successfully!${NC}"

    rm -rf $(tar -tf jh_taos.tar.gz)
}


## ==============================Main program starts from here============================
# Install or updata client and client
# if server is already install, don't install client
  if [ -e ${bin_dir}/jh_taosd ]; then
      echo -e "\033[44;32;1mThere are already installed jh_iot server, so don't need install client!${NC}"
      exit 0
  fi

  if [ -x ${bin_dir}/jh_taos ]; then
      update_flag=1
      update
  else
      install
  fi