_knife 7.69 KB
Newer Older
Frank Louwers's avatar
Frank Louwers committed
1
2
#compdef knife

3
4
5
# You can override the path to knife.rb and your cookbooks by setting
# KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb
# KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks
6
# If you want your local cookbooks path to be calculated relative to where you are then
7
# set the below option
8
# KNIFE_RELATIVE_PATH=true
9
10
# Read around where these are used for more detail.

Frank Louwers's avatar
Frank Louwers committed
11
# These flags should be available everywhere according to man knife
12
knife_general_flags=(--help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes)
Frank Louwers's avatar
Frank Louwers committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# knife has a very special syntax, some example calls are:
# knife status
# knife cookbook list
# knife role show ROLENAME
# knife data bag show DATABAGNAME
# knife role show ROLENAME --attribute ATTRIBUTENAME
# knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes

# 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
_knife() {
  local curcontext="$curcontext" state line
  typeset -A opt_args
  cloudproviders=(bluebox ec2 rackspace slicehost terremark)
  _arguments \
28
29
    '1: :->knifecmd' \
    '2: :->knifesubcmd' \
Frank Louwers's avatar
Frank Louwers committed
30
31
32
33
    '3: :->knifesubcmd2' \
    '4: :->knifesubcmd3' \
    '5: :->knifesubcmd4' \
    '6: :->knifesubcmd5'
34

Frank Louwers's avatar
Frank Louwers committed
35
36
  case $state in
  knifecmd)
37
    compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" diff exec environment index node recipe role search solo ssh status upload vault windows $cloudproviders
38
    ;;
Frank Louwers's avatar
Frank Louwers committed
39
40
  knifesubcmd)
    case $words[2] in
41
    bluebox|ec2|rackspace|slicehost|terremark)
Frank Louwers's avatar
Frank Louwers committed
42
      compadd "$@" server images
43
      ;;
Frank Louwers's avatar
Frank Louwers committed
44
45
    client)
      compadd -Q "$@" "bulk delete" list create show delete edit reregister
46
      ;;
Frank Louwers's avatar
Frank Louwers committed
47
48
    configure)
      compadd "$@" client
49
      ;;
Frank Louwers's avatar
Frank Louwers committed
50
51
    cookbook)
      compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
52
      ;;
53
54
    diff)
      _arguments '*:file or directory:_files -g "*"'
55
      ;;
56
    environment)
57
      compadd -Q "$@" list create delete edit show "from file"
58
      ;;
Frank Louwers's avatar
Frank Louwers committed
59
    node)
60
61
      compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
      ;;
Frank Louwers's avatar
Frank Louwers committed
62
    recipe)
63
64
      compadd "$@" list
      ;;
Frank Louwers's avatar
Frank Louwers committed
65
66
    role)
      compadd -Q "$@" "bulk delete" create delete edit "from file" list show
67
      ;;
68
69
70
    solo)
      compadd "$@" bootstrap clean cook init prepare
      ;;
71
    upload)
72
73
      _arguments '*:file or directory:_files -g "*"'
      ;;
74
75
    vault)
      compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
76
      ;;
Frank Louwers's avatar
Frank Louwers committed
77
78
    windows)
      compadd "$@" bootstrap
79
      ;;
Frank Louwers's avatar
Frank Louwers committed
80
    *)
81
82
      _arguments '2:Subsubcommands:($(_knife_options1))'
      ;;
Frank Louwers's avatar
Frank Louwers committed
83
    esac
84
85
    ;;
  knifesubcmd2)
Frank Louwers's avatar
Frank Louwers committed
86
    case $words[3] in
87
    server)
Frank Louwers's avatar
Frank Louwers committed
88
      compadd "$@" list create delete
89
90
      ;;
    images)
Frank Louwers's avatar
Frank Louwers committed
91
      compadd "$@" list
92
93
      ;;
    site)
Frank Louwers's avatar
Frank Louwers committed
94
      compadd "$@" vendor show share search download list unshare
95
96
97
98
99
100
101
      ;;
    show|delete|edit)
      _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
      ;;
    upload|test)
      _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
      ;;
Frank Louwers's avatar
Frank Louwers committed
102
    list)
103
104
      compadd -a "$@" knife_general_flags
      ;;
Frank Louwers's avatar
Frank Louwers committed
105
106
    bag)
      compadd -Q "$@" show edit list "from file" create delete
107
      ;;
108
109
110
111
112
113
    bootstrap|clean|cook|prepare)
      compadd "$@" nodes/*.json(N:t:r); done
      ;;
    init)
      compadd "$@" ./*(/N:t); done
      ;;
Frank Louwers's avatar
Frank Louwers committed
114
115
    *)
      _arguments '3:Subsubcommands:($(_knife_options2))'
116
      ;;
Frank Louwers's avatar
Frank Louwers committed
117
118
    esac
    ;;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  knifesubcmd3)
    case $words[3] in
    show)
      case $words[2] in
      cookbook)
        versioncomp=1
        _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
        ;;
      node|client|role)
        compadd "$@" --attribute
        ;;
      esac
      ;;
    esac
    case $words[4] in
    show|edit)
      _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
      ;;
    file)
138
139
140
      case $words[2] in
      environment)
        _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
141
        ;;
142
143
      node)
        _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
144
        ;;
145
146
      role)
        _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
147
        ;;
148
149
150
151
      *)
        _arguments '*:Subsubcommands:($(_knife_options3))'
        ;;
      esac
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
      ;;
    list)
      compadd -a "$@" knife_general_flags
      ;;
    *)
      _arguments '*:Subsubcommands:($(_knife_options3))'
      ;;
    esac
    ;;
  knifesubcmd4)
    if ((versioncomp > 0)); then
      compadd "$@" attributes definitions files libraries providers recipes resources templates
    else
      case $words[5] in
      file)
        _arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
168
        ;;
169
      *) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
170
      esac
171
172
173
174
175
176
177
178
179
180
181
182
183
    fi
    ;;
  knifesubcmd5)
    case $words[5] in
    file)
      _arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
      ;;
    *)
      _arguments '*:Subsubcommands:($(_knife_options3))'
      ;;
    esac
    ;;
  esac
Frank Louwers's avatar
Frank Louwers committed
184
185
186
187
}

# Helper functions to provide the argument completion for several depths of commands
_knife_options1() {
188
  (for line in $(knife $words[2] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
Frank Louwers's avatar
Frank Louwers committed
189
190
191
}

_knife_options2() {
192
  (for line in $(knife $words[2] $words[3] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
Frank Louwers's avatar
Frank Louwers committed
193
194
195
}

_knife_options3() {
196
  (for line in $(knife $words[2] $words[3] $words[4] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
Frank Louwers's avatar
Frank Louwers committed
197
198
199
200
}

# The chef_x_remote functions use knife to get a list of objects of type x on the server
_chef_roles_remote() {
201
  (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
Frank Louwers's avatar
Frank Louwers committed
202
203
204
}

_chef_clients_remote() {
205
  (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
Frank Louwers's avatar
Frank Louwers committed
206
207
208
}

_chef_nodes_remote() {
209
  (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
Frank Louwers's avatar
Frank Louwers committed
210
211
212
}

_chef_cookbooks_remote() {
213
  (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
Frank Louwers's avatar
Frank Louwers committed
214
215
216
}

_chef_sitecookbooks_remote() {
217
  (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
Frank Louwers's avatar
Frank Louwers committed
218
219
220
}

_chef_data_bags_remote() {
221
  (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
Frank Louwers's avatar
Frank Louwers committed
222
223
}

224
225
226
227
_chef_environments_remote() {
  (knife environment list | awk '{print $1}')
}

Frank Louwers's avatar
Frank Louwers committed
228
229
# The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
_chef_cookbooks_local() {
230
  if [ $KNIFE_RELATIVE_PATH ]; then
231
    local cookbook_path="$(_chef_root)/cookbooks"
232
  else
233
234
235
236
    local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
    if [ -f ./.chef/knife.rb ]; then
      knife_rb="./.chef/knife.rb"
    fi
237
    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/')}
238
239
  fi
  (for i in $cookbook_path; do ls $i; done)
Frank Louwers's avatar
Frank Louwers committed
240
241
242
243
244
245
246
}

# This function extracts the available cookbook versions on the chef server
_cookbook_versions() {
  (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
}

247
248
249
# Searches up from current directory to find the closest folder that has a .chef folder
# Useful for the knife upload/from file commands
_chef_root() {
250
  directory="$PWD"
251
  while [ $directory != '/' ]; do
252
253
254
255
256
    test -e "$directory/.chef" && echo "$directory" && return
    directory="${directory:h}"
  done
}

Frank Louwers's avatar
Frank Louwers committed
257
_knife "$@"