<commit_before>#!/bin/bash -e

export SKIP_STEP=false

_update_install_status() {
  local update=$(cat $STATE_FILE | jq '.installStatus.'"$1"'='true'')
  _update_state "$update"
}

_check_component_status() {
  local status=$(cat $STATE_FILE | jq '.installStatus.'"$1"'')
  if [ "$status" = true ]; then
    SKIP_STEP=true;
  fi
}

generate_serviceuser_token() {
  SKIP_STEP=false
  _check_component_status "serviceuserTokenGenerated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Generating random token for serviceuser"
    local token=$(cat /proc/sys/kernel/random/uuid)
    local stateToken=$(cat $STATE_FILE | jq '.systemSettings.serviceUserToken="'$token'"')
    echo $stateToken > $STATE_FILE
    _update_install_status "serviceuserTokenGenerated"
  else
    __process_msg "Service user token already generated, skipping"
  fi
}

copy_node_scripts() {
  local node_scripts_location=$(cat $STATE_FILE \
    | jq -r '.systemSettings.nodeScriptsLocation')

  if [ "$node_scripts_location" != "" ] && [ "$node_scripts_location" != "null" ]; then
    local server_enabled=$(cat $STATE_FILE \
      | jq -r '.systemSettings.serverEnabled')

    if [ $server_enabled == false ]; then
      __process_msg "Server enabled is set to false, not updating systemSettings.nodeScriptsLocation"
    else
      __process_msg "Server enabled is set to true, downloading scripts and updating statefile"
      local node_scripts_remote_location=$(cat $STATE_FILE \
        | jq -r '.systemSettings.nodeScriptsRemoteLocation')
      local node_scripts_archive="node.tar.gz"
      local node_scripts_download_location="$USR_DIR/$node_scripts_archive"

      local download_command="curl -LkSs \
        '$node_scripts_remote_location' \
        -o $node_scripts_download_location"

      eval $download_command

      __process_msg "Copying node scripts to DB host"
      local db_host=$(cat $STATE_FILE \
        | jq '.machines[] | select (.group=="core" and .name=="db")')
      local db_ip=$(echo $db_host | jq -r '.ip')

      _copy_script_remote $db_ip "$node_scripts_download_location" "$SCRIPT_DIR_REMOTE"

      __process_msg "Scripts copied to DB host, updating systemSettings.nodeScriptsLocation in state file"
      node_scripts_location="$SSH_USER@$db_ip:$SCRIPT_DIR_REMOTE/$node_scripts_archive"
      node_scripts_location=$(cat $STATE_FILE \
        | jq '.systemSettings.nodeScriptsLocation="'$node_scripts_location'"')
      _update_state "$node_scripts_location"
      __process_msg "State file updated with systemSettings.nodeScriptsLocation"
    fi

  else
    __process_error "systemSettings.nodeScriptsLocation not set. Please update the value
      and run installer again"
    exit 1
  fi
}

generate_root_bucket_name() {
  __process_msg "Generating root bucket name"
  local root_bucket_name=$(cat $STATE_FILE | jq -r '.systemSettings.rootS3Bucket')

  if [ "$root_bucket_name" == "" ] || [ "$root_bucket_name" == null ]; then
    __process_msg "Root bucket name not set, setting it to random value"
    local random_uuid=$(cat /proc/sys/kernel/random/uuid)
    local install_mode=$(cat $STATE_FILE | jq -r '.installMode')<commit_msg>fixing logic for setting nodeScriptsLocation<commit_after>#!/bin/bash -e

export SKIP_STEP=false

_update_install_status() {
  local update=$(cat $STATE_FILE | jq '.installStatus.'"$1"'='true'')
  _update_state "$update"
}

_check_component_status() {
  local status=$(cat $STATE_FILE | jq '.installStatus.'"$1"'')
  if [ "$status" = true ]; then
    SKIP_STEP=true;
  fi
}

generate_serviceuser_token() {
  SKIP_STEP=false
  _check_component_status "serviceuserTokenGenerated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Generating random token for serviceuser"
    local token=$(cat /proc/sys/kernel/random/uuid)
    local stateToken=$(cat $STATE_FILE | jq '.systemSettings.serviceUserToken="'$token'"')
    echo $stateToken > $STATE_FILE
    _update_install_status "serviceuserTokenGenerated"
  else
    __process_msg "Service user token already generated, skipping"
  fi
}

copy_node_scripts() {
  __process_msg "Copying node scripts"
  local server_enabled=$(cat $STATE_FILE \
    | jq -r '.systemSettings.serverEnabled')
  local node_scripts_location=$(cat $STATE_FILE \
    | jq -r '.systemSettings.nodeScriptsLocation')
  local node_scripts_remote_location=$(cat $STATE_FILE \
    | jq -r '.systemSettings.nodeScriptsRemoteLocation')

  if [ "$node_scripts_remote_location" == "" ]; then
    __process_error "Missing value in systemSettings.nodeScriptsRemoteLocation"
    exit 1
  fi

  if [ $server_enabled == false ]; then
    __process_msg "Server enabled is set to false"
    if [ "$node_scripts_location" != "" ] && [ "$node_scripts_location" != "null" ]; then
      __process_msg "systemSettings.nodeScriptsRemoteLocation already set, skipping"
    else
      __process_msg "Copying scripts location from systemSettings.nodeScriptsRemoteLocation"
      node_scripts_location=$(cat $STATE_FILE \
        | jq '.systemSettings.nodeScriptsLocation="'$node_scripts_remote_location'"')
      _update_state "$node_scripts_location"
      __process_msg "Updated systemSettings.nodeScriptsLocation"
    fi
  else
    __process_msg "Server enabled is set to true, downloading scripts and updating statefile"
    local node_scripts_archive="node.tar.gz"
    local node_scripts_download_location="$USR_DIR/$node_scripts_archive"

    local download_command="curl -LkSs \
      '$node_scripts_remote_location' \
      -o $node_scripts_download_location"

    eval $download_command

    __process_msg "Copying node scripts to DB host"
    local db_host=$(cat $STATE_FILE \
      | jq '.machines[] | select (.group=="core" and .name=="db")')
    local db_ip=$(echo $db_host | jq -r '.ip')

    _copy_script_remote $db_ip "$node_scripts_download_location" "$SCRIPT_DIR_REMOTE"

    __process_msg "Scripts copied to DB host, updating systemSettings.nodeScriptsLocation in state file"
    node_scripts_location="$SSH_USER@$db_ip:$SCRIPT_DIR_REMOTE/$node_scripts_archive"
    node_scripts_location=$(cat $STATE_FILE \
      | jq '.systemSettings.nodeScriptsLocation="'$node_scripts_location'"')
    _update_state "$node_scripts_location"
    __process_msg "State file updated with systemSettings.nodeScriptsLocation"
  fi
}

generate_root_bucket_name() {
  __process_msg "Generating root bucket name"
  local root_bucket_name=$(cat $STATE_FILE | jq -r '.systemSettings.rootS3Bucket')

  if [ "$root_bucket_name" == "" ] || [ "$root_bucket_name" == null ]; then
    __process_msg "Root bucket name not set, setting it to random value"
    local random_uuid=$(cat /proc/sys/kernel/random/uuid)
    local install_mode=$(cat $STATE_FILE | jq -r '.installMode')
{'fraction_non_alphanumeric': 0.11000297707651087, 'fraction_numerical': 0.0011908306043465317, 'mean_word_length': 4.480424143556281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5289470', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2057, 'n_words': 475}