github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/bin/server-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/server-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"
    34  INSTALL_DIR="/opt/smart-home"
    35  ARCHIVE="server.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.4/smart-home-server.tar.gz"
    38  COMMAND=$1
    39  
    40  OS_TYPE="unknown"
    41  OS_ARCH="unknown"
    42  
    43  JQ=`which jq`
    44  
    45  main() {
    46  
    47      case "${COMMAND}" in
    48          --install)
    49          __install
    50          ;;
    51          --update)
    52          __update
    53          ;;
    54          --remove)
    55          __remove
    56          ;;
    57          *)
    58          __help
    59          exit 1
    60          ;;
    61      esac
    62  }
    63  
    64  log()  { printf "%b\n" "$*"; }
    65  debug(){ [[ ${server_debug_flag:-0} -eq 0 ]] || printf "%b\n" "Running($#): $*"; }
    66  fail() { log "\nERROR: $*\n" ; exit 1 ; }
    67  
    68  __install_initialize() {
    69  
    70      log ""
    71      log "Install smart home server to: ${INSTALL_DIR}/server"
    72  
    73      if [ -z "$JQ" ]; then
    74        log "Install jq"
    75        sudo apt-get install jq
    76      fi
    77  
    78      log "Trying to install GNU version of tar, might require sudo password"
    79  
    80      sudo mkdir -p ${INSTALL_DIR}
    81      sudo chown $USER:$USER ${INSTALL_DIR} -R
    82      mkdir -p ${INSTALL_DIR}/server
    83  
    84      cd ${INSTALL_DIR}/server
    85  
    86      log "Download latest release from:"
    87      log "URL: ${DOWNLOAD_URL}"
    88      curl -sSL -o ${ARCHIVE} ${DOWNLOAD_URL}
    89  
    90      log "Unpack archive"
    91      tar -zxf ${ARCHIVE}
    92  }
    93  
    94  __install_default_settings() {
    95  
    96      cd ${INSTALL_DIR}/server
    97  
    98      file="${INSTALL_DIR}/server/conf/config.json"
    99      if [ ! -f "$file" ]; then
   100          log "Create file $file"
   101          cp ${INSTALL_DIR}/server/conf/config.dev.json $file
   102      fi
   103  
   104      file="${INSTALL_DIR}/server/conf/dbconfig.yml"
   105      if [ ! -f "$file" ]; then
   106          log "Create file $file"
   107          cp ${INSTALL_DIR}/server/conf/dbconfig.dev.yml $file
   108      fi
   109  
   110      if [ ! -d "${INSTALL_DIR}/data" ]; then
   111          log "Move data directory ../"
   112          mv data ../
   113      fi
   114  
   115      if [ ! -d "${INSTALL_DIR}/assets" ]; then
   116          log "Move assets directory ../"
   117          mv assets ../
   118      fi
   119  
   120      if [ ! -d "${INSTALL_DIR}/snapshots" ]; then
   121          log "Move snapshots directory ../"
   122          mv snapshots ../
   123      fi
   124  
   125  }
   126  
   127  __install_main() {
   128  
   129  #    log "Install server as service"
   130  #    sudo /opt/smart-home/server/server install
   131  
   132      log "Server installed"
   133      exec /opt/smart-home/server/server help
   134  
   135      return 0
   136  }
   137  
   138  __install() {
   139    __install_initialize
   140    __install_default_settings
   141    __install_main
   142  }
   143  
   144  __update() {
   145  
   146      cd ${INSTALL_DIR}/server
   147  
   148      log "Download latest release from:"
   149      log "URL: ${DOWNLOAD_URL}"
   150      curl -sSL -o ${ARCHIVE} ${DOWNLOAD_URL}
   151  
   152      log "Unpack archive"
   153      tar -zxf ${ARCHIVE}
   154  
   155      log "Move data directory ../"
   156      mv assets ../
   157      mv data/icons ../icons
   158      mv data/scripts ../scripts
   159      mv snapshots ../
   160  }
   161  
   162  __remove() {
   163  
   164      log "Remove Smart home server"
   165  
   166      log "Stop service"
   167      sudo ${INSTALL_DIR}/server/server stop
   168  
   169      log "Remove service"
   170      sudo ${INSTALL_DIR}/server/server remove
   171  
   172      log "Remove server directory"
   173      rm -rf ${INSTALL_DIR}/server
   174  
   175      return 0
   176  }
   177  
   178  __help() {
   179    cat <<EOF
   180  Usage: server-installer.sh [options]
   181  
   182  OPTIONS:
   183  
   184    --install - install server
   185    --update - update server
   186    --remove - remove server
   187  
   188    -h / --help - show this help text and exit 0
   189  
   190  EOF
   191  }
   192  
   193  __check_os() {
   194  
   195      # get os type
   196      case `${UNAME} -s` in
   197          (Linux)
   198              OS_TYPE="linux"
   199          ;;
   200          (Darwin)
   201              OS_TYPE="darwin"
   202              INSTALL_DIR="${HOME}/smart-home"
   203          ;;
   204      esac
   205  }
   206  
   207  __check_os
   208  
   209  main "$@"