Compare commits

..

48 Commits

Author SHA1 Message Date
ff4bebdc53 Move wi-restricted function to base and fix bash version execution 2025-06-28 15:14:49 +03:00
d9a7864199 Fix running init.sh in non root user 2025-06-28 14:34:59 +03:00
03bba2ba7f Code Update 2025-06-26 23:08:49 +03:00
e75b95abac Code Update 2025-06-23 09:36:34 +03:00
73025e14d8 Code Update 2025-06-22 01:49:56 +03:00
0cdc32a743 Code Update 2025-06-22 00:45:15 +03:00
196a4ef2eb Move Hostname variable to init function 2025-06-21 23:48:10 +03:00
2220092a87 Code Update 2025-05-10 11:30:44 +03:00
746ae8ebf4 Code Update 2025-05-04 19:12:02 +03:00
944e9f020c Code Update 2025-01-05 02:45:16 +02:00
eb5f7980de Update copyright year 2025-01-01 18:00:36 +02:00
4111068772 Add epel-release to dependencies because lsb_release is only available in EPEL 2024-11-23 04:44:52 +02:00
b383c1ddd0 Add lsb_release to Ansible dependencies 2024-11-23 01:10:33 +02:00
280928e133 Install required packages in Rocky Linux 2024-11-22 04:45:08 +02:00
5e3d337b2c Add directory to ansible-pull command 2024-11-20 05:36:21 +02:00
28f807fb44 Code Update: Add extra vars to ansible-pull command 2024-10-06 22:49:32 +03:00
b236fdb65b Code Update 2024-09-26 18:19:53 +03:00
e0e25ef1ac Code Update 2024-07-01 06:23:18 +03:00
206bbbad1c Code Update 2024-07-01 02:21:11 +03:00
0ca9ad28dc Code Update 2024-06-30 17:42:08 +03:00
04699cccce Code Update 2024-06-30 17:19:43 +03:00
3488bb794d Code Update 2024-06-30 17:05:38 +03:00
6e11dc8e8e Code Update 2024-06-30 17:03:46 +03:00
605366d786 Code Update 2024-06-30 16:57:14 +03:00
e76cd7f45e Documentation Update 2024-06-30 16:56:57 +03:00
570d533df3 Code Update 2024-06-29 20:21:16 +03:00
cf58a86458 Maintainer Update 2024-06-29 20:18:46 +03:00
7517d2d7c9 Code Update 2024-06-06 03:03:25 +03:00
0ff81837f9 Replace separate scripts with Maintainer script 2024-06-06 03:03:11 +03:00
ad4954d5ef Add Update script 2024-06-06 02:38:32 +03:00
d9254aac36 Code Update 2024-06-05 23:24:34 +03:00
352b7b2325 Fix Code 2024-06-01 15:59:50 +03:00
9387e15062 Code Update 2024-06-01 14:47:24 +03:00
d6344baf70 Code Update 2024-05-22 05:00:07 +03:00
045fdd0ab0 Fix typo in Git repository 2024-05-18 02:20:13 +03:00
a9e0a77f45 Code Update 2024-05-18 02:16:27 +03:00
32e79f389c Code Update 2024-05-18 00:20:55 +03:00
e1a31d2497 Code Update 2024-05-18 00:09:58 +03:00
d9c370ad91 README.md Create 2024-05-01 03:23:05 +03:00
85f2dd9acd Build Code 2024-05-01 03:22:32 +03:00
9de267de43 Source Update 2024-05-01 03:20:46 +03:00
19b4325920 Change Git repository 2024-04-12 02:14:45 +03:00
7f6617e112 Update 2024-01-09 10:25:45 +02:00
1656fbeaf6 Update copyright year 2024-01-03 08:26:16 +02:00
14f13b8761 Reorder organization before hostname 2023-12-30 14:58:57 +02:00
463588bb31 Disallow using local as username 2023-12-30 13:18:23 +02:00
813d1cd38a Add Python 3 library - jmespath 2023-12-30 12:50:53 +02:00
36162b861b Remove Hashicorp Vault password 2023-12-30 12:48:43 +02:00
13 changed files with 728 additions and 139 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 Warén Group
Copyright (c) 2023-2025 Warén Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Warén Group - Init.sh
## Run Command
```
curl https://waren.io/init.sh -o init.sh
chmod +x init.sh
./init.sh
```
## Maintaining Command
```
./maintainer.sh
```

28
generator.php Normal file
View File

@ -0,0 +1,28 @@
<?php
$base = file_get_contents(__DIR__."/src/base.sh");
$dirs = [
__DIR__.'/src/functions/*.sh'
];
$codes = [];
foreach($dirs as $dir){
foreach(glob($dir) as $file){
$codes[$file] = file_get_contents($file);
}
}
$code = str_replace("{{ CODES }}", implode("\n", $codes), $base);
try {
$file = __DIR__.'/init.sh';
$file = fopen($file, "w");
fwrite($file, $code);
fclose($file);
} catch (\Error $e) {
}
?>

432
init.sh
View File

@ -1,165 +1,335 @@
#!/bin/bash
if [ ! "$BASH_VERSION" ] ; then
sudo curl https://waren.io/init.sh -o $PWD/init.sh
sudo bash $PWD/init.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
exit 1
fi
echo "
==============================
Warén Group
Init Script
------------------------------
"
stop () {
unset HVT
echo "
==============================
"
if [[ $USER != "root" ]]
then
sudo curl https://waren.io/init.sh -o $PWD/init.sh
sudo bash $PWD/init.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
exit 1
fi
wiRed=$(tput setaf 196)
wiGreen=$(tput setaf 46)
wiYellow=$(tput setaf 226)
wiBlue=$(tput setaf 21)
wiPurple=$(tput setaf 165)
wiTurquoise=$(tput setaf 14)
wiPink=$(tput setaf 198)
wiOrange=$(tput setaf 202)
wiBold=$(tput bold)
wiNormal=$(tput sgr0)
wi-header(){
wx-header "$1" "$2"
}
mkdir -p ~/.ssh &> /dev/null
wi-repeat(){
wx-repeat "$1" "$2"
}
apt-get install -y python3-pip python3-venv jq git curl &> /dev/null
wi-login(){
wx-login "$1" "$2"
}
wx-start(){
wi-start
}
wx-stop(){
wi-stop
}
wxBold=$wiBold
wxNormal=$wiNormal
wx-header(){
if [[ $2 == "h1" ]]
then
echo ""
echo ""
echo ""
echo "=============================="
wx-repeat " " $((30/2-${#1}/2))
echo -n "$wxBold"
echo -n "$1"
echo -n "$wxNormal"
echo ""
echo "=============================="
fi
if [[ $2 == "h2" || -z $2 ]]
then
wx-start
wx-repeat " " $((30/2-6/2-${#1}/2))
echo -n ">> $1 <<"
echo ""
echo "------------------------------"
fi
}
wi-init(){
wi-login $1
wi-header "Init"
if [[ ! -z $2 && ${#2} -gt 5 ]]
then
HOSTNAME="$2.$DEVICE_DOMAIN"
elif [[ $(hostname -d) ]]
then
HOSTNAME=$(hostname --fqdn)
else
echo "Status: Hostname Required"
wx-stop
fi
mkdir -p ~/.ssh/keys &> /dev/null
apt-get update &> /dev/null
apt-get install -y python3-pip python3-venv jq git curl lsb-release &> /dev/null
dnf install -y epel-release &> /dev/null
dnf install -y python3-pip jq git curl lsb_release &> /dev/null
python3 -m venv /opt/ansible &> /dev/null
/opt/ansible/bin/pip3 install ansible hvac netaddr &> /dev/null
/opt/ansible/bin/pip3 install ansible &> /dev/null
/opt/ansible/bin/pip3 install cryptography dnspython hvac jmespath netaddr pexpect &> /dev/null
HOSTNAME=$1
if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then
stop
curl \
-H "X-Vault-Token: ${config["login",$ORG]}" \
-X GET \
https://$VAULT_DOMAIN/v1/init.sh/data/ssh -s | jq -r '.data.data.privkey' > ~/.ssh/keys/init
chmod 700 ~/.ssh/keys/init &> /dev/null
mkdir -p ~/.ansible &> /dev/null
if [ ! -f ~/.ansible/vars.yml ]
then
echo "---" > ~/.ansible/vars.yml
echo "hostname: $HOSTNAME" >> ~/.ansible/vars.yml
echo "config:" >> ~/.ansible/vars.yml
echo " identity:" >> ~/.ansible/vars.yml
echo " vault:" >> ~/.ansible/vars.yml
echo " domain: $VAULT_DOMAIN" >> ~/.ansible/vars.yml
fi
if [[ -z $USER || $USER == "root" ]]; then
if [[ -z $SUDO_USER ]]; then
if [[ -z LOGNAME ]]; then
echo -n ""
else
USER=$LOGNAME
fi
else
USER=$SUDO_USER
fi
fi
ORG=$2
case $ORG in
warengroup)
DOMAIN="waren.io"
FOLDER="warengroup"
;;
cwinfo)
DOMAIN="cwinfo.net"
FOLDER="cwinfo"
;;
cwchristerw)
DOMAIN="christerwaren.fi"
FOLDER="cwchristerw"
;;
*)
echo "Organization is required."
stop
;;
esac
DEVICE_DOMAIN="devices.$DOMAIN"
IDM_DOMAIN="idm.$DOMAIN"
VAULT_DOMAIN="vault.cwinfo.net"
GIT_DOMAIN="git.cwinfo.net"
GIT_PORT=2222
GIT_REPOSITORY="warengroup-private/ansible-pull"
GIT_PORT="2222"
GIT_REPOSITORY="warengroup-private/infra-plus"
HOSTNAME="$HOSTNAME.$DEVICE_DOMAIN"
export HVT=${config["login",$ORG]}
/opt/ansible/bin/ansible-galaxy collection install ansible.posix ansible.utils community.crypto community.dns community.docker community.general community.hashi_vault community.libvirt community.mongodb community.mysql community.postgresql containers.podman --upgrade &> /dev/null
/opt/ansible/bin/ansible-pull -U ssh://git@$GIT_DOMAIN:$GIT_PORT/$GIT_REPOSITORY -d ~/.ansible/pull/infra --accept-host-key --private-key ~/.ssh/keys/init --extra-vars @~/.ansible/vars.yml playbooks/init.yml -t init
unset HVT
}
wx-login(){
if [[ ! -z $1 ]]
then
ORG=$1
jq '.org = "'$ORG'"' $HOME/.warengroup/config.json 1> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
elif [[ $(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 [[ $USER != "root" && $USER != "local" && -f "$HOME/.warengroup/config.json" && $(cat $HOME/.warengroup/config.json | jq -r .org) != "null" ]]
then
ORG=$(cat $HOME/.warengroup/config.json | jq -r .org)
else
wx-header "Login"
echo "Status: Organization Required"
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
wx-header "Login"
echo "Status: Organization Unsupported"
wx-stop
fi
FOLDER=$ORG
DEVICE_DOMAIN="devices.$DOMAIN"
IDM_DOMAIN="idm.waren.io"
VAULT_DOMAIN="vault.cwinfo.net"
if [[ -z $USER || $USER == "root" || $USER == "local" ]]
then
if [[ -z $SUDO_USER ]]
then
if [[ -z LOGNAME ]]
then
wx-header "Login"
echo "Status: Username Required"
wx-stop
else
USERNAME=$LOGNAME
fi
else
USERNAME=$SUDO_USER
fi
else
USERNAME=$USER
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 "$VAULT_DOMAIN - Login"
echo "Method: LDAP"
if [[ $VAULT_STATUS -eq 200 ]]
then
if [[ -z $USER || $USER == "root" ]]; then
if [[ $USER != "root" && $USER != "local" && -f "$HOME/.warengroup/config.json" ]]
then
if [[ $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.username) != "null" ]]
then
USERNAME="$(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.username)"
fi
TOKEN="$(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token)"
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} == 95 ]]
then
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG'.token = "'$VAULT_LOGIN'"' $HOME/.warengroup/config.json 1> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
else
IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN)
if [[ $IDM_STATUS -eq 301 ]]
then
wx-header "Login"
echo $wxBold$ORG$wxNormal
if [[ -z $USERNAME || $USERNAME == "root" || $USERNAME == "local" || $USERNAME == "nobody" ]]
then
echo -n "Username: "
read USERNAME
else
echo "Username: $USER"
USERNAME=$USER
echo "Username: $USERNAME"
fi
jq '.login.'$ORG'.username = "'$USERNAME'"' $HOME/.warengroup/config.json 1> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
echo -n "Password: "
read -s PASSWORD
echo "****************"
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
stop
fi
VAULT_TOKEN=$VAULT_LOGIN
export HVT="$VAULT_TOKEN"
echo -e "\n"
if [[ -z $USERNAME || -z $PASSWORD ]]
then
echo "Status: Username & Password Required"
wx-stop
else
echo "$VAULT_DOMAIN - Login"
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"
wx-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG'.token = "'$VAULT_LOGIN'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
fi
else
wx-header "Login"
echo $wxBold$ORG$wxNormal
echo -n "Token: "
read -s VAULT_TOKEN
echo "***********************************************************************************************"
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo "Status: Vault Token Required"
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 "Status: Login Failed"
wx-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG'.token = "'$VAULT_LOGIN'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
fi
fi
else
wx-header "Login"
echo $wxBold$ORG$wxNormal
echo "Status: Vault Offline"
wx-stop
fi
}
wx-repeat() {
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
}
wi-start(){
wi-header "Warén Init" h1
mkdir -p $HOME/.warengroup &> /dev/null
if [[ ! -f "$HOME/.warengroup/config.json" || $(jq -e . < $HOME/.warengroup/config.json &>/dev/null; echo $?) -gt 0 ]]
then
echo '{}' | jq > $HOME/.warengroup/config.json
fi
mkdir -p $HOME/.ssh/keys
chmod 700 -R $HOME/.ssh/keys
}
wi-stop (){
echo " "
if [[ -z $VAULT_TOKEN || ${#VAULT_TOKEN} -lt 95 || ${#VAULT_TOKEN} -gt 95 ]]; then
stop
echo " "
echo " "
rm -rf /.ssh/keys/init &> /dev/null
exit 1
}
if [[ ! -f /bin/jq ]]
then
apt update &> /dev/null
apt install -y jq &> /dev/null
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/lookup-self -X GET -H "X-Vault-Token: $VAULT_TOKEN" -s | jq -r '.data.id')
if [[ -z $VAULT_LOGIN ]]; then
stop
fi
VAULT_TOKEN=$VAULT_LOGIN
export HVT="$VAULT_TOKEN"
echo -e "\n"
fi
fi
curl \
-H "X-Vault-Token: $VAULT_TOKEN" \
-X GET \
https://$VAULT_DOMAIN/v1/init.sh/data/ssh -s | jq -r '.data.data.privkey' > ~/.ssh/init
chmod 700 ~/.ssh/init
mkdir -p ~/.ansible > /dev/null
if [ ! -f ~/.ansible/vars.yml ]; then
openssl rand -base64 64 | tr -d '\n' | head -c 64 > ~/.ansible/vault.yml
ANSIBLE_VAULT_SECRET=$(<~/.ansible/vault.yml)
HASHICORP_VAULT_SECRET=$(openssl rand -base64 64 | tr -d '\n' | head -c 64)
echo "---" > ~/.ansible/vars.yml
echo "hostname: $HOSTNAME" >> ~/.ansible/vars.yml
echo "folder: /$FOLDER/" >> ~/.ansible/vars.yml
echo "vault:" >> ~/.ansible/vars.yml
echo " ansible:" >> ~/.ansible/vars.yml
echo " secret: $ANSIBLE_VAULT_SECRET" >> ~/.ansible/vars.yml
echo " hashicorp:" >> ~/.ansible/vars.yml
echo " domain: $VAULT_DOMAIN" >> ~/.ansible/vars.yml
echo " password: $HASHICORP_VAULT_SECRET" >> ~/.ansible/vars.yml
/opt/ansible/bin/ansible-vault encrypt --vault-password-file ~/.ansible/vault.yml ~/.ansible/vars.yml > /dev/null
fi
ssh-keyscan -p $GIT_PORT $GIT_DOMAIN &> ~/.ssh/known_hosts
/opt/ansible/bin/ansible-pull -U ssh://git@$GIT_DOMAIN:$GIT_PORT/$GIT_REPOSITORY --vault-password-file ~/.ansible/vault.yml --private-key ~/.ssh/init playbooks/init.yml -t init
unset HVT
echo "
==============================
"
wi-init $1 $2
wi-stop

49
maintainer.sh Executable file
View File

@ -0,0 +1,49 @@
if [ ! "$BASH_VERSION" ] ; then
bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
exit 1
fi
wiBold=$(tput bold)
wiNormal=$(tput sgr0)
echo ""
echo ""
echo ""
echo "=============================="
echo -n "$wiBold"
echo " Warén Group "
echo -n "$wiNormal"
echo " Init "
echo "=============================="
echo " >> Maintainer << "
echo "------------------------------"
case $1 in
build)
echo "Building..."
podman run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp docker.io/library/php:8-cli php generator.php &> /dev/null
chmod +x init.sh &> /dev/null
;;
update)
echo "Updating..."
cp ../wx/src/functions/header.sh src/functions/header.sh
cp ../wx/src/functions/repeat.sh src/functions/repeat.sh
cp ../wx/src/commands/auth/login.sh src/functions/login.sh
;;
ready)
echo "Ready"
;;
*)
echo "Initializing..."
sleep 3
./$0 update
sleep 3
./$0 build
sleep 3
./$0 ready
;;
esac
echo " "
echo " "
echo " "
exit 1

36
src/base.sh Normal file
View File

@ -0,0 +1,36 @@
#!/bin/bash
if [ ! "$BASH_VERSION" ] ; then
sudo curl https://waren.io/init.sh -o $PWD/init.sh
sudo bash $PWD/init.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
exit 1
fi
if [[ $USER != "root" ]]
then
sudo curl https://waren.io/init.sh -o $PWD/init.sh
sudo bash $PWD/init.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
exit 1
fi
wiRed=$(tput setaf 196)
wiGreen=$(tput setaf 46)
wiYellow=$(tput setaf 226)
wiBlue=$(tput setaf 21)
wiPurple=$(tput setaf 165)
wiTurquoise=$(tput setaf 14)
wiPink=$(tput setaf 198)
wiOrange=$(tput setaf 202)
wiBold=$(tput bold)
wiNormal=$(tput sgr0)
{{ CODES }}
if [[ ! -f /bin/jq ]]
then
apt update &> /dev/null
apt install -y jq &> /dev/null
fi
wi-init $1 $2
wi-stop

View File

@ -0,0 +1,22 @@
wi-header(){
wx-header "$1" "$2"
}
wi-repeat(){
wx-repeat "$1" "$2"
}
wi-login(){
wx-login "$1" "$2"
}
wx-start(){
wi-start
}
wx-stop(){
wi-stop
}
wxBold=$wiBold
wxNormal=$wiNormal

24
src/functions/header.sh Normal file
View File

@ -0,0 +1,24 @@
wx-header(){
if [[ $2 == "h1" ]]
then
echo ""
echo ""
echo ""
echo "=============================="
wx-repeat " " $((30/2-${#1}/2))
echo -n "$wxBold"
echo -n "$1"
echo -n "$wxNormal"
echo ""
echo "=============================="
fi
if [[ $2 == "h2" || -z $2 ]]
then
wx-start
wx-repeat " " $((30/2-6/2-${#1}/2))
echo -n ">> $1 <<"
echo ""
echo "------------------------------"
fi
}

56
src/functions/init.sh Normal file
View File

@ -0,0 +1,56 @@
wi-init(){
wi-login $1
wi-header "Init"
if [[ ! -z $2 && ${#2} -gt 5 ]]
then
HOSTNAME="$2.$DEVICE_DOMAIN"
elif [[ $(hostname -d) ]]
then
HOSTNAME=$(hostname --fqdn)
else
echo "Status: Hostname Required"
wx-stop
fi
mkdir -p ~/.ssh/keys &> /dev/null
apt-get update &> /dev/null
apt-get install -y python3-pip python3-venv jq git curl lsb-release &> /dev/null
dnf install -y epel-release &> /dev/null
dnf install -y python3-pip jq git curl lsb_release &> /dev/null
python3 -m venv /opt/ansible &> /dev/null
/opt/ansible/bin/pip3 install ansible &> /dev/null
/opt/ansible/bin/pip3 install cryptography dnspython hvac jmespath netaddr pexpect &> /dev/null
curl \
-H "X-Vault-Token: ${config["login",$ORG]}" \
-X GET \
https://$VAULT_DOMAIN/v1/init.sh/data/ssh -s | jq -r '.data.data.privkey' > ~/.ssh/keys/init
chmod 700 ~/.ssh/keys/init &> /dev/null
mkdir -p ~/.ansible &> /dev/null
if [ ! -f ~/.ansible/vars.yml ]
then
echo "---" > ~/.ansible/vars.yml
echo "hostname: $HOSTNAME" >> ~/.ansible/vars.yml
echo "config:" >> ~/.ansible/vars.yml
echo " identity:" >> ~/.ansible/vars.yml
echo " vault:" >> ~/.ansible/vars.yml
echo " domain: $VAULT_DOMAIN" >> ~/.ansible/vars.yml
fi
GIT_DOMAIN="git.cwinfo.net"
GIT_PORT="2222"
GIT_REPOSITORY="warengroup-private/infra-plus"
export HVT=${config["login",$ORG]}
/opt/ansible/bin/ansible-galaxy collection install ansible.posix ansible.utils community.crypto community.dns community.docker community.general community.hashi_vault community.libvirt community.mongodb community.mysql community.postgresql containers.podman --upgrade &> /dev/null
/opt/ansible/bin/ansible-pull -U ssh://git@$GIT_DOMAIN:$GIT_PORT/$GIT_REPOSITORY -d ~/.ansible/pull/infra --accept-host-key --private-key ~/.ssh/keys/init --extra-vars @~/.ansible/vars.yml playbooks/init.yml -t init
unset HVT
}

164
src/functions/login.sh Normal file
View File

@ -0,0 +1,164 @@
wx-login(){
if [[ ! -z $1 ]]
then
ORG=$1
jq '.org = "'$ORG'"' $HOME/.warengroup/config.json 1> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
elif [[ $(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 [[ $USER != "root" && $USER != "local" && -f "$HOME/.warengroup/config.json" && $(cat $HOME/.warengroup/config.json | jq -r .org) != "null" ]]
then
ORG=$(cat $HOME/.warengroup/config.json | jq -r .org)
else
wx-header "Login"
echo "Status: Organization Required"
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
wx-header "Login"
echo "Status: Organization Unsupported"
wx-stop
fi
FOLDER=$ORG
DEVICE_DOMAIN="devices.$DOMAIN"
IDM_DOMAIN="idm.waren.io"
VAULT_DOMAIN="vault.cwinfo.net"
if [[ -z $USER || $USER == "root" || $USER == "local" ]]
then
if [[ -z $SUDO_USER ]]
then
if [[ -z LOGNAME ]]
then
wx-header "Login"
echo "Status: Username Required"
wx-stop
else
USERNAME=$LOGNAME
fi
else
USERNAME=$SUDO_USER
fi
else
USERNAME=$USER
fi
VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health)
if [[ $VAULT_STATUS -eq 200 ]]
then
if [[ $USER != "root" && $USER != "local" && -f "$HOME/.warengroup/config.json" ]]
then
if [[ $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.username) != "null" ]]
then
USERNAME="$(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.username)"
fi
TOKEN="$(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token)"
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} == 95 ]]
then
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG'.token = "'$VAULT_LOGIN'"' $HOME/.warengroup/config.json 1> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
else
IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN)
if [[ $IDM_STATUS -eq 301 ]]
then
wx-header "Login"
echo $wxBold$ORG$wxNormal
if [[ -z $USERNAME || $USERNAME == "root" || $USERNAME == "local" || $USERNAME == "nobody" ]]
then
echo -n "Username: "
read USERNAME
else
echo "Username: $USERNAME"
fi
jq '.login.'$ORG'.username = "'$USERNAME'"' $HOME/.warengroup/config.json 1> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
echo -n "Password: "
read -s PASSWORD
echo "****************"
if [[ -z $USERNAME || -z $PASSWORD ]]
then
echo "Status: Username & Password Required"
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 "Status: Login Failed"
wx-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG'.token = "'$VAULT_LOGIN'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
fi
else
wx-header "Login"
echo $wxBold$ORG$wxNormal
echo -n "Token: "
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo "Status: Vault Token Required"
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 "Status: Login Failed"
wx-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG'.token = "'$VAULT_LOGIN'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
fi
fi
else
wx-header "Login"
echo $wxBold$ORG$wxNormal
echo "Status: Vault Offline"
wx-stop
fi
}

5
src/functions/repeat.sh Normal file
View File

@ -0,0 +1,5 @@
wx-repeat() {
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
}

13
src/functions/start.sh Normal file
View File

@ -0,0 +1,13 @@
wi-start(){
wi-header "Warén Init" h1
mkdir -p $HOME/.warengroup &> /dev/null
if [[ ! -f "$HOME/.warengroup/config.json" || $(jq -e . < $HOME/.warengroup/config.json &>/dev/null; echo $?) -gt 0 ]]
then
echo '{}' | jq > $HOME/.warengroup/config.json
fi
mkdir -p $HOME/.ssh/keys
chmod 700 -R $HOME/.ssh/keys
}

9
src/functions/stop.sh Normal file
View File

@ -0,0 +1,9 @@
wi-stop (){
echo " "
echo " "
echo " "
rm -rf /.ssh/keys/init &> /dev/null
exit 1
}