<commit_before>#!/bin/bash -e

export SKIP_STEP=false
export sleep_time=1

_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
}

update_docker_creds() {
  SKIP_STEP=false
  _check_component_status "dockerCredsUpdated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Updating docker credentials to pull shippable images"
    local gitlab_host=$(cat $STATE_FILE | jq '.machines[] | select (.group=="core" and .name=="swarm")')
    local host=$(echo "$gitlab_host" | jq '.ip')

    local credentials_template="$REMOTE_SCRIPTS_DIR/credentials.template"
    local credentials_file="$REMOTE_SCRIPTS_DIR/credentials"

    __process_msg "Updating : installerAccessKey"
    local aws_access_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerAccessKey')
    sed "s#{{aws_access_key}}#$aws_access_key#g" $credentials_template > $credentials_file

    __process_msg "Updating : installerSecretKey"
    local aws_secret_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerSecretKey')
    sed -i "s#{{aws_secret_key}}#$aws_secret_key#g" $credentials_file

    _copy_script_remote $host "credentials" "/root/.aws/"
    local save_docker_login_cmd='aws ecr --region us-east-1 get-login > /tmp/docker_login.sh'
    _exec_remote_cmd $host "$save_docker_login_cmd"
    local update_perm='chmod +x /tmp/docker_login.sh'
    _exec_remote_cmd $host "$update_perm"
    local docker_login_cmd='/tmp/docker_login.sh'
    _exec_remote_cmd $host "$docker_login_cmd"

    _update_install_status "dockerCredsUpdated"
  else
    __process_msg "Docker credentials already updated, skipping"
  fi
}

update_docker_creds_local() {
  SKIP_STEP=false
  _check_component_status "dockerCredsUpdated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Updating docker credentials to pull shippable images"

    local credentials_template="$REMOTE_SCRIPTS_DIR/credentials.template"
    local credentials_file="$REMOTE_SCRIPTS_DIR/credentials"

    __process_msg "Updating : installerAccessKey"
    local aws_access_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerAccessKey')

    sed "s#{{aws_access_key}}#$aws_access_key#g" $credentials_template > $credentials_file

    __process_msg "Updating : installerSecretKey"
    local aws_secret_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerSecretKey')
    sed -i "s#{{aws_secret_key}}#$aws_secret_key#g" $credentials_file

    cp -vr $credentials_file $HOME/.aws/
    echo "aws ecr --region us-east-1 get-login" | sudo tee /tmp/docker_login.sh
    sudo chmod +x /tmp/docker_login.sh
    local docker_login_cmd=$(eval "/tmp/docker_login.sh")
    __process_msg "Docker login generated, logging into ecr "
    eval "$docker_login_cmd"

    _update_install_status "dockerCredsUpdated"
  else
    __process_msg "Docker credentials already updated, skipping"
  fi
}

generate_system_config() {
  SKIP_STEP=false
  _check_component_status "systemConfigSqlCreated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Inserting data into systemConfigs Table"
    local db_host=$(cat $STATE_FILE | jq '.machines[] | select (.group=="core" and .name=="db")')
    local host=$(echo $db_host | jq '.ip')
    local db_ip=$(echo $db_host | jq '.ip')

    #TODO: put sed update into a function and call it for each variable
    local system_configs_template="$REMOTE_SCRIPTS_DIR/systemConfigsData.sql.template"
    local system_configs_sql="$REMOTE_SCRIPTS_DIR/system_configs_data.sql"
<commit_msg>prompting user for access keys<commit_after>#!/bin/bash -e

export SKIP_STEP=false
export sleep_time=1

_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
}

update_docker_creds() {
  SKIP_STEP=false
  _check_component_status "dockerCredsUpdated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Updating docker credentials to pull shippable images"
    local gitlab_host=$(cat $STATE_FILE | jq '.machines[] | select (.group=="core" and .name=="swarm")')
    local host=$(echo "$gitlab_host" | jq '.ip')

    local credentials_template="$REMOTE_SCRIPTS_DIR/credentials.template"
    local credentials_file="$REMOTE_SCRIPTS_DIR/credentials"

    __process_msg "Updating : installerAccessKey"
    local aws_access_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerAccessKey')
    sed "s#{{aws_access_key}}#$aws_access_key#g" $credentials_template > $credentials_file

    __process_msg "Updating : installerSecretKey"
    local aws_secret_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerSecretKey')
    sed -i "s#{{aws_secret_key}}#$aws_secret_key#g" $credentials_file

    _copy_script_remote $host "credentials" "/root/.aws/"
    local save_docker_login_cmd='aws ecr --region us-east-1 get-login > /tmp/docker_login.sh'
    _exec_remote_cmd $host "$save_docker_login_cmd"
    local update_perm='chmod +x /tmp/docker_login.sh'
    _exec_remote_cmd $host "$update_perm"
    local docker_login_cmd='/tmp/docker_login.sh'
    _exec_remote_cmd $host "$docker_login_cmd"

    _update_install_status "dockerCredsUpdated"
  else
    __process_msg "Docker credentials already updated, skipping"
  fi
}

update_docker_creds_local() {
  SKIP_STEP=false
  _check_component_status "dockerCredsUpdated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Updating docker credentials to pull shippable images"

    local credentials_template="$REMOTE_SCRIPTS_DIR/credentials.template"
    local credentials_file="$REMOTE_SCRIPTS_DIR/credentials"

    __process_msg "Updating : installerAccessKey"
    local aws_access_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerAccessKey')
    if [ -z "$aws_access_key" ]; then
      __process_msg "Please update 'systemSettings.installerAccessKey' in state.json and run installer again"
      exit 1
    fi

    sed "s#{{aws_access_key}}#$aws_access_key#g" $credentials_template > $credentials_file

    __process_msg "Updating : installerSecretKey"
    local aws_secret_key=$(cat $STATE_FILE | jq -r '.systemSettings.installerSecretKey')
    if [ -z "$aws_secret_key" ]; then
      __process_msg "Please update 'systemSettings.installerSecretKey' in state.json and run installer again"
      exit 1
    fi
    sed -i "s#{{aws_secret_key}}#$aws_secret_key#g" $credentials_file

    cp -vr $credentials_file $HOME/.aws/
    echo "aws ecr --region us-east-1 get-login" | sudo tee /tmp/docker_login.sh
    sudo chmod +x /tmp/docker_login.sh
    local docker_login_cmd=$(eval "/tmp/docker_login.sh")
    __process_msg "Docker login generated, logging into ecr "
    eval "$docker_login_cmd"

    _update_install_status "dockerCredsUpdated"
  else
    __process_msg "Docker credentials already updated, skipping"
  fi
}

generate_system_config() {
  SKIP_STEP=false
  _check_component_status "systemConfigSqlCreated"
  if [ "$SKIP_STEP" = false ]; then
    __process_msg "Inserting data into systemConfigs Table"
    local db_host=$(cat $STATE_FILE | jq '.machines[] | select (.group=="core" and .name=="db")')
    local host=$(echo $db_host | jq '.ip')
    local db_ip=$(echo $db_host | jq '.ip')

    #TODO: put sed update into a function and call it for each variable
    local system_configs_template="$REMOTE_SCRIPTS_DIR/systemConfigsData.sql.template"
    local system_configs_sql="$REMOTE_SCRIPTS_DIR/system_configs_data.sql"

{'fraction_non_alphanumeric': 0.11143087371935076, 'fraction_numerical': 0.0013813744675952573, 'mean_word_length': 4.8663065496286295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4857384', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2885, 'n_words': 624}