Compare commits

...

59 Commits

Author SHA1 Message Date
Christer Warén
655c86e508 Fix Hostname in Init script 2025-12-07 17:34:11 +02:00
Christer Warén
b544f7af1e Update Device Types feature in Init script 2025-12-07 17:19:58 +02:00
Christer Warén
a710bfa4b2 Refactoring Code 2025-12-04 04:53:44 +02:00
Christer Warén
3582fc211a Generator Update 2025-12-04 04:49:57 +02:00
Christer Warén
9d8e3d0cff Init.ps1 Update 2025-11-01 11:49:31 +02:00
Christer Warén
a69fb755f9 Change Git domain 2025-10-08 15:46:05 +03:00
Christer Warén
73d9e77ee0 Update variables and add new collections 2025-10-03 10:45:05 +03:00
Christer Warén
a9e9902d6e README: Add Windows command 2025-09-29 00:33:35 +03:00
Christer Warén
373f010215 Init.ps1 Update 2025-09-28 22:00:36 +03:00
Christer Warén
89dd3b084b Add xmltodict as Python library dependency 2025-09-27 01:09:27 +03:00
Christer Warén
bb9fdcd013 Add Init script for Windows 2025-09-26 15:15:37 +03:00
Christer Warén
2e6d165baf Add quotes to path 2025-09-07 10:32:38 +03:00
Christer Warén
17cb8fe954 Add Type question to Init script 2025-08-30 12:50:51 +03:00
Christer Warén
ff4bebdc53 Move wi-restricted function to base and fix bash version execution 2025-06-28 15:14:49 +03:00
Christer Warén
d9a7864199 Fix running init.sh in non root user 2025-06-28 14:34:59 +03:00
Christer Warén
03bba2ba7f Code Update 2025-06-26 23:08:49 +03:00
Christer Warén
e75b95abac Code Update 2025-06-23 09:36:34 +03:00
Christer Warén
73025e14d8 Code Update 2025-06-22 01:49:56 +03:00
Christer Warén
0cdc32a743 Code Update 2025-06-22 00:45:15 +03:00
Christer Warén
196a4ef2eb Move Hostname variable to init function 2025-06-21 23:48:10 +03:00
Christer Warén
2220092a87 Code Update 2025-05-10 11:30:44 +03:00
Christer Warén
746ae8ebf4 Code Update 2025-05-04 19:12:02 +03:00
Christer Warén
944e9f020c Code Update 2025-01-05 02:45:16 +02:00
Christer Warén
eb5f7980de Update copyright year 2025-01-01 18:00:36 +02:00
Christer Warén
4111068772 Add epel-release to dependencies because lsb_release is only available in EPEL 2024-11-23 04:44:52 +02:00
Christer Warén
b383c1ddd0 Add lsb_release to Ansible dependencies 2024-11-23 01:10:33 +02:00
Christer Warén
280928e133 Install required packages in Rocky Linux 2024-11-22 04:45:08 +02:00
Christer Warén
5e3d337b2c Add directory to ansible-pull command 2024-11-20 05:36:21 +02:00
Christer Warén
28f807fb44 Code Update: Add extra vars to ansible-pull command 2024-10-06 22:49:32 +03:00
Christer Warén
b236fdb65b Code Update 2024-09-26 18:19:53 +03:00
Christer Warén
e0e25ef1ac Code Update 2024-07-01 06:23:18 +03:00
Christer Warén
206bbbad1c Code Update 2024-07-01 02:21:11 +03:00
Christer Warén
0ca9ad28dc Code Update 2024-06-30 17:42:08 +03:00
Christer Warén
04699cccce Code Update 2024-06-30 17:19:43 +03:00
Christer Warén
3488bb794d Code Update 2024-06-30 17:05:38 +03:00
Christer Warén
6e11dc8e8e Code Update 2024-06-30 17:03:46 +03:00
Christer Warén
605366d786 Code Update 2024-06-30 16:57:14 +03:00
Christer Warén
e76cd7f45e Documentation Update 2024-06-30 16:56:57 +03:00
Christer Warén
570d533df3 Code Update 2024-06-29 20:21:16 +03:00
Christer Warén
cf58a86458 Maintainer Update 2024-06-29 20:18:46 +03:00
Christer Warén
7517d2d7c9 Code Update 2024-06-06 03:03:25 +03:00
Christer Warén
0ff81837f9 Replace separate scripts with Maintainer script 2024-06-06 03:03:11 +03:00
Christer Warén
ad4954d5ef Add Update script 2024-06-06 02:38:32 +03:00
Christer Warén
d9254aac36 Code Update 2024-06-05 23:24:34 +03:00
Christer Warén
352b7b2325 Fix Code 2024-06-01 15:59:50 +03:00
Christer Warén
9387e15062 Code Update 2024-06-01 14:47:24 +03:00
Christer Warén
d6344baf70 Code Update 2024-05-22 05:00:07 +03:00
Christer Warén
045fdd0ab0 Fix typo in Git repository 2024-05-18 02:20:13 +03:00
Christer Warén
a9e0a77f45 Code Update 2024-05-18 02:16:27 +03:00
Christer Warén
32e79f389c Code Update 2024-05-18 00:20:55 +03:00
Christer Warén
e1a31d2497 Code Update 2024-05-18 00:09:58 +03:00
Christer Warén
d9c370ad91 README.md Create 2024-05-01 03:23:05 +03:00
Christer Warén
85f2dd9acd Build Code 2024-05-01 03:22:32 +03:00
Christer Warén
9de267de43 Source Update 2024-05-01 03:20:46 +03:00
Christer Warén
19b4325920 Change Git repository 2024-04-12 02:14:45 +03:00
Christer Warén
7f6617e112 Update 2024-01-09 10:25:45 +02:00
Christer Warén
1656fbeaf6 Update copyright year 2024-01-03 08:26:16 +02:00
Christer Warén
14f13b8761 Reorder organization before hostname 2023-12-30 14:58:57 +02:00
Christer Warén
463588bb31 Disallow using local as username 2023-12-30 13:18:23 +02:00
19 changed files with 1037 additions and 130 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

16
README.md Normal file
View File

@@ -0,0 +1,16 @@
# Warén Group - Init.sh
## Linux - Run Command
```
bash <(https://waren.io/init.sh)
```
## Windows - Run Command
```
curl.exe https://waren.io/init.ps1 | powershell -
```
## Maintaining Command
```
./maintainer.sh
```

35
generator.php Normal file
View File

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

33
init.ps1 Normal file
View File

@@ -0,0 +1,33 @@
echo ""
echo ""
echo ""
echo "=============================="
echo ""
echo "Waren Init"
echo ""
echo "=============================="
echo ""
echo ""
echo "Generating SSH Key"
New-Item -ItemType Directory -Path "$Env:USERPROFILE\.ssh\keys" -Force
ssh-keygen -f "$Env:USERPROFILE\.ssh\keys\infra" -t ed25519 -C "$Env:USERDOMAIN"
echo ""
echo ""
echo "Copy SSH Key"
Get-Content "$Env:USERPROFILE\.ssh\keys\infra.pub"
echo ""
echo ""
echo "Install OpenSSH Server"
Add-WindowsCapability -Online -Name OpenSSH.Server
echo ""
echo ""
echo "Enable OpenSSH Server"
Set-Service -Name sshd -StartupType 'Automatic'
echo ""
echo ""
echo "Start OpenSSH Server"
Start-Service -Name sshd
echo ""
echo ""
echo "Install Debian"
wsl --install Debian

569
init.sh
View File

@@ -1,163 +1,474 @@
#!/bin/bash
if [ ! "$BASH_VERSION" ] ; then
exit 1
fi
#if [ ! "$BASH_VERSION" ] ; then
# bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
# exit 1
#fi
echo "
==============================
Warén Group
Init Script
------------------------------
"
stop () {
unset HVT
echo "
==============================
"
exit 1
declare -Ax args
declare -Ax config
declare -Ax messages
wxi-config(){
echo -n ""
}
mkdir -p ~/.ssh &> /dev/null
wx-init(){
wx-login
wxi-header "Init"
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 &> /dev/null
if [[ ! -z ${args['hostname']} && ${#args['hostname']} -gt 5 ]]
then
HOSTNAME="${args['hostname']}.devices.$DOMAIN"
elif [[ $(hostname -d) ]]
then
HOSTNAME=$(hostname --fqdn)
else
wxi-content status "Hostname" "Required"
wxi-stop
fi
HOSTNAME=$1
if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then
stop
fi
if [[ -z $USER || $USER == "root" ]]; then
if [[ -z $SUDO_USER ]]; then
if [[ -z LOGNAME ]]; then
echo -n ""
if [[ ! -z ${args['device-type']} ]]
then
if [[ ${args['device-type']} == "server" ]]
then
DEVICE_TYPE="Server"
elif [[ ${args['device-type']} == "workstation" ]]
then
DEVICE_TYPE="Workstation"
else
USER=$LOGNAME
wxi-content status "Device Type" "Invalid"
wxi-stop
fi
else
USER=$SUDO_USER
wxi-content status "Device Type" "Required"
wxi-stop
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
mkdir -p ~/.ssh/keys &> /dev/null
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"
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 xmltodict &> /dev/null
HOSTNAME="$HOSTNAME.$DEVICE_DOMAIN"
curl \
-H "X-Vault-Token: $TOKEN" \
-X GET \
https://$VAULT_DOMAIN/v1/init.sh/data/ssh -s | jq -r '.data.data.privkey' > ~/.ssh/keys/init
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"
chmod 700 ~/.ssh/keys/init &> /dev/null
if [[ -z $USER || $USER == "root" ]]; then
echo -n "Username: "
read USERNAME
mkdir -p ~/.ansible &> /dev/null
if [ ! -f ~/.ansible/vars.yml ]
then
echo "---" > ~/.ansible/vars.yml
echo "hostname: $HOSTNAME" >> ~/.ansible/vars.yml
echo "info:" >> ~/.ansible/vars.yml
echo " type: $DEVICE_TYPE" >> ~/.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.waren.io"
GIT_PORT="2222"
GIT_REPOSITORY="warengroup-private/infra-plus"
export HVT=$TOKEN
/opt/ansible/bin/ansible-galaxy collection install ansible.posix ansible.utils ansible.windows community.crypto community.dns community.docker community.general community.grafana community.hashi_vault community.libvirt community.mongodb community.mysql community.postgresql community.windows 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(){
wxi-header "Login"
wxi-restricted --user
wxi-restricted --org
wxi-restricted --vault
wxi-header "$ORG_HEADER" h3
if [[ ! -z ${args['auth-method']} ]]
then
AUTH_METHOD=${args['auth-method']}
elif [[ ! -z ${args['token']} ]]
then
AUTH_METHOD=token
elif [[ -f "$HOME/.warengroup/config.json" && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != 'null' && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != '' ]]
then
AUTH_METHOD=token
elif [[ ! -z ${args['username']} ]]
then
AUTH_METHOD=ldap
else
AUTH_METHOD=ldap
fi
if [[ ! -z $AUTH_METHOD ]]
then
case $AUTH_METHOD in
ldap)
echo -n "Username: "
if [[ ! -z ${args['username']} ]]
then
USERNAME=${args['username']}
wxi-content text "$USERNAME"
else
echo "Username: $USER"
USERNAME=$USER
read USERNAME
fi
echo -n "Password: "
read -s PASSWORD
echo "****************"
echo ""
if [[ ! -z ${args['password']} ]]
then
PASSWORD=${args['password']}
else
read -s PASSWORD
fi
if [[ ! -z $PASSWORD ]]
then
wxi-content text "****************"
else
wxi-content text ""
fi
if [[ -z $USERNAME || -z $PASSWORD ]]
then
wxi-content status "Username & Password" "Required"
wxi-footer
wxi-stop
fi
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
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
wxi-content status "Login" "Failed"
wxi-stop
fi
VAULT_TOKEN=$VAULT_LOGIN
export HVT="$VAULT_TOKEN"
echo -e "\n"
else
echo "$VAULT_DOMAIN - Login"
TOKEN=$VAULT_LOGIN
wxi-config login
;;
token)
echo -n "Token: "
read -s VAULT_TOKEN
echo "***********************************************************************************************"
echo ""
if [[ -z $VAULT_TOKEN || ${#VAULT_TOKEN} -lt 95 || ${#VAULT_TOKEN} -gt 95 ]]; then
stop
if [[ ! -z ${args['token']} ]]
then
if [[ ${args['token']} != "true" ]]
then
TOKEN=${args['token']}
fi
elif [[ -f "$HOME/.warengroup/config.json" && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != 'null' && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != '' ]]
then
TOKEN=$(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token)
else
read -s TOKEN
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
if [[ ! -z $TOKEN ]]
then
wxi-content text "***********************************************************************************************"
fi
VAULT_TOKEN=$VAULT_LOGIN
export HVT="$VAULT_TOKEN"
echo -e "\n"
if [[ -z $TOKEN ]]
then
wxi-content status "Token" "Required"
wxi-footer
wxi-stop
fi
if [[ ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
wxi-content status "Token" "Invalid"
wxi-footer
wxi-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew-self -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
wxi-content status "Login" "Failed"
wxi-stop
fi
TOKEN=$VAULT_LOGIN
wxi-config login
;;
*)
wxi-content status "Login Type" "Unsupported"
wxi-footer
wxi-stop
;;
esac
fi
VAULT_USERNAME=$(curl https://$VAULT_DOMAIN/v1/auth/token/lookup-self -X GET --header "X-Vault-Token: $TOKEN" -s | jq -r '.data.display_name')
if [[ -z $VAULT_USERNAME ]]
then
wxi-content status "Login" "Username Missing"
wxi-stop
elif [[ $VAULT_USERNAME != ldap* && $VAULT_USERNAME != oidc* ]]
then
wxi-content status "Login" "Authentication Method Invalid"
wxi-stop
elif [[ $VAULT_USERNAME == ldap* ]]
then
USERNAME=${VAULT_USERNAME#ldap-}
elif [[ $VAULT_USERNAME == oidc* ]]
then
USERNAME=${VAULT_USERNAME#oidc-}
fi
wxi-footer
}
wxi-restricted(){
if [[ -z $1 || $1 == "--user" ]]
then
if [[ $USER != "root" && $USER != "local" ]]
then
wxi-content status "Command" "Restricted"
wxi-content text "It's not permitted to execute this command as $USER."
wxi-footer
wxi-repeat "\n" 3
exit 1
fi
fi
fi
if [[ $1 == "--org" ]]
then
if [[ ! -z ${args['org']} ]]
then
case ${args['org']} in
warengroup)
ORG=warengroup
;;
cwchristerw)
ORG=cwchristerw
;;
*)
wxi-content status "Organization" "Unsupported"
wxi-footer
wxi-stop
;;
esac
elif [[ $(hostname -d) == "devices.waren.io" ]]
then
ORG=warengroup
elif [[ $(hostname -d) == "devices.christerwaren.fi" ]]
then
ORG=cwchristerw
fi
if [[ ! -z $ORG ]]
then
case $ORG in
warengroup)
DOMAIN=waren.io
VAULT_DOMAIN=vault.cwinfo.net
ORG_HEADER="Warén Group"
;;
cwchristerw)
DOMAIN=christerwaren.fi
VAULT_DOMAIN=vault.cwinfo.net
ORG_HEADER="Christer Warén"
;;
*)
wxi-content status "Organization" "Unsupported"
wxi-footer
wxi-stop
;;
esac
else
wxi-content status "Organization" "Required"
wxi-footer
wxi-stop
fi
fi
if [[ -z $1 || $1 == "--vault" ]]
then
if [[ -z $VAULT_DOMAIN ]]
then
wxi-content status "Vault" "Unavailable"
wxi-footer
wxi-repeat "\n" 3
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
wxi-content status "Vault" "Offline"
wxi-footer
wxi-repeat "\n" 3
exit 1
fi
fi
}
wxi-start(){
wxi-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
}
wxi-stop (){
rm -rf /.ssh/keys/init &> /dev/null
wxi-repeat "\n" 3
exit 1
}
wxi-content(){
if [[ $1 == "text" ]]
then
echo "$2"
elif [[ $1 == "status" ]]
then
wxi-repeat "\n" 2
echo -n "$wxiBold"
echo "Status"
echo -n "$wxiNormal"
echo "$2 - $3"
elif [[ $1 == "link" ]]
then
echo "$2 - $3"
fi
}
wxi-footer(){
echo ""
echo "------------------------------"
wxi-repeat " " $((30/2-12/2))
echo -n "$wxiBold"
echo "Warén Group™"
echo -n "$wxiNormal"
wxi-repeat " " $((30/2-17/2))
echo "https://waren.io"
echo "=============================="
}
wxiRed=$(tput setaf 196)
wxiGreen=$(tput setaf 46)
wxiYellow=$(tput setaf 226)
wxiBlue=$(tput setaf 21)
wxiPurple=$(tput setaf 165)
wxiTurquoise=$(tput setaf 14)
wxiPink=$(tput setaf 198)
wxiOrange=$(tput setaf 202)
wxiUnderline=$(tput smul)
wxiBold=$(tput bold)
wxiNormal=$(tput sgr0)
wxi-header(){
if [[ $2 == "h1" ]]
then
wxi-repeat "\n" 3
echo "=============================="
wxi-repeat " " $((30/2-${#1}/2))
echo -n "$wxiBold"
echo "$1"
echo -n "$wxiNormal"
echo "=============================="
fi
if [[ $2 == "h2" || -z $2 ]]
then
wxi-start
wxi-repeat " " $((30/2-6/2-${#1}/2))
echo -n "$wxiBold"
echo ">> $1 <<"
echo -n "$wxiNormal"
echo "------------------------------"
echo ""
fi
if [[ $2 == "h3" ]]
then
echo -n "$wxiBold"
echo "$1"
echo -n "$wxiNormal"
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)
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
/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
wxi-repeat() {
if [[ $1 == " " ]]
then
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
else
for i in $(seq 1 $2);
do
echo -en $1
done
fi
}
unset HVT
i=1
while [[ "$1" != "" ]]
do
case $1 in
--*)
key="${1%%=*}"
value="${1#*=}"
echo "
==============================
"
if [[ "$value" == "$key" ]]
then
shift
value="$1"
fi
if [[ -z $value ]]
then
value=true
fi
args["${key#--}"]="$value"
;;
-*)
key="${1%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]]
then
shift
value="$1"
fi
if [[ -z $value ]]
then
value=true
fi
args["${key#-}"]="$value"
;;
*)
args["$i"]="${1%%=*}"
i=$((i + 1))
;;
esac
shift
done
wx-init
wxi-stop

48
maintainer.sh Executable file
View File

@@ -0,0 +1,48 @@
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 -r ../wx/src/ui src
cp -r ../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

62
src/base.sh Normal file
View File

@@ -0,0 +1,62 @@
#!/bin/bash
#if [ ! "$BASH_VERSION" ] ; then
# bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
# exit 1
#fi
declare -Ax args
declare -Ax config
declare -Ax messages
{{ FUNCTIONS }}
{{ UI }}
i=1
while [[ "$1" != "" ]]
do
case $1 in
--*)
key="${1%%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]]
then
shift
value="$1"
fi
if [[ -z $value ]]
then
value=true
fi
args["${key#--}"]="$value"
;;
-*)
key="${1%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]]
then
shift
value="$1"
fi
if [[ -z $value ]]
then
value=true
fi
args["${key#-}"]="$value"
;;
*)
args["$i"]="${1%%=*}"
i=$((i + 1))
;;
esac
shift
done
wx-init
wxi-stop

3
src/functions/config.sh Normal file
View File

@@ -0,0 +1,3 @@
wxi-config(){
echo -n ""
}

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

@@ -0,0 +1,75 @@
wx-init(){
wx-login
wxi-header "Init"
if [[ ! -z ${args['hostname']} && ${#args['hostname']} -gt 5 ]]
then
HOSTNAME="${args['hostname']}.devices.$DOMAIN"
elif [[ $(hostname -d) ]]
then
HOSTNAME=$(hostname --fqdn)
else
wxi-content status "Hostname" "Required"
wxi-stop
fi
if [[ ! -z ${args['device-type']} ]]
then
if [[ ${args['device-type']} == "server" ]]
then
DEVICE_TYPE="Server"
elif [[ ${args['device-type']} == "workstation" ]]
then
DEVICE_TYPE="Workstation"
else
wxi-content status "Device Type" "Invalid"
wxi-stop
fi
else
wxi-content status "Device Type" "Required"
wxi-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 xmltodict &> /dev/null
curl \
-H "X-Vault-Token: $TOKEN" \
-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 "info:" >> ~/.ansible/vars.yml
echo " type: $DEVICE_TYPE" >> ~/.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.waren.io"
GIT_PORT="2222"
GIT_REPOSITORY="warengroup-private/infra-plus"
export HVT=$TOKEN
/opt/ansible/bin/ansible-galaxy collection install ansible.posix ansible.utils ansible.windows community.crypto community.dns community.docker community.general community.grafana community.hashi_vault community.libvirt community.mongodb community.mysql community.postgresql community.windows 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
}

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

@@ -0,0 +1,140 @@
wx-login(){
wxi-header "Login"
wxi-restricted --user
wxi-restricted --org
wxi-restricted --vault
wxi-header "$ORG_HEADER" h3
if [[ ! -z ${args['auth-method']} ]]
then
AUTH_METHOD=${args['auth-method']}
elif [[ ! -z ${args['token']} ]]
then
AUTH_METHOD=token
elif [[ -f "$HOME/.warengroup/config.json" && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != 'null' && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != '' ]]
then
AUTH_METHOD=token
elif [[ ! -z ${args['username']} ]]
then
AUTH_METHOD=ldap
else
AUTH_METHOD=ldap
fi
if [[ ! -z $AUTH_METHOD ]]
then
case $AUTH_METHOD in
ldap)
echo -n "Username: "
if [[ ! -z ${args['username']} ]]
then
USERNAME=${args['username']}
wxi-content text "$USERNAME"
else
read USERNAME
fi
echo -n "Password: "
if [[ ! -z ${args['password']} ]]
then
PASSWORD=${args['password']}
else
read -s PASSWORD
fi
if [[ ! -z $PASSWORD ]]
then
wxi-content text "****************"
else
wxi-content text ""
fi
if [[ -z $USERNAME || -z $PASSWORD ]]
then
wxi-content status "Username & Password" "Required"
wxi-footer
wxi-stop
fi
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
wxi-content status "Login" "Failed"
wxi-stop
fi
TOKEN=$VAULT_LOGIN
wxi-config login
;;
token)
echo -n "Token: "
if [[ ! -z ${args['token']} ]]
then
if [[ ${args['token']} != "true" ]]
then
TOKEN=${args['token']}
fi
elif [[ -f "$HOME/.warengroup/config.json" && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != 'null' && $(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token) != '' ]]
then
TOKEN=$(cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token)
else
read -s TOKEN
fi
if [[ ! -z $TOKEN ]]
then
wxi-content text "***********************************************************************************************"
fi
if [[ -z $TOKEN ]]
then
wxi-content status "Token" "Required"
wxi-footer
wxi-stop
fi
if [[ ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
wxi-content status "Token" "Invalid"
wxi-footer
wxi-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew-self -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
wxi-content status "Login" "Failed"
wxi-stop
fi
TOKEN=$VAULT_LOGIN
wxi-config login
;;
*)
wxi-content status "Login Type" "Unsupported"
wxi-footer
wxi-stop
;;
esac
fi
VAULT_USERNAME=$(curl https://$VAULT_DOMAIN/v1/auth/token/lookup-self -X GET --header "X-Vault-Token: $TOKEN" -s | jq -r '.data.display_name')
if [[ -z $VAULT_USERNAME ]]
then
wxi-content status "Login" "Username Missing"
wxi-stop
elif [[ $VAULT_USERNAME != ldap* && $VAULT_USERNAME != oidc* ]]
then
wxi-content status "Login" "Authentication Method Invalid"
wxi-stop
elif [[ $VAULT_USERNAME == ldap* ]]
then
USERNAME=${VAULT_USERNAME#ldap-}
elif [[ $VAULT_USERNAME == oidc* ]]
then
USERNAME=${VAULT_USERNAME#oidc-}
fi
wxi-footer
}

View File

@@ -0,0 +1,84 @@
wxi-restricted(){
if [[ -z $1 || $1 == "--user" ]]
then
if [[ $USER != "root" && $USER != "local" ]]
then
wxi-content status "Command" "Restricted"
wxi-content text "It's not permitted to execute this command as $USER."
wxi-footer
wxi-repeat "\n" 3
exit 1
fi
fi
if [[ $1 == "--org" ]]
then
if [[ ! -z ${args['org']} ]]
then
case ${args['org']} in
warengroup)
ORG=warengroup
;;
cwchristerw)
ORG=cwchristerw
;;
*)
wxi-content status "Organization" "Unsupported"
wxi-footer
wxi-stop
;;
esac
elif [[ $(hostname -d) == "devices.waren.io" ]]
then
ORG=warengroup
elif [[ $(hostname -d) == "devices.christerwaren.fi" ]]
then
ORG=cwchristerw
fi
if [[ ! -z $ORG ]]
then
case $ORG in
warengroup)
DOMAIN=waren.io
VAULT_DOMAIN=vault.cwinfo.net
ORG_HEADER="Warén Group"
;;
cwchristerw)
DOMAIN=christerwaren.fi
VAULT_DOMAIN=vault.cwinfo.net
ORG_HEADER="Christer Warén"
;;
*)
wxi-content status "Organization" "Unsupported"
wxi-footer
wxi-stop
;;
esac
else
wxi-content status "Organization" "Required"
wxi-footer
wxi-stop
fi
fi
if [[ -z $1 || $1 == "--vault" ]]
then
if [[ -z $VAULT_DOMAIN ]]
then
wxi-content status "Vault" "Unavailable"
wxi-footer
wxi-repeat "\n" 3
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
wxi-content status "Vault" "Offline"
wxi-footer
wxi-repeat "\n" 3
exit 1
fi
fi
}

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

@@ -0,0 +1,13 @@
wxi-start(){
wxi-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
}

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

@@ -0,0 +1,6 @@
wxi-stop (){
rm -rf /.ssh/keys/init &> /dev/null
wxi-repeat "\n" 3
exit 1
}

16
src/ui/content.sh Normal file
View File

@@ -0,0 +1,16 @@
wxi-content(){
if [[ $1 == "text" ]]
then
echo "$2"
elif [[ $1 == "status" ]]
then
wxi-repeat "\n" 2
echo -n "$wxiBold"
echo "Status"
echo -n "$wxiNormal"
echo "$2 - $3"
elif [[ $1 == "link" ]]
then
echo "$2 - $3"
fi
}

11
src/ui/footer.sh Normal file
View File

@@ -0,0 +1,11 @@
wxi-footer(){
echo ""
echo "------------------------------"
wxi-repeat " " $((30/2-12/2))
echo -n "$wxiBold"
echo "Warén Group™"
echo -n "$wxiNormal"
wxi-repeat " " $((30/2-17/2))
echo "https://waren.io"
echo "=============================="
}

11
src/ui/formatting.sh Normal file
View File

@@ -0,0 +1,11 @@
wxiRed=$(tput setaf 196)
wxiGreen=$(tput setaf 46)
wxiYellow=$(tput setaf 226)
wxiBlue=$(tput setaf 21)
wxiPurple=$(tput setaf 165)
wxiTurquoise=$(tput setaf 14)
wxiPink=$(tput setaf 198)
wxiOrange=$(tput setaf 202)
wxiUnderline=$(tput smul)
wxiBold=$(tput bold)
wxiNormal=$(tput sgr0)

30
src/ui/header.sh Normal file
View File

@@ -0,0 +1,30 @@
wxi-header(){
if [[ $2 == "h1" ]]
then
wxi-repeat "\n" 3
echo "=============================="
wxi-repeat " " $((30/2-${#1}/2))
echo -n "$wxiBold"
echo "$1"
echo -n "$wxiNormal"
echo "=============================="
fi
if [[ $2 == "h2" || -z $2 ]]
then
wxi-start
wxi-repeat " " $((30/2-6/2-${#1}/2))
echo -n "$wxiBold"
echo ">> $1 <<"
echo -n "$wxiNormal"
echo "------------------------------"
echo ""
fi
if [[ $2 == "h3" ]]
then
echo -n "$wxiBold"
echo "$1"
echo -n "$wxiNormal"
fi
}

0
src/ui/messages.sh Normal file
View File

13
src/ui/repeat.sh Normal file
View File

@@ -0,0 +1,13 @@
wxi-repeat() {
if [[ $1 == " " ]]
then
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
else
for i in $(seq 1 $2);
do
echo -en $1
done
fi
}