_salt
13420 bytes
1#compdef salt salt-call salt-cp salt-run salt-key
2# The use-cache style is checked in a few places to allow caching minions, modules,
3# or the directory salt is installed in.
4# you can cache those three with:
5# zstyle ':completion:*:salt(|-cp|-call|-run|-key):*' use-cache true
6# and/or selectively:
7# zstyle ':completion::complete:salt-key:set-option-a-1:' use-cache false
8# zstyle ':completion::complete:salt(|-cp|-call):minions:' use-cache true
9# zstyle ':completion::complete:salt(|-call):modules:' use-cache true
10# zstyle ':completion::complete:salt(|-cp|-call|-run|-key):salt_dir:' use-cache true
11#
12# cache validation can be controlled with the style cache-ttl.
13# it expects two arguments: number (days|hours|weeks|months)
14# to invalidate the minion cache after four days:
15# zstyle ':completion::complete:salt(|-cp|-call):minions:' cache-ttl 4 days
16
17
18local state line curcontext="$curcontext" salt_dir
19
20_modules(){
21 local _funcs expl curcontext=${curcontext%:*}:modules
22
23 if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
24 zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
25 fi
26
27 if _cache_invalid salt/modules || ! _retrieve_cache salt/modules; then
28 _funcs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --log-level error --out txt sys.list_functions)"}%%[],]##}#\[}:#local:} )
29 _store_cache salt/modules _funcs
30 fi
31
32 _wanted modules expl modules _multi_parts "$@" . _funcs
33}
34
35_runners(){
36 local _runs expl curcontext=${curcontext%:*}:runners
37
38 if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
39 zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
40 fi
41
42 if _cache_invalid salt/runners || ! _retrieve_cache salt/runners; then
43 _runs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --log-level error --out txt sys.list_runner_functions)"}%%[],]##}#\[}:#local:} )
44 _store_cache salt/runners _runs
45 fi
46
47 _wanted modules expl runners _multi_parts "$@" . _runs
48}
49
50_minions(){
51 local type requested_type include_all key expl; typeset -A _peons
52
53 # when completing the minion argument for salt and salt-cp, set the argument section
54 # of the context to `minion' not `argument-1'
55 if [[ $service = salt(|-cp) ]]; then
56 curcontext=${curcontext%:*}:minions
57 fi
58
59 # only pass the argument accepted, unaccepted, rejected, denied or all to -t/-T
60 # the argument is used as part of an tag, accepted-minions, rejected-minions, etc.
61 # while un, acc, den, etc will work, you will possibly ignore user customized tags.
62 zparseopts -D -E 't+:=requested_type' 'T+:=include_all'
63
64 if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
65 zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
66 fi
67
68 if _cache_invalid salt/minions || ! _retrieve_cache salt/minions; then
69 # it would be awesome if salt-key could prefix or suffix a word to denote
70 # the key's state. It would remove the need for this loop, calling salt-key N times.
71 for type in accepted unaccepted rejected denied; do
72 salt-key -l $type 2>/dev/null | while read -r key; do
73 [[ $key == *' Keys:' ]] && continue
74 _peons+=( "$key" $type )
75 done
76 done
77 _store_cache salt/minions _peons
78 fi
79
80 # if salt-key's --include-all option isn't on the line, ignore the -T options
81 (( words[(I)--include-all] )) || unset include_all
82
83 if (( requested_type[(I)all] )); then
84 requested_type=( -t accepted -t unaccepted -t rejected -t denied )
85 unset include_all
86 fi
87
88 for type in ${${requested_type:#-t}:-accepted} ${include_all:#-T}; do
89 _wanted $type-minions expl minion compadd "$@" -M 'r:|.=* r:|=*' ${(k)_peons[(R)$~type]}
90 done
91}
92
93(( $+functions[_salt_caching_policy] )) ||
94_salt_caching_policy() {
95 local oldp ttl d t
96 zstyle -a ":completion:$curcontext:" cache-ttl ttl
97
98 if (( $#ttl >= 2 )); then
99 [[ $ttl[1] == <-> ]] && integer t=$ttl[1]
100
101 case $ttl[2] in
102 seconds#)d=s;;
103 months#) d=M;;
104 weeks#) d=w;;
105 hours#) d=h;;
106 *) d=d;;
107 esac
108 fi
109
110 oldp=( "$1"(Nm${d:-d}+${t:-1}) )
111 (( $#oldp ))
112}
113
114local -a _{target,master,logging,minion}_options _{common,out}_opts _target_opt_pat
115_target_opt_pat=(
116 '(-[ELGNRCIS]|--(pcre|list|grain(|-pcre)|nodegroup|range|compound|pillar|ipcidr))'
117 '(-E --pcre -L --list -G --grain --grain-pcre -N --nodegroup -R --range -C --compound -I --pillar -S --ipcidr)'
118)
119
120_target_options=(
121 "$_target_opt_pat[2]"{-E,--pcre}'[use pcre regular expressions]:pcre:'
122 "$_target_opt_pat[2]"{-L,--list}'[take a comma or whitespace delimited list of servers.]:list:'
123 "$_target_opt_pat[2]"{-G,--grain}'[use a grain value to identify targets]:Grains:'
124 "$_target_opt_pat[2]--grain-pcre[use a grain value to identify targets.]:pcre:"
125 "$_target_opt_pat[2]"{-N,--nodegroup}'[use one of the predefined nodegroups to identify a list of targets.]:Nodegroup:'
126 "$_target_opt_pat[2]"{-R,--range}'[use a range expression to identify targets.]:Range:'
127 "$_target_opt_pat[2]"{-C,--compound}'[Use multiple targeting options.]:Compound:'
128 "$_target_opt_pat[2]"{-I,--pillar}'[use a pillar value to identify targets.]:Pillar:'
129 "$_target_opt_pat[2]"{-S,--ipcidr}'[Match based on Subnet (CIDR notation) or IPv4 address.]:Cidr:'
130)
131
132_common_opts=(
133 "--version[show program's version number and exit]"
134 "--versions-report[show program's dependencies version number and exit]"
135 '(-h --help)'{-h,--help}'[show this help message and exit]'
136 '(-c --config-dir)'{-c,--config-dir}'[Pass in an alternative configuration directory.(default: /etc/salt/)]:Config Directory:_files -/'
137 '(-t --timeout)'{-t,--timeout}'[Change the timeout for the running command; default=5]:Timeout (seconds):'
138)
139
140_master_options=(
141 '(-s --static)'{-s,--static}'[Return the data from minions as a group after they all return.]'
142 "--async[Run the salt command but don't wait for a reply]"
143 '(--state-output --state_output)'{--state-output,--state_output}'[Override the configured state_output value for minion output. Default: full]:Outputs:(full terse mixed changes)'
144 '--subset[Execute the routine on a random subset of the targeted minions]:Subset:'
145 '(-v --verbose)'{-v,--verbose}'[Turn on command verbosity, display jid and active job queries]'
146 '--hide-timeout[Hide minions that timeout]'
147 '(-b --batch --batch-size)'{-b,--batch,--batch-size}'[Execute the salt job in batch mode, pass number or percentage to batch.]:Batch Size:'
148 '(-a --auth --eauth --external-auth)'{-a,--auth,--eauth,--external-auth}'[Specify an external authentication system to use.]:eauth:'
149 '(-T --make-token)'{-T,--make-token}'[Generate and save an authentication token for re-use.]'
150 '--return[Set an alternative return method.]:Returners:_path_files -W "$salt_dir/returners" -g "[^_]*.py(\:r)"'
151 '(-d --doc --documentation)'{-d,--doc,--documentation}'[Return the documentation for the specified module]'
152 '--args-separator[Set the special argument used as a delimiter between command arguments of compound commands.]:Arg separator:'
153)
154
155_minion_options=(
156 '--return[Set an alternative return method.]:Returners:_path_files -W "$salt_dir"/returners" -g "[^_]*.py(\:r)"'
157 '(-d --doc --documentation)'{-d,--doc,--documentation}'[Return the documentation for the specified module]'
158 '(-g --grains)'{-g,--grains}'[Return the information generated by the salt grains]'
159 {*-m,*--module-dirs}'[Specify an additional directory to pull modules from.]:Module Dirs:_files -/'
160 '--master[Specify the master to use.]:Master:'
161 '--local[Run salt-call locally, as if there was no master running.]'
162 '--file-root[Set this directory as the base file root.]:File Root:_files -/'
163 '--pillar-root[Set this directory as the base pillar root.]:Pillar Root:_files -/'
164 '--retcode-passthrough[Exit with the salt call retcode and not the salt binary retcode]'
165 '--id[Specify the minion id to use.]:Minion ID:'
166 '--skip-grains[Do not load grains.]'
167 '--refresh-grains-cache[Force a refresh of the grains cache]'
168)
169
170_runner_options=(
171 '--hard-crash[raise any original exception rather than exiting gracefully]'
172 '(-d --doc --documentation)'{-d,--doc,--documentation}'[Return the documentation for the specified module]'
173)
174
175_key_options=(
176 '(-u --user)'{-u+,--user=}'[specify user to run salt-key]:user:_users'
177 '--hard-crash[raise any original exception rather than exiting gracefully]'
178 '(-q --quiet)'{-q,--quiet}'[quiet mode]'
179 '(-y --yes)'{-y,--yes}'[assume yes]'
180 '--rotate-aes-key[prevents the master from refreshing the key session when keys are deleted or rejected]:boolean:(true false)'
181 '--gen-keys=[set a name to generate a keypair for use with salt]:key name'
182 '--gen-keys-dir=[set the directory to save the generated keypair]: : _directories'
183 '--keysize=[set the size for keypair]:key size'
184 '--gen-signature[create a signature file of the masters public-key]'
185 '--priv=[the private-key file to create a signature with]:private key:_files'
186 '--signature-path=[the path where the signature file should be written]: : _directories'
187 '--pub=[the public-key file to create a signature for]:public key:_files'
188 '--auto-create[auto-create a signing key-pair if it does not yet exist]'
189 '--include-all[include non-pending keys when accepting/rejecting]'
190 - '(set)'
191 {-l+,--list=}'[list public keys]:key type:((
192 preaccepted\:"unaccepted/unsigned keys" unaccepted\:"unaccepted/unsigned keys" un\:"unaccepted/unsigned keys"
193 accepted\:"accepted/signed keys" acc\:"accepted/signed keys"
194 rejected\:"rejected keys" rej\:"rejected keys"
195 den\:"denied keys" denied\:"denied keys" all
196 ))'
197 {-a+,--accept=}'[accept key]:key:_minions -t unaccepted -T rejected'
198 {-A,--accept-all}'[accept all keys]'
199 {-r+,--reject=}'[reject key]:key:_minions -t rejected -T accepted'
200 {-p+,--print=}'[print the specified public key]:key:_minions -t all'
201 {-P,--print-all}'[print all public keys]'
202 {-d+,--delete=}'[delete the specified public key]:key:_minions -t all'
203 {-D,--delete-all}'[delete all public keys]'
204 {-f+,--finger=}'[print the specified key'\''s fingerprint]:key:_minions -t all'
205 {-F,--finger-all}'[print the fingerprint of all keys]'
206)
207
208_logging_options=(
209 '(-l --log-level)'{-l,--log-level}'[Console logging log level.(default: warning)]:Log Level:(all garbage trace debug info warning error critical quiet)'
210 '--log-file[Log file path. Default: /var/log/salt/master.]:Log File:_files'
211 '--log-file-level=[Logfile logging log level.Default: warning]:Log Level:(all garbage trace debug info warning error critical quiet)'
212)
213
214_out_opts=(
215 '(--out --output)'{--out,--output}'[Print the output using the specified outputter.]:Outputters:_path_files -W "$salt_dir/output" -g "[^_]*.py(\:r)"'
216 '(--out-indent --output-indent)'{--out-indent,--output-indent}'[Print the output indented by the provided value in spaces.]:Number:'
217 '(--out-file --output-file)'{--out-file,--output-file}'[Write the output to the specified file]:Output File:_files'
218 '(--no-color --no-colour)'{--no-color,--no-colour}'[Disable all colored output]'
219 '(--force-color --force-colour)'{--force-color,--force-colour}'[Force colored output]'
220)
221
222_salt_comp(){
223 case "$service" in
224 salt)
225 _arguments -C \
226 "${words[(r)$_target_opt_pat[1]]+!}:minions:_minions" \
227 ':modules:_modules' \
228 "$_target_options[@]" \
229 "$_common_opts[@]" \
230 "$_master_options[@]" \
231 "$_logging_options[@]" \
232 "$_out_opts[@]"
233 ;;
234 salt-call)
235 _arguments -C \
236 ':modules:_modules' \
237 "$_minion_options[@]" \
238 "$_common_opts[@]" \
239 "$_logging_options[@]" \
240 "$_out_opts[@]"
241 ;;
242 salt-cp)
243 _arguments -C \
244 "${words[(r)$_target_opt_pat[1]]+!}:minions:_minions" \
245 "$_target_options[@]" \
246 "$_common_opts[@]" \
247 "$_logging_options[@]" \
248 ':Source File:_files' \
249 ':Destination File:_files'
250 ;;
251 salt-run)
252 _arguments -C \
253 ":runners:_runners" \
254 "$_runner_options[@]" \
255 "$_common_opts[@]" \
256 "$_logging_options[@]"
257 ;;
258 salt-key)
259 _arguments -C \
260 "$_key_options[@]" \
261 "${_common_opts[@]:#'-t --timeout\)'*}" \
262 "${_logging_options[@]:#'(-l --log-level)'*}"
263 ;;
264 esac
265}
266
267() {
268 local curcontext=${curcontext%:*}:salt_dir
269 if ! zstyle -m ":completion:$curcontext:" cache-policy '*'; then
270 zstyle ":completion:$curcontext:" cache-policy _salt_caching_policy
271 fi
272
273 if _cache_invalid salt/salt_dir || ! _retrieve_cache salt/salt_dir; then
274 salt_dir="${$(python2 -c 'import sys; del sys.path[0]; import salt; print(salt.__file__);')%__init__*}"
275 _store_cache salt/salt_dir salt_dir
276 fi
277}
278
279_salt_comp "$@"