/
oc-flogin
executable file
·125 lines (115 loc) · 3.25 KB
/
oc-flogin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# Author: rbost@redhat.com (2020)
VERSION="0.0.2"
if [[ -n ${ZSH_VERSION} ]] && [[ ! -z ${ZSH_VERSION} ]]; then
INDEX_START=1
INDEX_OFFSET=0
elif [[ -n $BASH_VERSION ]]; then
INDEX_START=0
INDEX_OFFSET=1
else
echo " unknown"
fi
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
KUBE_PATH=''
INITIAL_OPTIONS="$@"
usage() {
echo -e "Login to OpenShift 4 cluster using installation assets directory.\n"
echo -e "Usage: oc flogin <INSTALL_PATH> [-h|-v|-D <depth>]"
echo -e " -v: Display the version"
echo -e " -h: Display this help"
echo -e " -D: Set the MAX_DEPTH used by the find command"
echo -e "Example:\n $ oc flogin ~/installations/ -D 4"
echo -e " $ oc flogin -v"
echo -e " $ oc flogin -h"
exit 1
}
if [ $# -eq 0 ];
then
echo -e "\n${RED}Error: Missing path.${NC}\n"
usage
else
while [ $# -gt 0 ]
do
case "${1}" in
-v)
echo $VERSION && exit 0
;;
-D)
MAX_DEPTH="${2}"
shift
;;
-D=?*)
MAX_DEPTH="${1#-D=}"
;;
-D?*)
MAX_DEPTH="${1#-D}"
;;
-h|--help)
usage
;;
*)
if [[ ! -z $KUBE_PATH ]]
then
echo "\n${RED}Error: Please specify only one path '${KUBE_PATH}' or '${1}'.${NC}\n"
usage
else
if [[ ! -d ${1} ]]
then
echo "\n${RED}Error: Invalid path '${1}'.${NC}\n"
usage
else
KUBE_PATH=$1
fi
fi
;;
esac
shift
done
fi
MAX_DEPTH=${MAX_DEPTH:-5}
# Create the Array of available KUBECONFIG
KUBECONFIG_LIST=($(find ${KUBE_PATH} -maxdepth ${MAX_DEPTH} -type f -name kubeconfig))
# Display choices & select desired kubeconfig
length=${#KUBECONFIG_LIST[@]}
if [[ ${length} == 1 ]]
then
KUBEINDEX=$INDEX_START
else
if [[ ${length} -gt 1 ]]
then
echo "Multiple kubeconfig files found."
if [[ -z $ZSH_VERSION ]]; then
for index in "${!KUBECONFIG_LIST[@]}"; do
printf '[%s] %s\n' "$index" "${KUBECONFIG_LIST[index]}"
done
echo
read -p "Please select the desired kubeconfig file: " KUBEINDEX
else
for index in $(seq 1 ${length}); do
printf '[%s] %s\n' "$index" "${KUBECONFIG_LIST[index]}"
done
echo
read "KUBEINDEX?Please select the desired kubeconfig file: "
fi
else
echo -e "${RED}No kubeconfig file found in given path: ${KUBE_PATH}${NC}"
exit 5
fi
fi
kubeconfig=${KUBECONFIG_LIST[KUBEINDEX]}
kubeconfig_path=$(dirname ${KUBECONFIG_LIST[KUBEINDEX]})
kubeadminpassword="${kubeconfig_path}/kubeadmin-password"
server=$(grep -m1 'server: ' $kubeconfig | awk '{print $2}')
if [[ -f ${kubeadminpassword} ]]
then
kpass=$(cat ${kubeadminpassword})
echo -e "${GREEN}Login with user/password from $kubeconfig${NC}"
oc login -u kubeadmin --insecure-skip-tls-verify=true -p ${kpass} ${server}
else
echo -e "${RED}No kubeadmin-password file found in given path: ${kuebconfig_path}${NC}"
echo -e "${GREEN}Please consider to Source the script by running the command: '. $0 ${INITIAL_OPTIONS}'${NC}"
echo -e "${GREEN}Or exporting the variable: 'export KUBECONFIG=${kubeconfig}'${NC}"
fi