-
Notifications
You must be signed in to change notification settings - Fork 2
/
lxcx
executable file
·220 lines (177 loc) · 5.76 KB
/
lxcx
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
#
# Provides: lxcx
# Short-Description: functions over lxc to manipulate containers
# Description: functions over lxc to manipulate containers
######################## GLOBAL FUNCTIONS
#S_TRACE=debug
S_GLOBAL_FUNCTIONS="${S_GLOBAL_FUNCTIONS:-/usr/local/bs/inc-functions}"
! . "${S_GLOBAL_FUNCTIONS}" && echo -e "[error] - Unable to source file '${S_GLOBAL_FUNCTIONS}' from '${BASH_SOURCE[0]}'" && exit 1
! . "${S_PATH_SCRIPT}/inc-lxc" && echo -e "[error] - Unable to source file '${S_PATH_SCRIPT}/inc-lxc' from '${BASH_SOURCE[0]}'" && exit 1
######################## VARIABLES
USAGE="lxcx: manage containers with following commands
delete list publish purge restart start stop
Global usage: lxcx <args> [action] <containers>
[containers] is partial name of containers (allow regexp)
args:
-r, --running selects from running containers
-s, --stopped selects from stopped containers
-a,--all select all containers for action
-f, --force force certain actions (delete publish)
-h, --help show usage
list, specific arguments:
-1 show containers names in one column (default in one line)
"
######################## FUNCTION
# @ containers
__delete() {
_echod "${FUNCNAME}::${LINENO} IN \$@="$@
local cts_stopped= ct= cts_force=
cts_stopped=" $(echo ${CTS_STOPPED}) "
for ct in $@; do
if [ "${cts_stopped/ ${ct} /}" != "${cts_stopped}" ]; then
_eval lxc delete ${ct}
elif [[ "${FORCE}" && "${cts_stopped/ ${ct} /}" = "${cts_stopped}" ]]; then
_eval lxc delete --force ${ct}
else
cts_force+=" ${ct}"
fi
done
[ "${cts_force}" ] && _echoE "Use 'force' argument to delete this running containers:\n${cts_force# }"
}
# 1 containers
__list() {
_echod "${FUNCNAME}::${LINENO} IN \$@="$@
[ "${COLUMN}" ] && _echo "$*" || _echo $*
}
# @ containers
__publish() {
_echod "${FUNCNAME}::${LINENO} IN \$@="$@
local cts_stopped= ct= cts_force= cmd=
cts_stopped=" $(echo ${CTS_STOPPED}) "
for ct in $@; do
cmd=
if [ "${cts_stopped/ ${ct} /}" != "${cts_stopped}" ]; then
cmd=" lxc publish ${ct} --alias ${ct}"
elif [[ "${FORCE}" && "${cts_stopped/ ${ct} /}" = "${cts_stopped}" ]]; then
cmd="lxc publish --force ${ct} --alias ${ct}"
else
cts_force+=" ${ct}"
fi
if [ "${cmd}" ]; then
lxc image list -f csv -c l | grep -q ^${ct}$ && _eval lxc image alias rename ${ct} ${ct}-${_SDATE}
_echod "${FUNCNAME}::${LINENO} cmd=${cmd}"
_eval ${cmd}
fi
done
[ "${cts_force}" ] && _echoE "Use 'force' argument to publish this running containers:\n${cts_force# }"
}
# @ containers
__purge() {
__delete $@
}
# 1 containers
__restart() {
_echod "${FUNCNAME}::${LINENO} IN \$@="$@
_eval lxc restart $1
}
# 1 containers
__start() {
_echod "${FUNCNAME}::${LINENO} IN \$@="$@
_eval lxc start $1
}
# 1 containers
__stop() {
_echod "${FUNCNAME}::${LINENO} IN \$@="$@
_eval lxc stop $1
}
# 1 selected containers
# 2 containers
__select_reduce() {
_echod "${FUNCNAME}::${LINENO} \$1='"$1"'"
_echod "${FUNCNAME}::${LINENO} \$2='"$2"'"
local cts ct
CTS=
cts=`echo $2|xargs`
for ct in $1; do
[[ " ${cts} " = *" ${ct} "* ]] && CTS+="${ct}
"
done
}
# 1 container names to select
__select() {
_echod "${FUNCNAME}::${LINENO} \$1='$1'"
[[ " delete publish restart start stop " = *" ${ACTION} "* && -z "${ALL}" && -z "${CTS}" ]] && _exite "You have to give a selection of containers"
if [ "${ACTION}" = list ]; then
if [ "$1" ]; then
CTS=`lxc list --format=json | jq -r '.[] | select(.name | test("'$1'")).name'`
else
CTS=`lxc list -f csv -c n`
fi
elif [ "${ACTION}" = purge ]; then
CTS=`lxc list --format=json | jq -r '.[] | select(.name | test("-1635.*$")).name'`
else
if [ "${ALL}" ]; then
CTS=`lxc list -f csv -c n`
elif [ "$1" ];then
CTS=`lxc list --format=json | jq -r '.[] | select(.name | test("'$1'")).name'`
fi
fi
case "${ACTION}" in
start)
__select_reduce "${CTS}" "${CTS_STOPPED}";;
stop|restart)
__select_reduce "${CTS}" "${CTS_RUNNING}";;
esac
_echod "${FUNCNAME}::${LINENO} unique CTS="${CTS}
[ -z "${CTS}" ] && _echoI "No containers for your selection" && exit
}
__opts() {
_echod "${FUNCNAME}::${LINENO} IN \$@=$@"
local opts_given="$@"
local opts_short="afrsh1"
local opts_long="all,force,running,stopped,help"
local opts=$(getopt -o ${opts_short} -l ${opts_long} -n "${0##*/}" -- "$@") || _exite "Wrong or missing options"
eval set -- "${opts}" || exit 1
_echod "${FUNCNAME}::${LINENO} opts_given=${opts_given} opts=${opts}"
while [ "$1" != "--" ]
do
case "$1" in
-a|--all) ALL=a ;;
-f|--force) FORCE=f ;;
-r|--running) RUNNING=r ;;
-s|--stopped) STOPPED=s ;;
-h|--help) echo "${USAGE}" && _exit 0 ;;
-1) COLUMN=1 ;;
*) _exite "Wrong argument: '$1' for arguments '$opts_given'" ;;
esac
shift
done
shift
ACTION="$1"
shift
CTS="$@"
_echod "${FUNCNAME}::${LINENO} ALL='${ALL}' FORCE='${FORCE}'"
_echod "${FUNCNAME}::${LINENO} ACTION='${ACTION}' \$@='$@'"
}
__main()
{ _echod "======================================================"
_echod "$(ps -o args= ${PPID})"
local ACTION= ALL= FORCE= RUNNING= STOPPED= CTS=
local ACTIONS="delete list publish purge restart start stop"
local CTS_RUNNING=`lxc list -f csv -c n status=Running`
local CTS_STOPPED=`lxc list -f csv -c n status=Stopped`
_echod "${FUNCNAME}::${LINENO} CTS_RUNNING="${CTS_RUNNING}
_echod "${FUNCNAME}::${LINENO} CTS_STOPPED="${CTS_STOPPED}
type jq >/dev/null 2>&1 || _exite "Unable to find the binary: jq"
# get options
__opts "$@"
[ -z "${ACTION}" ] && _exite "You have to give an action to execute" # no action
[[ " ${ACTIONS} " == *" ${ACTION} "* ]] || _exite "Wrong action: '${ACTION}'. select in: ${ACTIONS}"
__select "${CTS}"
# call action with arguments
__${ACTION} "${CTS}"
}
######################## MAIN
__main "$@"
_exit 0