Skip to content

Commit 3aa6b23

Browse files
committed
edit#005: v1.2.1 with two more commands (master + worker)
1 parent a0aa32b commit 3aa6b23

File tree

1 file changed

+94
-13
lines changed

1 file changed

+94
-13
lines changed

maestro

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ BOLD="\e[1m"
1313
ULINE="\e[4m"
1414
RESET="\e[0m"
1515

16-
VERSION="1.1.1"
16+
VERSION="1.2.1"
1717

1818
help() {
1919
cat << "EOF"
@@ -25,17 +25,19 @@ help() {
2525
2626
EOF
2727

28-
echo -e "${GREEN}Maestro${RESET} version ${BROWN}$VERSION${RESET} $(date +'%Y-%m-%d %H-%m-%S')"
28+
echo -e "${GREEN}Maestro${RESET} version ${BROWN}$VERSION${RESET} $(date +'%Y-%m-%d %H:%m:%S')"
2929
echo -e "\n${BROWN}Usage:${RESET}\n maestro [options] [arguments]"
3030
echo -e "\n${BROWN}Options:${RESET}"
3131
echo -e " ${GREEN}-h, --help${RESET}\t\t\t Display help for the given command. When no command is given display help for the ${GREEN}list${RESET} command"
3232
echo -e " ${GREEN}-c, --create-token${RESET}\t\t Create a new bearer token for the default kubernetes account"
3333
echo -e " ${GREEN}-r, --retrieve-token${RESET}\t\t Retrieve the existing bearer token for the default kubernetes account"
3434
echo -e " ${GREEN}-p, --permissions${RESET}\t\t Grant the necessary permissions for the default kubernetes user to access the REST API"
35-
echo -e " ${GREEN}-m, --master-node [name]${RESET}\t Create a k3s master node with a dynamic name passed through an argument"
35+
echo -e " ${GREEN}-m, --master-token${RESET}\t\t Retrieve the existing master token for the current node master"
3636
echo -e "\n${BROWN}Available commands:${RESET}"
3737
echo -e " ${GREEN}about${RESET}\t\t\t\t Shows a short information about Maestro"
3838
echo -e " ${GREEN}list${RESET}\t\t\t\t List all the commands and usefull information"
39+
echo -e " ${GREEN}master${RESET}\t\t\t Create a k3s master node with a given name"
40+
echo -e " ${GREEN}worker${RESET}\t\t\t Create a k3s worker node with a given name, master node address and token"
3941
}
4042

4143
about() {
@@ -52,8 +54,12 @@ fi
5254
create_token=false
5355
retrieve_token=false
5456
permissions=false
55-
master_node_flag=false
56-
master_node_name=""
57+
master_token=false
58+
master_flag=false
59+
worker_flag=false
60+
name="" # For both "master" and "worker" commands
61+
ip="" # For the "worker" command
62+
token="" # For the "worker" command
5763

5864
# Process options
5965
while [[ $# -gt 0 ]]; do
@@ -64,6 +70,12 @@ while [[ $# -gt 0 ]]; do
6470
'list')
6571
help
6672
;;
73+
'master')
74+
master_flag=true
75+
;;
76+
'worker')
77+
worker_flag=true
78+
;;
6779
--help | -h)
6880
help
6981
;;
@@ -76,10 +88,20 @@ while [[ $# -gt 0 ]]; do
7688
--permissions | -p)
7789
permissions=true
7890
;;
79-
--master-node | -m)
91+
--master-token | -m)
92+
master_token=true
93+
;;
94+
--name | -n)
95+
shift
96+
name=$1 # For both "master" and "worker" commands
97+
;;
98+
--ip | -i)
99+
shift
100+
ip=$1 # For the "worker" command
101+
;;
102+
--token | -t)
80103
shift
81-
master_node_flag=true
82-
master_node_name=$1
104+
token=$1 # For the "worker" command
83105
;;
84106
*)
85107
echo -e "\n ${RED_BG} ERROR ${RESET} The option \"$1\" does not exist. \n" >&2 && exit 1
@@ -88,27 +110,84 @@ while [[ $# -gt 0 ]]; do
88110
shift
89111
done
90112

91-
if $master_node_flag; then
92-
if [ ! -z "$master_node_name" ]; then
113+
# Creates a new master node: ./maestro master [-n | --name NAME]
114+
if $master_flag; then
115+
if [ ! -z "$name" ]; then
93116
if [ ! "$UID" -eq 0 ]; then
94117
echo -e "\n ${RED_BG} ERROR ${RESET} You must run this option with sudo.\n" >&2 ; exit 1
95118
else
96119
echo -ne "\n ${BLUE_BG} INFO ${RESET} We are creating a node master for you...\r" >&2
97120

98-
curl -sfL https://get.k3s.io | sh -s - --disable traefik --write-kubeconfig-mode 644 --node-name $master_node_name > /dev/null 2>&1
121+
curl -sfL https://get.k3s.io | sh -s - --disable traefik --write-kubeconfig-mode 644 --node-name $name > /dev/null 2>&1
99122
error_code=$?
100123

101124
if [ $error_code -ne 0 ]; then
102125
echo -ne " ${RED_BG} ERROR ${RESET} There was an error while creating the node master. (Code: $error_code) \r" >&2 ; echo -ne '\n\n' ; exit 1
103126
else
104-
echo -ne " ${BLUE_BG} INFO ${RESET} The node master '$master_node_name' was created with success.\r" >&2 ; echo -ne '\n\n'
127+
echo -ne " ${BLUE_BG} INFO ${RESET} The node master '$name' was created with success.\r" >&2 ; echo -ne '\n\n'
105128
fi
106129
fi
107130
else
108-
echo -e "\n ${RED_BG} ERROR ${RESET} You must provide a name to the master node. \n" >&2 ; exit 1
131+
echo -e "\n ${RED_BG} ERROR ${RESET} You must provide a name to the master node. \n" >&2
132+
echo -e " ${GREEN}master [-n|--name NAME]\n"
133+
exit 1
134+
fi
135+
fi
136+
137+
# Creates a new master node: ./maestro worker [-n | --name NAME] [-i | --ip IP ADDRESS] [-t | --token TOKEN]
138+
if $worker_flag; then
139+
if [ -z "$name" ]; then
140+
echo -e "\n ${RED_BG} ERROR ${RESET} You must provide a name to the worker node. \n" >&2
141+
echo -e " ${GREEN}worker [-n|--name NAME] [-i|--ip IP ADDRESS] [-t|--token TOKEN]\n"
142+
exit 1
143+
fi
144+
145+
if [ -z "$ip" ]; then
146+
echo -e "\n ${RED_BG} ERROR ${RESET} You must provide the IP address of the master node. \n" >&2
147+
echo -e " ${GREEN}worker [-n|--name NAME] [-i|--ip IP ADDRESS] [-t|--token TOKEN]\n"
148+
exit 1
149+
fi
150+
151+
if [ -z "$token" ]; then
152+
echo -e "\n ${RED_BG} ERROR ${RESET} You must provide the token of the master node. Use the \"-t\" option to retrieve it. \n" >&2
153+
echo -e " ${GREEN}worker [-n|--name NAME] [-i|--ip IP ADDRESS] [-t|--token TOKEN]\n"
154+
exit 1
155+
fi
156+
157+
if [ ! "$UID" -eq 0 ]; then
158+
echo -e "\n ${RED_BG} ERROR ${RESET} You must run this option with sudo.\n" >&2 ; exit 1
159+
fi
160+
161+
echo -ne "\n ${BLUE_BG} INFO ${RESET} We are creating a worker node for you...\r" >&2
162+
163+
curl -sfL https://get.k3s.io | K3S_NODE_NAME=$name K3S_URL=https://$ip:6443 K3S_TOKEN=$token sh - > /dev/null 2>&1
164+
error_code=$?
165+
166+
if [ $error_code -ne 0 ]; then
167+
echo -ne " ${RED_BG} ERROR ${RESET} There was an error while creating the worker node. (Code: $error_code) \r" >&2 ; echo -ne '\n\n' ; exit 1
168+
else
169+
echo -ne " ${BLUE_BG} INFO ${RESET} The worker node '$name' was created with success.\r" >&2 ; echo -ne '\n\n'
170+
fi
171+
fi
172+
173+
# Retrieves the master token: ./maestro [-t | --master_token]
174+
if $master_token; then
175+
if [ ! "$UID" -eq 0 ]; then
176+
echo -e "\n ${RED_BG} ERROR ${RESET} You must run this option with sudo.\n" >&2 ; exit 1
177+
else
178+
cat /var/lib/rancher/k3s/server/node-token > /dev/null 2>&1
179+
error_code=$?
180+
181+
if [ $error_code -ne 0 ]; then
182+
echo -ne "\n ${RED_BG} ERROR ${RESET} There was an error while retrieving the master token. (Code: $error_code)" >&2 ; echo -ne '\n\n' ; exit 1
183+
else
184+
MASTERTOKEN=$(cat /var/lib/rancher/k3s/server/node-token)
185+
echo -ne "\n ${BLUE_BG} INFO ${RESET} Master token: $MASTERTOKEN" >&2 ; echo -ne '\n\n'
186+
fi
109187
fi
110188
fi
111189

190+
# Creates a new master token: ./maestro [-c | --create_token]
112191
if $create_token; then
113192
kubectl > /dev/null 2>&1
114193
error_code=$?
@@ -153,6 +232,7 @@ EOF
153232
fi
154233
fi
155234

235+
# Retrieve the master token for API access: ./maestro [-r | --retrieve_token]
156236
if $retrieve_token; then
157237
kubectl get secret default-token -o jsonpath='{.data.token}' > /dev/null 2>&1
158238
error_code=$?
@@ -165,6 +245,7 @@ if $retrieve_token; then
165245
fi
166246
fi
167247

248+
# Grant permissions for API access: ./maestro [-p | --permissions]
168249
if $permissions; then
169250
kubectl create clusterrolebinding default-admin --clusterrole=cluster-admin --serviceaccount=default:default > /dev/null 2>&1
170251
error_code=$?

0 commit comments

Comments
 (0)