Skip to content

Commit

Permalink
Better UI for command execution; Fix some templates;
Browse files Browse the repository at this point in the history
  • Loading branch information
caruccio committed Oct 12, 2023
1 parent 94a5c8a commit eb473b4
Show file tree
Hide file tree
Showing 15 changed files with 482 additions and 79 deletions.
81 changes: 70 additions & 11 deletions root/etc/profile.d/getup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,13 @@ read_config()
{
local _name="${1}"
local prompt="${2}"

local _opt_name="opt_$_name"
local _secret=${_secret:-false}
local read_opts='-e'

if $_secret; then
read_opts+=' -s'
fi

if [ -v "$_opt_name" ]; then
local _opt_value="${!_opt_name}"
Expand All @@ -194,11 +199,13 @@ read_config()
else
local _default=""
fi
shift


if [ -n "$_default" ]; then
prompt+=" [$_default]"
if $_secret; then
prompt+=" [${_default//[^*]/*}]"
else
prompt+=" [$_default]"
fi
fi

if [ -n "$_opt_value" ]; then
Expand All @@ -209,7 +216,7 @@ read_config()
fi

line
read -e -p "$(prompt "$prompt")" $_name
read $read_opts -p "$(prompt "$prompt")" $_name

local _value="${!_name}"
if [ -z "$_value" ]; then
Expand All @@ -219,6 +226,29 @@ read_config()
fi
}

function get_tf_config()
{
local sh_var_name=$1
local tf_var_name=$2

if [ -v $sh_var_name ]; then
echo ${!sh_var_name}
return
elif [ -v TF_VAR_$tf_var_name ]; then
local v=TV_VAR_$tf_var_name
echo ${!v}
return
fi

case "$(hcl2json "$TF_VARS_FILE" | jq -Mrc ".${tf_var_name}|type")" in
string|number|object)
hcl2json "$TF_VARS_FILE" | jq -Mrc ".${tf_var_name}"
;;
array)
hcl2json "$TF_VARS_FILE" | jq -Mrc ".${tf_var_name}|join(\"\n\")"
esac
}

ask()
{
unset ask_response
Expand Down Expand Up @@ -266,21 +296,49 @@ ask_any()
# done
#}

function fill_line()
{
local cmd="$@"
local cmd_len=${#cmd}
local line_pre_fmt="------- [%s] "
local line_pre=$(printf -- "$line_pre_fmt" '')
local line_len=$[$(tput cols) - cmd_len - ${#line_pre}]
local line=$(printf -- '%*s' $line_len|tr ' ' -)

printf -- "${COLOR_GREEN}${COLOR_BOLD}$line_pre_fmt%s${COLOR_RESET}\n" "$cmd" "$line"
}

function execute_command_with_time_track()
{
local _print_cmd="${_print_cmd:-$@}"
local TIMEFORMAT="${COLOR_CYAN}Command [$_print_cmd] took ${COLOR_BOLD}%2lR${COLOR_RESET}"
time "$@"
}

function execute_command()
{
if [ $# -eq 0 ]; then
return
fi

local _print_cmd="${_print_cmd:-$@}"

fill_line "$_print_cmd"
_print_cmd="${_print_cmd}" execute_command_with_time_track $@
}

ask_execute_command()
{
local _default="${_default:-y}"
local _print_cmd="${_print_cmd:-$@}"

if [ "$_default" == "n" ]; then
local _sel="[y/N]"
else
local _sel="[Y/n]"
fi

if [ $BASH_VERSINFO -lt 5 ]; then
read -e -p "$(prompt COLOR_GREEN "Execute [${COLOR_BOLD}${@}${COLOR_RESET}${COLOR_GREEN}] now? $_sel")" res
else
read -e -p "$(prompt COLOR_GREEN "Execute [${COLOR_BOLD}${@@Q}${COLOR_RESET}${COLOR_GREEN}] now? $_sel")" res
fi
read -e -p "$(prompt COLOR_GREEN "Execute [${COLOR_BOLD}${_print_cmd}${COLOR_RESET}${COLOR_GREEN}] now? $_sel")" res

if [ "$_default" == "n" ]; then
res="${res:-n}"
Expand All @@ -289,7 +347,8 @@ ask_execute_command()
fi

case "${res,,}" in
y|yes|s|sim) "$@"
y|yes|s|sim)
_print_cmd="${_print_cmd}" execute_command "$@"
esac
}

Expand Down
7 changes: 3 additions & 4 deletions root/usr/local/bin/check-unused-manifests
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ except:
dir = "/cluster/manifests/cluster/"

unused = []
print("Detecting unused resources: ", end='', flush=True)
#print("Detecting unused resources: ", end='', flush=True)

def verify_dir(dir):
dir = os.path.abspath(dir)
Expand Down Expand Up @@ -51,6 +51,5 @@ if os.path.isdir(dir):
verify_dir(dir)

if unused:
print('\n', '\n'.join([ f'- {f}' for f in unused ]), sep='')
else:
print("none")
print(' '.join(unused))
sys.exit(2)
38 changes: 38 additions & 0 deletions root/usr/local/bin/get-var
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3

import os
import sys
import json
import json
import glob
from hcl2.parser import hcl2

def flatten(l):
return [item for sublist in l for item in sublist]

if len(sys.argv) > 1:
sources = sys.argv[1:]
else:
prefix = os.environ.get("CLUSTER_DIR", "./")
sources = [ os.path.join(prefix, "variable*.tf") ]


variables = []
for f in flatten([ glob.glob(i) for i in sources ]):
with open(f, 'r') as f:
data = hcl2.parse(f.read())
for v in data.get('variable', []):
variables.append(v)

variables = sorted(variables, key=lambda a: tuple(a.keys())[0])
print(json.dumps(variables))

def read_var(v):
if v['default'] != null:
return v
return v

for v in variables:
v = read_var(v)


104 changes: 66 additions & 38 deletions root/usr/local/bin/kubeconfig-get
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,63 @@ if [ -v KUBECONFIG ]; then
debug Using KUBECONFIG=$KUBECONFIG
fi

if [ $# -gt 0 ]; then
cluster_type=$1
while [ $# -gt 0 ]; do
case "$1" in
-n|--cluster-name)
shift
cluster_name=$1
;;
-t|--cluster-type)
shift
cluster_type=$1
;;
-f|--force)
read_config=":"
esac
shift
fi

if [ $# -gt 0 ]; then
cluster_name=$1
shift
fi
done

info Retrieving kubeconfig for $cluster_name/$cluster_type

case $cluster_type in
kind)
exec kind export kubeconfig --name $cluster_name "$@"
execute_command kind export kubeconfig --name $cluster_name "$@"
;;

eks)
exec aws eks update-kubeconfig --name $cluster_name "$@"
export AWS_ACCESS_KEY_ID=$(get_tf_config AWS_ACCESS_KEY_ID aws_access_key_id)
export AWS_SECRET_ACCESS_KEY=$(get_tf_config AWS_SECRET_ACCESS_KEY aws_secret_access_key)
export AWS_DEFAULT_REGION=$(get_tf_config AWS_DEFAULT_REGION region)

[ -n "$AWS_ACCESS_KEY_ID " ] || read_config AWS_ACCESS_KEY_ID 'AWS Access Key ID'
[ -n "$AWS_SECRET_ACCESS_KEY" ] || _secret=true read_config AWS_SECRET_ACCESS_KEY 'AWS Secret Access Key'
[ -n "$AWS_DEFAULT_REGION" ] || read_config AWS_DEFAULT_REGION 'AWS Default Region'

execute_command aws eks update-kubeconfig --name $cluster_name "$@"
;;

doks)
if ! [ -e ~/.config/doctl/config.yaml ]; then
doctl auth init
execute_command doctl auth init
fi
exec doctl kubernetes cluster kubeconfig save ${cluster_name}
execute_command doctl kubernetes cluster kubeconfig save ${cluster_name}
;;

gke)
export GCP_PROJECT=$(get_tf_config GCP_PROJECT project_id)
export GCE_ZONE=$(get_tf_config GCE_ZONE zones | head -1)

[ -n "$GCP_PROJECT" ] || read_config GCP_PROJECT "GCP Project"
[ -n "$GCE_ZONE" ] || read_config GCE_ZONE "GCE Zone"

if [ -e "$GOOGLE_APPLICATION_CREDENTIALS" ]; then
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS
read_config GCP_PROJECT "GCP Project"
gcloud config set project $GCP_PROJECT
read_config GCE_ZONE "GCE Zone"
gcloud container clusters get-credentials --zone $GCE_ZONE --project $GCP_PROJECT $cluster_name
else
read_config GCP_PROJECT "GCP Project"
gcloud config set project $GCP_PROJECT

read_config GCE_ZONE "GCE Zone"
gcloud container clusters get-credentials --zone $GCE_ZONE --project $GCP_PROJECT $cluster_name
execute_command gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS
else
warn "Missing service account file $GOOGLE_APPLICATION_CREDENTIALS (\$GOOGLE_APPLICATION_CREDENTIALS)"
fi

execute_command gcloud config set project $GCP_PROJECT
execute_command gcloud container clusters get-credentials --zone $GCE_ZONE --project $GCP_PROJECT $cluster_name
;;

oke)
Expand Down Expand Up @@ -82,7 +97,7 @@ case $cluster_type in
oci ce cluster list --compartment-id $OCI_COMPARTMENT_ID --all --output table --query 'data[].name'
done
OCI_CLUSTER_ID=$(oci ce cluster list --name $OCI_CLUSTER_NAME --compartment-id $OCI_COMPARTMENT_ID | jq -r '.data[0].id')
oci ce cluster create-kubeconfig --cluster-id $OCI_CLUSTER_ID \
execute_command oci ce cluster create-kubeconfig --cluster-id $OCI_CLUSTER_ID \
--file $CLUSTER_DIR/.kube/config --region $OCI_CLI_REGION \
--token-version 2.0.0
;;
Expand All @@ -98,20 +113,33 @@ case $cluster_type in
;;

aks)
export ARM_CLIENT_ID=$(sed -ne 's/^client_id.*"\([^"]\+\)"/\1/p' /cluster/terraform.tfvars)
export ARM_CLIENT_SECRET=$(sed -ne 's/^client_secret.*"\([^"]\+\)"/\1/p' /cluster/terraform.tfvars)
export ARM_TENANT_ID=$(sed -ne 's/^tenant_id.*"\([^"]\+\)"/\1/p' /cluster/terraform.tfvars)
export ARM_SUBSCRIPTION_ID=$(sed -ne 's/^subscription_id.*"\([^"]\+\)"/\1/p' /cluster/terraform.tfvars)
export AKS_RESOURCE_GROUP_NAME=$(sed -ne 's/^resource_group_name.*"\([^"]\+\)"/\1/p' /cluster/terraform.tfvars)

read_config ARM_CLIENT_ID "ARM_CLIENT_ID"
read_config ARM_CLIENT_SECRET "ARM_CLIENT_SECRET"
read_config ARM_TENANT_ID "ARM_TENANT_ID"
read_config ARM_SUBSCRIPTION_ID "AKS Subscription ID"
read_config AKS_RESOURCE_GROUP_NAME "AKS Resource Group Name"

ask_execute_command az login --service-principal -u $ARM_CLIENT_ID -p $ARM_CLIENT_SECRET -t $ARM_TENANT_ID
az aks get-credentials --admin --name $cluster_name --resource-group $AKS_RESOURCE_GROUP_NAME --subscription $ARM_SUBSCRIPTION_ID
export ARM_CLIENT_ID=$(get_tf_config ARM_CLIENT_DIR client_id)
export ARM_CLIENT_SECRET=$(get_tf_config ARM_CLIENT_SECRET client_secret)
export ARM_TENANT_ID=$(get_tf_config ARM_TENANT_ID tenant_id)
export ARM_SUBSCRIPTION_ID=$(get_tf_config ARM_SUBSCRIPTION_ID subscription_id)
export AKS_RESOURCE_GROUP_NAME=$(get_tf_config AKS_RESOURCE_GROUP_NAME resource_group_name)

[ -n "$ARM_CLIENT_ID" ] || read_config ARM_CLIENT_ID 'ARM Client ID'
[ -n "$ARM_CLIENT_SECRET" ] || _secret=true read_config ARM_CLIENT_SECRET 'ARM Client Secret'
[ -n "$ARM_TENANT_ID" ] || read_config ARM_TENANT_ID 'ARM Tenant ID'
[ -n "$ARM_SUBSCRIPTION_ID" ] || read_config ARM_SUBSCRIPTION_ID 'AKS Subscription ID'
[ -n "$AKS_RESOURCE_GROUP_NAME" ] || read_config AKS_RESOURCE_GROUP_NAME 'AKS Resource Group Name'

_print_cmd="az login --service-principal -u $ARM_CLIENT_ID -p <SECRET> -t $ARM_TENANT_ID" \
execute_command \
az login --service-principal \
-u $ARM_CLIENT_ID \
-p $ARM_CLIENT_SECRET \
-t $ARM_TENANT_ID \
|| exit

execute_command \
az aks get-credentials \
--admin \
--name $cluster_name \
--resource-group $AKS_RESOURCE_GROUP_NAME \
--subscription $ARM_SUBSCRIPTION_ID \
--overwrite-existing
;;

*)
Expand Down
43 changes: 43 additions & 0 deletions root/usr/local/bin/kubespray-copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

set -eu

source /etc/profile.d/getup.sh

user=$(get_tf_config SSH_USER ssh_user)

if [ -z "$user" ]; then
user=centos
fi

hosts=all
remote_src=false

src=$1
dest=$2
shift 2

src_host=${src%%:*}
dest_host=${dest%%:*}
src=${src#*:}
dest=${dest#*:}

if [ -n "$src_host" -a -n "$dest_host" ] || [ -z "$src_host$dest_host" ]; then
echo "Usage:"
echo " Upload: $0 local-file hosts:remote-file"
echo " Download: $0 host:remote-file local-file"
exit 1
fi

if [ -n "$src_host" ]; then
# downloading
hosts=$src_host
remote_src=true
elif [ -n "$dest_host" ]; then
# uploading
hosts=$dest_host
fi

echo "See available flags in https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html" >&2

execute_command ansible $hosts -i $INVENTORY_FILE --become --user $user -m copy -a "src=$src dest=$dest remote_src=$remote_src $*"
Loading

0 comments on commit eb473b4

Please sign in to comment.