aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/scripts/node-repo.sh
blob: 82f99f28e72e6ad5522463b1a40958e3cf3e666d (plain) (blame)
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
360
#!/bin/bash
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

set -e

declare -r VESPA_WEB_SERVICE_PORT=4080

# Output from InnerCurlNodeRepo, see there for details.
declare CURL_RESPONSE

function Usage {
    cat <<EOF
Usage: ${0##*/} <command> [<args>...]
Script for manipulating the Node Repository.

Commands
    add [-c <configserverhost>] -p <parent-hostname> [-f <parent-flavor>]
        [-n <flavor> <hostname>...]
        With -f, provision "host" node <parent-hostname> with flavor
        <parent-flavor>. With -n, provision "tenant" nodes <hostname...> with
        flavor <flavor> and parent host <parenthostname>.
    reprovision [-c <configserverhost>] -p <parenthostname> <hostname>...
        Fail node <hostname>, then rm and add.
    rm [-c <configserverhost>] <hostname>...
        Remove nodes from node repo.
    set-state [-c <configserverhost>] <state> <hostname>...
        Set the node repo state.

By default, <configserverhost> is config-server.

Example
    To remove docker-1--1 through docker-1--5 from the node repo at configserver.com,

        ${0##*/} rm -c configserver.com \
            docker-1--{1,2,3,4,5}.dockerhosts.com
EOF

    exit 1
}

function Fail {
    printf "%s\nRun '${0##*/} help' for usage\n" "$*"
    exit 1
}

# Invoke curl such that:
#
# - Arguments to this function are passed to curl.
#
# - Additional arguments are passed to curl to filter noise (--silent
#   --show-error).
#
# - The curl stdout (i.e. the response) is stored in the global CURL_RESPONSE
#   variable on return of the function.
#
# - If curl returns 0 (i.e. a successful HTTP response) with a JSON response
#   that contains an "error-code" key, this function will instead return 22. 22
#   does not conflict with a curl return code, because curl only ever returns
#   22 when --fail is specified.
#
#   Except, if the JSON response contains a "message" of the form "Cannot add
#   tmp-cnode-0: A node with this name already exists", InnerCurlNodeRepo
#   returns 0 instead of 22, even if the HTTP response status code is an error.
#
#   Note: Why not use --fail with curl? Because as of 2015-11-24, if the node
#   exists when provisioning a node, the node repo returns a 400 Bad Request
#   with a JSON body containing a "message" field as described above. With
#   --fail, this would result in curl exiting with code 22, which is
#   indistinguishable from other HTTP errors. Can the output from --show-error
#   be used in combination with --fail? No, because that ends up saying "curl:
#   (22) The requested URL returned error: 400 Bad Request" when the node
#   exists, making it indistinguishable from other malformed request error
#   messages.
#
#   TODO: Make node repo return a unique HTTP error code when node already
#   exists. It's also fragile to test for the error message in the response.
function InnerCurlNodeRepo {
    # --show-error causes error message to be printed on error, even with
    # --silent, which is useful when we print the error message to Fail.
    local -a command=(curl --silent --show-error "$@")

    # We need the 'if' here, because a non-zero exit code of a command will
    # exit the process, with 'set -e'.
    if CURL_RESPONSE=$("${command[@]}" 2>&1)
    then
        # Match a JSON of the form:
        #   {
        #     "error-code": "BAD_REQUEST",
        #     "message": "Cannot add cnode-0: A node with this name already exists"
        #   }
        if [[ "$CURL_RESPONSE" =~ '"error-code"' ]]
        then
            if [[ "$CURL_RESPONSE" =~ '"message"'[^\"]*\"(.*)\" ]]
            then
                local message="${BASH_REMATCH[1]}"
                if [[ "$message" =~ 'already exists' ]]
                then
                    return 0
                fi
            fi

            return 22
        fi

        return 0
    else
        # Do not move this statement outside of this else: $? gets cleared when
        # the execution passes out of the else-fi block.
        return $?
    fi
}

function CurlOrFail {
    if InnerCurlNodeRepo "$@"
    then
        : # This form of if-else is used to preserve $?.
    else
        local error_code=$?

        # Terminate the current progress-bar-like line
        printf ' failed\n'

        Fail "Error ($error_code) from the node repo at '$url': '$CURL_RESPONSE'"
    fi
}

function ProvisionDockerNode {
    local config_server_hostname="$1"
    local container_hostname="$2"
    local parent_hostname="$3"
    local flavor="$4"

    local json="[
                  {
                    \"hostname\":\"$container_hostname\",
                    \"parentHostname\":\"$parent_hostname\",
                    \"openStackId\":\"fake-$container_hostname\",
                    \"flavor\":\"$flavor\",
                    \"type\":\"tenant\"
                  }
                ]"

    ProvisionNode $config_server_hostname "$json"
}


# Docker host, the docker nodes points to this host in parentHostname in their node config
function ProvisionDockerHost {
    local config_server_hostname="$1"
    local docker_host_hostname="$2"
    local flavor="$3"

    local json="[
                  {
                    \"hostname\":\"$docker_host_hostname\",
                    \"openStackId\":\"$docker_host_hostname\",
                    \"flavor\":\"$flavor\",
                    \"type\":\"host\"
                  }
                ]"

    ProvisionNode $config_server_hostname "$json"
}

# Docker node in node repo (both docker hosts and docker nodes)
function ProvisionNode {
    local config_server_hostname="$1"
    local json="$2"

    local url="http://$config_server_hostname:$VESPA_WEB_SERVICE_PORT/nodes/v2/node"

    CurlOrFail -H "Content-Type: application/json" -X POST -d "$json" "$url"
}

function SetNodeState {
    local config_server_hostname="$1"
    local hostname="$2"
    local state="$3"

    local url="http://$config_server_hostname:$VESPA_WEB_SERVICE_PORT/nodes/v2/state/$state/$hostname"
    CurlOrFail -X PUT "$url"
}

function AddCommand {
    local config_server_hostname=config-server
    local parent_hostname=

    OPTIND=1
    local option
    while getopts "c:p:f:n:" option
    do
        case "$option" in
            c) config_server_hostname="$OPTARG" ;;
            p) parent_hostname="$OPTARG" ;;
            f) parent_host_flavor="$OPTARG" ;;
            n) node_flavor="$OPTARG" ;;
            ?) exit 1 ;; # E.g. option lacks argument, in case error has been
                         # already been printed
            *) Fail "Unknown option '$option' with value '$OPTARG'"
        esac
    done

    if [ -z "$parent_hostname" ]
    then
        Fail "Parent hostname not specified (-p)"
    fi

    shift $((OPTIND - 1))

    if [ -n "$parent_host_flavor" ]
    then
        echo "Provisioning Docker host $parent_hostname with flavor $parent_host_flavor"
        ProvisionDockerHost "$config_server_hostname" \
                            "$parent_hostname" \
                            "$parent_host_flavor"
    fi

    if [ -n "$node_flavor" ]
    then
        echo -n "Provisioning $# nodes with parent host $parent_hostname"
        local container_hostname
        for container_hostname in "$@"
        do
            ProvisionDockerNode "$config_server_hostname" \
                                "$container_hostname" \
                                "$parent_hostname" \
                                "$node_flavor"
            echo -n .
        done

        echo " done"
    fi
}

function ReprovisionCommand {
    local config_server_hostname=config-server
    local parent_hostname=

    OPTIND=1
    local option
    while getopts "c:p:" option
    do
        case "$option" in
            c) config_server_hostname="$OPTARG" ;;
            p) parent_hostname="$OPTARG" ;;
            ?) exit 1 ;; # E.g. option lacks argument, in case error has been
                         # already been printed
            *) Fail "Unknown option '$option' with value '$OPTARG'"
        esac
    done

    if [ -z "$parent_hostname" ]
    then
        Fail "Parent hostname not specified (-p)"
    fi

    shift $((OPTIND - 1))

    if (($# == 0))
    then
        Fail "No node hostnames were specified"
    fi

    # Simulate calls to the following commands.
    SetStateCommand -c "$config_server_hostname" failed "$@"
    RemoveCommand -c "$config_server_hostname" "$@"
    AddCommand -c "$config_server_hostname" -p "$parent_hostname" "$@"
}

function RemoveCommand {
    local config_server_hostname=config-server

    OPTIND=1
    local option
    while getopts "c:" option
    do
        case "$option" in
            c) config_server_hostname="$OPTARG" ;;
            ?) exit 1 ;; # E.g. option lacks argument, in case error has been
                         # already been printed
            *) Fail "Unknown option '$option' with value '$OPTARG'"
        esac
    done

    shift $((OPTIND - 1))

    if (($# == 0))
    then
        Fail "No nodes were specified"
    fi

    echo -n "Removing $# nodes"

    local hostname
    for hostname in "$@"
    do
        local url="http://$config_server_hostname:$VESPA_WEB_SERVICE_PORT/nodes/v2/node/$hostname"
        CurlOrFail -X DELETE "$url"
        echo -n .
    done

    echo " done"
}

function SetStateCommand {
    local config_server_hostname=config-server

    OPTIND=1
    local option
    while getopts "c:" option
    do
        case "$option" in
            c) config_server_hostname="$OPTARG" ;;
            ?) exit 1 ;; # E.g. option lacks argument, in case error has been
                         # already been printed
            *) Fail "Unknown option '$option' with value '$OPTARG'"
        esac
    done

    shift $((OPTIND - 1))

    if (($# <= 1))
    then
        Fail "Too few arguments"
    fi

    local state="$1"
    shift

    echo -n "Setting $# nodes to $state"

    local hostname
    for hostname in "$@"
    do
        SetNodeState "$config_server_hostname" "$hostname" "$state"
        echo -n .
    done

    echo " done"
}

function Main {
    if (($# == 0))
    then
        Usage
    fi
    local command="$1"
    shift

    case "$command" in
        add) AddCommand "$@" ;;
        reprovision) ReprovisionCommand "$@" ;;
        rm) RemoveCommand "$@" ;;
        set-state) SetStateCommand "$@" ;;
        help) Usage "$@" ;;
        *) Usage ;;
    esac
}

Main "$@"