<commit_before>#!/usr/bin/env bash

set -u

user_id=$(id -u)

# requires sudo
[ "${user_id}" -ne 0 ] && echo "update_hosts must be run as root" && exit 1


update_hosts_script_path=$(readlink -e "${BASH_SOURCE[0]}")
update_hosts_script_dir="${update_hosts_script_path%/*}"

# ec2-metadata script must be ini the same directory
[ ! -e "${update_hosts_script_dir}/ec2-metadata" ] && echo "ec2-metadata script must be in the same directory as create-instance-env.sh" && exit 1


function parse_metadata_result() {
    local metadata="${1}"
    local value="${metadata#*: }"
    echo "${value}"
}

local_hostname=$("${update_hosts_script_dir}/ec2-metadata" -h)
local_hostname=$(parse_metadata_result "${local_hostname}")
internal_hostname="${local_hostname}.ec2.internal"
local_ipv4=$("${update_hosts_script_dir}/ec2-metadata" -o)
local_ipv4=$(parse_metadata_result "${local_ipv4}")
public_hostname=$("${update_hosts_script_dir}/ec2-metadata" -p)
public_hostname=$(parse_metadata_result "${public_hostname}")
public_ipv4=$("${update_hosts_script_dir}/ec2-metadata" -v)
public_ipv4=$(parse_metadata_result "${public_ipv4}")

if [ -z "${public_ipv4}" ] || [ "${public_ipv4}" == "not available" ] || [ -z "${public_hostname}" ] || [ "${public_hostname}" == "not available" ]; then
    public_hostname=""
fi

echo "${local_ipv4}    ${internal_hostname} ${local_hostname} ${public_hostname}" >> /etc/hosts

sed -i "s/HOSTNAME=.*/HOSTNAME=${local_hostname}/g" /etc/sysconfig/network
sudo hostname "${local_hostname}"

sudo service network restart

echo "hostname=$(hostname)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -i=$(hostname -i)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -I=$(hostname -I)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -f=$(hostname -f)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -A=$(hostname -A)" | tee -a "/home/ec2-user/update_hosts.log"

echo "finished update_hosts"<commit_msg>updated bootstrap/update_hosts.sh to removed appending internal ec2 suffix to hostname<commit_after>#!/usr/bin/env bash

set -u

user_id=$(id -u)

# requires sudo
[ "${user_id}" -ne 0 ] && echo "update_hosts must be run as root" && exit 1


update_hosts_script_path=$(readlink -e "${BASH_SOURCE[0]}")
update_hosts_script_dir="${update_hosts_script_path%/*}"

# ec2-metadata script must be ini the same directory
[ ! -e "${update_hosts_script_dir}/ec2-metadata" ] && echo "ec2-metadata script must be in the same directory as create-instance-env.sh" && exit 1


function parse_metadata_result() {
    local metadata="${1}"
    local value="${metadata#*: }"
    echo "${value}"
}

local_hostname=$("${update_hosts_script_dir}/ec2-metadata" -h)
local_hostname=$(parse_metadata_result "${local_hostname}")
local_ipv4=$("${update_hosts_script_dir}/ec2-metadata" -o)
local_ipv4=$(parse_metadata_result "${local_ipv4}")
public_hostname=$("${update_hosts_script_dir}/ec2-metadata" -p)
public_hostname=$(parse_metadata_result "${public_hostname}")
public_ipv4=$("${update_hosts_script_dir}/ec2-metadata" -v)
public_ipv4=$(parse_metadata_result "${public_ipv4}")

if [ -z "${public_ipv4}" ] || [ "${public_ipv4}" == "not available" ] || [ -z "${public_hostname}" ] || [ "${public_hostname}" == "not available" ]; then
    public_hostname=""
fi

echo "${local_ipv4}    ${local_hostname} ${public_hostname}" >> /etc/hosts

sed -i "s/HOSTNAME=.*/HOSTNAME=${local_hostname}/g" /etc/sysconfig/network
sudo hostname "${local_hostname}"

sudo service network restart

echo "hostname=$(hostname)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -i=$(hostname -i)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -I=$(hostname -I)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -f=$(hostname -f)" | tee -a "/home/ec2-user/update_hosts.log"
echo "hostname -A=$(hostname -A)" | tee -a "/home/ec2-user/update_hosts.log"

echo "finished update_hosts"
{'fraction_non_alphanumeric': 0.18202706152667858, 'fraction_numerical': 0.013786060760786317, 'mean_word_length': 5.778546712802768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1275909', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1386, 'n_words': 284}