_salt 13 KB

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