-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdm.sh
executable file
·144 lines (127 loc) · 3.78 KB
/
cdm.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
#set -x
ENV_SETTINGS_FILE=${HOME}/.chimera_env
function banner {
echo "#####################################"
echo " Welcome to CDM! "
echo " "
echo " A simple utility to switch between "
echo " docker daemons in a swarm cluster. "
echo "#####################################"
echo
}
function version {
echo "Version: 0.1"
}
function help {
# help
banner
version
echo "---------------------------------------------------------------------"
echo "Help:"
echo " help, -h - print this help text"
echo " version, -v - print the version"
echo " list, ls - list all the swarm nodes"
echo " connect, co - switch swarm node"
echo " options:"
echo " -l - to local, "
echo " -sm - to swarm master, "
echo " -n - to node 'm' or <number>"
echo "---------------------------------------------------------------------"
}
function ctl_dm_helper {
# commands switch
if [ -z "$1" ] || [ "$1" == "help" ] || [ "$1" == "-h" ]; then
help
exit
elif [ "$1" == "version" ] || [ "$1" == "-v" ]; then
version
exit
elif [ "$1" == "connect" ] || [ "$1" == "co" ]; then
connect $1 $2 $3
exit
elif [ "$1" == "list" ] || [ "$1" == "ls" ]; then
list
exit
else
echo "Invalid command. Use 'cdm help' for usage."
fi
}
function connect {
# valid params: local, master, slave <no>
if [ -z "$2" ] || ([ "$2" != "-l" ] && [ "$2" != "-sm" ] && [ "$2" != "-n" ]) ; then
echo "Options: '-l', '-sm' or '-n'"
exit
else
if [ "$2" == "-sm" ]; then
echo "Switching to docker daemon on master '$SWARM_MASTER' ..."
eval "$(docker-machine env --swarm $SWARM_MASTER)"
elif [ "$2" == "-n" ]; then
if [ "$3" == "m" ]; then
echo "Switching to docker daemon on node '$SWARM_MASTER' ..."
eval "$(docker-machine env $SWARM_MASTER)"
elif [ -z "$3" ] || ([ "$3" != "m" ] && ([ "$3" -gt "$NODE_COUNT" ] || [ "$3" -le 0 ])); then
echo "Invalid node number, switching to docker daemon on node '$SWARM_MASTER' ..."
eval "$(docker-machine env $SWARM_MASTER)"
else
echo "Switching to node '$SWARM_PREFIX-$3' ..."
eval "$(docker-machine env $SWARM_PREFIX-$3)"
fi
elif [ "$2" == "-l" ]; then
echo "Switching to local..."
eval "$(docker-machine env -u)"
fi
# show a warning message to exit the subshell
msg
# this hack sets the envs in a new shell
exec $SHELL -i
fi
}
function list {
# list all swarm nodes
echo "Listing all swarm nodes..."
echo
docker-machine ls
}
function msg {
# if [ "$SHLVL" -gt 2 ]; then
echo
echo "Sub-Shell Level: $SHLVL created."
echo "** Please exit the current Sub-Shell $SHLVL before running the next 'connect' command. **"
echo
# fi
}
# function set_prompt {
# # reset prompt
# if [ "$ORIG_PS" != "" ]; then
# reset_prompt
# fi
# local prompt=$1
# # set_ev ORIG_PS "$PROMPT"
# echo "export ORIG_PS=\"$PROMPT\"" >> "${HOME}/.zshrc"
# # set_ev PROMPT "$ORIG_PS [$prompt]"
# echo "export PROMPT=\"$ORIG_PS [$prompt]\"" >> "${HOME}/.zshrc"
# source "${HOME}/.zshrc"
# }
# function reset_prompt {
# # set_ev PROMPT $ORIG_PS
# echo "export PROMPT=\"$ORIG_PS\"" >> "${HOME}/.zshrc"
# source "${HOME}/.zshrc"
# }
# function set_ev {
# local evn=$1
# echo $"`sed "/$evn=/d" "$ENV_SETTINGS_FILE"`" > "$ENV_SETTINGS_FILE"
# echo "export $1=\"$2\"" >> "$ENV_SETTINGS_FILE"
# export $1="$2"
# source $ENV_SETTINGS_FILE
# }
########################
# set envs
if [ -f "$ENV_SETTINGS_FILE" ]; then
source $ENV_SETTINGS_FILE
else
echo "Chimera environment settings are not set."
exit
fi
# call the function
ctl_dm_helper $1 $2 $3