Init: Update

This commit is contained in:
Christer Waren
2026-09-17 14:29:36 +03:00
parent 2887ef5564
commit b36f783554
2 changed files with 177 additions and 57 deletions
+172 -56
View File
@@ -1,16 +1,156 @@
#!/bin/bash #!/bin/bash
if [ ! "$BASH_VERSION" ] ; then
echo "Warén Group - Init"
echo "This command requires using BASH shell."
exit 1
fi
#if [ ! "$BASH_VERSION" ] ; then if [[ -L "$0" ]]
# bash $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 then
# exit 1 PWD=$(dirname -- "$(readlink -f -- "$0")")
#fi fi
declare -Ax args wxi-args(){
declare -Ax config declare -gAx args
declare -Ax messages args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--*=*)
key="${1%%=*}"
value="${1#*=}"
args["${key#--}"]="$value"
shift
;;
--*)
key="$1"
opt="${key#--}"
shift
if [[ $# -gt 0 && "${1}" != -* ]]; then
args["$opt"]="$1"
shift
else
args["$opt"]=true
fi
;;
-*=*)
key="${1%%=*}"
value="${1#*=}"
args["${key#-}"]="$value"
shift
;;
-*)
key="$1"
opt="${key#-}"
shift
if [[ $# -gt 0 && "${1}" != -* ]]; then
args["$opt"]="$1"
shift
else
args["$opt"]=true
fi
;;
*)
args["$((++pos))"]="${1%%=*}"
shift
;;
esac
done
}
wxi-config(){ wxi-config(){
declare -gAx config
case $1 in
create)
if [[ ! -f $HOME/.warengroup/config.json ]]
then
echo '{}' > $HOME/.warengroup/config.json
fi
;;
deploy)
if [[ ! $(jq -e . $HOME/.warengroup/config.json.tmp >/dev/null 2>&1) ]]
then
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
fi
;;
write)
jq '.'"$2"' = "'$3'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
wxi-config deploy
;;
load)
while IFS=$'\t' read -r k v; do config["$k"]="$v"; done < <(
jq -r '
paths(scalars) as $p
| "\( $p|map(tostring)|join(".") )\t\( getpath($p) )"
' $HOME/.warengroup/config.json
)
;;
reload)
wxi-config load
;;
*)
WX_HOME=$HOME/.warengroup
export WX_HOME
mkdir -p $WX_HOME
chmod 700 -R "$WX_HOME"
mkdir -p "$WX_HOME/credentials" \
"$WX_HOME/credentials/ssh" \
"$WX_HOME/credentials/ssh/eas" \
"$WX_HOME/credentials/ansible-vault/eas" \
"$PWD/eas/credentials/ssh"
find "$WX_HOME" -type d -exec chmod 700 {} +
find "$WX_HOME" -type f -exec chmod 600 {} +
wxi-config create
if [[ -n ${args['proxy']} ]]
then
wxi-config write proxy "${args['proxy']}"
fi
wxi-config load
PROXY_ARGS=()
if [[ -n "${config['proxy']}" ]]; then
PROXY_ARGS=(--proxy "${config['proxy']}")
fi
;;
esac
}
wxi-config-legacy(){
case $1 in
login)
case $2 in
write)
jq '.login.'$ORG'.token = "'$TOKEN'"' $HOME/.warengroup/config.json &> $HOME/.warengroup/config.json.tmp
;;
read)
if [[ -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
cat $HOME/.warengroup/config.json | jq -r .login.$ORG.token
else
return 1
fi
;;
erase)
TOKEN=""
wxi-config login write
;;
esac
;;
*)
echo -n "" echo -n ""
;;
esac
mv $HOME/.warengroup/config.json.tmp $HOME/.warengroup/config.json &> /dev/null
} }
wx-init(){ wx-init(){
@@ -103,7 +243,7 @@ wx-login(){
elif [[ ! -z ${args['token']} ]] elif [[ ! -z ${args['token']} ]]
then then
AUTH_METHOD=token AUTH_METHOD=token
elif [[ $(wxi-config login read) ]] elif [[ $(wxi-config-legacy login read) ]]
then then
AUTH_METHOD=token AUTH_METHOD=token
elif [[ ! -z ${args['username']} ]] elif [[ ! -z ${args['username']} ]]
@@ -157,7 +297,7 @@ wx-login(){
fi fi
TOKEN=$VAULT_LOGIN TOKEN=$VAULT_LOGIN
wxi-config login write wxi-config-legacy login write
;; ;;
token) token)
echo -n "Token: " echo -n "Token: "
@@ -167,9 +307,9 @@ wx-login(){
then then
TOKEN=${args['token']} TOKEN=${args['token']}
fi fi
elif [[ $(wxi-config login read) ]] elif [[ $(wxi-config-legacy login read) ]]
then then
TOKEN=$(wxi-config login read) TOKEN=$(wxi-config-legacy login read)
else else
read -s TOKEN read -s TOKEN
fi fi
@@ -199,7 +339,7 @@ wx-login(){
wxi-content status "Login" "Failed" wxi-content status "Login" "Failed"
wxi-footer wxi-footer
if [[ $(wxi-config login read) ]] if [[ $(wxi-config-legacy login read) ]]
then then
wx-logout &> /dev/null wx-logout &> /dev/null
wx-login wx-login
@@ -209,7 +349,7 @@ wx-login(){
fi fi
TOKEN=$VAULT_LOGIN TOKEN=$VAULT_LOGIN
wxi-config login write wxi-config-legacy login write
;; ;;
*) *)
wxi-content status "Login Type" "Unsupported" wxi-content status "Login Type" "Unsupported"
@@ -432,52 +572,28 @@ wxi-repeat() {
fi fi
} }
#{{ COMMANDS }}
i=1 wxi-args "$@"
while [[ "$1" != "" ]] wxi-config
do
case $1 in
--*)
key="${1%%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]] echo -n $wxiBold$wxiRed
then cat <<'EOF'
shift
value="$1"
fi
if [[ -z $value ]] (`\ .-') /` ('-. _ .-') ('-. .-') _
then `.( OO ),' ( OO ).-.( \( -O ) _( OO) ( OO ) )
value=true ,--./ .--. / . --. / ,------. (,------.,--./ ,--,'
fi | | | | \-. \ | /`. ' | .---'| \ | |\
| | | |,.-'-' | | | / | | | | | \| | )
| |.'.| |_)\| |_.' | | |_.' |(| '--. | . |/
| | | .-. | | . '.' | .--' | |\ |
| ,'. | | | | | | |\ \ | `---.| | \ |
'--' '--' `--' `--' `--' '--' `------'`--' `--'
args["${key#--}"]="$value" EOF
;; echo -n $wxiNormal
-*)
key="${1%=*}"
value="${1#*=}"
if [[ "$value" == "$key" ]] wxi-start
then wxi-init
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 wxi-stop
Executable
+4
View File
@@ -0,0 +1,4 @@
rm init.ps1
cp ../infra/init.ps1 init.ps1
rm init.sh
cp ../infra/init.sh init.sh