<commit_before>#!/bin/bash

# reset templating from last run
git status | grep modified | awk '{print $2}' | grep -v template-bootcfg.bash | xargs git checkout

#kill container from last run
docker ps -a | grep bootcfg | awk '{print $1}' | xargs -I{} bash -c 'docker kill {} && docker rm {}'

# remove ignition.json from last run
rm -v ignition.json

this_script="$(basename $0)"

# next two functions:
# http://stackoverflow.com/questions/20762575/explanation-of-convertor-of-cidr-to-netmask-in-linux-shell-netmask2cdir-and-cdir
mask2cdr ()
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x=${1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}
   echo $(( $2 + (${#x}/4) ))
}

cdr2mask ()
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift
   echo ${1-0}.${2-0}.${3-0}.${4-0}
}

if [[ "$(git rev-parse --show-toplevel)" != "${PWD}" ]]
then
    echo "ERROR: ${this_script} cannot continue"
    echo "${this_script} must be run from the root of the kpc git repository as 'sudo -E ./script.bash'"
    echo ""
    echo "FIXME: add some notes on how this is determined in case it is flaky"
    echo "formatting here is terrible"
fi

# source some files to determine CoreOS channel and version, then sub them in
[ -e /usr/share/coreos/update.conf ] && source /usr/share/coreos/update.conf
[ -e /etc/coreos/update.conf ] && source /etc/coreos/update.conf
source /etc/os-release

echo "This deploy host is booted to CoreOS ${VERSION} from the ${GROUP} release channel."
echo " - Will download and deploy CoreOS ${VERSION} from release channel ${GROUP} to any target nodes needing install."
grep -rlZ KPC_coreos_channel . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_coreos_channel/${GROUP}/"
grep -rlZ KPC_coreos_version . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_coreos_version/${VERSION}/"

# needed for next sections
SSH_CONNECTION_ARRAY=( ${SSH_CONNECTION} )

# how do nodes reach bootcfg? where do they get coreos images to boot?
echo "Your SSH session to this deploy host is to its IP address '${SSH_CONNECTION_ARRAY[2]}'; this address is assumed to be on the management network."
export KPC_bootcfg_endpoint="${SSH_CONNECTION_ARRAY[2]}"
echo " - Will instruct booting nodes to reach bootcfg service at ${KPC_bootcfg_endpoint}:8080 for configs and images."
grep -rlZ KPC_bootcfg_endpoint . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_bootcfg_endpoint/${KPC_bootcfg_endpoint}/"

# a hint for our ip-metadata-kpc systemd unit to determine which IP/interface services like etcd2 should bootstrap on
export KPC_private_subnet_hint="${KPC_bootcfg_endpoint%.*}."
grep -rlZ KPC_private_subnet_hint . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_private_subnet_hint/${KPC_private_subnet_hint}/"

# MAY BE ADDED LATER... This is a partial implementation for manual selection option of the subnet hint/bootcfg endpoint
# choose bootcfg endpoint and mgmt subnet hint
# we assume IPv4, and that the CIDR mask is /24 or larger (that is, nodes have the same first 3 octets for their IP on this network)
#echo "Please choose the IP address that represents this host's interface to the management network:"
#select ipadr in $(ip -4 -o addr show scope global | awk '{print $4}'); do echo $ipadr selected; export THEIP="${ipadr}"; break; done

# get list of authorized_keys for the current user and pass them along to the booted nodes
mapfile -t pubkeys_pre < ~/.ssh/authorized_keys
pubkeys=()
echo
echo "WARNING WARNING WARNING"
echo "Nodes booting from network and installing will be configured to allow the core user to SSH "
echo "in with **ALL** of the keys found in ~/.ssh/authorized_keys. These include:"
echo
for key in "${pubkeys_pre[@]}"; do
    if [[ "${key}" != '' && ! "${key}" =~ ^# ]]
    then
        echo "${key}" | sed -e 's/\(.\{25\}\).*\(.\{25\}\)/ - \1 ... ... ... \2/'
        pubkeys+=("\"${key}\",")
    fi
done

if [[ ${#pubkeys[@]} < 1 ]]
then
    echo ' - NO KEYS (you may not be able to log in to booted nodes!!)'
fi

echo

pubkeys_string_temp="${pubkeys[@]}"
export KPC_ssh_authorized_keys="${pubkeys_string_temp%,}"

# sed delimiter changed to avoid escaping '/'
grep -rlZ KPC_ssh_authorized_keys . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s|KPC_ssh_authorized_keys|${KPC_ssh_authorized_keys}|"



# create a token to bootstrap etcd - remember to set size to the number of target nodes
echo "Retrieving an etcd discovery token for installed nodes to bootstrap with"
#FIXME need a more elegant way to set the cluster size
export KPC_discovery_fullurl="$(curl -w "\n" 'https://discovery.etcd.io/new?size=3' 2>/dev/null)"
export KPC_discvoery_token="${KPC_discovery_fullurl##*/}"  # cut off everything but the token string, via parameter substitution

echo "Got ${KPC_discovery_fullurl}"
#FIXME error out if no token
# sed delimiter changed to avoid escaping '/'
grep -rlZ KPC_discovery_token . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s|KPC_discovery_token|${KPC_discovery_token}|"

echo "Downloading required coreos images using upstream get-coreos script"
bootcfg/scripts/get-coreos "${GROUP}" "${VERSION}" ./bootcfg/assets

echo "Starting up a temporary bootcfg instance to render the Ignition config this host will run after install/reboot"
docker run -d -p 8080:8080 -v $PWD/bootcfg:/var/lib/bootcfg:Z \
    -v $PWD/bootcfg/groups/kolla:/var/lib/bootcfg/groups:Z \
    quay.io/coreos/bootcfg:v0.4.0 -address=0.0.0.0:8080 -log-level=debug

sleep 2

echo "Retrieving Ignition config for deploy host"
curl "http://10.101.0.15:8080/ignition?mac=00-00-00-00-00-00&modekey=deployhost&etcd_discovery_id=${KPC_discovery_token}&coreos_private_subnet_hint=10.101.0." --retry 5 --retry-delay 2 -o ignition.json


DEVICE=/dev/sda
read -r -p "Are you sure you want to run coreos-install and ERASE ${DEVICE}? [y/N] " response
response=${response,,}    # tolower
if [[ ! $response =~ ^(yes|y)$ ]]
then
    echo "You said no"
    exit 1
fi



coreos-install -d "${DEVICE}" -C "${GROUP}" -V "${VERSION}" -i ignition.json -b http://"${KPC_bootcfg_endpoint}":8080/assets/coreos
udevadm settle
<commit_msg>actually prompt for device. reorder some steps to make it more reasonable to bail-out later<commit_after>#!/bin/bash

# reset templating from last run
git status | grep modified | awk '{print $2}' | grep -v template-bootcfg.bash | xargs git checkout

#kill container from last run
docker ps -a | grep bootcfg | awk '{print $1}' | xargs -I{} bash -c 'docker kill {} && docker rm {}'

# remove ignition.json from last run
rm -v ignition.json

this_script="$(basename $0)"

# next two functions:
# http://stackoverflow.com/questions/20762575/explanation-of-convertor-of-cidr-to-netmask-in-linux-shell-netmask2cdir-and-cdir
mask2cdr ()
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x=${1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}
   echo $(( $2 + (${#x}/4) ))
}

cdr2mask ()
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift
   echo ${1-0}.${2-0}.${3-0}.${4-0}
}

if [[ "$(git rev-parse --show-toplevel)" != "${PWD}" ]]
then
    echo "ERROR: ${this_script} cannot continue"
    echo "${this_script} must be run from the root of the kpc git repository as 'sudo -E ./script.bash'"
    echo ""
    echo "FIXME: add some notes on how this is determined in case it is flaky"
    echo "formatting here is terrible"
fi

# source some files to determine CoreOS channel and version, then sub them in
[ -e /usr/share/coreos/update.conf ] && source /usr/share/coreos/update.conf
[ -e /etc/coreos/update.conf ] && source /etc/coreos/update.conf
source /etc/os-release

echo "This deploy host is booted to CoreOS ${VERSION} from the ${GROUP} release channel."
echo " - Will download and deploy CoreOS ${VERSION} from release channel ${GROUP} to any target nodes needing install."
grep -rlZ KPC_coreos_channel . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_coreos_channel/${GROUP}/"
grep -rlZ KPC_coreos_version . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_coreos_version/${VERSION}/"

# needed for next sections
SSH_CONNECTION_ARRAY=( ${SSH_CONNECTION} )

# how do nodes reach bootcfg? where do they get coreos images to boot?
echo "Your SSH session to this deploy host is to its IP address '${SSH_CONNECTION_ARRAY[2]}'; this address is assumed to be on the management network."
export KPC_bootcfg_endpoint="${SSH_CONNECTION_ARRAY[2]}"
echo " - Will instruct booting nodes to reach bootcfg service at ${KPC_bootcfg_endpoint}:8080 for configs and images."
grep -rlZ KPC_bootcfg_endpoint . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_bootcfg_endpoint/${KPC_bootcfg_endpoint}/"

# a hint for our ip-metadata-kpc systemd unit to determine which IP/interface services like etcd2 should bootstrap on
export KPC_private_subnet_hint="${KPC_bootcfg_endpoint%.*}."
grep -rlZ KPC_private_subnet_hint . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s/KPC_private_subnet_hint/${KPC_private_subnet_hint}/"

# MAY BE ADDED LATER... This is a partial implementation for manual selection option of the subnet hint/bootcfg endpoint
# choose bootcfg endpoint and mgmt subnet hint
# we assume IPv4, and that the CIDR mask is /24 or larger (that is, nodes have the same first 3 octets for their IP on this network)
#echo "Please choose the IP address that represents this host's interface to the management network:"
#select ipadr in $(ip -4 -o addr show scope global | awk '{print $4}'); do echo $ipadr selected; export THEIP="${ipadr}"; break; done

# get list of authorized_keys for the current user and pass them along to the booted nodes
mapfile -t pubkeys_pre < ~/.ssh/authorized_keys
pubkeys=()
echo
echo "WARNING WARNING WARNING"
echo "Nodes booting from network and installing will be configured to allow the core user to SSH "
echo "in with **ALL** of the keys found in ~/.ssh/authorized_keys. These include:"
echo
for key in "${pubkeys_pre[@]}"; do
    if [[ "${key}" != '' && ! "${key}" =~ ^# ]]
    then
        echo "${key}" | sed -e 's/\(.\{25\}\).*\(.\{25\}\)/ - \1 ... ... ... \2/'
        pubkeys+=("\"${key}\",")
    fi
done

if [[ ${#pubkeys[@]} < 1 ]]
then
    echo ' - NO KEYS (you may not be able to log in to booted nodes!!)'
fi

echo

pubkeys_string_temp="${pubkeys[@]}"
export KPC_ssh_authorized_keys="${pubkeys_string_temp%,}"

# sed delimiter changed to avoid escaping '/'
grep -rlZ KPC_ssh_authorized_keys . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s|KPC_ssh_authorized_keys|${KPC_ssh_authorized_keys}|"



# create a token to bootstrap etcd - remember to set size to the number of target nodes
echo "Retrieving an etcd discovery token for installed nodes to bootstrap with"
#FIXME need a more elegant way to set the cluster size
export KPC_discovery_fullurl="$(curl -w "\n" 'https://discovery.etcd.io/new?size=3' 2>/dev/null)"
export KPC_discvoery_token="${KPC_discovery_fullurl##*/}"  # cut off everything but the token string, via parameter substitution

echo "Got ${KPC_discovery_fullurl}"
#FIXME error out if no token
# sed delimiter changed to avoid escaping '/'
grep -rlZ KPC_discovery_token . | grep -zZv "${this_script}" | xargs -0 sed -i -e "s|KPC_discovery_token|${KPC_discovery_token}|"

echo "Downloading required coreos images using upstream get-coreos script"
bootcfg/scripts/get-coreos "${GROUP}" "${VERSION}" ./bootcfg/assets

read -r -p "Enter name of block device (not a partition) to destroy and install CoreOS to (ex.: /dev/sdx): " DEVICE
if [[ "${DEVICE:-}" == '' ]]
then
    echo "ERROR: You must enter a device name to destroy and install CoreOS to." >&2
    exit 1
fi
read -r -p "Are you sure you want to run coreos-install and ERASE ${DEVICE}? [y/N] " response
response=${response,,}    # tolower
if [[ ! $response =~ ^(yes|y)$ ]]
then
    echo "You said no"
    exit 1
fi

echo "Starting up a temporary bootcfg instance to render the Ignition config this host will run after install/reboot"
docker run -d -p 8080:8080 -v $PWD/bootcfg:/var/lib/bootcfg:Z \
    -v $PWD/bootcfg/groups/kolla:/var/lib/bootcfg/groups:Z \
    quay.io/coreos/bootcfg:v0.4.0 -address=0.0.0.0:8080 -log-level=debug

sleep 2

echo "Retrieving Ignition config for deploy host"
curl "http://10.101.0.15:8080/ignition?mac=00-00-00-00-00-00&modekey=deployhost&etcd_discovery_id=${KPC_discovery_token}&coreos_private_subnet_hint=10.101.0." --retry 5 --retry-delay 2 -o ignition.json

coreos-install -d "${DEVICE}" -C "${GROUP}" -V "${VERSION}" -i ignition.json -b http://"${KPC_bootcfg_endpoint}":8080/assets/coreos
udevadm settle

{'fraction_non_alphanumeric': 0.14351062180182975, 'fraction_numerical': 0.02868661808032253, 'mean_word_length': 3.976466049382716, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4700878', 'n_tokens_mistral': 4662, 'n_tokens_neox': 4325, 'n_words': 1524}