github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/bin/node-installer.sh (about)

     1  #
     2  # This file is part of the Smart Home
     3  # Program complex distribution https://github.com/e154/smart-home
     4  # Copyright (C) 2016-2023, Filippov Alex
     5  #
     6  # This library is free software: you can redistribute it and/or
     7  # modify it under the terms of the GNU Lesser General Public
     8  # License as published by the Free Software Foundation; either
     9  # version 3 of the License, or (at your option) any later version.
    10  #
    11  # This library is distributed in the hope that it will be useful,
    12  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14  # Library General Public License for more details.
    15  #
    16  # You should have received a copy of the GNU Lesser General Public
    17  # License along with this library.  If not, see
    18  # <https://www.gnu.org/licenses/>.
    19  
    20  #!/usr/bin/env bash
    21  
    22  #
    23  # sudo apt-get install jq
    24  #
    25  # curl -s https://e154.github.io/smart-home/node-installer.sh | bash /dev/stdin --install
    26  #
    27  
    28  shopt -s extglob
    29  set -o errtrace
    30  set -o errexit
    31  
    32  GIT_USER="e154"
    33  GIT_REPO="smart-home-node"
    34  INSTALL_DIR="/opt/smart-home"
    35  ARCHIVE="node.tar.gz"
    36  DOWNLOAD_URL="$( curl -s https://api.github.com/repos/${GIT_USER}/${GIT_REPO}/releases/latest | jq -r ".assets[].browser_download_url" )"
    37  #DOWNLOAD_URL="https://github.com/e154/smart-home-old/releases/download/v0.0.5/smart-home-node.tar.gz"
    38  COMMAND=$1
    39  
    40  JQ=`which jq`
    41  
    42  main() {
    43  
    44      case "${COMMAND}" in
    45          --install)
    46          __install
    47          ;;
    48          --update)
    49          __update
    50          ;;
    51          --node)
    52          __node
    53          ;;
    54          *)
    55          __help
    56          exit 1
    57          ;;
    58      esac
    59  }
    60  
    61  log()  { printf "%b\n" "$*"; }
    62  debug(){ [[ ${node_debug_flag:-0} -eq 0 ]] || printf "%b\n" "Running($#): $*"; }
    63  fail() { log "\nERROR: $*\n" ; exit 1 ; }
    64  
    65  __install_initialize() {
    66  
    67      log ""
    68      log "Install smart home node to: ${INSTALL_DIR}/node"
    69  
    70      if [ -z "$JQ" ]; then
    71        log "Install jq"
    72        sudo apt-get install jq
    73      fi
    74  
    75      log "Trying to install GNU version of tar, might require sudo password"
    76  
    77      sudo mkdir -p ${INSTALL_DIR}
    78      sudo chown $USER:$USER ${INSTALL_DIR} -R
    79      mkdir -p ${INSTALL_DIR}/node
    80  
    81      cd ${INSTALL_DIR}/node
    82  
    83      log "Download latest release from:"
    84      log "URL: ${DOWNLOAD_URL}"
    85      curl -sSL -o ${ARCHIVE} ${DOWNLOAD_URL}
    86  
    87      log "Unpack archive"
    88      tar -zxf ${ARCHIVE}
    89  }
    90  
    91  __install_default_settings() {
    92  
    93      cd ${INSTALL_DIR}/node
    94  
    95      file="${INSTALL_DIR}/node/conf/config.json"
    96      if [ ! -f "$file" ]; then
    97          log "Create file $file"
    98          cp ${INSTALL_DIR}/node/conf/config.dev.json $file
    99      fi
   100  
   101  }
   102  
   103  __install_main() {
   104  
   105  #    log "Install node as service"
   106  #    sudo /opt/smart-home/node/node install
   107  
   108      log "node installed"
   109      exec /opt/smart-home/node/node help
   110  
   111      return 0
   112  }
   113  
   114  __install() {
   115    __install_initialize
   116    __install_default_settings
   117    __install_main
   118  }
   119  
   120  __update() {
   121  
   122      cd ${INSTALL_DIR}/node
   123  
   124      log "Download latest release from:"
   125      log "URL: ${DOWNLOAD_URL}"
   126      curl -sSL -o ${ARCHIVE} ${DOWNLOAD_URL}
   127  
   128      log "Unpack archive"
   129      tar -zxf ${ARCHIVE}
   130  
   131  }
   132  
   133  __remove() {
   134  
   135      log "Remove Smart home node"
   136  
   137      log "Stop service"
   138      sudo ${INSTALL_DIR}/server/node stop
   139  
   140      log "Remove service"
   141      sudo ${INSTALL_DIR}/server/node remove
   142  
   143      log "Remove node directory"
   144      rm -rf ${INSTALL_DIR}/node
   145  
   146      return 0
   147  }
   148  
   149  __help() {
   150    cat <<EOF
   151  Usage: node-installer.sh [options]
   152  
   153  OPTIONS:
   154  
   155    --install - install node
   156    --update - update node
   157    --node - node node
   158  
   159    -h / --help - show this help text and exit 0
   160  
   161  EOF
   162  }
   163  
   164  main "$@"