github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/install_with_docker.sh (about)

     1  #! /bin/bash
     2  
     3  # Extract the version number from pkg/config/version.go by getting everything
     4  # inside the quotes. Use -n to supress printing each line, and p to print the
     5  # modified line.
     6  SIGLENS_VERSION=`\
     7      curl  --silent "https://api.github.com/repos/siglens/siglens/releases/latest" |
     8      grep '"tag_name":' |
     9      sed -E 's/.*"([^"]+)".*/\1/'`
    10  
    11  sudo_cmd=""
    12  
    13  # Text color
    14  RED_TEXT='\e[31m'
    15  GREEN_TEXT='\e[32m'
    16  RESET_COLOR='\e[0m'
    17  
    18  # Check sudo permissions
    19  if (( $EUID != 0 )); then
    20      echo "===> Running installer with non-sudo permissions."
    21      echo "     In case of any failure or prompt, run the script with sudo privileges."
    22      echo ""
    23  else
    24      sudo_cmd="sudo"
    25  fi
    26  
    27  os=""
    28  case "$(uname -sr)" in
    29     Darwin*)
    30       os="darwin"
    31       package_manager="brew" ;;
    32     Ubuntu*|Pop!_OS)
    33       os="linux"
    34       package_manager="apt-get" ;;
    35     Linux*amzn2*)
    36       os="amazon linux"
    37       package_manager="yum" ;;
    38     Linux*)
    39       os="linux"
    40       package_manager="apt-get" ;;
    41     Debian*)
    42       os="linux"
    43       package_manager="apt-get" ;;
    44     Linux\ Mint*)
    45       os="linux"
    46       package_manager="apt-get" ;;
    47     Red\ Hat*)
    48       os="linux"
    49       package_manager="yum" ;;
    50     CentOS*)
    51       os="linux"
    52       package_manager="yum" ;;
    53     *)
    54       os="Not Found: $os_name"
    55       echo 'Not Supported OS'
    56       exit 1
    57       ;;
    58  esac
    59  
    60  arch=$(uname -m)
    61  if [[ $arch == x86_64* ]]; then
    62      arch="amd64"
    63  elif  [[ $arch == arm* || $arch == "aarch64" ]]; then
    64      arch="arm64"
    65  else
    66      echo 'Not Supported Architecture'
    67  fi
    68  
    69  fetch_ip_info() {
    70      response=$(curl -s https://ipinfo.io)
    71  
    72      ip=$(echo "$response" | awk -F'"' '/ip/{print $4}' | head -n 1)
    73      city=$(echo "$response" | awk -F'"' '/city/{print $4}')
    74      region=$(echo "$response" | awk -F'"' '/region/{print $4}')
    75      country=$(echo "$response" | awk -F'"' '/country/{print $4}')
    76      loc=$(echo "$response" | awk -F'"' '/loc/{print $4}')
    77      timezone=$(echo "$response" | awk -F'"' '/timezone/{print $4}')
    78      latitude=$(echo "$loc" | cut -d',' -f1)
    79      longitude=$(echo "$loc" | cut -d',' -f2)
    80  }
    81  
    82  fetch_ip_info
    83  
    84  post_event() {
    85    local event_code=$1
    86    local message=$2
    87      curl -X POST \
    88      https://api.segment.io/v1/track \
    89      -H 'Content-Type: application/json' \
    90      -H 'Authorization: Basic QlBEam5lZlBWMEpjMkJSR2RHaDdDUVRueWtZS2JEOGM6' \
    91      -d '{
    92      "userId": "'"$csi"'",
    93      "event":  "'"$event_code"'",
    94      "properties": {
    95          "runtime_os": "'"$os"'",
    96          "runtime_arch": "'"$arch"'",
    97          "package_manager": "'"$package_manager"'",
    98          "message": "'"$message"'",
    99          "ip": "'"$ip"'",
   100          "city": "'"$city"'",
   101          "region": "'"$region"'",
   102          "country": "'"$country"'"
   103      }
   104      }'
   105  }
   106  
   107  print_error_and_exit() {
   108      printf "${RED_TEXT}$1${RESET_COLOR}\n"
   109      exit 1
   110  }
   111  
   112  print_success_message() {
   113      printf "${GREEN_TEXT}$1${RESET_COLOR}\n"
   114  }
   115  
   116  is_command_present() {
   117      type "$1" >/dev/null 2>&1
   118  }
   119  
   120  request_sudo() {
   121      if hash sudo 2>/dev/null; then
   122          echo -e "\n Need sudo access to complete the installation."
   123          if (( $EUID != 0 )); then
   124              sudo_cmd="sudo"
   125              echo -e "Please enter your sudo password, if prompted."
   126              if ! $sudo_cmd -l | grep -e "NOPASSWD: ALL" > /dev/null && ! $sudo_cmd -v; then
   127                  post_event "install_failed" "request_sudo: Sudo access required but not available"
   128                  print_error_and_exit "Need sudo privileges to proceed with the installation."
   129              fi
   130  
   131              echo -e "Got Sudo access.\n"
   132          fi
   133      fi
   134  }
   135  
   136  install_docker() {
   137      echo "----------Setting up docker----------"
   138      if [[ $package_manager == apt-get ]]; then
   139          apt_cmd="$sudo_cmd apt-get --yes --quiet"
   140          $apt_cmd update
   141          $apt_cmd install software-properties-common gnupg-agent
   142          curl -fsSL "https://download.docker.com/linux/$os/gpg" | $sudo_cmd apt-key add -
   143          $sudo_cmd add-apt-repository \
   144              "deb [arch=$arch] https://download.docker.com/linux/$os $(lsb_release -cs) stable"
   145          $apt_cmd update
   146          echo "Installing docker"
   147          $apt_cmd install docker-ce docker-ce-cli containerd.io || {
   148              post_event "install_failed" "install_docker: Docker installation failed during apt-get install on $os"
   149              print_error_and_exit "install_docker: Docker installation failed during apt-get install on $os"
   150          }
   151      elif [[ $package_manager == yum && $os == 'amazon linux' ]]; then
   152          $sudo_cmd yum install -y amazon-linux-extras
   153          $sudo_cmd amazon-linux-extras enable docker
   154          $sudo_cmd yum install -y docker || {
   155              post_event "install_failed" "install_docker: Docker installation failed during yum install on Amazon Linux"
   156              print_error_and_exit "install_docker: Docker installation failed during yum install on Amazon Linux"
   157          }
   158      else
   159          yum_cmd="$sudo_cmd yum --assumeyes --quiet"
   160          $yum_cmd install yum-utils
   161          $sudo_cmd yum-config-manager --add-repo https://download.docker.com/linux/$os/docker-ce.repo
   162          echo "Installing docker"
   163          $yum_cmd install docker-ce docker-ce-cli containerd.io || {
   164              post_event "install_failed" "install_docker: Docker installation failed during yum install on $os"
   165              print_error_and_exit "install_docker: Docker installation failed during yum install on $os"
   166          }
   167      fi
   168      docker_version=$(docker --version) || {
   169          post_event "install_failed" "install_docker: Failed to check docker version post-installation on $os"
   170          print_error_and_exit "Docker is not working correctly. Please install docker manually and re-run the command."
   171      }
   172      print_success_message "Docker installed successfully. $docker_version"
   173  }
   174  
   175  install_docker_compose() {
   176      echo "----------Setting up docker compose----------"
   177      if [[ $package_manager == apt-get ]]; then
   178          apt_cmd="$sudo_cmd apt-get --yes --quiet"
   179          $apt_cmd update || {
   180              post_event "install_failed" "install_docker_compose: apt-get update failed during Docker Compose setup"
   181              print_error_and_exit "apt-get update failed."
   182          }
   183          $apt_cmd install docker-compose || {
   184              post_event "install_failed" "install_docker_compose: apt-get install docker-compose failed during Docker Compose setup"
   185              print_error_and_exit "Docker Compose installation failed."
   186          }
   187      elif [[ $package_manager == yum && $os == 'amazon linux' ]]; then
   188          curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose || {
   189              post_event "install_failed" "install_docker_compose: Downloading Docker Compose binary failed during Docker Compose setup"
   190              print_error_and_exit "Downloading Docker Compose binary failed."
   191          }
   192          chmod +x /usr/local/bin/docker-compose || {
   193              post_event "install_failed" "install_docker_compose: Making Docker Compose executable failed during Docker Compose setup"
   194              print_error_and_exit "Making Docker Compose executable failed."
   195          }
   196      elif [[ $package_manager == brew ]]; then
   197          brew install docker-compose || {
   198              post_event "install_failed" "install_docker_compose: Docker Compose installation via brew failed during Docker Compose setup"
   199              print_error_and_exit "Docker Compose installation failed."
   200          }
   201      else
   202          post_event "install_failed" "install_docker_compose: Docker Compose not installed, manual installation required during Docker Compose setup"
   203          echo "---------Docker Compose must be installed manually to proceed---------"
   204          print_error_and_exit "Docker Compose Not installed"
   205      fi
   206      docker_compose_version=$(docker-compose --version) || {
   207          post_event "install_failed" "install_docker_compose: Docker Compose post-installation check failed during Docker Compose setup"
   208          print_error_and_exit "Docker Compose is not working correctly."
   209      }
   210      print_success_message "Docker Compose installed successfully. $docker_compose_version"
   211  }
   212  
   213  start_docker() {
   214      echo -e "\n===> Starting Docker ...\n"
   215      if [[ $os == "darwin" ]]; then
   216          open --background -a Docker && while ! docker system info > /dev/null 2>&1; do sleep 1; done || {
   217              post_event "install_failed" "start_docker: Failed to start Docker on macOS"
   218              print_error_and_exit "Failed to start Docker"
   219          }
   220      else
   221          if ! $sudo_cmd systemctl is-active docker.service > /dev/null; then
   222              echo "Starting docker service"
   223              $sudo_cmd systemctl start docker.service || {
   224                  post_event "install_failed" "start_docker: Failed to start systemctl docker service"
   225                  print_error_and_exit "Failed to start Docker service"
   226              }
   227          fi
   228          if [[ -z $sudo_cmd ]]; then
   229              if ! docker ps > /dev/null && true; then
   230                  request_sudo
   231              fi
   232          fi
   233      fi
   234      docker info > /dev/null 2>&1 || {
   235          post_event "install_failed" "start_docker: Failed to retrieve Docker info, Docker may not have started correctly"
   236          print_error_and_exit "Docker did not start correctly."
   237      }
   238      print_success_message "Docker started successfully."
   239  }
   240  
   241  if ! is_command_present docker; then
   242      if [[ $package_manager == "apt-get" || $package_manager == "yum" ]]; then
   243          request_sudo
   244          install_docker
   245          install_docker_compose
   246      elif [[ $os == "darwin" ]]; then
   247          post_event "install_failed" "Docker Desktop not installed on Mac OS. Automatic installation is not supported."
   248          print_error_and_exit "\nDocker Desktop must be installed manually on Mac OS to proceed. \n You can install Docker from here - https://docs.docker.com/docker-for-mac/install/"
   249      else
   250          post_event "install_failed" "Docker not installed. Automatic installation is only supported on Ubuntu / Redhat."
   251          print_error_and_exit "\nDocker must be installed manually on your machine to proceed. Docker can only be installed automatically on Ubuntu / Redhat. \n You can install Docker from here - https://docs.docker.com/get-docker/"
   252      fi
   253  fi
   254  
   255  start_docker_with_timeout() {
   256      start_docker &
   257      start_docker_pid=$!
   258  
   259      # Wait for up to 180 seconds for start_docker to finish
   260      for i in {1..180}; do
   261          if ! ps -p $start_docker_pid > /dev/null; then
   262              wait $start_docker_pid
   263              exit_code=$?
   264              if [ $exit_code -ne 0 ]; then
   265                  print_error_and_exit "Docker failed to start"
   266              fi
   267              break
   268          fi
   269          if (( i % 30 == 0 )); then
   270              echo "Attempting to start docker... ($((i / 60)) minute(s))"
   271          fi
   272          sleep 1
   273      done
   274  
   275      # If start_docker is still running after 180 seconds, print an error message and exit
   276      if ps -p $start_docker_pid > /dev/null; then
   277          print_error_and_exit "Docker failed to start in 3 minutes. Please start docker and try again."
   278      fi
   279  }
   280  
   281  start_docker_with_timeout
   282  DOCKER_IMAGE_NAME="${DOCKER_IMAGE_NAME:-siglens/siglens:${SIGLENS_VERSION}}"
   283  DOCKER_COMPOSE_FILE="${DOCKER_COMPOSE_FILE:-docker-compose.yml}"
   284  
   285  echo -e "\n----------Pulling the latest docker image for SigLens----------"
   286  
   287  if [ "$USE_LOCAL_DOCKER_COMPOSE" != true ]; then
   288      if [ "$SERVERNAME" = "playground" ]; then
   289          curl -O -L "https://github.com/siglens/siglens/releases/download/${SIGLENS_VERSION}/playground.yaml"
   290      fi
   291      curl -O -L "https://github.com/siglens/siglens/releases/download/${SIGLENS_VERSION}/server.yaml"
   292      curl -O -L "https://github.com/siglens/siglens/releases/download/${SIGLENS_VERSION}/docker-compose.yml"
   293      echo "Pulling the latest docker image for SigLens from upstream"
   294      $sudo_cmd docker pull $DOCKER_IMAGE_NAME || {
   295      post_event "install_failed" "Failed to pull Docker image $DOCKER_IMAGE_NAME"
   296      print_error_and_exit "Failed to pull $DOCKER_IMAGE_NAME. Please check your internet connection and Docker installation."
   297  }
   298  fi
   299  
   300  mkdir -p data || {
   301      post_event "install_failed" "Failed to create directory 'data'."
   302      print_error_and_exit "Failed to create directory 'data'. Please check your permissions."
   303  }
   304  chmod a+rwx data || {
   305      post_event "install_failed" "Failed to change permissions for directory 'data'."
   306      print_error_and_exit "Failed to change permissions for directory 'data'. Please check your file permissions."
   307  }
   308  
   309  mkdir -p logs || {
   310      post_event "install_failed" "Failed to create directory 'logs'"
   311      print_error_and_exit "Failed to create directory 'logs'. Please check your permissions."
   312  }
   313  chmod a+rwx logs || {
   314      post_event "install_failed" "Failed to change permissions for directory 'logs'."
   315      print_error_and_exit "Failed to change permissions for directory 'logs'. Please check your file permissions."
   316  }
   317  print_success_message "\n===> SigLens installation complete with version: ${SIGLENS_VERSION}"
   318  
   319  csi=$(ifconfig 2>/dev/null | grep -o -E --color='never' '([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})' | head -n 1)
   320  if [ -z "$csi" ]; then
   321    csi=$(hostname)
   322  fi
   323  
   324  runtime_os=$(uname)
   325  runtime_arch=$(uname -m)
   326  
   327  check_ports() {
   328      PORT=$1
   329      if lsof -Pi :$PORT -sTCP:LISTEN -t > /dev/null || docker ps --format "{{.Ports}}" | grep -q "0.0.0.0:${PORT}->"; then
   330          CONTAINER_ID=$(docker ps --format "{{.ID}}:{{.Image}}:{{.Ports}}" | grep "siglens/siglens.*0.0.0.0:${PORT}" | cut -d ":" -f 1 2>/dev/null)
   331          if [ -n "$CONTAINER_ID" ]; then
   332              docker stop $CONTAINER_ID
   333              if lsof -Pi :$PORT -sTCP:LISTEN -t > /dev/null || docker ps --format "{{.Ports}}" | grep -q "0.0.0.0:${PORT}->"; then
   334                  post_event "install_failed" "Port ${PORT} is already in use after attempting to stop our Docker container"
   335                  print_error_and_exit "\nError: Port ${PORT} is already in use."
   336              fi
   337          else
   338              post_event "install_failed" "Port ${PORT} is already in use and no Docker container could be stopped"
   339              print_error_and_exit "\nError: Port ${PORT} is already in use."
   340          fi
   341      fi
   342      print_success_message "Port ${PORT} is available"
   343  }
   344  
   345  check_ports 5122
   346  check_ports 8081
   347  
   348  send_events() {
   349      curl -s -L https://github.com/siglens/pub-datasets/releases/download/v1.0.0/2kevents.json.tar.gz -o 2kevents.json.tar.gz
   350      if [ $? -ne 0 ]; then
   351          post_event "install_failed" "send_events: Failed to download sample log dataset from https://github.com/siglens/pub-datasets/releases/download/v1.0.0/2kevents.json.tar.gz"
   352          print_error_and_exit "Failed to download sample log dataset"
   353      fi
   354      tar -xvf 2kevents.json.tar.gz || {
   355          post_event "install_failed" "send_events: Failed to extract 2kevents.json.tar.gz"
   356          print_error_and_exit "Failed to extract 2kevents.json.tar.gz"
   357      }
   358      for i in $(seq 1 20)
   359      do
   360          curl -s http://localhost:8081/elastic/_bulk --data-binary "@2kevents.json" -o res.txt
   361          if [ $? -ne 0 ]; then
   362              post_event "install_failed" "send_events: Failed to send sample log dataset to http://localhost:8081/elastic/_bulk"
   363              print_error_and_exit "Failed to send sample log dataset"
   364          fi
   365      done
   366      print_success_message "\n Sample log dataset sent successfully"
   367  }
   368  
   369  UI_PORT=5122
   370  
   371  CFILE="server.yaml"
   372  
   373  # Check if CONFIG_FILE is set and not empty
   374  if [ -n "${CONFIG_FILE}" ]; then
   375      CFILE=${CONFIG_FILE}
   376  # Check if the script is running in playground environment
   377  elif [ "$SERVERNAME" = "playground" ]; then
   378      echo "Running in playground environment. Using playground.yaml."
   379      CFILE="playground.yaml"
   380  fi
   381  
   382  print_success_message "\n Starting Siglens with image: ${DOCKER_IMAGE_NAME}"
   383  CSI=${csi} UI_PORT=${UI_PORT} CONFIG_FILE=${CFILE} WORK_DIR="$(pwd)" IMAGE_NAME=${DOCKER_IMAGE_NAME} docker-compose -f $DOCKER_COMPOSE_FILE up -d || {
   384      post_event "install_failed" "Failed to start Docker Compose on $os with $DOCKER_COMPOSE_FILE"
   385      print_error_and_exit "Failed to start Docker Compose"
   386  }
   387  CSI=${csi} UI_PORT=${UI_PORT} CONFIG_FILE=${CFILE} WORK_DIR="$(pwd)" IMAGE_NAME=${DOCKER_IMAGE_NAME} docker-compose logs -t --tail 20 >> dclogs.txt
   388  sample_log_dataset_status=$(curl -s -o /dev/null -I -X HEAD -w "%{http_code}" http://localhost:5122/elastic/sample-log-dataset)
   389  
   390  if [ "$sample_log_dataset_status" -eq 200 ]; then
   391      FIRST_RUN=false
   392  elif [ "$sample_log_dataset_status" -eq 404 ]; then
   393      FIRST_RUN=true
   394  else
   395      echo "Failed to check sample log dataset status"
   396      FIRST_RUN=true
   397  fi
   398  
   399  if $FIRST_RUN; then
   400      send_events
   401      post_event "fresh_install_success" "Fresh installation was successful using docker on $os"
   402  else
   403      post_event "repeat_install_success" "Repeat installation of Docker was successful using docker on $os"
   404      echo "Skipping sendevents as this is not the first run"
   405  fi
   406  
   407  tput bold
   408  print_success_message "\n===> Frontend can be accessed on http://localhost:${UI_PORT}"
   409  echo ""
   410  tput sgr0
   411  
   412  if [ $? -ne 0 ]; then
   413      tput bold
   414      printf "\n${RED_TEXT}Error: Docker failed to start. This could be due to a permission issue.${RESET_COLOR}"
   415      printf "\nPlease try these steps:"
   416      printf "\n1. Run: sudo groupadd docker"
   417      echo ""
   418      printf '2. Run: sudo usermod -aG docker ${USER}'
   419      printf "\n3. You should log out and log back in so that your group membership is re-evaluated\n"
   420      tput sgr0
   421      exit 1
   422  fi
   423  
   424  echo -e "\n*** Thank you! ***\n"