#!/bin/bash

set -eux

source /root/tripleo-undercloud-passwords
source /root/stackrc

INSTACK_ROOT=${INSTACK_ROOT:-""}
export INSTACK_ROOT
if [ -n "$INSTACK_ROOT" ]; then
    PATH=$PATH:$INSTACK_ROOT/instack-undercloud/scripts
    export PATH
fi

if [ ! -f /root/.ssh/authorized_keys ]; then
    sudo mkdir -p /root/.ssh
    sudo chmod 7000 /root/.ssh/
    sudo touch /root/.ssh/authorized_keys
    sudo chmod 600 /root/.ssh/authorized_keys
fi

if [ ! -f /root/.ssh/id_rsa ]; then
    ssh-keygen -b 1024 -N '' -f /root/.ssh/id_rsa
fi

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

if [ -e /usr/sbin/getenforce ]; then
    if [ "$(getenforce)" == "Enforcing" ]; then
        set +e
        selinux_wrong_permission="$(find /root/.ssh/ -exec ls -lZ {} \; | grep -v 'ssh_home_t')"
        set -e
        if [ -n "${selinux_wrong_permission}" ]; then
            semanage fcontext -a -t ssh_home_t '/root/.ssh(/.*)?'
            restorecon -R /root/.ssh/
        fi
    fi
fi

# Disable nova quotas
openstack quota set --cores -1 --instances -1 --ram -1 $(openstack project show admin | awk '$2=="id" {print $4}')

# instack-prepare-for-overcloud
rm -rf $HOME/.novaclient
