From 3daa8a84dcbb31e157c6cb54143012e541172795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Sat, 27 Apr 2024 20:08:15 +0300 Subject: [PATCH] Source Update --- src/base.sh | 195 ++----------------------------------- src/commands/help.sh | 3 +- src/commands/settings.sh | 4 +- src/commands/ssh.sh | 4 +- src/commands/ssh/config.sh | 4 +- src/commands/ssh/sign.sh | 4 +- src/commands/welcome.sh | 3 + src/elements.sh | 6 ++ src/functions/login.sh | 143 +++++++++++++++++++++++++++ src/functions/start.sh | 8 ++ src/functions/stop.sh | 19 ++++ 11 files changed, 199 insertions(+), 194 deletions(-) create mode 100644 src/commands/welcome.sh create mode 100644 src/elements.sh create mode 100644 src/functions/login.sh create mode 100644 src/functions/start.sh create mode 100644 src/functions/stop.sh diff --git a/src/base.sh b/src/base.sh index 8bd5ff3..9fb1c97 100644 --- a/src/base.sh +++ b/src/base.sh @@ -5,192 +5,17 @@ if [ ! "$BASH_VERSION" ] ; then exit 1 fi -syntax-line-douple() { -echo -n " -============================== -" -} - -syntax-line-single() { -echo -n " ------------------------------- -" -} - -start() { - syntax-line-douple - echo -n "Warén CLI " - syntax-line-douple -} - -login() { - echo ">>> Login <<<" - ORG=$2 - - if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then - 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 "Organization: Required" - exit 1 - fi - - if [[ $ORG == "warengroup" ]] - then - DOMAIN=waren.io - elif [[ $ORG == "cwinfo" ]] - then - DOMAIN=cwinfo.net - elif [[ $ORG == "cwchristerw" ]] - then - DOMAIN=christerwaren.fi - else - echo "Organization: Unsupported" - exit 1 - 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 "Hostname: Required" - exit 1 - fi - - if [[ -z $USER ]] - then - if [[ -z $SUDO_USER ]] - then - if [[ -z LOGNAME ]] - then - echo "Username: Required" - exit 1 - 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 301 ]] - then - echo "Organization: $ORG" - if [[ -z $USER || $USER == "root" || $USER == "local" ]] - then - echo -n "Username: " - read USERNAME - else - echo "Username: $USER" - USERNAME=$USER - fi - - echo -n "Password: " - read -s PASSWORD - echo -n "****************" - 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 "Status: Login Failed" - exit 1 - fi - else - echo "Organization: $ORG" - echo "Status: IDM Offline" - exit 1 - fi - else - echo "Organization: $ORG" - echo "Status: Vault Offline" - exit 1 - fi - - - - # VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://vault.$DOMAIN/v1/sys/health) - # if [[ $VAULT_STATUS -eq 200 ]]; then - # export VAULT_ADDR="https://vault.$DOMAIN" - # VAULT_LOGIN=$(vault token lookup -address=https://vault.$DOMAIN 2> /dev/null) - # if [[ -z $VAULT_LOGIN ]]; then - # IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://idm.$DOMAIN) - # if [[ $IDM_STATUS -eq 301 ]]; then - # echo "vault.$DOMAIN - Login" - # echo "Username: $USER" - # echo -n "Password: " - # export VAULT_ADDR="https://vault.$DOMAIN" - # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=ldap username=$USER 2> /dev/null) - # echo "" - # if [[ -z $VAULT_LOGIN ]]; then - # exit 1 - # fi - # else - # echo "vault.$DOMAIN - Login" - # echo -n "Token: " - # export VAULT_ADDR="https://vault.$DOMAIN" - # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=token 2> /dev/null) - # echo "" - # if [[ -z $VAULT_LOGIN || ${#VAULT_TOKEN} -lt 95 || ${#VAULT_TOKEN} -gt 95 ]]; then - # exit 1 - # fi - # fi - # fi - # fi - - - # if [[ $USER == "root" || $USER == "local" ]] - # then - # echo "Status: Login Failed" - # fi - start -} - -stop () { - 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 - echo " " - echo " " - exit 1 -} - +{{ ELEMENTS }} +{{ FUNCTIONS }} {{ COMMANDS }} -start -if [[ ! -z $1 ]] && [[ $(type -t warencli-$1) == function ]] +wx-start +if [[ ! -z $1 ]] && [[ $(type -t wx-$1) == function ]] then - warencli-$1 $2 $3 $4 $5 $6 $7 $8 $9 + wx-login + wx-start + wx-$1 $2 $3 $4 $5 $6 $7 $8 $9 +else + wx-welcome fi -stop +wx-stop diff --git a/src/commands/help.sh b/src/commands/help.sh index bbf1a3e..d1cd81f 100644 --- a/src/commands/help.sh +++ b/src/commands/help.sh @@ -1,4 +1,5 @@ -warencli-help() { +wx-help() { +echo -n ">>> Help <<<"; echo " Usage: $0 COMMAND [OPTIONS] diff --git a/src/commands/settings.sh b/src/commands/settings.sh index ae081fa..c151072 100644 --- a/src/commands/settings.sh +++ b/src/commands/settings.sh @@ -1,3 +1,3 @@ -warencli-settings() { - echo ">>> Settings <<<"; +wx-settings() { + echo -n ">>> Settings <<<"; } diff --git a/src/commands/ssh.sh b/src/commands/ssh.sh index 15f430f..6e869b9 100644 --- a/src/commands/ssh.sh +++ b/src/commands/ssh.sh @@ -1,3 +1,3 @@ -warencli-ssh() { - echo ">>> SSH <<<"; +wx-ssh() { + echo -n ">>> SSH <<<"; } diff --git a/src/commands/ssh/config.sh b/src/commands/ssh/config.sh index 687b5c9..7af28cc 100644 --- a/src/commands/ssh/config.sh +++ b/src/commands/ssh/config.sh @@ -1,3 +1,3 @@ -warencli-ssh-config(){ - echo ">>> SSH: Config <<<" +wx-ssh-config(){ + echo -n ">>> SSH: Config <<<" } diff --git a/src/commands/ssh/sign.sh b/src/commands/ssh/sign.sh index 2892c01..a055ccf 100644 --- a/src/commands/ssh/sign.sh +++ b/src/commands/ssh/sign.sh @@ -1,3 +1,3 @@ -warencli-ssh-sign(){ - echo ">>> SSH: Certificates <<<" +wx-ssh-sign(){ + echo -n ">>> SSH: Certificates <<<" } diff --git a/src/commands/welcome.sh b/src/commands/welcome.sh new file mode 100644 index 0000000..bf1b277 --- /dev/null +++ b/src/commands/welcome.sh @@ -0,0 +1,3 @@ +wx-welcome() { + echo -n ">>> Welcome <<<" +} diff --git a/src/elements.sh b/src/elements.sh new file mode 100644 index 0000000..d2bea33 --- /dev/null +++ b/src/elements.sh @@ -0,0 +1,6 @@ +wx-elements-line-douple () { + echo "==============================" +} +wx-elements-line-single () { + echo "------------------------------" +} diff --git a/src/functions/login.sh b/src/functions/login.sh new file mode 100644 index 0000000..f72459d --- /dev/null +++ b/src/functions/login.sh @@ -0,0 +1,143 @@ +wx-login() { + echo ">>> Login <<<" + ORG=$2 + + if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then + 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 "Organization: Required" + 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 "Organization: Unsupported" + 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 "Hostname: Required" + stop + fi + + if [[ -z $USER ]] + then + if [[ -z $SUDO_USER ]] + then + if [[ -z LOGNAME ]] + then + echo "Username: Required" + 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 301 ]] + then + echo "Organization: $ORG" + if [[ -z $USER || $USER == "root" || $USER == "local" ]] + then + echo -n "Username: " + read USERNAME + else + echo "Username: $USER" + USERNAME=$USER + fi + + echo -n "Password: " + read -s PASSWORD + echo "****************" + 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 "Status: Login Failed" + stop + fi + echo "" + else + echo "Organization: $ORG" + echo "Status: IDM Offline" + stop + fi + else + echo "Organization: $ORG" + echo "Status: Vault Offline" + stop + fi + + + + # VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://vault.$DOMAIN/v1/sys/health) + # if [[ $VAULT_STATUS -eq 200 ]]; then + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault token lookup -address=https://vault.$DOMAIN 2> /dev/null) + # if [[ -z $VAULT_LOGIN ]]; then + # IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://idm.$DOMAIN) + # if [[ $IDM_STATUS -eq 301 ]]; then + # echo "vault.$DOMAIN - Login" + # echo "Username: $USER" + # echo -n "Password: " + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=ldap username=$USER 2> /dev/null) + # echo "" + # if [[ -z $VAULT_LOGIN ]]; then + stop + # fi + # else + # echo "vault.$DOMAIN - Login" + # echo -n "Token: " + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=token 2> /dev/null) + # echo "" + # if [[ -z $VAULT_LOGIN || ${#VAULT_TOKEN} -lt 95 || ${#VAULT_TOKEN} -gt 95 ]]; then + # stop + # fi + # fi + # fi + # fi + + + # if [[ $USER == "root" || $USER == "local" ]] + # then + # echo "Status: Login Failed" + # fi +} diff --git a/src/functions/start.sh b/src/functions/start.sh new file mode 100644 index 0000000..fce337f --- /dev/null +++ b/src/functions/start.sh @@ -0,0 +1,8 @@ +wx-start() { + echo "" + echo "" + echo "" + wx-elements-line-douple + echo "Warén CLI" + wx-elements-line-douple +} diff --git a/src/functions/stop.sh b/src/functions/stop.sh new file mode 100644 index 0000000..be08ed2 --- /dev/null +++ b/src/functions/stop.sh @@ -0,0 +1,19 @@ +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 +}