salt.bash 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # written by David Pravec
  2. # - feel free to /msg alekibango on IRC if you want to talk about this file
  3. # TODO: check if --config|-c was used and use configured config file for queries
  4. # TODO: solve somehow completion for salt -G pythonversion:[tab]
  5. # (not sure what to do with lists)
  6. # TODO: --range[tab] -- how?
  7. # TODO: --compound[tab] -- how?
  8. # TODO: use history to extract some words, esp. if ${cur} is empty
  9. # TODO: TEST EVERYTHING a lot
  10. # TODO: is it ok to use '--timeout 2' ?
  11. _salt_get_grains(){
  12. if [ "$1" = 'local' ] ; then
  13. salt-call --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
  14. else
  15. salt '*' --timeout 2 --hide-timeout --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
  16. fi
  17. }
  18. _salt_get_grain_values(){
  19. if [ "$1" = 'local' ] ; then
  20. salt-call --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
  21. else
  22. salt '*' --timeout 2 --hide-timeout --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
  23. fi
  24. }
  25. _salt_get_keys(){
  26. for type in $*; do
  27. # remove header from data:
  28. salt-key --no-color -l $type | tail -n+2
  29. done
  30. }
  31. _salt(){
  32. CACHE_DIR="$HOME/.cache/salt-comp-cache_functions"
  33. local _salt_cache_functions=${SALT_COMP_CACHE_FUNCTIONS:=$CACHE_DIR}
  34. local _salt_cache_timeout=${SALT_COMP_CACHE_TIMEOUT:='last hour'}
  35. if [ ! -d "$(dirname ${_salt_cache_functions})" ]; then
  36. mkdir -p "$(dirname ${_salt_cache_functions})"
  37. fi
  38. local cur prev opts _salt_grains _salt_coms pprev ppprev
  39. COMPREPLY=()
  40. cur="${COMP_WORDS[COMP_CWORD]}"
  41. prev="${COMP_WORDS[COMP_CWORD-1]}"
  42. if [ ${COMP_CWORD} -gt 2 ]; then
  43. pprev="${COMP_WORDS[COMP_CWORD-2]}"
  44. fi
  45. if [ ${COMP_CWORD} -gt 3 ]; then
  46. ppprev="${COMP_WORDS[COMP_CWORD-3]}"
  47. fi
  48. opts="-h --help -d --doc --documentation --version --versions-report -c \
  49. --config-dir= -v --verbose -t --timeout= -s --static -b --batch= \
  50. --batch-size= -E --pcre -L --list -G --grain --grain-pcre -N \
  51. --nodegroup -R --range -C --compound -I --pillar \
  52. --return= -a --auth= --eauth= --extended-auth= -T --make-token -S \
  53. --ipcidr --out=pprint --out=yaml --out=overstatestage --out=json \
  54. --out=raw --out=highstate --out=key --out=txt --no-color --out-indent= "
  55. if [[ "${cur}" == -* ]] ; then
  56. COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  57. return 0
  58. fi
  59. # 2 special cases for filling up grain values
  60. case "${pprev}" in
  61. -G|--grain|--grain-pcre)
  62. if [ "${cur}" = ":" ]; then
  63. COMPREPLY=($(compgen -W "`_salt_get_grain_values ${prev}`"))
  64. return 0
  65. fi
  66. ;;
  67. esac
  68. case "${ppprev}" in
  69. -G|--grain|--grain-pcre)
  70. if [ "${prev}" = ":" ]; then
  71. COMPREPLY=( $(compgen -W "`_salt_get_grain_values ${pprev}`" -- ${cur}) )
  72. return 0
  73. fi
  74. ;;
  75. esac
  76. if [ "${cur}" = "=" ] && [[ "${prev}" == --* ]]; then
  77. cur=""
  78. fi
  79. if [ "${prev}" = "=" ] && [[ "${pprev}" == --* ]]; then
  80. prev="${pprev}"
  81. fi
  82. case "${prev}" in
  83. -c|--config)
  84. COMPREPLY=($(compgen -f -- ${cur}))
  85. return 0
  86. ;;
  87. salt)
  88. COMPREPLY=($(compgen -W "\'*\' ${opts} $(_salt_get_keys acc)" -- ${cur}))
  89. return 0
  90. ;;
  91. -E|--pcre)
  92. COMPREPLY=($(compgen -W "$(_salt_get_keys acc)" -- ${cur}))
  93. return 0
  94. ;;
  95. -G|--grain|--grain-pcre)
  96. COMPREPLY=($(compgen -W "$(_salt_get_grains)" -- ${cur}))
  97. return 0
  98. ;;
  99. -C|--compound)
  100. COMPREPLY=() # TODO: finish this one? how?
  101. return 0
  102. ;;
  103. -t|--timeout)
  104. COMPREPLY=($( compgen -W "1 2 3 4 5 6 7 8 9 10 15 20 30 40 60 90 120 180" -- ${cur}))
  105. return 0
  106. ;;
  107. -b|--batch|--batch-size)
  108. COMPREPLY=($(compgen -W "1 2 3 4 5 6 7 8 9 10 15 20 30 40 50 60 70 80 90 100 120 150 200"))
  109. return 0
  110. ;;
  111. -N|--nodegroup)
  112. MASTER_CONFIG='/etc/salt/master'
  113. COMPREPLY=($(compgen -W "`awk -F ':' 'BEGIN {print_line = 0}; /^nodegroups/ {print_line = 1;getline } print_line && /^ */ {print $1} /^[^ ]/ {print_line = 0}' <${MASTER_CONFIG}`" -- ${cur}))
  114. return 0
  115. ;;
  116. esac
  117. # Regenerate cache if timed out
  118. if [[ "$(stat --format=%Z ${_salt_cache_functions} 2>/dev/null)" -lt "$(date --date="${_salt_cache_timeout}" +%s)" ]]; then
  119. # salt: get all functions on all minions
  120. # sed: remove all array overhead and convert to newline separated list
  121. # sort: chop out doubled entries, so overhead is minimal later during actual completion
  122. salt '*' --timeout 2 --hide-timeout --out=txt -- sys.list_functions \
  123. | sed "s/^.*\[//;s/[],']//g;s/ /\n/g" \
  124. | sort -u \
  125. > "${_salt_cache_functions}"
  126. fi
  127. # filter results, to only print the part to next dot (or end of function)
  128. _salt_coms="$(sed 's/^\('${cur}'\(\.\|[^.]*\)\)\?.*/\1/' "${_salt_cache_functions}" | sort -u)"
  129. # If there are still dots in the suggestion, do not append space
  130. grep "^${cur}.*\." "${_salt_cache_functions}" &>/dev/null && compopt -o nospace
  131. all="${opts} ${_salt_coms}"
  132. COMPREPLY=( $(compgen -W "${all}" -- ${cur}) )
  133. return 0
  134. }
  135. complete -F _salt salt
  136. _saltkey(){
  137. local cur prev opts prev pprev
  138. COMPREPLY=()
  139. cur="${COMP_WORDS[COMP_CWORD]}"
  140. prev="${COMP_WORDS[COMP_CWORD-1]}"
  141. opts="-c --config-dir= -h --help --version --versions-report -q --quiet \
  142. -y --yes --gen-keys= --gen-keys-dir= --keysize= --key-logfile= \
  143. -l --list= -L --list-all -a --accept= -A --accept-all \
  144. -r --reject= -R --reject-all -p --print= -P --print-all \
  145. -d --delete= -D --delete-all -f --finger= -F --finger-all \
  146. --out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
  147. --out=highstate --out=key --out=txt --no-color --out-indent= "
  148. if [ ${COMP_CWORD} -gt 2 ]; then
  149. pprev="${COMP_WORDS[COMP_CWORD-2]}"
  150. fi
  151. if [ ${COMP_CWORD} -gt 3 ]; then
  152. ppprev="${COMP_WORDS[COMP_CWORD-3]}"
  153. fi
  154. if [[ "${cur}" == -* ]] ; then
  155. COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  156. return 0
  157. fi
  158. if [ "${cur}" = "=" ] && [[ "${prev}" == --* ]]; then
  159. cur=""
  160. fi
  161. if [ "${prev}" = "=" ] && [[ "${pprev}" == --* ]]; then
  162. prev="${pprev}"
  163. fi
  164. case "${prev}" in
  165. -a|--accept)
  166. COMPREPLY=($(compgen -W "$(_salt_get_keys un rej)" -- ${cur}))
  167. return 0
  168. ;;
  169. -r|--reject)
  170. COMPREPLY=($(compgen -W "$(_salt_get_keys acc)" -- ${cur}))
  171. return 0
  172. ;;
  173. -d|--delete)
  174. COMPREPLY=($(compgen -W "$(_salt_get_keys acc un rej)" -- ${cur}))
  175. return 0
  176. ;;
  177. -c|--config)
  178. COMPREPLY=($(compgen -f -- ${cur}))
  179. return 0
  180. ;;
  181. --keysize)
  182. COMPREPLY=($(compgen -W "2048 3072 4096 5120 6144" -- ${cur}))
  183. return 0
  184. ;;
  185. --gen-keys)
  186. return 0
  187. ;;
  188. --gen-keys-dir)
  189. COMPREPLY=($(compgen -d -- ${cur}))
  190. return 0
  191. ;;
  192. -p|--print)
  193. COMPREPLY=($(compgen -W "$(_salt_get_keys acc un rej)" -- ${cur}))
  194. return 0
  195. ;;
  196. -l|--list)
  197. COMPREPLY=($(compgen -W "pre un acc accepted unaccepted rej rejected all" -- ${cur}))
  198. return 0
  199. ;;
  200. --accept-all)
  201. return 0
  202. ;;
  203. esac
  204. COMPREPLY=($(compgen -W "${opts} " -- ${cur}))
  205. return 0
  206. }
  207. complete -F _saltkey salt-key
  208. _saltcall(){
  209. local cur prev opts _salt_coms pprev ppprev
  210. COMPREPLY=()
  211. cur="${COMP_WORDS[COMP_CWORD]}"
  212. prev="${COMP_WORDS[COMP_CWORD-1]}"
  213. opts="-h --help -d --doc --documentation --version --versions-report \
  214. -m --module-dirs= -g --grains --return= --local -c --config-dir= -l --log-level= \
  215. --out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
  216. --out=highstate --out=key --out=txt --no-color --out-indent= "
  217. if [ ${COMP_CWORD} -gt 2 ]; then
  218. pprev="${COMP_WORDS[COMP_CWORD-2]}"
  219. fi
  220. if [ ${COMP_CWORD} -gt 3 ]; then
  221. ppprev="${COMP_WORDS[COMP_CWORD-3]}"
  222. fi
  223. if [[ "${cur}" == -* ]] ; then
  224. COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  225. return 0
  226. fi
  227. if [ "${cur}" = "=" ] && [[ ${prev} == --* ]]; then
  228. cur=""
  229. fi
  230. if [ "${prev}" = "=" ] && [[ ${pprev} == --* ]]; then
  231. prev="${pprev}"
  232. fi
  233. case ${prev} in
  234. -m|--module-dirs)
  235. COMPREPLY=( $(compgen -d ${cur} ))
  236. return 0
  237. ;;
  238. -l|--log-level)
  239. COMPREPLY=( $(compgen -W "info none garbage trace warning error debug" -- ${cur}))
  240. return 0
  241. ;;
  242. -g|grains)
  243. return 0
  244. ;;
  245. salt-call)
  246. COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  247. return 0
  248. ;;
  249. esac
  250. _salt_coms="$(salt-call --out=txt -- sys.list_functions|sed 's/^.*\[//' | tr -d ",']" )"
  251. COMPREPLY=( $(compgen -W "${opts} ${_salt_coms}" -- ${cur} ))
  252. return 0
  253. }
  254. complete -F _saltcall salt-call
  255. _saltcp(){
  256. local cur prev opts target prefpart postpart helper filt pprev ppprev
  257. COMPREPLY=()
  258. cur="${COMP_WORDS[COMP_CWORD]}"
  259. prev="${COMP_WORDS[COMP_CWORD-1]}"
  260. opts="-t --timeout= -s --static -b --batch= --batch-size= \
  261. -h --help --version --versions-report -c --config-dir= \
  262. -E --pcre -L --list -G --grain --grain-pcre -N --nodegroup \
  263. -R --range -C --compound -I --pillar \
  264. --out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
  265. --out=highstate --out=key --out=txt --no-color --out-indent= "
  266. if [[ "${cur}" == -* ]] ; then
  267. COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  268. return 0
  269. fi
  270. if [ "${cur}" = "=" ] && [[ "${prev}" == --* ]]; then
  271. cur=""
  272. fi
  273. if [ "${prev}" = "=" ] && [[ "${pprev}" == --* ]]; then
  274. prev=${pprev}
  275. fi
  276. case ${prev} in
  277. salt-cp)
  278. COMPREPLY=($(compgen -W "${opts} $(_salt_get_keys acc)" -- ${cur}))
  279. return 0
  280. ;;
  281. -t|--timeout)
  282. # those numbers are just a hint
  283. COMPREPLY=($(compgen -W "2 3 4 8 10 15 20 25 30 40 60 90 120 180 240 300" -- ${cur} ))
  284. return 0
  285. ;;
  286. -E|--pcre)
  287. COMPREPLY=($(compgen -W "$(_salt_get_keys acc)" -- ${cur}))
  288. return 0
  289. ;;
  290. -L|--list)
  291. # IMPROVEMENTS ARE WELCOME
  292. prefpart="${cur%,*},"
  293. postpart=${cur##*,}
  294. filt="^\($(echo ${cur}| sed 's:,:\\|:g')\)$"
  295. helper=($(_salt_get_keys acc | grep -v "${filt}" | sed "s/^/${prefpart}/"))
  296. COMPREPLY=($(compgen -W "${helper[*]}" -- ${cur}))
  297. return 0
  298. ;;
  299. -G|--grain|--grain-pcre)
  300. COMPREPLY=($(compgen -W "$(_salt_get_grains)" -- ${cur}))
  301. return 0
  302. ;;
  303. # FIXME
  304. -R|--range)
  305. # FIXME ??
  306. return 0
  307. ;;
  308. -C|--compound)
  309. # FIXME ??
  310. return 0
  311. ;;
  312. -c|--config)
  313. COMPREPLY=($(compgen -f -- ${cur}))
  314. return 0
  315. ;;
  316. esac
  317. # default is using opts:
  318. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  319. }
  320. complete -F _saltcp salt-cp