Last active
December 1, 2017 08:23
-
-
Save ThinGuy/011d0a26ecdc3c7ef1cdfe90665dd9d1 to your computer and use it in GitHub Desktop.
Auto login to maas - if the right information is there; Set maas creds from maas list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maas-login() { | |
[[ -z ${MAAS_PROFILE} ]] && export MAAS_PROFILE=$(maas 2>/dev/null list|awk '{print $1}') | |
if [[ -z ${MAAS_PROFILE} ]];then | |
printf "\n\e[0;33m WARNING\e[0m:Cannot automatically determine the name of the MaaS profile.\n\n" | |
read -erp "Please enter the name of the MaaS profile that you created: " MAAS_PROFILE | |
fi | |
[[ -z ${MAAS_PROFILE} ]] && { printf "\n\e[0;31mERROR\e[0m: MaaS profile name cannot be blank\n\n";return 1; } | |
export MAAS_APIKEY=$(sudo maas 2>/dev/null apikey --username ${MAAS_PROFILE}) | |
[[ -z ${MAAS_APIKEY} ]] && { printf "Cannot determine MaaS API Key for ${MAAS_PROFILE}. Did you create a profile?\n\nUse sudo maas-region createadmin\n\n";return 0; } | |
export MAAS_URL=$(sudo awk '/maas_url/{print $2}' /etc/maas/regiond.conf) | |
if [[ ${MAAS_PROFILE} && ${MAAS_URL} && ${MAAS_APIKEY} ]];then | |
maas login ${MAAS_PROFILE} ${MAAS_URL} ${MAAS_APIKEY} > /dev/null 2>&1 | |
[[ $? -eq 0 ]] && { printf "\e[1G\e[0;32mSuccess! \e[0m\n"; return 0; } || { printf "\e[0;31mERROR\e[0m: Failed to logon to MaaS @ ${MAAS_URL} using profile ${MAAS_PROFILE}\n";return 1; } | |
else | |
[[ -z ${MAAS_PROFILE} ]] && printf "Cannot determine MaaS profile name.\n" | |
[[ -z ${MAAS_URL} ]] && printf "Cannot determine MaaS URL.\n" | |
[[ -z ${MAAS_APIKEY} ]] && printf "Cannot determine MaaS API Key.\n" | |
#if being called from a function, return | |
[[ $0 = bash ]] && return 1 | |
#if being called from a script, exit | |
[[ -f $0 ]] && exit 1 | |
fi | |
} | |
maas-creds() { | |
[[ -z $(mass list) ]] && maas-login | |
eval $(maas list|awk '{print "export MAAS_PROFILE=\""$1"\"\nexport MAAS_URL=\""$2"\"\nexport MAAS_APIKEY=\""$3"\"\n";exit}') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment