_docker-machine 13.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#compdef docker-machine
# Description
# -----------
#  zsh completion for docker-machine
#  https://github.com/leonhartX/docker-machine-zsh-completion
# -------------------------------------------------------------------------
# Version
# -------
#  0.1.1
# -------------------------------------------------------------------------
# Authors
# -------
#  * Ke Xu <leonhartx.k@gmail.com>
# -------------------------------------------------------------------------
# Inspiration
# -----------
#  * @sdurrheimer docker-compose-zsh-completion https://github.com/sdurrheimer/docker-compose-zsh-completion
#  * @ilkka _docker-machine


__docker-machine_get_hosts() {
    [[ $PREFIX = -* ]] && return 1
    local state
    declare -a hosts
    state=$1; shift
    if [[ $state != all ]]; then
        hosts=(${(f)"$(_call_program commands docker-machine ls -q --filter state=$state)"})
    else
        hosts=(${(f)"$(_call_program commands docker-machine ls -q)"})
    fi
    _describe 'host' hosts "$@" && ret=0
    return ret
}

__docker-machine_hosts_with_state() {
    declare -a hosts
    hosts=(${(f)"$(_call_program commands docker-machine ls -f '{{.Name}}\:{{.DriverName}}\({{.State}}\)\ {{.URL}}')"})
    _describe 'host' hosts
}

__docker-machine_hosts_all() {
    __docker-machine_get_hosts all "$@"
}

__docker-machine_hosts_running() {
    __docker-machine_get_hosts Running "$@"
}

__docker-machine_get_swarm() {
    declare -a swarms
    swarms=(${(f)"$(_call_program commands docker-machine ls -f {{.Swarm}} | awk '{print $1}')"})
    _describe 'swarm' swarms
}

__docker-machine_hosts_and_files() {
    _alternative "hosts:host:__docker-machine_hosts_all -qS ':'" 'files:files:_path_files'
}

__docker-machine_filters() {
    [[ $PREFIX = -* ]] && return 1
    integer ret=1

    if compset -P '*='; then
        case "${${words[-1]%=*}#*=}" in
            (driver)
                _describe -t driver-filter-opts "driver filter" opts_driver && ret=0
                ;;
            (swarm)
                __docker-machine_get_swarm && ret=0
                ;;
            (state)
                opts_state=('Running' 'Paused' 'Saved' 'Stopped' 'Stopping' 'Starting' 'Error')
                _describe -t state-filter-opts "state filter" opts_state && ret=0
                ;;
            (name)
                __docker-machine_hosts_all && ret=0
                ;;
            (label)
                _message 'label' && ret=0
                ;;
            *)
                _message 'value' && ret=0
                ;;
        esac
    else
        opts=('driver' 'swarm' 'state' 'name' 'label')
        _describe -t filter-opts "filter" opts -qS "=" && ret=0
    fi
    return ret
}

__get_swarm_discovery() {
    declare -a masters serivces
    local service
    services=()
    masters=($(docker-machine ls -f {{.Swarm}} |grep '(master)' |awk '{print $1}'))
    for master in $masters; do
         service=${${${(f)"$(_call_program commands docker-machine inspect -f '{{.HostOptions.SwarmOptions.Discovery}}:{{.Name}}' $master)"}/:/\\:}}
         services=($services $service)
    done
    _describe -t services "swarm service" services && ret=0
    return ret
}

__get_create_argument() {
    typeset -g docker_machine_driver
    if [[ CURRENT -le 2 ]]; then
        docker_machine_driver="none"
    elif [[ CURRENT > 2 && $words[CURRENT-2] = '-d' || $words[CURRENT-2] = '--driver' ]]; then
        docker_machine_driver=$words[CURRENT-1]
    elif [[ $words[CURRENT-1] =~ '^(-d|--driver)=' ]]; then
        docker_machine_driver=${${words[CURRENT-1]}/*=/}
    fi
    local driver_opt_cmd
    local -a opts_provider opts_common opts_read_argument
    opts_read_argument=(
        ": :->argument"
    )
    opts_common=(
        $opts_help \
        '(--driver -d)'{--driver=,-d=}'[Driver to create machine with]:dirver:->driver-option' \
        '--engine-install-url=[Custom URL to use for engine installation]:url' \
        '*--engine-opt=[Specify arbitrary flags to include with the created engine in the form flag=value]:flag' \
        '*--engine-insecure-registry=[Specify insecure registries to allow with the created engine]:registry' \
        '*--engine-registry-mirror=[Specify registry mirrors to use]:mirror' \
        '*--engine-label=[Specify labels for the created engine]:label' \
        '--engine-storage-driver=[Specify a storage driver to use with the engine]:storage-driver:->storage-driver-option' \
        '*--engine-env=[Specify environment variables to set in the engine]:environment' \
        '--swarm[Configure Machine with Swarm]' \
        '--swarm-image=[Specify Docker image to use for Swarm]:image' \
        '--swarm-master[Configure Machine to be a Swarm master]' \
        '--swarm-discovery=[Discovery service to use with Swarm]:service:->swarm-service' \
        '--swarm-strategy=[Define a default scheduling strategy for Swarm]:strategy:(spread binpack random)' \
        '*--swarm-opt=[Define arbitrary flags for swarm]:flag' \
        '*--swarm-join-opt=[Define arbitrary flags for Swarm join]:flag' \
        '--swarm-host=[ip/socket to listen on for Swarm master]:host' \
        '--swarm-addr=[addr to advertise for Swarm (default: detect and use the machine IP)]:address' \
        '--swarm-experimental[Enable Swarm experimental features]' \
        '*--tls-san=[Support extra SANs for TLS certs]:option'
    )
    driver_opt_cmd="docker-machine create -d $docker_machine_driver | grep $docker_machine_driver | sed -e 's/\(--.*\)\ *\[\1[^]]*\]/*\1/g' -e 's/\(\[[^]]*\)/\\\\\\1\\\\/g' -e 's/\".*\"\(.*\)/\1/g' | awk '{printf \"%s[\", \$1; for(i=2;i<=NF;i++) {printf \"%s \", \$i}; print \"]\"}'"
    if [[ $docker_machine_driver != "none" ]]; then
        opts_provider=(${(f)"$(_call_program commands $driver_opt_cmd)"})
        _arguments \
            $opts_provider \
            $opts_read_argument \
            $opts_common && ret=0
    else
        _arguments $opts_common && ret=0
    fi
    case $state in
        (driver-option)
            _describe -t driver-option "driver" opts_driver && ret=0
            ;;
        (storage-driver-option)
            _describe -t storage-driver-option "storage driver" opts_storage_driver && ret=0
            ;;
        (swarm-service)
            __get_swarm_discovery && ret=0
            ;;
        (argument)
            ret=0
            ;;
    esac
    return ret
}


__docker-machine_subcommand() {
    local -a opts_help
    opts_help=("(- :)--help[Print usage]")
    local -a opts_only_host opts_driver opts_storage_driver opts_stragery
    opts_only_host=(
        "$opts_help"
        "*:host:__docker-machine_hosts_all"
    )
    opts_driver=('amazonec2' 'azure' 'digitalocean' 'exoscale' 'generic' 'google' 'hyperv' 'none' 'openstack' 'rackspace' 'softlayer' 'virtualbox' 'vmwarefusion' 'vmwarevcloudair' 'vmwarevsphere')
    opts_storage_driver=('overlay' 'aufs' 'btrfs' 'devicemapper' 'vfs' 'zfs')
    integer ret=1

    case "$words[1]" in
        (active)
            _arguments \
                $opts_help \
                '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' && ret=0
            ;;
        (config)
            _arguments \
                $opts_help \
                '--swarm[Display the Swarm config instead of the Docker daemon]' \
                "*:host:__docker-machine_hosts_all" && ret=0
            ;;
        (create)
            __get_create_argument
           ;;
        (env)
            _arguments \
                $opts_help \
                '--swarm[Display the Swarm config instead of the Docker daemon]' \
                '--shell=[Force environment to be configured for a specified shell: \[fish, cmd, powershell\], default is auto-detect]:shell' \
                '(--unset -u)'{--unset,-u}'[Unset variables instead of setting them]' \
                '--no-proxy[Add machine IP to NO_PROXY environment variable]' \
                '*:host:__docker-machine_hosts_running' && ret=0
            ;;
        (help)
            _arguments ':subcommand:__docker-machine_commands' && ret=0
            ;;
        (inspect)
            _arguments \
                $opts_help \
                '(--format -f)'{--format=,-f=}'[Format the output using the given go template]:template' \
                '*:host:__docker-machine_hosts_all' && ret=0
            ;;
        (ip)
            _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_running' && ret=0
            ;;
        (kill)
             _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_with_state' && ret=0
            ;;
        (ls)
            _arguments \
                $opts_help \
                '(--quiet -q)'{--quiet,-q}'[Enable quiet mode]' \
                '*--filter=[Filter output based on conditions provided]:filter:->filter-options' \
                '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' \
                '(--format -f)'{--format=,-f=}'[Pretty-print machines using a Go template]:template' && ret=0
            case $state in
                (filter-options)
                    __docker-machine_filters && ret=0
                    ;;
            esac
            ;;
        (provision)
            _arguments $opts_only_host && ret=0
            ;;
        (regenerate-certs)
            _arguments \
                $opts_help \
                '(--force -f)'{--force,-f}'[Force rebuild and do not prompt]' \
                '*:host:__docker-machine_hosts_all' && ret=0
            ;;
        (restart)
            _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_with_state' && ret=0
            ;;
        (rm)
            _arguments \
                $opts_help \
                '(--force -f)'{--force,-f}'[Remove local configuration even if machine cannot be removed, also implies an automatic yes (`-y`)]' \
                '-y[Assumes automatic yes to proceed with remove, without prompting further user confirmation]' \
                '*:host:__docker-machine_hosts_with_state' && ret=0
            ;;
        (scp)
            _arguments \
                $opts_help \
                '(--recursive -r)'{--recursive,-r}'[Copy files recursively (required to copy directories))]' \
                '*:files:__docker-machine_hosts_and_files' && ret=0
            ;;
        (ssh)
            _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_running' && ret=0
            ;;
        (start)
            _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_with_state' && ret=0
            ;;
        (status)
            _arguments $opts_only_host && ret=0
            ;;
        (stop)
            _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_with_state' && ret=0
            ;;
        (upgrade)
            _arguments $opts_only_host && ret=0
            ;;
        (url)
            _arguments \
                $opts_help \
                '*:host:__docker-machine_hosts_running' && ret=0
            ;;
    esac

    return ret
}


__docker-machine_commands() {
    local cache_policy

    zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
    if [[ -z "$cache_policy" ]]; then
        zstyle ":completion:${curcontext}:" cache-policy __docker-machine_caching_policy
    fi

    if ( [[ ${+_docker_machine_subcommands} -eq 0 ]] || _cache_invalid docker_machine_subcommands) \
        && ! _retrieve_cache docker_machine_subcommands;
    then
        local -a lines
        lines=(${(f)"$(_call_program commands docker-machine 2>&1)"})
        _docker_machine_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I)  *]}]}## #}/$'\t'##/:})
        (( $#_docker_machine_subcommands > 0 )) && _store_cache docker_machine_subcommands _docker_machine_subcommands
    fi
    _describe -t docker-machine-commands "docker-machine command" _docker_machine_subcommands
}

__docker-machine_caching_policy() {
    oldp=( "$1"(Nmh+1) )
    (( $#oldp ))
}

_docker-machine() {
    if [[ $service != docker-machine ]]; then
        _call_function - _$service
        return
    fi

    local curcontext="$curcontext" state line
    integer ret=1
    typeset -A opt_args

    _arguments -C \
        "(- :)"{-h,--help}"[Show help]" \
        "(-D --debug)"{-D,--debug}"[Enable debug mode]" \
        '(-s --stroage-path)'{-s,--storage-path}'[Configures storage path]:file:_files' \
        '--tls-ca-cert[CA to verify remotes against]:file:_files' \
        '--tls-ca-key[Private key to generate certificates]:file:_files' \
        '--tls-client-cert[Client cert to use for TLS]:file:_files' \
        '--tls-client-key[Private key used in client TLS auth]:file:_files' \
        '--github-api-token[Token to use for requests to the Github API]' \
        '--native-ssh[Use the native (Go-based) SSH implementation.]' \
        '--bugsnag-api-token[BugSnag API token for crash reporting]' \
        '(- :)'{-v,--version}'[Print the version]' \
        "(-): :->command" \
        "(-)*:: :->option-or-argument" && ret=0

    case $state in
        (command)
            __docker-machine_commands && ret=0
            ;;
        (option-or-argument)
            curcontext=${curcontext%:*:*}:docker-machine-$words[1]:
            __docker-machine_subcommand && ret=0
            ret=0
            ;;
    esac

    return ret
}

_docker-machine "$@"