#!/bin/bash

if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

ANSIBLE_DEPLOY_HOSTAME="ironic-ansible-deploy"

echo $ANSIBLE_DEPLOY_HOSTAME > /etc/hostname

# not having a hostname in hosts produces an extra output
# on every "sudo" command like the following:
#
#   sudo: unable to resolve host <HOSTNAME>\r\n
#
# which as of Ansible 2.0.1.0 fails JSON parsing
# in case of tasks using become+async.
# Ansible issues #13965 (fixed in 2.0.1.0), #14568, #14714

# ensure /etc/hosts has hostname in it
sed -i "s/127.0.0.1\s*localhost/127.0.0.1 localhost $ANSIBLE_DEPLOY_HOSTAME/g" /etc/hosts

# ensure SSH host keys exist
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa
ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t ed25519
