_knife 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #compdef knife
  2. # You can override the path to knife.rb and your cookbooks by setting
  3. # KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb
  4. # KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks
  5. # If you want your local cookbooks path to be calculated relative to where you are then
  6. # set the below option
  7. # KNIFE_RELATIVE_PATH=true
  8. # Read around where these are used for more detail.
  9. # These flags should be available everywhere according to man knife
  10. knife_general_flags=( --help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes )
  11. # knife has a very special syntax, some example calls are:
  12. # knife status
  13. # knife cookbook list
  14. # knife role show ROLENAME
  15. # knife data bag show DATABAGNAME
  16. # knife role show ROLENAME --attribute ATTRIBUTENAME
  17. # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
  18. # The -Q switch in compadd allow for completions of things like "data bag" without having to go through two rounds of completion and avoids zsh inserting a \ for escaping spaces
  19. _knife() {
  20. local curcontext="$curcontext" state line
  21. typeset -A opt_args
  22. cloudproviders=(bluebox ec2 rackspace slicehost terremark)
  23. _arguments \
  24. '1: :->knifecmd'\
  25. '2: :->knifesubcmd'\
  26. '3: :->knifesubcmd2' \
  27. '4: :->knifesubcmd3' \
  28. '5: :->knifesubcmd4' \
  29. '6: :->knifesubcmd5'
  30. case $state in
  31. knifecmd)
  32. compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" diff exec environment index node recipe role search ssh status upload vault windows $cloudproviders
  33. ;;
  34. knifesubcmd)
  35. case $words[2] in
  36. (bluebox|ec2|rackspace|slicehost|terremark)
  37. compadd "$@" server images
  38. ;;
  39. client)
  40. compadd -Q "$@" "bulk delete" list create show delete edit reregister
  41. ;;
  42. configure)
  43. compadd "$@" client
  44. ;;
  45. cookbook)
  46. compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
  47. ;;
  48. diff)
  49. _arguments '*:file or directory:_files -g "*"'
  50. ;;
  51. environment)
  52. compadd -Q "$@" list create delete edit show "from file"
  53. ;;
  54. node)
  55. compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
  56. ;;
  57. recipe)
  58. compadd "$@" list
  59. ;;
  60. role)
  61. compadd -Q "$@" "bulk delete" create delete edit "from file" list show
  62. ;;
  63. upload)
  64. _arguments '*:file or directory:_files -g "*"'
  65. ;;
  66. vault)
  67. compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
  68. ;;
  69. windows)
  70. compadd "$@" bootstrap
  71. ;;
  72. *)
  73. _arguments '2:Subsubcommands:($(_knife_options1))'
  74. esac
  75. ;;
  76. knifesubcmd2)
  77. case $words[3] in
  78. server)
  79. compadd "$@" list create delete
  80. ;;
  81. images)
  82. compadd "$@" list
  83. ;;
  84. site)
  85. compadd "$@" vendor show share search download list unshare
  86. ;;
  87. (show|delete|edit)
  88. _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
  89. ;;
  90. (upload|test)
  91. _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
  92. ;;
  93. list)
  94. compadd -a "$@" knife_general_flags
  95. ;;
  96. bag)
  97. compadd -Q "$@" show edit list "from file" create delete
  98. ;;
  99. *)
  100. _arguments '3:Subsubcommands:($(_knife_options2))'
  101. esac
  102. ;;
  103. knifesubcmd3)
  104. case $words[3] in
  105. show)
  106. case $words[2] in
  107. cookbook)
  108. versioncomp=1
  109. _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
  110. ;;
  111. (node|client|role)
  112. compadd "$@" --attribute
  113. esac
  114. esac
  115. case $words[4] in
  116. (show|edit)
  117. _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
  118. ;;
  119. file)
  120. case $words[2] in
  121. environment)
  122. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
  123. ;;
  124. node)
  125. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
  126. ;;
  127. role)
  128. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
  129. ;;
  130. *)
  131. _arguments '*:Subsubcommands:($(_knife_options3))'
  132. esac
  133. ;;
  134. list)
  135. compadd -a "$@" knife_general_flags
  136. ;;
  137. *)
  138. _arguments '*:Subsubcommands:($(_knife_options3))'
  139. esac
  140. ;;
  141. knifesubcmd4)
  142. if (( versioncomp > 0 )); then
  143. compadd "$@" attributes definitions files libraries providers recipes resources templates
  144. else
  145. case $words[5] in
  146. file)
  147. _arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
  148. ;;
  149. *) _arguments '*:Subsubcommands:($(_knife_options2))'
  150. esac
  151. fi
  152. ;;
  153. knifesubcmd5)
  154. case $words[5] in
  155. file)
  156. _arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
  157. ;;
  158. *)
  159. _arguments '*:Subsubcommands:($(_knife_options3))'
  160. esac
  161. esac
  162. }
  163. # Helper functions to provide the argument completion for several depths of commands
  164. _knife_options1() {
  165. ( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  166. }
  167. _knife_options2() {
  168. ( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  169. }
  170. _knife_options3() {
  171. ( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  172. }
  173. # The chef_x_remote functions use knife to get a list of objects of type x on the server
  174. _chef_roles_remote() {
  175. (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  176. }
  177. _chef_clients_remote() {
  178. (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  179. }
  180. _chef_nodes_remote() {
  181. (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  182. }
  183. _chef_cookbooks_remote() {
  184. (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  185. }
  186. _chef_sitecookbooks_remote() {
  187. (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  188. }
  189. _chef_data_bags_remote() {
  190. (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  191. }
  192. _chef_environments_remote() {
  193. (knife environment list | awk '{print $1}')
  194. }
  195. # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
  196. _chef_cookbooks_local() {
  197. if [ $KNIFE_RELATIVE_PATH ]; then
  198. local cookbook_path="$(_chef_root)/cookbooks"
  199. else
  200. local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
  201. if [ -f ./.chef/knife.rb ]; then
  202. knife_rb="./.chef/knife.rb"
  203. fi
  204. local cookbook_path=${KNIFE_COOKBOOK_PATH:-$(grep cookbook_path $knife_rb | awk 'BEGIN {FS = "[" }; {print $2}' | sed 's/\,//g' | sed "s/'//g" | sed 's/\(.*\)]/\1/' )}
  205. fi
  206. (for i in $cookbook_path; do ls $i; done)
  207. }
  208. # This function extracts the available cookbook versions on the chef server
  209. _cookbook_versions() {
  210. (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
  211. }
  212. # Searches up from current directory to find the closest folder that has a .chef folder
  213. # Useful for the knife upload/from file commands
  214. _chef_root () {
  215. directory="$PWD"
  216. while [ $directory != '/' ]
  217. do
  218. test -e "$directory/.chef" && echo "$directory" && return
  219. directory="${directory:h}"
  220. done
  221. }
  222. _knife "$@"