323 lines
7.5 KiB
Bash
Executable File
323 lines
7.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
if [ ! "$BASH_VERSION" ] ; then
|
|
bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
|
|
exit 1
|
|
fi
|
|
|
|
wxRed=$(tput setaf 196)
|
|
wxGreen=$(tput setaf 46)
|
|
wxYellow=$(tput setaf 226)
|
|
wxBlue=$(tput setaf 21)
|
|
wxPurple=$(tput setaf 165)
|
|
wxTurquoise=$(tput setaf 14)
|
|
wxPink=$(tput setaf 198)
|
|
wxOrange=$(tput setaf 202)
|
|
wxUnderline=$(tput smul)
|
|
wxItalic=$(tput sitm)
|
|
wxBold=$(tput bold)
|
|
wxNormal=$(tput sgr0)
|
|
|
|
wx-login() {
|
|
echo -n "$wxItalic"
|
|
echo " >> Login << "
|
|
echo -n "$wxNormal";
|
|
echo "------------------------------"
|
|
|
|
ORG=$2
|
|
|
|
if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then
|
|
wx-stop
|
|
fi
|
|
|
|
if [[ $(hostname -d) == "devices.waren.io" ]]
|
|
then
|
|
ORG=warengroup
|
|
elif [[ $(hostname -d) == "devices.cwinfo.net" ]]
|
|
then
|
|
ORG=cwinfo
|
|
elif [[ $(hostname -d) == "devices.christerwaren.fi" ]]
|
|
then
|
|
ORG=cwchristerw
|
|
elif [[ -z $ORG ]]
|
|
then
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Organization Required"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
|
|
if [[ $ORG == "warengroup" ]]
|
|
then
|
|
DOMAIN=waren.io
|
|
elif [[ $ORG == "cwinfo" ]]
|
|
then
|
|
DOMAIN=cwinfo.net
|
|
elif [[ $ORG == "cwchristerw" ]]
|
|
then
|
|
DOMAIN=christerwaren.fi
|
|
else
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Organization Unsupported"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
|
|
FOLDER=$ORG
|
|
DEVICE_DOMAIN="devices.$DOMAIN"
|
|
IDM_DOMAIN="idm.cwinfo.net"
|
|
VAULT_DOMAIN="vault.cwinfo.net"
|
|
GIT_DOMAIN="git.cwinfo.net"
|
|
|
|
if [[ $(hostname -d) ]]
|
|
then
|
|
HOSTNAME=$(hostname --fqdn)
|
|
elif [[ -z $(hostname -d) ]] && [[ ! -z $2 ]]
|
|
then
|
|
HOSTNAME="$2.$DEVICE_DOMAIN"
|
|
else
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Hostname Required"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
|
|
if [[ -z $USER ]]
|
|
then
|
|
if [[ -z $SUDO_USER ]]
|
|
then
|
|
if [[ -z LOGNAME ]]
|
|
then
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Username Required"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
else
|
|
USER=$LOGNAME
|
|
fi
|
|
else
|
|
USER=$SUDO_USER
|
|
fi
|
|
fi
|
|
|
|
VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health)
|
|
if [[ $VAULT_STATUS -eq 200 ]]
|
|
then
|
|
IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN)
|
|
if [[ $IDM_STATUS -eq 302 ]]
|
|
then
|
|
echo -n $wxBold
|
|
echo -n $ORG
|
|
echo $wxNormal
|
|
|
|
if [[ -z $USER || $USER == "root" || $USER == "local" ]]
|
|
then
|
|
echo -n "Username: "
|
|
echo -n $wxItalic
|
|
read USERNAME
|
|
echo -n $wxNormal
|
|
else
|
|
echo -n "Username: "
|
|
echo -n $wxItalic
|
|
echo -n $USER
|
|
echo $wxNormal
|
|
USERNAME=$USER
|
|
fi
|
|
|
|
echo -n "Password: "
|
|
echo -n $wxItalic
|
|
read -s PASSWORD
|
|
echo -n "****************"
|
|
echo $wxNormal
|
|
|
|
if [[ -z $USERNAME || -z $PASSWORD ]]
|
|
then
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Username & Password Required"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
else
|
|
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/ldap/login/$USERNAME -X POST -d '{ "password": "'$PASSWORD'" }' -s | jq -r '.auth.client_token')
|
|
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
|
|
then
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Login Failed"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
fi
|
|
echo ""
|
|
echo $VAULT_LOGIN
|
|
wx-start
|
|
else
|
|
echo -n $wxBold
|
|
echo -n $ORG
|
|
echo $wxNormal
|
|
|
|
echo -n "Token: "
|
|
read -s TOKEN
|
|
echo "****************"
|
|
|
|
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
|
|
then
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Vault Token Required"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
|
|
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
|
|
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
|
|
then
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Login Failed"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
|
|
wx-start
|
|
fi
|
|
else
|
|
echo -n $wxBold
|
|
echo -n $ORG
|
|
echo $wxNormal
|
|
|
|
echo -n "Status: "
|
|
echo -n $wxItalic
|
|
echo -n "Vault Offline"
|
|
echo -n $wxNormal
|
|
|
|
wx-stop
|
|
fi
|
|
}
|
|
|
|
wx-start() {
|
|
echo ""
|
|
echo ""
|
|
echo ""
|
|
echo "=============================="
|
|
echo -n "$wxBold"
|
|
echo " Warén CLI "
|
|
echo -n "$wxNormal"
|
|
echo "=============================="
|
|
}
|
|
|
|
wx-stop () {
|
|
echo " "
|
|
echo " "
|
|
echo " "
|
|
if [[ -d "./src" ]]
|
|
then
|
|
podman run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp docker.io/library/php:8-cli php generator.php &> /dev/null
|
|
mv wx.tmp wx &> /dev/null
|
|
chmod +x wx &> /dev/null
|
|
else
|
|
sudo curl https://git.waren.io/warengroup/wx/raw/branch/master/wx -o /opt/wx &> /dev/null
|
|
if [[ -d "/usr/bin" ]]
|
|
then
|
|
sudo ln -sf /opt/wx /usr/bin/wx &> /dev/null
|
|
fi
|
|
sudo chmod +x /usr/bin/wx &> /dev/null
|
|
fi
|
|
exit 1
|
|
}
|
|
|
|
wx-help() {
|
|
echo -n "$wxItalic"
|
|
echo " >> Help << "
|
|
echo -n "$wxNormal";
|
|
echo "------------------------------"
|
|
echo -n "
|
|
Usage: $0 COMMAND [OPTIONS]
|
|
|
|
Common Commands:
|
|
init Init
|
|
ssh SSH
|
|
config Config
|
|
sign Certificates
|
|
|
|
Management Commands:
|
|
settings Settings
|
|
";
|
|
}
|
|
|
|
wx-init () {
|
|
wx-login
|
|
|
|
echo -n "$wxItalic"
|
|
echo " >> Init << "
|
|
echo -n "$wxNormal";
|
|
echo "------------------------------"
|
|
|
|
mkdir -p ~/.ssh &> /dev/null
|
|
|
|
apt-get install -y python3-pip python3-venv jq git curl &> /dev/null
|
|
python3 -m venv /opt/ansible &> /dev/null
|
|
/opt/ansible/bin/pip3 install ansible hvac netaddr jmespath pexpect &> /dev/null
|
|
}
|
|
|
|
wx-settings() {
|
|
wx-login
|
|
|
|
echo -n "$wxItalic"
|
|
echo " >> Settings << "
|
|
echo -n "$wxNormal";
|
|
echo "------------------------------"
|
|
}
|
|
|
|
wx-ssh() {
|
|
wx-login
|
|
|
|
echo -n "$wxItalic"
|
|
echo " >> SSH << "
|
|
echo -n "$wxNormal";
|
|
echo "------------------------------"
|
|
}
|
|
|
|
wx-welcome() {
|
|
echo -n "$wxItalic"
|
|
echo " >> Welcome << "
|
|
echo -n "$wxNormal"
|
|
echo "------------------------------"
|
|
}
|
|
|
|
wx-ssh-config(){
|
|
echo -n "$wxItalic"
|
|
echo " >> SSH : Config << "
|
|
echo -n "$wxNormal"
|
|
echo "------------------------------"
|
|
}
|
|
|
|
wx-ssh-sign(){
|
|
echo -n "$wxItalic"
|
|
echo " >> SSH : Certificates << "
|
|
echo -n "$wxNormal"
|
|
echo "------------------------------"
|
|
}
|
|
|
|
|
|
wx-start
|
|
if [[ ! -z $1 ]] && [[ $(type -t wx-$1) == function ]]
|
|
then
|
|
wx-$1 $2 $3 $4 $5 $6 $7 $8 $9
|
|
else
|
|
wx-welcome
|
|
fi
|
|
wx-stop
|